在StandardScaler转换后,Python,类型错误,对象而不是浮点数

时间:2017-09-14 15:54:32

标签: python scikit-learn typeerror

我的代码是

a, b = train_df1.iloc[:,1:7].values, train_df1.iloc[:,0].values
c = test_df1.iloc[:,0:6].values

from sklearn.preprocessing import StandardScaler
std = StandardScaler()
a_t= std.fit_transform(a)
c_t = std.transform(c)

我有两个数据帧train_df1和test_df1。

我用这些创建了a,b,c。

这里的问题是a和b分别是float64和int64类型。

但是c是对象类型,它为什么显示下一个代码的类型错误。

如何将c更改为float类型以避免后续代码出现类型错误?

错误消息是

TypeError:float()参数必须是字符串或数字,而不是'方法'

在运行最后一行代码之后。

修改

train_df1.head(3)

出[64]:

   Survived  Pclass  Sex   Age  SibSp  Parch     Fare  Embarked
0         0       3    0  22.0      1      0   7.2500         0
1         1       1    1  38.0      1      0  71.2833         1
2         1       3    1  26.0      0      0   7.9250         0

test_df1.head(3)

出[65]:

   Pclass  Sex   Age  SibSp  Parch    Fare  Embarked
0       3    0  34.5      0      0  7.8292         2
1       3    1  47.0      1      0       7         0
2       2    0  62.0      0      0  9.6875         2

1 个答案:

答案 0 :(得分:2)

由于您已经显示了很少的代码,我无法在我的ide中编写代码并对其进行调试。 所以,我使用了你问题中的单个数据框并缩放了数据

这是我们的数据框:

Survived  Pclass  Sex   Age  SibSp  Parch     Fare  Embarked
0         0       3    0  22.0      1      0   7.2500         0
1         1       1    1  38.0      1      0  71.2833         1
2         1       3    1  26.0      0      0   7.9250         0

以下是代码(附有注释供您参考):

# SWAMI KARUPPASWAMI THUNNAI

import pandas
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split

if __name__ == "__main__":
    data_set = pandas.read_csv("data.csv")
    a = data_set.iloc[:,1:7].values # a will get the values of 1st six columns
    b = data_set.iloc[:,7].values  # b will get the values of 7th columns
    # since the data set seems to be preprocessed (considering the small amount of data)
    # we will create training set and testing set
    a_train, a_test, b_train, b_test = train_test_split(a, b, test_size = 0.2, random_state = 0)
    # test data size = 20 % and pseudo random generator is set to 0
    scaler = StandardScaler()
    # now we are about to scale the data
    a_train = scaler.fit_transform(a_train) # scale the training set
    # use the mean and standard deviation of training set to scale the testing set
    a_test = scaler.transform(a_test)

所以,最后我缩放了值

enter image description here

注意:考虑到我所承担的各种信息和代码,我希望这可以帮助您解决问题。