将输出转换为numpy数组

时间:2022-11-11 01:42:15

I am facing the following issue. I have 4 data files (Data_1...Data_4) and I am trying to get a count of the number of items in column 2 which are less than or equal to 5. My following code does that job.

我面临以下问题。我有4个数据文件(Data_1 ... Data_4),我试图计算第2列中小于或等于5的项目数。我的下面的代码完成了这项工作。

import numpy as np
filelist=[]
for i in list(range(1,5)):
    filelist.append("/Users/Hrihaan/Desktop/Data_%s.txt" %i)
for fname in filelist:
    data=np.loadtxt(fname)
    z=data[:,1]
    count= len([i for i in z if i <= -5]) # output 5 3 0 9
    x= np.array(count)
    Average=np.mean(x)
    print(Average)

But I am stuck on how to deal with the output (5,3,0,9), I want to create an array from the output (count) to do simple mathematical calculations such as finding mean or median.So the mean should be (5+3+0+9/4 = 4.25) but when I tried the print(average), I am getting the same output 5 3 0 9, not the mean.

但我坚持如何处理输出(5,3,0,9),我想从输出(计数)创建一个数组来做简单的数学计算,如找到平均值或中位数。所以平均值应该是(5 + 3 + 0 + 9/4 = 4.25)但是当我尝试打印(平均值)时,我得到相同的输出5 3 0 9,而不是平均值。

Any help would be greatly appreciated.

任何帮助将不胜感激。

2 个解决方案

#1


2  

Instead of creating an array for every cycle in the loop, first create a list and convert this list into an array after the loop has finished.

而不是为循环中的每个循环创建一个数组,首先创建一个列表,并在循环结束后将此列表转换为数组。

import numpy as np
filelist=[]
for i in list(range(1,5)):
    filelist.append("/Users/Hrihaan/Desktop/Data_%s.txt" %i)

counts = []
for fname in filelist:
    data=np.loadtxt(fname)
    z=data[:,1]
    count= len([i for i in z if i <= -5]) # output 5 3 0 9
    counts.append(count)

x = np.array(counts)
Average=np.mean(x)
print(Average)

#2


1  

I'm not sure I understand what you're asking, and your code can't be reproduced, as we don't have your files.

我不确定我理解你在问什么,你的代码无法复制,因为我们没有你的文件。

But if I understand right, you should remove len() from your code :

但如果我理解正确,你应该从你的代码中删除len():

for fname in filelist:
    #...
    count = [i for i in z if i <= -5]
    count = [5, 3, 0, 9] # for instance
    x = np.array(count) # x will output: array([5, 3, 0, 9])
    Average = np.mean(x) 
    print(Average) # will print: 4.25

#1


2  

Instead of creating an array for every cycle in the loop, first create a list and convert this list into an array after the loop has finished.

而不是为循环中的每个循环创建一个数组,首先创建一个列表,并在循环结束后将此列表转换为数组。

import numpy as np
filelist=[]
for i in list(range(1,5)):
    filelist.append("/Users/Hrihaan/Desktop/Data_%s.txt" %i)

counts = []
for fname in filelist:
    data=np.loadtxt(fname)
    z=data[:,1]
    count= len([i for i in z if i <= -5]) # output 5 3 0 9
    counts.append(count)

x = np.array(counts)
Average=np.mean(x)
print(Average)

#2


1  

I'm not sure I understand what you're asking, and your code can't be reproduced, as we don't have your files.

我不确定我理解你在问什么,你的代码无法复制,因为我们没有你的文件。

But if I understand right, you should remove len() from your code :

但如果我理解正确,你应该从你的代码中删除len():

for fname in filelist:
    #...
    count = [i for i in z if i <= -5]
    count = [5, 3, 0, 9] # for instance
    x = np.array(count) # x will output: array([5, 3, 0, 9])
    Average = np.mean(x) 
    print(Average) # will print: 4.25