我有一个来自 kaggle.com 的 python 项目。我在读取数据集时遇到问题。它有一个csv 文件。我们需要读入它并将目标和训练部分放入数组中。
这里是数据集的前 3 行(目标列是第 19 列,特征是前 18 列):
user gender age how_tall_in_meters weight body_mass_index x1
debora Woman 46 1.62 75 28.6 -3
debora Woman 46 1.62 75 28.6 -3
此处未显示的目标列具有字符串值。
from pandas import read_csv
import numpy as np
from sklearn.linear_model.stochastic_gradient import SGDClassifier
from sklearn import preprocessing
import sklearn.metrics as metrics
from sklearn.cross_validation import train_test_split
#d = pd.read_csv("data.csv", dtype={'A': np.str(), 'B': np.str(), 'S': np.str()})
dataset = np.genfromtxt(open('data.csv','r'), delimiter=',', dtype='f8')[1:]
target = np.array([x[19] for x in dataset])
train = np.array([x[1:] for x in dataset])
print(target)
我得到的错误是:
Traceback (most recent call last):
File "C:\Users\Cameron\Desktop\Project - Machine learning\datafilesforproj\SGD_classifier.py", line 12, in <module>
dataset = np.genfromtxt(open('data.csv','r'), delimiter=',', dtype='f8')[1:]
File "C:\Python33\lib\site-packages\numpy\lib\npyio.py", line 1380, in genfromtxt
first_values = split_line(first_line)
File "C:\Python33\lib\site-packages\numpy\lib\_iotools.py", line 217, in _delimited_splitter
line = line.split(self.comments)[0]
TypeError: Can't convert 'bytes' object to str implicitly