将分类型变量转换成数值型变量的两种方法
一、map映射
性别映射数值
先创建一个map ,再将map映射到表格中的值
sex_mapping = {"male": 0, "female": 1}
train_df['Sex'] = train_df['Sex'].map(sex_mapping)
俗称独热编码(one-hot编码)
pandas.get_dummies(data, prefix=None, prefix_sep='_', dummy_na=False, columns=None, sparse=False, drop_first=False)
参数说明:
Sex性别列处理:male用0,female用1
data_train.loc[data_train["Sex"] == "male","Sex"] = 0
data_train.loc[data_train["Sex"] == "female","Sex"] = 1
结果与第一种方法一致