gpt4 book ai didi

python - 当只有 1 行时,数组的形状和 __len__ 返回 4?

转载 作者:行者123 更新时间:2023-11-30 22:31:06 26 4
gpt4 key购买 nike

对 CSV 文件执行 np.loadtxt() 后,我看到基于仅包含 1 行数据的文件的以下结果。

dcsv: [ 45.29625588 -75.92420601  64.66075832   2.25      ]
dcsv shape: (4L,)
dcsv type: float64
DataSet Length dcsv: 4 Rows

我不知道,为什么它认为有4行数据。它识别出有 0 行和 2 行或更多,但当只有 1 行时返回 4。

2 行示例:

dcsv: [[ 45.29638366 -75.92428038  64.8255732    1.        ]
[ 45.29638376 -75.92427927 64.82507261 13. ]]
dcsv shape: (2L, 4L)
dcsv type: float64
DataSet Length dcsv: 2 Rows

0 行示例:

dcsv: []
dcsv shape: (0L,)
dcsv type: float64
DataSet Length dcsv: 0 Rows
dcsv = []
dcsv = np.loadtxt('Start5SecondsSIMPy2JS.csv',delimiter=',',skiprows=0,usecols = (0,1,2,3))
print "dcsv:", dcsv
print "dcsv shape:", dcsv.shape
print "dcsv type: ", dcsv.dtype
print "DataSet Length dcsv:",dcsvLen,"Rows"

如果您提供解决方案,我们将不胜感激。

我需要处理 0,1,2.....10,000+ 行的解决方案。 np.atleast_2d 和 ndim 都生成 1、0 行和 1 行的结果。我的解决方案是使用 np.size,它返回 0 表示 0 行,4 表示 1 行,并且这些方法中的任何一种都允许我辨别具有 0 行或 1 行的文件。任意数量 >= 2 的行都将由 np.ndarray.len 处理。

最佳答案

出于某种原因,numpy.loadtxt 默认压缩长度为 1 的轴,而不是保留输入的行列结构。您可以指定 ndmin=2 来保留结构:

ndmin : int, optional
The returned array will have at least ndmin dimensions. Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2.

关于python - 当只有 1 行时,数组的形状和 __len__ 返回 4?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45909788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com