RandomForestClassfier.fit(): ValueError: could not convert string to float

后端 未结 8 871
礼貌的吻别
礼貌的吻别 2020-12-23 09:16

Given is a simple CSV file:

A,B,C
Hello,Hi,0
Hola,Bueno,1

Obviously the real dataset is far more complex than this, but this one reproduces

相关标签:
8条回答
  • 2020-12-23 09:59

    LabelEncoding worked for me (basically you've to encode your data feature-wise) (mydata is a 2d array of string datatype):

    myData=np.genfromtxt(filecsv, delimiter=",", dtype ="|a20" ,skip_header=1);
    
    from sklearn import preprocessing
    le = preprocessing.LabelEncoder()
    for i in range(*NUMBER OF FEATURES*):
        myData[:,i] = le.fit_transform(myData[:,i])
    
    0 讨论(0)
  • 2020-12-23 09:59

    You can't pass str to your model fit() method. as it mentioned here

    The training input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csc_matrix.

    Try transforming your data to float and give a try to LabelEncoder.

    0 讨论(0)
提交回复
热议问题