初入Kaggle,泰坦尼克号遇难预测项目详解。

train = pd.read_csv('../input/titanic/train.csv')
test = pd.read_csv('../input/titanic/test.csv')
train.head()

在这里插入图片描述老规矩,导入数据集,看一下数据长什么样。
因为是一个简单的二分类问题,所以先删除一些无关紧要的特征。
比如name,ID之类的,灾难面前众生平等对吧?
然后把训练数据分成x,y。

train_x = train.drop(['PassengerId','Name','Survived'], axis = 1)
train_y = train['Survived']
train_x.isnull().sum()

看一下空数据
在这里插入图片描述
填充空数据。测试数据集也是同理。

# fill missing values with median age
train_x.Age = train_x.Age.fillna(train_x.Age.median())
train_x.Cabin = train_x.Cabin.fillna(train_x.Cabin.mode())
train_x.Embarked = train_x.Embarked.fillna(train_x.Embarked.mode())
test_x = test.drop(['PassengerId', 'Name'], axis = 1)
test_x.info()

test_x.Age.fillna(test_x.Age.median(), inplace = True)
test_x.Cabin.fillna(test_x.Cabin.mode(), inplace = True)
test_x.Fare.fillna(test_x.Fare.median(), inplace = True)
test_x.isnull().sum()

然后我们要处理这里面的离散型数据了,什么是离散型数据不懂的自行百度。

因为合并在一起将离散数据encode比较方便,所以先打上标签,处理之后再拆开

train_x['type'] = 'train'
test_x['type'] = 'test'
df = pd.concat([train_x,test_x])
from sklearn.preprocessing import LabelEncoder
#LabelEncoder() 即将离散型的数据转换成 0 到 n−1n−1 之间的数,这里 nn 是一个列表的不同取值的个数,可以认为是某个特征的所有不同取值的个数。

for f in df.select_dtypes('object'):
    if f not in ['type']:
        print(f)
        lbl = LabelEncoder()
        df[f] = lbl.fit_transform(df[f].astype(str))

搞定,把数据还原。

train_x = df[df.type == "train"].drop("type", axis = 1)
test_x = df[df.type == "test"].drop("type", axis = 1)

下面开始分割训练集,进行训练

from sklearn.model_selection import train_test_split

# Create the training and test sets
X_train, X_test, y_train, y_test= train_test_split(train_x, train_y, test_size=0.3, random_state=42)
import xgboost as xgb
from sklearn.model_selection import RandomizedSearchCV

model = xgb.XGBClassifier(objective='binary:logistic', n_estimators=10, seed=42)

model.fit(X_train,y_train)

# Predict the labels of the test set: preds
preds = model.predict(X_test)

# Compute the accuracy: accuracy
accuracy = float(np.sum(preds==y_test.ravel()))/y_test.shape[0]
print("accuracy: %f" % (accuracy))

在这里插入图片描述感觉不太理想,那我们使用随机搜索调参一下试试。模块是:RandomizedSearchCV

gbm_param_grid = {
    'num_rounds': [1,5, 10, 15, 20],
    'eta_vals' : [0.001, 0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.7, 0.8, 0.9],
        'colsample_bytree': [0.3,0.4, 0.5, 0.7, 0.9],
    'n_estimators': [5,10,15,20,25],
    'max_depth': range(2, 20)
}

# Instantiate the classifier: gbm
gbm = xgb.XGBClassifier(objective='binary:logistic', seed=42)

# Perform random search: grid_mse
randomized_accuracy = RandomizedSearchCV(param_distributions=gbm_param_grid, estimator = gbm, scoring = "accuracy", n_iter = 5, cv = 4, verbose = 1)


# Fit randomized_accuracy to the data
randomized_accuracy.fit(train_x,train_y)

# Print the best parameters and best accuracy
print("Best parameters found: ", randomized_accuracy.best_params_)
print("Best Accuracy Score: ", np.sqrt(np.abs(randomized_accuracy.best_score_)))

在这里插入图片描述预测一下:

y_predicted = randomized_accuracy.best_estimator_.predict(test_x)

做到这里,这个项目基本上就搞定了,网格调参后,Best Accuracy Score: 0.9082706430035642

如果你觉得不爽,我们换个算法试试。来用lgbm试一下吧。

from lightgbm.sklearn import LGBMClassifier
model = LGBMClassifier(n_estimators = 100)
model.fit(X_train,y_train)

# Predict the labels of the test set: preds
preds = model.predict(X_test)

# Compute the accuracy: accuracy
accuracy = float(np.sum(preds==y_test.ravel()))/y_test.shape[0]
print("accuracy: %f" % (accuracy))

accuracy: 0.805970 大同小异

    'num_rounds': [1,5, 10, 15, 20],
    'eta_vals' : [0.001, 0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.7, 0.8, 0.9],
        'colsample_bytree': [0.3,0.4, 0.5, 0.7, 0.9],
    'n_estimators': [5,10,15,20,25],
    'max_depth': range(2, 20)
}

# Instantiate the classifier: gbm
gbm = LGBMClassifier()

# Perform random search: grid_mse
randomized_accuracy = RandomizedSearchCV(param_distributions=gbm_param_grid, estimator = gbm, scoring = "accuracy", n_iter = 5, cv = 4, verbose = 1)


# Fit randomized_accuracy to the data
randomized_accuracy.fit(train_x,train_y)

# Print the best parameters and best accuracy
print("Best parameters found: ", randomized_accuracy.best_params_)
print("Best Accuracy Score: ", np.sqrt(np.abs(randomized_accuracy.best_score_)))

在这里插入图片描述
其实感觉也差不多,因为是在服务器上跑的,并且数据量很小,所以也就没什么感觉了。

博主CSDN新人,如有不足请多多指教,欢迎留言,一起交流,共同探讨进步。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值