python – 使用numpy.genfromtxt给出TypeError:无法隐式地将’bytes’对象转换为str

我在
python中有一个来自kaggle.com的项目.我在读取数据集时遇到问题.它有一个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

最佳答案 对我有用的是改变界限

dataset = np.genfromtxt(open('data.csv','r'), delimiter=',', dtype='f8')[1:]

dataset = np.genfromtxt('data.csv', delimiter=',', dtype='f8')[1:]

(不幸的是,我不太清楚底层问题是什么)

点赞