Python Sci-Kit学习:多标签分类ValueError:无法将字符串转换为float: [英] Python Sci-Kit Learn : Multilabel Classification ValueError: could not convert string to float:

查看:151
本文介绍了Python Sci-Kit学习:多标签分类ValueError:无法将字符串转换为float:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sci-kit学习0.17进行多标签分类 我的数据看起来像

培训

Col1                  Col2
asd dfgfg             [1,2,3]
poioi oiopiop         [4]

测试

Col1                    
asdas gwergwger    
rgrgh hrhrh

到目前为止我的代码

import numpy as np
from sklearn import svm, datasets
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier

def getLabels():
    traindf = pickle.load(open("train.pkl","rb"))
    X = traindf['Col1']
    y = traindf['Col2']

    # Binarize the output
    from sklearn.preprocessing import MultiLabelBinarizer  
    y=MultiLabelBinarizer().fit_transform(y)      

    random_state = np.random.RandomState(0)


    # Split into training and test
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5,
                                                        random_state=random_state)

    # Run classifier
    from sklearn import svm, datasets
    classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
                                     random_state=random_state))
    y_score = classifier.fit(X_train, y_train).decision_function(X_test)

但是现在我得到了

ValueError: could not convert string to float: <value of Col1 here>

y_score = classifier.fit(X_train, y_train).decision_function(X_test) 

我也必须对X进行二值化吗?为什么我需要将X尺寸转换为浮点型?

解决方案

是的,您必须将X转换为数字表示形式(不是必需的二进制),也要将y转换成数字表示形式.那是因为所有机器学习方法都是在数字矩阵上运算的.

该怎么做呢?如果Col1中的每个样本都可以包含不同的单词(即,它代表一些文本)-您可以使用

i am trying to do multilabel classification using sci-kit learn 0.17 my data looks like

training

Col1                  Col2
asd dfgfg             [1,2,3]
poioi oiopiop         [4]

test

Col1                    
asdas gwergwger    
rgrgh hrhrh

my code so far

import numpy as np
from sklearn import svm, datasets
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier

def getLabels():
    traindf = pickle.load(open("train.pkl","rb"))
    X = traindf['Col1']
    y = traindf['Col2']

    # Binarize the output
    from sklearn.preprocessing import MultiLabelBinarizer  
    y=MultiLabelBinarizer().fit_transform(y)      

    random_state = np.random.RandomState(0)


    # Split into training and test
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5,
                                                        random_state=random_state)

    # Run classifier
    from sklearn import svm, datasets
    classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
                                     random_state=random_state))
    y_score = classifier.fit(X_train, y_train).decision_function(X_test)

but now i get

ValueError: could not convert string to float: <value of Col1 here>

on

y_score = classifier.fit(X_train, y_train).decision_function(X_test) 

do i have to binarize X as well? why do i need to convert the X dimension to float?

解决方案

Yes, you must to transform X into numeric representation (not necessary binary) as well as y. That's because all machine learning methods operate on matrices of numbers.

How to do this exactly? If every sample in Col1 can have different words in it (i.e. it represents some text) - you can transform that column with CountVectorizer

from sklearn.feature_extraction.text import CountVectorizer

col1 = ["cherry banana", "apple appricote", "cherry apple", "banana apple appricote cherry apple"]

cv = CountVectorizer()
cv.fit_transform(col1) 
#<4x4 sparse matrix of type '<class 'numpy.int64'>'
#   with 10 stored elements in Compressed Sparse Row format>

cv.fit_transform(col1).toarray()
#array([[0, 0, 1, 1],
#       [1, 1, 0, 0],
#       [1, 0, 0, 1],
#       [2, 1, 1, 1]], dtype=int64)

这篇关于Python Sci-Kit学习:多标签分类ValueError:无法将字符串转换为float:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆