基于随机森林算法实现泰坦尼克号的生存预测,python

Kaggle有这样一个经典的题目,根据船上的用户基本信息,判断剩下的人是否能生存下来。话不多说直接进入主题。

下载数据集

Kaggle

数据集下载

在这里插入图片描述
包含了源代码+训练集+ 测试集

整理数据

  • 这一部主要处理缺失的数据,
  • 将年龄等常数用平均值代替
  • 将登船口用众数代替
def select_data():
    selected_features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare','Embarked']
    train_data = load_data()
    test_data = load_data("test")
    train_x = train_data[selected_features]
    train_y = train_data["Survived"]
    test_x = test_data[selected_features]
    
    train_x["Age"].fillna(train_x["Age"].mean(), inplace = True)
    
    train_x['Embarked'].fillna('S',inplace=True) #'S'出现次数最多,因此以'S'进行填充
    test_x["Age"].fillna(test_x["Age"].mean(), inplace = True)
    test_x["Fare"].fillna(test_x["Fare"].mean(), inplace = True)
    
    train_x = format_data(train_x)
    test_x = format_data(test_x)
    print(test_x.info())
    return train_x, train_y, test_x

数据数字化

  • 将性别,登机口用 数学的形式来表示,方便训练
def format_data(train_x):
    # 数据化性别
    train_x.loc[train_x['Sex'] == "male", "Sex"] = 0
    train_x.loc[train_x["Sex"] == "female", "Sex"] = 1
    
    train_x.loc[train_x['Embarked'] == "S", "Embarked"] = 0
    train_x.loc[train_x["Embarked"] == "C", "Embarked"] = 1
    train_x.loc[train_x['Embarked'] == "Q", "Embarked"] = 2
    return train_x

训练模型,并预测,写入到文件当中

def random_forest():
    test_data = load_data('test')
    x_train, y_train, x_test = select_data()
    model = RandomForestClassifier()
    paras = {'n_estimators': np.arange(10, 100, 10), 'criterion': ['gini', 'entropy'], 'max_depth': np.arange(5, 50, 5)}
    gs = GridSearchCV(model, paras, cv=5, verbose=1,n_jobs=-1)
    gs.fit(x_train, y_train)
    y_pre = gs.predict(x_test)
    print('best score:', gs.best_score_)
    print('best parameters:', gs.best_params_)
    print((test_data))
    result = ''
    with open('./result.csv', 'w', encoding="utf-8") as f:
        f.write("PassengerId,Survived" + "\n")
        for i in range(len(y_pre)): 
           
            result = str(test_data.iloc[i,0]) + "," +  str(y_pre[i])
            f.write(result + "\n")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东哥aigc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值