英文:
How to skip a row of zeros with np.loadtxt?
问题
我有一个包含许多行数值的.txt文件。每一行看起来如下
3.896784 0.465921 1.183185 5.468042 ...
,其中数值根据行不同而异。
此外,每一行都包含900个数值。
有些行不包含真实数据,只包含900个零,如下所示
0 0 0 0 0 0 0 0 ...
。
我知道如何跳过只包含一个值的行,即一个零,例如通过以下代码:
import numpy as np
data = np.loadtxt("pathtodata")
data = data[~(data==0)]
但是这段代码无法处理每行包含2个或更多零值的情况。是否有一种方法可以不加载只包含900个或任意数量的零(或特定整数)的行?
英文:
I have a .txt file that contains many rows of numeric values. Each row looks as follows
3.896784 0.465921 1.183185 5.468042 ...
, where the values differ depending on the row.
Furthermore, every row contains 900 values.
Some rows do not contain real data, but only 900 zeros as follows
0 0 0 0 0 0 0 0 ...
.
I know how to skip rows that only contain one value, i.e., one zero, such as via the following code:
import numpy as np
data = np.loadtxt("pathtodata")
data = data[~(data==0)]
But this code does not work for 2 zero values or more per row. Is there a way to not load rows that only contain 900 or any arbitrary number of zeros (or a specific integer)?
答案1
得分: 2
import numpy as np
data = np.loadtxt("data.txt")
data = data[~np.all(data == 0, axis=1)]
for row in data:
print(row)
这应该可以解决问题,np.all(data == 0, axis=1)
本质上就是你所要求的,它检查每行中的每个元素是否满足条件,在这里你排除了每个元素都等于0的行。
英文:
import numpy as np
data = np.loadtxt("data.txt")
data = data[~np.all(data == 0, axis=1)]
for row in data:
print(row)
Should do the trick, np.all(data == 0, axis=1)
is essentially what you are asking for, it checks if every element in a given row verify a condition, here you exclude every row where every element are equal to 0
答案2
得分: 1
你可以使用 np.any
来实现这个功能。它会检查沿着特定轴是否有非零值。所以当一行全是零时,它会返回 False
。然后你可以使用这个输出来对数据集进行子集化:
import numpy as np
test = np.arange(20).reshape((4, -1))
test[0, :] = 0
print(test)
mask = np.any(test, axis=1)
print(test[mask, :])
英文:
You can use np.any
to do that. It will check if there are any values other than 0 along a certain axis. So it will return False
when a row is all zeros. Then you can use the ouptut of this to subset your dataset:
import numpy as np
test = np.arange(20).reshape((4,-1))
test[0,:] = 0
print(test)
mask = np.any(test, axis=1)
print(test[mask,:])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论