如何将选择的numpy数组追加到空的numpy数组 [英] How to append a selection of a numpy array to an empty numpy array

查看:134
本文介绍了如何将选择的numpy数组追加到空的numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个.txt文件,已将它们成功地制成numpy数组.如果您感到好奇,这些文件是 2级数据高级合成实验(ACE).特定文件位于MAG和SWEPAM部分中,分别为16秒平均值和64秒平均值.坚果壳中的数据代表入站粒子场的z分量磁场,通过单位面积计数及其速度来表示的成分.目前,研究的重点是入氢,但我离题了.下面提供了我用来读取和保存文件(以及修复任何错误)的代码,如下所示:

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)

但是,在这些数组中,我仅对第1349位到2024位特别感兴趣.由于我调查了这两点之间发生的异常,因此这些数字很有趣.因此,我认为以下内容将使我走向成功.它尚未实现,并且许多变体也都失败了.我向您介绍我现在拥有的最新脚本:

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)

我已经知道,通过制作空数组(我尝试使用np.empty(0)作为质数,却无法使它为我工作),我可以做一个for循环来定位并附加i_th位置. BzMag_time到指定范围内的空'prime'数组.但是,素数"数组不断弹出空数组.所以我的问题是,我哪里出错了,应该如何解决?

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?

推荐答案

列表追加对列表本身起作用:

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不会更改其参数:

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)

您必须将append的值分配回arr才能获得列表的等效行为:

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

每次使用np.append时,都会创建一个新副本(它使用np.concatenate).可以过一两次,但是如果反复进行,效率会很低.

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

您必须先了解np.concatenate,然后才能正确使用np.append.它不能替代列表附加.

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

这篇关于如何将选择的numpy数组追加到空的numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆