机器学习项目:泰坦尼克号幸存者预测

  • 随机森林分类模型
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn import preprocessing

# 导入数据
train_data = pd.read_csv(r"D:\xio习\python\python数据分析\案例\泰坦尼克\data\train.csv")
train_data.head()

test_data = pd.read_csv(r"D:\xio习\python\python数据分析\案例\泰坦尼克\data\test.csv")
test_data.head()

# 设置目标
train_y = train_data["Survived"]

# 设置特征
features = ["Pclass", "Sex", "SibSp", "Parch"]
train_X = pd.get_dummies(train_data[features])
test_X = pd.get_dummies(test_data[features])

# zscore标准化
zscore = preprocessing.StandardScaler()
train_X = zscore.fit_transform(train_X)
test_X = zscore.fit_transform(test_X)

# 正则化
train_X = preprocessing.normalize(train_X, norm='l2')
test_X = preprocessing.normalize(test_X, norm='l2')

# 训练随机森林模型
model = RandomForestClassifier(n_estimators=300, max_depth=3, random_state=1)
model.fit(train_X, train_y)

# 5折交叉验证
scores = -1 * cross_val_score(model, train_X, train_y,
                              cv=5,
                              scoring='neg_mean_absolute_error')
print("Average MAE score (across experiments):")
print(scores.mean())

# 应用模型
predictions = model.predict(test_X)
output = pd.DataFrame({
     'PassengerId': test_data.PassengerId, 'Survived': predictions})
output.to_csv('my_submission.csv', index=False)
print("Your submission was successfully saved!")
  • 运行结果
    在这里插入图片描述

你可能感兴趣的:(机器学习,python,机器学习)