简体   繁体   English

ValueError:无法将字符串转换为浮点数:'f'

[英]ValueError: could not convert string to float: 'f'

Want to use Decision Tree algorithm. 要使用决策树算法。 But getting some error as ValueError: could not convert string to float: 'f'. 但是由于出现ValueError:错误而无法将字符串转换为float:'f'。 Kindly help me where am I missing. 请帮助我,我在哪里想念。

import numpy as np
import pandas as pd

#Plotting
import matplotlib.pyplot as plt

#Machine Learning Libraries

from sklearn.neighbors import KNeighborsClassifier
from sklearn import tree   
from sklearn.model_selection import train_test_split

from sklearn import metrics
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix



#Loading Dataset
My_dataset = pd.read_csv('mushrooms.csv');
print (My_dataset.head())
print (My_dataset.shape)
#Dividing the datasets into Indicator and Predictor Variables
My_data = My_dataset.iloc[:,1:23].values
My_target = My_dataset.iloc[:,0].values
print()
print(My_data)
print()
print(My_target)
mushroom_train,mushroom_test,mushroomtarget_train,mushroomtarget_test = \
train_test_split(My_data,My_target, test_size = 0.3)

DT_Model_Mushroom = tree.DecisionTreeClassifier() 
DT_Model_Mushroom_Fitted = DT_Model_Mushroom.fit(mushroom_train, mushroomtarget_train)

Error: 错误:

Error
return array(a, dtype, copy=False, order=order)

ValueError: could not convert string to float: 'f'

The Decision Tree Classifier in scikit learn does not take Strings as input. scikit learning中的决策树分类器不将字符串作为输入。

If you have categorical variables in your data, you should encode them before (with one of the sklearn encoder for example : One hot encoder , Ordinal Encoder , ... ) 如果数据中包含分类变量,则应事先对其进行编码(例如,使用sklearn编码器之一: 一个热编码器Ordinal编码器 ,...)

If you don't have categorical variables in your data, pandas may not be able to correctly attribute types to your columns. 如果您的数据中没有分类变量,则熊猫可能无法正确将类型归因于您的列。 If this situation you should use the "dtype" argument of the read_csv function. 如果出现这种情况,则应使用read_csv函数的“ dtype ”参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM