Kaggle competition: Titanic - Machine Learning from Disaster
results.to_csv('submission.csv',index=False )<load_from_csv>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
X_train = np.loadtxt("/kaggle/input/roxie2/roxie_train_features.csv", delimiter=",")[:,1:]
X_test = np.loadtxt("/kaggle/input/roxie2/roxie_test_features.csv", delimiter=",")
 ids_test = X_test[:,(0,)]
X_test = X_test[:,1:]
y_train = np.loadtxt("/kaggle/input/roxie2/roxie_train_values.csv", delimiter=",", ndmin=2)[:,(1,)]
mdl = KNeighborsRegressor()
mdl.fit(X_train, y_train)
 y_pred = mdl.predict(X_test )<save_to_csv>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
output = np.concatenate(( ids_test, y_pred), axis=1)
 np.savetxt("submission_knn.csv", output, delimiter=",", fmt='%i,%1.4f', header='ID,intensity' )<load_from_csv>
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )
X_full = np.loadtxt("/kaggle/input/roxie2/roxie_full_features.csv", delimiter=",")
 y_pred = mdl.predict(X_full)
 plt.figure(figsize=(10,15))
plt.imshow(y_pred.reshape(( 650,430,3)) /255 )<set_options>
men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men )
%config InlineBackend.figure_format = 'retina'
%matplotlib inline<load_from_csv>
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )
print(os.listdir(".. /input"))
local = 0
if(local):
train = pd.read_csv("input/train.csv")
 test = pd.read_csv("input/test.csv")
 else:
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
<drop_column>
print("Before", train_data.shape, test_data.shape)
 train_data = train_data.drop(['Ticket', 'Cabin'], axis=1)
 test_data = test_data.drop(['Ticket', 'Cabin'], axis=1)
 combine = [train_data, test_data]
"After", train_data.shape, test_data.shape
train_clean = train.drop(['ID','price'],1)
 test_clean = test.drop('ID',1)
 test_clean.head()
train_clean.shape<concatenate>
combine = [train_data, test_data]
all_data = pd.concat(( train_clean[:],test_clean[:]))
all_data["caret_sqroot"] = np.sqrt(all_data["carat"])
 all_data["caret_cubtroot"] = all_data.carat **(1/3)
 all_data.shape 
<feature_engineering>
for dataset in combine:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_data['Title'], train_data['Sex'] )
train["logprice"] = np.log1p(train["price"])
 numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
print(numeric_feats )<feature_engineering>
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col',\
	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train_data[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
<categorify>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train_data.head()
all_data = pd.get_dummies(all_data)
 all_data.head()<data_type_conversions>
train_data = train_data.drop(['Name', 'PassengerId'], axis=1)
 test_data = test_data.drop(['Name'], axis=1)
 combine = [train_data, test_data]
train_data.shape, test_data.shape
all_data = all_data.fillna(all_data.mean() )<prepare_x_and_y>
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 train_data.head()
print(train.shape[0])
 print(all_data.shape[0])
 X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
train_price = train["logprice"]
print(train_price.head())
 y = train_price<compute_test_metric>
guess_ages = np.zeros(( 2,3))
guess_ages
def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, X_train, y, scoring="neg_mean_squared_error", cv = 5))
return(rmse)
 def rmse(ypred, yval):
rmseval = np.sqrt(( yval - ypred)^2)
 return(rmseval )<train_on_grid>
for dataset in combine:
for i in range(0, 2):
for j in range(0, 3):
guess_df = dataset[(dataset['Sex'] == i)& \ 
(dataset['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_df.median()
guess_ages[i,j] = int(age_guess/0.5 + 0.5)* 0.5
for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)&(dataset.Pclass == j+1),\
'Age'] = guess_ages[i,j]
dataset['Age'] = dataset['Age'].astype(int)
 train_data.head()
lm = LinearRegression()
lm_fit = lm.fit(X_train,y)
 lm_pred = np.array(lm_fit.predict(X_train))
print(lm_fit)
 print(round(rmse_cv(lm_fit ).mean() ,4)) 
<create_dataframe>
train_data['AgeBand'] = pd.cut(train_data['Age'], 5)
 train_data[['AgeBand', 'Survived']].groupby(['AgeBand'], as_index=False ).mean().sort_values(by='AgeBand', ascending=True )
predp = pd.DataFrame(lm_pred )<rename_columns>
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age']
train_data.head()
predp.columns = ['x']
predp.set_index('x')
<feature_engineering>
train_data = train_data.drop(['AgeBand'], axis=1)
 combine = [train_data, test_data]
train_data.head()
cdf = predp.sort_values('x' ).reset_index()
cdf['p'] = cdf.index / float(len(cdf)- 1)
 interp = interp1d(cdf['x'], cdf['p'])
 print(cdf.head())
 print(predp['x'].quantile(0.57))
print(interp(8.0403611))
print(interp(predp['x'].quantile(0.43)))
<define_variables>
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
train_data[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
sz = len(lm_pred)
 sz<predict_on_test>
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train_data[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()
lm_predict = lm.predict(X_train)
 print(r2_score(lm_predict,y))<choose_model_class>
train_data = train_data.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 test_data = test_data.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 combine = [train_data, test_data]
train_data.head()
model_ridge = Ridge()<find_best_params>
for dataset in combine:
dataset['Age*Class'] = dataset.Age * dataset.Pclass
train_data.loc[:, ['Age*Class', 'Age', 'Pclass']].head(10 )
alphas = [0.00001,0.0001, 0.001, 0.005, 0.01, 0.03, 0.05, 0.08, 0.1,.25,.5, 1]
cv_ridge = [rmse_cv(Ridge(alpha = alpha)).mean()
for alpha in alphas]<train_on_grid>
y = train_data["Survived"]
features = ["Pclass", "Sex", "Age", "Embarked", "Title", "IsAlone", "Age*Class"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )
<find_best_params><EOS> 
y = train_data["Survived"]
features = ["Pclass", "Sex", "Age", "Embarked", "Title", "IsAlone"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )