https://blog.csdn.net/guoxinian/article/details/73740746
Kaggle泰坦尼克预测(完整分析)
自己动手在Jupyter Notebook中跟着上面文章中的代码运行,将里面遇到的问题整理如下。
1.
unknown_age = age_df[age_df.Age.isnull()].as_matrix()
不断运行时会出现返回值为空矩阵的情况,是因为运行一次后已经将缺失值补全了,所以数据集中‘Age’属性就没有含有空值的样本了,所以再运行就会出现这种情况。
2.
age_scale_param = scaler.fit(df['Age'])
df['Age_scaled'] = scaler.fit_transform(df['Age'], age_scale_param)
fare_scale_param = scaler.fit(df['Fare'])
df['Fare_scaled'] = scaler.fit_transform(df['Fare'], fare_scale_param)
对单列属性进行数值标准化时,正确的代码为
df['Age_scaled'] = scaler.fit_transform(df['Age'].values.reshape(-1,1))
df['Fare_scaled'] = scaler.fit_transform(df['Fare'].values.reshape(-1,1))
在新的sklearn版本中会报如下错误:
ValueError: Expected 2D array, got 1D array instead:
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
新版本中所有数据元素都必须是一个2D矩阵,即使是一个简单的column或row,所以需要使用array.reshape(-1, 1)重新调整你的数据。而在Python3中需要用values.reshape(-1,1),单列的话用其转化为一列的二维数组。
reshape函数:numpy中一个调整矩阵行数、列数、维度数的一个函数。