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

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

问题描述

我有三个 .txt 文件,我已经成功地将它们制作成一个 numpy 数组.如果您好奇这些文件是Level 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 循环定位并附加 BzMag_time 中的 i_th 位置到指定范围内的空 '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 作用于列表本身:

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天全站免登陆