i=np.arange(1,4,dtype=np.int)
a=np.arange(9).reshape(3,3)
Run Code Online (Sandbox Code Playgroud)
和
a
>>>array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
a[:,0:1]
>>>array([[0],
[3],
[6]])
a[:,0:2]
>>>array([[0, 1],
[3, 4],
[6, 7]])
a[:,0:3]
>>>array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
Run Code Online (Sandbox Code Playgroud)
现在我想对数组进行矢量化以将它们一起打印出来.我试试
a[:,0:i]
Run Code Online (Sandbox Code Playgroud)
要么
a[:,0:i[:,None]]
Run Code Online (Sandbox Code Playgroud)
它给出了TypeError:只有整数标量数组可以转换为标量索引
j=np.arange(20,dtype=np.int)
site=np.ones((20,200),dtype=np.int)
sumkma=np.ones((100,20))
[sumkma[site[x],x] for x in range(20)]
Run Code Online (Sandbox Code Playgroud)
这有效,但我不想使用for循环.当我尝试
sumkma[site[j],j]
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
IndexError:形状不匹配:索引数组不能与形状一起广播(20,200)(20,)
如何修复错误?
我什么时候应该使用@vectorize?
我试过@jit并显示下面的部分代码,
from numba import jit
@jit
def kma(g,temp):
k=np.exp(-(g+np.abs(g))/(2*temp))
return k
Run Code Online (Sandbox Code Playgroud)
但我的代码没有加速算法.为什么?
我尝试将n*100*1003d矩阵对角化K,numpy.linalg.eig并获得特征值w和特征向量v。矩阵是100*100,但是我想通过广播来做,这就是n我设置的数字。矩阵不是厄米矩阵。
w,v=np.linalg.eig(K)
Run Code Online (Sandbox Code Playgroud)
起初,我尝试了n=1000,我得到真正的特征向量,即xxxxxxxxxe+xx,但当我尝试n=2000的,元素w和v表演xxxxxxxxxe+xx+0.j。由于+0.j,使用时给出了复数w并v进行了进一步的计算。
+0.j?我想绘制一个没有科学记数法的对数刻度图。
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.plot(np.arange(0,10,0.1))
plt.xscale('log')
plt.yscale('log')
plt.xlim(0.1,100)
plt.ylim(1,10)
plt.gca().xaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
plt.gca().yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
plt.show()
Run Code Online (Sandbox Code Playgroud)
问题:
a=np.arange(240).reshape(3,4,20)
b=np.arange(12).reshape(3,4)
c=np.zeros((3,4),dtype=int)
x=np.arange(3)
y=np.arange(4)
Run Code Online (Sandbox Code Playgroud)
我想通过以下没有循环的步骤获得2d(3,4)形状数组。
for i in x:
c[i]=a[i,y,b[i]]
c
array([[ 0, 21, 42, 63],
[ 84, 105, 126, 147],
[168, 189, 210, 231]])
Run Code Online (Sandbox Code Playgroud)
我试过了,
c=a[x,y,b]
Run Code Online (Sandbox Code Playgroud)
但它显示
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (4,) (3,4)
然后我还试图建立newaxis的[:,None],它也不起作用。
a=np.zeros((3,3,3))
b=np.arange(3)
c=np.arange(9).reshape(3,3)
Run Code Online (Sandbox Code Playgroud)
我想将数组的元素b或c沿着 3d 矩阵(张量)的对角线(或对角线上方/下方)a相对于特定轴放置。
我累了numpy.diagflat,但它只适用于二维矩阵。
例如,如何制作以下矩阵?
array([[[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 2.]],
[[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 2.]],
[[ 0., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 2.]]])
Run Code Online (Sandbox Code Playgroud) numpy ×7
python ×7
indexing ×3
matrix ×2
arrays ×1
broadcast ×1
diagonal ×1
eigenvalue ×1
jit ×1
matplotlib ×1
numba ×1
plot ×1
python-3.x ×1
scipy ×1