无法将字符串转换为浮点数 - 使用Pandas和Numpy作为SVM分类器

问题描述 投票:0回答:1

我正在尝试使用pandas来创建SVM分类器。我已经生成了我的功能并使用pandas lib中的to_csv保存它。此功能(颜色)包含整个直方图。所以,我有一个每行0到255浮点值的列表。有362行。这是我的一段代码:

if __name__ == '__main__':
    train = pd.read_csv('Train.csv',index_col='Object')
    XTrain = train['Color']
    ColorLabel = train['ColorLabel']
    leTrain = LabelEncoder()
    leTrain.fit(ColorLabel)
    ColorLabel = leTrain.transform(ColorLabel)

    svm = SVC()
    parameters = {'kernel': ('linear', 'rbf'), 'C': (1, 0.25, 0.5, 0.75,0.05), 'gamma': (0.5,1, 2, 3, 'auto'),
                  'decision_function_shape': ('ovo', 'ovr'),'class_weight': [{0: 1,1: w2} for w2 in [2, 4, 6, 10,12]]}
    clf = GridSearchCV(svm, parameters,verbose = 2)
    clf.fit(XTrain, ColorLabel)

我只是想在SVC.fit中使用功能列Color,但是我收到一条错误消息:

返回数组(a,dtype,copy = False,order = order)

ValueError:无法将字符串转换为float:'[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,1.0,0.0,2.0,10.0,0.0,2.0,0.0,7.0,0.0,0.0.0,2.0,18.0,36.0,0.0,87.0,34.0,13.0,41.0,30.0,118.0,137.0,169.0 ,530.0,4684.0,5746.0,1975.0,1815.0,4079.0,4725.0,2411.0,131.0,434.0,3799.0,1435.0,4380.0,5.0,0.0,546.0,0.0,1695.0,15.0,0.0,116.0,82.0,4.0,52.0,54.0 ,4.0,2.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0]'

这是我的Train.csv的负责人

对象,峰度,歪斜,颜色,TextureLabel,ColorLabel 0122_LSG.jpg,-0.19026044432874611,-0.9694201939544961,“[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,10.0,6.0,16.0,90.0,47.0,114.0,126.0,1918.0,733.0,5404.0,3956.0 ,12750.0,13551.0,3229.0,3927.0,5776.0,4896.0,3807.0,9007.0,8835.0,1029.0,684.0,495.0,172.0,121.0,125.0,37.0,93.0,31.0,96.0,73.0,7.0,1.0.0,0.0,22.0,0.0 ,0.0,7.0,5.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0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75.0,4.0,3.0.0,1.0,94.0, 0.0,18.0,198.0 ,0.0,4.0,225.0,16.0,158.0,147.0,8.0,0.0,6.0,22.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.0,0.0]“,L,S 0157_LSP.jpg,-0.604961472275447,-0.8074495729146061,”[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,0 .0,0.0,1.0,0.0,0.0,5.0,0.0,48.0,0.0,0.0,0.0,0.0,28.0,0.0,

我试过所有类型的铸造,我知道astype,dtype,转换器...请帮助我

python pandas numpy svm
1个回答
0
投票

XTrain = [list(map(float,hist))列表中的hist ['Color']]

加上使用;当读取和写入文件SOLVE IT时作为sep。

© www.soinside.com 2019 - 2024. All rights reserved.