gpt4 book ai didi

python - 我想将 .csv 文件转换为 Numpy 数组

转载 作者:行者123 更新时间:2023-12-03 18:50:09 30 4
gpt4 key购买 nike

我想转换一个 mydata.csv文件到 Numpy 数组。

我有一个矩阵表示 mydata.csv文件
(矩阵是 14*79,带符号值,没有任何标题名称。)

-0.094391   -0.086641   0.31659 0.66066 -0.33076    0.02751 …
-0.26169 -0.022418 0.47564 0.39925 -0.22232 0.16129 …
-0.33073 0.026102 0.62409 -0.098799 -0.086641 0.31832 …
-0.22134 0.15488 0.69289 -0.26515 -0.021011 0.47096 …

我认为这段代码适用于这种情况。
import numpy as np

data = np.genfromtxt('mydata.csv', dtype=float, delimiter=',', names=False)

但它没有用。

我希望最终的 Numpy 数据形状为 data.shape = (14, 79)
虽然我的错误消息看起来像这样..
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-060012d7c568> in <module>
1 import numpy as np
2
----> 3 data = np.genfromtxt('output.csv', dtype=float, delimiter=',', names=False)

~\Anaconda3\envs\tensorflow\lib\site-packages\numpy\lib\npyio.py in genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows, encoding)
1810 deletechars=deletechars,
1811 case_sensitive=case_sensitive,
-> 1812 replace_space=replace_space)
1813 # Make sure the names is a list (for 2.5)
1814 if names is not None:

~\Anaconda3\envs\tensorflow\lib\site-packages\numpy\lib\_iotools.py in easy_dtype(ndtype, names, defaultfmt, **validationargs)
934 # Simple dtype: repeat to match the nb of names
935 if nbtypes == 0:
--> 936 formats = tuple([ndtype.type] * len(names))
937 names = validate(names, defaultfmt=defaultfmt)
938 ndtype = np.dtype(list(zip(names, formats)))

TypeError: object of type 'bool' has no len()

最佳答案

尝试这个:

import pandas as pd
import numpy as np
mydata = pd.read_csv("mydata.csv")
mydata_array = np.array(mydata)

Out:
array([[-0.26169 , -0.022418, 0.47564 , 0.39925 , -0.22232 , 0.16129 ],
[-0.33073 , 0.026102, 0.62409 , -0.098799, -0.086641, 0.31832 ],
[-0.22134 , 0.15488 , 0.69289 , -0.26515 , -0.021011, 0.47096 ]])

关于python - 我想将 .csv 文件转换为 Numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58565642/

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