Python Sci-Kit Learn : Multilabel Classification ValueError: could not convert string to float:

南笙酒味 提交于 2019-12-05 20:06:04

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!