泰坦尼克号乘客生存预测模型
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import OneHotEncoder
# 读取数据
data = pd.read_csv("data.csv")
data.head(1)
# 数据清洗
data.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1, inplace=True)
data['Age'].fillna(data['Age'].mean(), inplace=True)
data['Embarked'].fillna('S', inplace=True)
# 特征提取
X = data.iloc[:, 1:].values
y = data.iloc[:, 0].values
enc = OneHotEncoder()
X = enc.fit_transform(X).toarray()
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 模型训练与评估
params = {
'max_depth': [5, 10, 15],
'max_features': [5, 10, 15],
'min_samples_leaf': [1, 2, 5],
}
clf = RandomForestClassifier(n_estimators=100, random_state=42)
grid_search = GridSearchCV(clf, params, cv=5)
grid_search.fit(X_train, y_train)
print(grid_search.best_params_)
print(grid_search.best_score_)
scores = cross_val_score(grid_search, X_train, y_train, cv=5)
print(scores.mean())
# 可视化
feature_importances = grid_search.best_estimator_.feature_importances_
plt.bar(range(X.shape[1]), feature_importances)
plt.show()
用户评论