简体   繁体   中英

Python Numpy Array indexing

I am having a small difficulty with Numpy indexing. The script gives only the index of the last array three times when it supposed to give index of three different arrays (F_fit in the script). I am sure it is a simple thing, but I haven't figured it out yet. The 3_phases.txt file contains these 3 lines

1  -1   -1  -1   1   1
1   1    1  -1   1   1
1   1   -1  -1  -1   1

Here is the code:

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)))))

Any help would be appreciated.

You are redefining F_cont each time you go through your first loop. By the time you get to your second loop (with all the _2 values) you only have access to the F_cont from the last row .

To fix this, move your _2 definitions above your first loop and only do the loop once, then you'll have access to each F_cont and your printouts will be different.

The following code is identical to yours except for the rearrangement described above, as well as the fact that I implemented my comment from above (using n/D in your Q 's).

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*n/D
I = (11.159, 43.857, 26.302, 2.047, 0.513, 0.998)    
phase = np.genfromtxt('3_phases.txt')

D2 = 12.3
I2 = (9.4, 38.6, 8.4, 3.25, 0, 0.37)
Q2 = 2*np.pi*n/D2
n2 = np.arange(1,7)

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.clf()

    F2 = (np.sqrt(np.square(n2)*I2/sum(I2)))*row
    plt.plot(Q2,F2,'o')
    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_fit is being calculating from I_data , which is in turn being calculated from Q2 . Q2 is set outside the loop, and doesn't depend on row - perhaps you meant I_data to be a function of F2 instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM