0
我想要實現使用Python中的矩陣以下循環:是否有可能使這些矩陣運算在python 2.7(numpy)中沒有循環?
import numpy as np
n = 5 # samples
k = 2 # inputs
m = 3 # gaussians
# X is nxk
X = np.array([[0.0, 10.0], [20.0, 30.0],[40, 50],[60,70],[80,90]])
#locations is mxk
locations = np.array([[0.01, 0.02], [0.03,0.04], [0.05, 0.06]])
dev = np.empty([n,k,m])
for samples in range(n):
for inputs in range(k):
for gaussians in range(m):
dev[samples,inputs,gaussians]=X[samples,inputs]-locations[gaussians,inputs]
output = np.empty([n,m])
for samples in range(n):
for gaussians in range(m):
output[samples,gaussians]=np.sum(dev[samples,:,gaussians]*dev[samples,:,gaussians])
我知道numpy的是能夠使使用不同尺寸(Broadcast)陣列的操作,但我不能在這裏使用這個概念。請注意,我基本上做的是去除矢量樣本的平均值並計算其平方規範。
你既需要'開發'和'output'或者你只需要'output'和'dev'就是中間結果? – Psidom
我只需要「輸出」! – DanielTheRocketMan