背景
- 分类任务,什么样的人会获取
- 891份样本,7个特征
- 有些特征存在缺失需要补充,有些特征意义不大需要删除不参与建模
- 分析每个特征对最终是否被救的重要度
年龄缺失值填充
# 中值填充
titanic["Age"] = titanic["Age"].fillna(titanic["Age"].median())
性别类别转换
# 类别转换
print (titanic["Sex"].unique())
# Replace all the occurences of male with the number 0.
titanic.loc[titanic["Sex"] == "male", "Sex"] = 0
titanic.loc[titanic["Sex"] == "female", "Sex"] = 1
线性回归预测
# 导入所需的库
from sklearn.linear_model import LinearRegression
# 交叉验证
from sklearn.model_selection import KFold
# 保留参与建模的特征
predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]
# 初始化算法
alg = LinearRegression()
# 交叉验证划分
kf = KFold(3, random_state=1)
# 预测概率列表
predictions = []
for train, test in kf.split(titanic):
train_predictors = (titanic[predictors].iloc[train,:])
train_target = titanic["Survived"].iloc[train]
alg.fit(train_predictors, train_target)
# We can now make predictions on the test fold
test_predictions = alg.predict(titanic[predictors].iloc[test,:])
predictions.append(test_predictions)
#
逻辑回归
随机森林
随机森林调参
发现,添加新特征
选取名字中的称谓,正则表达式
家庭成员个数
特征重要度
集成算法