我在使用 Numpy 索引时遇到了一点困难。该脚本只给出最后一个数组的索引三次,而它应该给出三个不同数组的索引(脚本中的 F_fit)。我确信这是一件简单的事情,但我还没有弄清楚。3_phases.txt 文件包含这 3 行
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)))))
任何帮助,将不胜感激。