下载Coursera中心的“深度神经网络应用程序v3”并点击“打开”
下载dnn_app_utils_v2.py文件
训练集包含11个特征,为神经网络的评估做好准备
from sklearn.model_selection import train_test_split
X = train.drop('Survived', axis=1)
y = train['Survived']
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2)
# Transpose your data
X_tr_T = X_tr.T
X_te_T = X_te.T
y_tr_T = y_tr.T.values.reshape(1,y_tr.shape[0])
y_te_T = y_te.T.values.reshape(1,y_te.shape[0])
### CONSTANTS ###
layers_dims = [11, 25, 25, 25, 25, 25, 10, 1] # 7-layer model
parameters = L_layer_model(X_tr_T, y_tr_T, layers_dims, num_iterations = 2500, print_cost = True)
# Calculate accuracy on the training set
pred_train = predict(X_tr_T, y_tr_T, parameters)
# Calculate accuracy on the test set
pred_test = predict(X_te_T, y_te_T, parameters)
第一次尝试获得85%的训练和82%的测试精度
# Load test data
test = pd.read_csv('datasets/titanic_test.csv')
# Preprocess test data
# Prepare X to generate predictions
X = test.drop(['PassengerId','Master'], axis=1)
# Transpose X to fit neural network architecture
X_tr_T = X.T
# Calculate predictions through forward propagation with the parameters
# obtained through fitting the neural net to the training data
probas, caches = L_model_forward(X_tr_T, parameters)
# Get classes from predictions
p = np.zeros((1,X_tr_T.shape[1]))
for i in range(0, probas.shape[1]):
if probas[0,i] > 0.5:
p[0,i] = 1
else:
p[0,i] = 0
# Generate the predictions file
pIds = pd.DataFrame(data=test['PassengerId'].astype(int), columns=['PassengerId'], dtype=int)
preds = pd.DataFrame(data=p.T,columns=['Survived'], dtype=int)
final_prediction = pd.concat([pIds, preds], axis=1)
final_prediction.to_csv('titanic_survival_predictions.csv', index=False)
最后一步:生成测试数据的预测
提交预测文件会使你进入前三名,并帮助你适应kaggle竞赛