加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 使用numpy.genfromtxt给出TypeError:无法隐式地将’b

发布时间:2020-12-20 11:35:11 所属栏目:Python 来源:网络整理
导读:我在 python中有一个来自kaggle.com的项目.我在读取数据集时遇到问题.它有一个csv文件.我们需要将其读入并放入目标并将其中的一部分训练成阵列. 以下是前3行数据集(目标列是第19列,功能是前18列): user gender age how_tall_in_meters weight body_mass_ind
我在 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:UsersCameronDesktopProject - Machine learningdatafilesforprojSGD_classifier.py",line 12,in <module>
    dataset = np.genfromtxt(open('data.csv',dtype='f8')[1:]
  File "C:Python33libsite-packagesnumpylibnpyio.py",line 1380,in genfromtxt
    first_values = split_line(first_line)
  File "C:Python33libsite-packagesnumpylib_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',dtype='f8')[1:]

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读