【问题标题】:my standardscaler outputs ValueError: could not convert string to float我的标准缩放器输出 ValueError: could not convert string to float
【发布时间】:2021-07-21 21:43:32
【问题描述】:

我作为 Python 和机器学习初学者正在做一个项目,并且遇到了 Titanic 数据集。将我的数据集拆分为训练和测试后,我想使用 StandardScaler 规范化 x_train,但这不断出现:

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

这是我的代码

    feature =df[['PassengerId', 'PClass', 'Age', 'SibSp', 'Parch']].values
    target = df[['Survived']].values
    x_train, x_test, y_train, y_test = train_test_split(feature, target, test_size=0.2)
    from sklearn.preprocessing import StandardScaler, OneHotEncoder
    scaler = StandardScaler().fit(x_train)
    x_train = scaler.transform(x_train)
    x_test = scaler.transform(x_test)

我该如何解决这个问题?

【问题讨论】:

    标签: python pandas machine-learning scikit-learn


    【解决方案1】:
    # it's a good idea to exclude PassengerId, since it's probabaly not a predictive feature
    # also convert all the values to float
    feature =df[['PClass', 'Age', 'SibSp', 'Parch']].astype('float').values
    target = df[['Survived']].values
        
    x_train, x_test, y_train, y_test = train_test_split(feature, target, test_size=0.2)
    from sklearn.preprocessing import StandardScaler, OneHotEncoder
    scaler = StandardScaler().fit(x_train)
    x_train = scaler.transform(x_train)
    x_test = scaler.transform(x_test)
    

    【讨论】:

    • 现在它给了我 ValueError: could not convert string to float: 'Pclass'
    猜你喜欢
    • 2019-12-18
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 2020-12-11
    相关资源
    最近更新 更多