0

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.

shx2
64.7k17 gold badges139 silver badges166 bronze badges
asked Apr 26, 2013 at 10:06
0

2 Answers 2

2

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)))))
answered Apr 26, 2013 at 21:10

Comments

0

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?

answered Apr 26, 2013 at 10:12

2 Comments

well, F_cont has three arrays and I want to pick the values from each array giving the same indices of I_data for each array. But I am having only three times the values for the last arrays instead of all three arrays of F_cont
@user2095624, No it doesn't, it's a 1d array and F_cont.shape is (2000,)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.