Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<predict_on_test>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
prediction = np.zeros(( test_X.shape[0], 1))
rList = [42, 3, 468, 12, 120, 130, 234, 1000, 100, 200, 1200, 5, 45,
49, 300, 320,980, 81, 2100, 1420, 768]
for r in rList:
y1 = predictTarget(test_X, getTheta(r))
print(prediction.shape)
 prediction = np.hstack(( prediction,y1.reshape(y1.shape[0], 1)) )<prepare_output>
ds_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 ds_output = pd.read_csv('/kaggle/input/titanic/test.csv' )
prediction = np.sum(prediction, axis = 1)/len(rList )<save_to_csv>
X_train = ds_train.drop(['PassengerId','Name','Ticket'],axis=1)
 X_output = ds_output.drop(['PassengerId','Name','Ticket'],axis=1 )
sub = pd.DataFrame()
sub['Id'] = test['Id']
sub['SalePrice'] = prediction
sub.to_csv("prediction.csv", index = False )<set_options>
X_train['Cabin'].isna().sum() / X_train.shape[0]
%matplotlib inline
sns.set()<load_from_csv>
X_train = X_train.drop(['Cabin'],axis=1)
 X_output = X_output.drop(['Cabin'],axis=1 )
sub = pd.read_csv('.. /input/sample_submission.csv' )<load_from_disk>
print(f"Count : {X_train['Embarked'].isna().sum() } / {X_train.shape[0]} = {X_train['Embarked'].isna().sum() /X_train.shape[0]}")
 print(f"Most common letter : {np.squeeze(X_train['Embarked'].mode())}" )
with open('.. /input/train_annotations.json/train_annotations.json','r')as anno_train:
train = json.load(anno_train)
 with open('.. /input/test_annotations.json/test_annotations.json','r')as anno_test:
test = json.load(anno_test )<create_dataframe>
letter_dictionnary = X_train['Embarked'].astype('category' ).cat.categories
test_df = pd.DataFrame()
test_df = test_df.append(test['images'], ignore_index=True )<create_dataframe>
X_train['Sex']=X_train['Sex'].astype('category' ).cat.codes
X_output["Sex"]=X_output["Sex"].astype('category' ).cat.codes
train_df = pd.DataFrame()
train_df = train_df.append(train['images'], ignore_index=True)
 train_df_anno = pd.DataFrame()
train_df_anno = train_df_anno.append(train['annotations'], ignore_index=True)
 train_df['category_id'] = train_df_anno['category_id']
del train_df_anno
gc.collect()<define_variables>
X_train['Embarked']=X_train['Embarked'].astype('category' ).cat.codes
X_output['Embarked']=X_output["Embarked"].astype('category' ).cat.codes
category_numbers = [0]*(np.max(train_df['category_id'])+1 )<feature_engineering>
X_train = pd.concat([X_train, pd.get_dummies(X_train.Pclass)], axis=1)
 X_output = pd.concat([X_output, pd.get_dummies(X_output.Pclass)], axis=1)
 X_train.rename(columns={1: "Pclass1", 2: "Pclass2",3:"Pclass3"}, inplace=True)
 X_output.rename(columns={1: "Pclass1", 2: "Pclass2",3:"Pclass3"}, inplace=True)
 X_train.drop(['Pclass'],inplace=True,axis=1)
 X_output.drop(['Pclass'],inplace=True,axis=1 )
for i in train_df['category_id']:
category_numbers[i]+=1<define_variables>
X_train = pd.concat([X_train, pd.get_dummies(X_train.Embarked)], axis=1)
 X_output = pd.concat([X_output, pd.get_dummies(X_output.Embarked)], axis=1)
 X_train.rename(columns={0: f"Embarked_{letter_dictionnary[0]}", 1: f"Embarked_{letter_dictionnary[1]}",2: f"Embarked_{letter_dictionnary[2]}"}, inplace=True)
 X_output.rename(columns={0: f"Embarked_{letter_dictionnary[0]}", 1: f"Embarked_{letter_dictionnary[1]}",2: f"Embarked_{letter_dictionnary[2]}"}, inplace=True)
 X_train.drop(['Embarked'],inplace=True,axis=1)
 X_output.drop(['Embarked'],inplace=True,axis=1 )
few_shot = 0
for i in category_numbers[1:]:
if i <= 200:
few_shot+=1<feature_engineering>
def normalization(column):
return(column-column.mean())/column.std()
few_shot/len(category_numbers )<load_from_csv>
X_train['Age'],X_train['Fare'] = normalization(X_train['Age']),normalization(X_train['Fare'] )
df = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<concatenate>
X_output['Age'],X_output['Fare'] = normalization(X_output['Age']),normalization(X_output['Fare'] )
df = df.append(test )<data_type_conversions>
y_train = X_train['Survived']
X_train = X_train.drop(['Survived'], axis=1 )
df['area'] = df['area'].str.replace(',','')
 df['densidade_dem'] = df['densidade_dem'].str.replace(',','')
 df['area'] = df['area'].astype(float)
 df['densidade_dem'] = df['densidade_dem'].astype(float )<feature_engineering>
X_train.reset_index(drop=True, inplace=True)
 y_train.reset_index(drop=True, inplace=True)
 X_output.reset_index(drop=True, inplace=True )
df['populacao'] = df['populacao'].str.replace(',','')
 df['populacao'] = df['populacao'].str.replace('(2)','')
 df['populacao'] = df['populacao'].str.replace('(','')
 df['populacao'] = df['populacao'].str.replace(')','')
 df['populacao'] = df['populacao'].str.replace('.','')
 df['populacao'] = df['populacao'].astype(int )<feature_engineering>
X_train,X_test,y_train,y_test = train_test_split(X_train,y_train )
df['comissionados_por_servidor'] = df['comissionados_por_servidor'].str.replace('
df['comissionados_por_servidor'] = df['comissionados_por_servidor'].str.replace('!','')
 df['comissionados_por_servidor'] = df['comissionados_por_servidor'].str.replace('%','')
 df['comissionados_por_servidor'] = df['comissionados_por_servidor'].astype(float)
 df['comissionados_por_servidor'] = df['comissionados_por_servidor']/100<categorify>
hyperparameters = {'C': np.arange(0.5,4,0.2), 'kernel':['linear', 'rbf', 'sigmoid'], 'gamma':np.linspace(10**-3,1,50)}
grid = GridSearchCV(svm.SVC() , hyperparameters, cv=5)
 grid.fit(X_train,y_train)
 C, kernel, gamma = grid.best_params_['C'],grid.best_params_['kernel'],grid.best_params_['gamma']
print(f"Best score : {grid.best_score_} for hyperparameters : {grid.best_params_}" )
df_porte = pd.get_dummies(df['porte'])
 df_regiao = pd.get_dummies(df['regiao'] )<concatenate>
KM = KMeans(n_clusters=30)
 KM.fit(X_train,y_train)
 print(f"Best score : {completeness_score(KM.predict(X_test),y_test)} for hyperparameters : n_clusters = 15" )
df = pd.concat([df, df_porte], axis=1)
 df = pd.concat([df, df_regiao], axis=1 )<import_modules>
hyperparameters = {'n_neighbors': np.arange(1,50), 'metric': ['euclidean','manhattan']}
grid = GridSearchCV(KNeighborsClassifier() , hyperparameters, cv=5)
 grid.fit(X_train,y_train)
 n_neighbors,metric = grid.best_params_['n_neighbors'],grid.best_params_['metric']
print(f"Best score : {grid.best_score_} for hyperparameters : {grid.best_params_}" )
import matplotlib.pyplot as plt
import seaborn as sns<split>
hyperparameters = {'n_estimators': [1500,2000,2500,3000,4000],'max_features':['auto','sqrt','log2'], 'max_depth':[10,20,30]}
grid = GridSearchCV(RandomForestClassifier() , hyperparameters, cv=5)
 grid.fit(X_train,y_train)
 n_estimators,max_features,max_depth = grid.best_params_['n_estimators'],grid.best_params_['max_features'],grid.best_params_['max_depth']
print(f"Best score : {grid.best_score_} for hyperparameters : {grid.best_params_}" )
test = df[df['nota_mat'].isnull() ] 
<filter>
hyperparameters = {'n_estimators': [500,700,800,900,1000],'max_features':['auto','sqrt','log2'], 'max_depth':[10,20,30]}
grid = GridSearchCV(ExtraTreesClassifier() , hyperparameters, cv=5)
 grid.fit(X_train,y_train)
 n_e,max_f,max_d= grid.best_params_['n_estimators'],grid.best_params_['max_features'],grid.best_params_['max_depth']
print(f"Best score : {grid.best_score_} for hyperparameters : {grid.best_params_}" )
df = df[~df['nota_mat'].isnull() ]<split>
best_mean_score = 0
train, valid = train_test_split(df, random_state=42 )<define_variables>
sv = svm.SVC(C=C, kernel=kernel, gamma=gamma)
 sv.fit(X_train,y_train)
 sv_mean_score, sv_std_score = cross_val_score(sv,X_test, y_test, cv = 5 ).mean() ,cross_val_score(sv,X_test, y_test, cv = 5 ).std()
print(sv_mean_score, sv_std_score)
 if sv_mean_score>best_mean_score:
best_mean_score = sv_mean_score
model = "sv"
removed_cols = ['municipio', 'estado', 'codigo_mun', 'porte', 'regiao', 'nota_mat','capital']
feats = [c for c in df.columns if c not in removed_cols]<import_modules>
knn = KNeighborsClassifier(n_neighbors, metric=metric)
 knn.fit(X_train,y_train)
 knn_mean_score, knn_std_score = cross_val_score(knn,X_test, y_test, cv = 5 ).mean() ,cross_val_score(knn,X_test, y_test, cv = 5 ).std()
print(knn_mean_score, knn_std_score)
 if knn_mean_score>best_mean_score:
best_mean_score = knn_mean_score
model = "knn"
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier<choose_model_class>
rdf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, max_features=max_features)
 rdf.fit(X_train,y_train)
 rdf_mean_score, rdf_std_score = cross_val_score(rdf, X_test, y_test, cv = 5 ).mean() ,cross_val_score(rdf, X_test, y_test, cv = 5 ).std()
print(rdf_mean_score, rdf_std_score)
 if rdf_mean_score>best_mean_score:
best_mean_score = rdf_mean_score
model = "rdf"
models = {'RandomForest': RandomForestClassifier(random_state=42, min_samples_leaf=8,
min_samples_split=3, n_jobs=-1, n_estimators=75),
'ExtraTrees': ExtraTreesClassifier(random_state=42, n_jobs=-1),
'GradientBoosting': GradientBoostingClassifier(random_state=42),
'DecisionTree': DecisionTreeClassifier(random_state=42),
'AdaBoost': AdaBoostClassifier(random_state=42),
'KNM 11': KNeighborsClassifier(n_neighbors=11, n_jobs=-1),}<import_modules>
clf = ExtraTreesClassifier(n_estimators=n_e, max_depth=max_d,max_features=max_f)
 clf.fit(X_train,y_train)
 clf_mean_score, clf_std_score = cross_val_score(clf, X_test, y_test, cv = 5 ).mean() ,cross_val_score(clf, X_test, y_test, cv = 5 ).std()
print(clf_mean_score, clf_std_score)
 if clf_mean_score>best_mean_score:
best_mean_score = clf_mean_score
model = "clf"
from sklearn.metrics import accuracy_score<predict_on_test>
if model=="svm":
y_output = sv.predict(X_output)
 elif model=="knn":
y_output = knn.predict(X_output)
 elif model=="rdf":
y_output = rdf.predict(X_output)
 else:
y_output = clf.predict(X_output)
 print("Chosen model :", model )
def run_model(models, train, valid, feats, y_name):
model.fit(train[feats], train[y_name])
 preds = model.predict(valid[feats])
 return accuracy_score(valid[y_name], preds )<find_best_params>
submission = ds_output["PassengerId"]
scores = []
for name, model in models.items() :
score = run_model(model, train.fillna(-1), valid.fillna(-1), feats, 'nota_mat')
 scores.append(score)
 print(name, ':', score )<concatenate>
submission = pd.DataFrame(submission )
train = train.append(valid )<choose_model_class>
submission['Survived']=y_output
rf = RandomForestClassifier(random_state=42, min_samples_leaf=8, min_samples_split=3, n_jobs=-1, n_estimators=200 )<train_model>
submission.set_index('PassengerId',drop=True, inplace=True )
rf.fit(train[feats].fillna(-1), train['nota_mat'] )<predict_on_test>
submission.to_csv(r'/kaggle/working/submission.csv' )