Python 错误:numpy.narray 对象不可调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18186936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error:numpy.narray object not callable
提问by marriam nayyer
I am trying to convert Matlab code to Python but it gives an error when I convert the following line:
我正在尝试将 Matlab 代码转换为 Python,但在转换以下行时出现错误:
Matlab Code
MATLAB 代码
md(1,index) = (-1)^bits(m);
Python equivalent
Python 等价物
md[index]=(-1)**bits(m)
Error
错误
md[index]=(-1)**bits(m)
TypeError: 'numpy.ndarray' object is not callable
Matlab Code
MATLAB 代码
fdel=2;
fn=10;
zeta=1/sqrt(2);
spb=100;
npts=2000;
fs=2000;
freq_in = zeros(1,2000); % initialize input frequency array
phi_in = zeros(1,2000); % initialize input phase array
t = (0:(npts-1))/fs; % generate vector of sample times
nsettle = fix(npts/10); % set settle time as 0.1*npts
tsettle = nsettle/fs; % set settle time
%
% % The following three lines of code generate the arrays of the
% % input frequency and input phase.
%
phin1 = 2*pi*fdel*(t-tsettle);
freq_in = [zeros(1,nsettle),fdel*ones(1,npts-nsettle)];
phi_in = [zeros(1,nsettle),phin1(1,(nsettle+1):npts)];
%
% % Generate the QPSK input signal and input signal.
%
nbits = npts/spb; % Determine number of bits
md = zeros(1,nbits*spb);
bits = round(rand(1,nbits));
for m=1:nbits
for n=1:spb
index = (m-1)*spb + n;
% error making line
md(1,index) = (-1)^bits(m);
end
end
Python Code
Python代码
fdel=2
fn=10
zeta=1/sqrt(2)
spb=100
npts=2000
fs=2000
freq_in=zeros(2000)
phi_in=zeros(2000)
t=(arange(0,npts-1))/fs
nsettle=fix(npts/10)
tsettle=nsettle/fs
phin1=2*pi*fdel*(t-tsettle)
freq_in=array([zeros(nsettle),fdel*ones(npts-nsettle)])
phi_in=array([zeros(nsettle),phin1[nsettle+1:npts]])
nbits=npts/spb
md=zeros(nbits*spb)
bits=around(np.random.uniform((nbits,)))
for m in arange(0,nbits):
for n in arange(0,spb):
index=(m-1)*spb+n
md[index]=(-1)**bits(m)
采纳答案by Saullo G. P. Castro
This error is becase you are indexing an array with ()
instead of []
, example:
此错误是因为您使用()
而不是索引数组[]
,例如:
np.arange(10)(1)
gives:
给出:
TypeError: 'numpy.ndarray' object is not callable
While:
尽管:
np.arange(10)[1]
gives:
给出:
1
回答by jabaldonedo
As Saullo pointed out in his answer you are not indexing in the right way, however you are not porting the code correctly. freq_in
and phi_in
are not properly defined and you are not generating a random vector in bits
, take a look to the following code:
正如 Saullo 在他的回答中指出的那样,您没有以正确的方式编制索引,但是您没有正确移植代码。freq_in
并且phi_in
没有正确定义并且您没有在 中生成随机向量bits
,请查看以下代码:
import numpy as np
fdel = 2
fn = 10
zeta = 1 / np.sqrt(2)
spb = 100
npts = 2000
fs = 2000
freq_in = np.zeros((2000))
phi_in = np.zeros((2000))
t = np.arange(0,npts) / fs
nsettle = np.fix(npts/10)
tsettle = nsettle
phin1 = 2 * np.pi * fdel * (t-tsettle)
freq_in = np.hstack((np.zeros(nsettle), fdel * np.ones(npts-nsettle)))
phi_in = np.hstack((np.zeros(nsettle), phin1[int(nsettle):int(npts)]))
nbits = npts / spb
md = np.zeros(nbits * spb);
bits = np.random.randint(2, size=nbits)
for m in np.arange(0,nbits):
for n in np.arange(0,spb):
index = (m-1) * spb + n
md[index]=(-1)**bits[m]