我在这方面遇到困难。我在以下情况中遇到了ValueError :(我已经在线尝试了解决方案,但无济于事)

这是我的原始代码,该代码返回将String转换为Float错误


  ValueError:无法将字符串转换为float:'3,1,0,0,0,1,0,1,89874,49.99'):


from sklearn.preprocessing import MinMaxScaler
import pandas as pd

training_data_df = pd.read_csv('./data/sales_data_training.csv')
scaler = MinMaxScaler(feature_range=(0,1))
scaled_training= scaler.fit_transform(training_data_df)
scaled_training_df = pd.DataFrame(scaled_training,columns= training_data_df.columns.values)


我的CSV数据:

"critic_rating,is_action,is_exclusive_to_us,is_portable,is_role_playing,is_sequel,is_sports,suitable_for_kids,total_earnings,unit_price"
"3.5,1,0,1,0,1,0,0,132717,59.99"
"4.5,0,0,0,0,1,1,0,83407,49.99"...
'3,1,0,0,0,1,0,1,89874,49.99'


我在1000行中有9列数据(〜9999个数据,第一行是标题)。

问候,
行纪

完整错误如下:

Traceback (most recent call last):

  File "C:/Users/YukiKawaii/PycharmProjects/PandasTest/module2_NN/test.py", line 6, in <module>
    scaled_training= scaler.fit_transform(training_data_df)

  File "C:\Users\YukiKawaii\Python\Python35\lib\site-packages\sklearn\base.py", line 517, in fit_transform
    return self.fit(X, **fit_params).transform(X)

  File "C:\Users\YukiKawaii\Python\Python35\lib\site-packages\sklearn\preprocessing\data.py", line 308, in fit
    return self.partial_fit(X, y)

  File "C:\Users\YukiKawaii\Python\Python35\lib\site-packages\sklearn\preprocessing\data.py", line 334, in partial_fit
    estimator=self, dtype=FLOAT_DTYPES)

  File "C:\Users\YukiKawaii\Python\Python35\lib\site-packages\sklearn\utils\validation.py", line 433, in check_array
    array = np.array(array, dtype=dtype, order=order, copy=copy)

ValueError: could not convert string to float: '3,1,0,0,0,1,0,1,89874,49.99'

最佳答案

您应该删除csv文件中每行周围的""''
默认情况下,pd.read_csv(),分隔每一行,因此,如果""''在那里,则无法将字符串转换为浮点数。

因此,csv文件应如下所示。

critic_rating,is_action,is_exclusive_to_us,is_portable,is_role_playing,is_sequel,is_sports,suitable_for_kids,total_earnings,unit_price
3.5,1,0,1,0,1,0,0,132717,59.99
4.5,0,0,0,0,1,1,0,83407,49.99
3,1,0,0,0,1,0,1,89874,49.99


完成上述更改后,我刚刚通过运行您的代码进行了验证。

关于python-3.x - MinMaxScaler和Transform之后的ValueError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54855072/

10-12 03:26