Kaggle -- Titanic - Machine Learning from Disaster

新手kaggle之旅:1 . 泰坦尼克号 

使用一个简单的决策树进行模型构建,达到75.8%的准确率(有点低,但是刚开始)

完整代码如下:

import pandas as pd
import numpy as np

df = pd.read_csv("train.csv")

df.info

label = ['Pclass','Sex','Age','SibSp','Fare','Embarked']

x = df[label]
y = df['Survived']
print(x.loc[0])

x['Embarked'] = x['Embarked'].map({'C': 1, 'Q': 2, 'S': 3})


x['Sex'] = x['Sex'].map({'male': 1,'female' : 2})
print(x.loc[0])

x = x.fillna(x.mean())


import sklearn
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

train_x,test_x,train_y,test_y = train_test_split(x,y,test_size=0.2,random_state=42,shuffle=True)

clf = DecisionTreeClassifier()
clf.fit(train_x,train_y)


y_pred = clf.predict(test_x)

accuracy = accuracy_score(y_pred,test_y)
print(f"Accuracy: {accuracy * 100:.2f}%")


res = pd.read_csv('test.csv')
print(res.loc[0])


res_x = res[label]
res_x['Embarked'] = res_x['Embarked'].map({'C': 1, 'Q': 2, 'S': 3})
res_x['Sex'] = res_x['Sex'].map({'male': 1,'female' : 2})
print(res_x.loc[0])

res_x = res_x.fillna(res_x.mean())


pred = clf.predict(res_x)
print(pred[0])

ans = res[['PassengerId']].copy()
ans['Survived'] = pred

print(ans.loc[0])

ans.to_csv("ans.csv")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值