Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

python - How to append a selection of a numpy array to an empty numpy array

I have a three .txt files to which I have successfully made into a numpy array. If you are curious these files are Level 2 data from the Advanced Composition Experiment (ACE). The particular files are found in the MAG and SWEPAM sections and are 16 second average and 64 second average, respectively. The data in a nut shell is representative of the z-component magnetic field of an inbound particle field, its constituents by measure of counts per area, and its velocity. Currently the focus of the study is on inbound hydrogen, but I digress. The code is as follows I use to read and save the files (as well as fix any errors) is provided below:

Bz = np.loadtxt(r"/home/ary/Desktop/Arya/Project/Data/AC/MAG/ACE_MAG_Data_SEPT_18_2015.txt", dtype = bytes).astype(float)
SWEPAM_HV = np.loadtxt(r"/home/ary/Desktop/Arya/Project/Data/ACE/SWEPAM/Proton_Density/ACE_SWEPAM_H_Density_20150918.txt", dtype = bytes).astype(float)
SWEPAM_HD = np.loadtxt(r"/home/ary/Desktop/Arya/Project/Data/ACE/SWEPAM/Proton_Speed/ACE_SWEPAM_H_Velocity_20150918.txt",dtype = bytes).astype(float)

Bz = np.ma.masked_array(Bz, Bz <= -999, fill_value = 0)
SWEPAM_HD = np.ma.masked_array(SWEPAM_HD, SWEPAM_HD <= -999, fill_value = 0)
SWEPAM_HV = np.ma.masked_array(SWEPAM_HV, SWEPAM_HV <= -999, fill_value = 0)

Mag_time = np.arange(0,86400, 16, dtype = float)
SWEPAM_time = np.arange(0,86400,64, dtype = float)

However, within these array I am particularly interested in only the 1349th position to the 2024th position. These numbers are of interest because of my investigation into an anomaly which happened between these two points. So I figured the following would lead me to success. To which it hasn't and many variations have failed too. I present to you the most recent script I have right now:

Mag_time_prime = np.array([])
Bz_prime = np.array([])
for i in range(1349,2024):
    append(Mag_time_prime,Mag_time[i]).astype(float)
    append(Bz_prime,Bz[i]).astype(float)
print(Mag_time_prime.shape)
print(Bz_prime.shape)

I had figured that by making empty arrays (I did try np.empty(0) for the primes and couldn't get that to work for me) that I could just make a for loop to locate and append the i_th position from the Bz and Mag_time to the empty 'prime' arrays within the specified range. However the 'prime' arrays have continuously popped out empty arrays. So my question, where have I gone wrong and how should I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

List append acts on the list itself:

In [1]: alist = []
In [2]: alist.append(5)
In [3]: alist.append(3)
In [4]: alist
Out[4]: [5, 3]

np.append does not change its arguments:

In [5]: arr = np.array([])
In [6]: np.append(arr,1)
Out[6]: array([ 1.])
In [7]: np.append(arr,2)
Out[7]: array([ 2.])
In [8]: arr
Out[8]: array([], dtype=float64)

You have to assign the value of append back to arr to get the list equivalent behavior:

In [9]: arr=np.append(arr,1)
In [10]: arr=np.append(arr,2)
In [11]: arr
Out[11]: array([ 1.,  2.])

Each time you use np.append you create a new copy (it uses np.concatenate). For one or two times that's ok, but if done repeatedly it is inefficient.

The preferred way is to use list append to build a list, and then make an array from that:

In [12]: np.array(alist)
Out[12]: array([5, 3])

You have to understand np.concatenate before you can use np.append properly. It is a poor substitute for list append.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...