我在使用Numpy索引時遇到了一些小問題。當腳本給出三個不同數組的索引(腳本中的F_fit)時,該腳本只給出最後一個數組的索引。我相信這是一件簡單的事情,但我還沒有弄明白。該3_phases.txt文件中包含這3個行Python Numpy數組索引
1 -1 -1 -1 1 1
1 1 1 -1 1 1
1 1 -1 -1 -1 1
下面是代碼:
import numpy as np
import matplotlib.pyplot as plt
D = 12.96
n = np.arange(1,7)
F0 = 1.0
x = np.linspace(0.001,4,2000)
Q = 2*np.pi*np.array([1/D, 2/D, 3/D, 4/D, 5/D, 6/D])
I = (11.159, 43.857, 26.302, 2.047, 0.513, 0.998)
phase = np.genfromtxt('3_phases.txt')
for row in phase:
F = (np.sqrt(np.square(n)*I/sum(I)))*row
d = sum(i*(np.sin(x*D/2+np.pi*j)/(x*D/2+np.pi*j))for i,j in zip(F,n))
e = sum(i*(np.sin(x*D/2-np.pi*j)/(x*D/2-np.pi*j))for i,j in zip(F,n))
f_0 = F0*(np.sin(x*D/2)/(x*D/2))
F_cont = np.array(d) + np.array(e) + np.array(f_0)
plt.plot(x,F_cont,'r')
#plt.show()
plt.clf()
D2 = 12.3
I2 = (9.4, 38.6, 8.4, 3.25, 0, 0.37)
Q2 = 2*np.pi*np.array([1/D2, 2/D2, 3/D2, 4/D2, 5/D2, 6/D2])
n2 = np.arange(1,7)
for row in phase:
F2 = (np.sqrt(np.square(n2)*I2/sum(I2)))*row
plt.plot(Q2,F2,'o')
#plt.show()
F_data = F2
Q_data = Q2
I_data = np.around(2000*Q2/(4-0.001))
I_data = np.array(map(int,I_data))
F_fit = F_cont[I_data]
print F_fit
R2 = (1-(sum(np.square(F_data-F_fit))/sum(np.square(F_data-np.mean(F_data)))))
任何幫助,將不勝感激。
好吧,F_cont有三個數組,我想從每個數組中選取給出每個數組的相同索引I_data的值。但我只有最後一個數組的值的三倍,而不是F_cont – user2095624 2013-04-26 10:31:09
@ user2095624的所有三個數組,它不是,它是一個1d數組,而'F_cont.shape'是'(2000,)' – askewchan 2013-04-26 20:30:46