英文:
How can I use genfromtxt in numpy to get 2D array instead of tupled or 1-D array
问题
a = np.genfromtxt("winequality-red.csv", delimiter=":", dtype=None, encoding=None, skip_header=1, missing_values="??")
我想要获取一个二维数组。我知道数据集可能不均匀,但我可以采取什么措施来处理它,以获取一个容易切片的数组?
英文:
a=np.genfromtxt("winequality-red.csv", delimiter=":", dtype=None, encoding=None,\
skip_header=1, missing_values="??")
['7.40,0.70,0.00,1.90,0.08,11.00,34.00,1.00,3.51,0.56,9.40,5.00'
'7.80,0.88,0.00,2.60,0.10,25.00,67.00,1.00,3.20,0.68,9.80,5.00'
'7.80,0.76,0.04,2.30,0.09,15.00,54.00,1.00,3.26,0.65,9.80,5.00' ...
'6.30,0.51,0.13,2.30,0.08,29.00,40.00,1.00,3.42,0.75,11.00,6.00'
'5.90,0.65,0.12,2.00,0.08,32.00,44.00,1.00,3.57,0.71,10.20,5.00'
'6.00,0.31,0.47,3.60,0.07,18.00,42.00,1.00,3.39,0.66,11.00,6.00']
I want to get 2-D array. I know the dataset maybe not homologous but what tick can I do to deal with that and get an array witch is easy to slice?
答案1
得分: -1
你面临的问题是你使用了错误的分隔符并将整个行读取为单个字符串。您可以使用以下代码将CSV文件读取到二维数组中:
import numpy as np
# 将文件读取为字符串的一维数组
a = np.genfromtxt("winequality-red.csv", delimiter="\n", dtype=str, skip_header=1)
# 转换为浮点数的二维数组
data = np.array([list(map(float, line.split(','))) for line in a])
# 得到的二维数组
print(data)
确保您的CSV文件具有相同列数的行,这段代码将为您提供所需的二维数组。
英文:
The issue you're facing is that you're using the wrong delimiter and reading the entire row as a single string. You can use the following code to read the CSV file into a 2D array:
import numpy as np
# Read the file as 1D array of strings
a = np.genfromtxt("winequality-red.csv", delimiter="\n", dtype=str, skip_header=1)
# Convert to a 2D array of floats
data = np.array([list(map(float, line.split(','))) for line in a])
# Resulting 2D array
print(data)
Make sure your CSV file has rows with the same number of columns, and this code will give you the 2D array you need.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论