Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
%matplotlib inline
def agrupados() :
df = pd.read_csv(fichero)
 lista = df.values
print(df.columns)
 X = lista[:, :-1]
y = lista[:, -1]
train_x,train_y = X[:n_test], y[:n_test]
test_x, test_y = X[n_test:], y[n_test:]
clf = lightgbm_model(train_x, train_y, test_x, test_y, df.columns)
 return clf<save_to_csv>
train_data= pd.read_csv(".. /input/titanic/train.csv")
 test_data = pd.read_csv(".. /input/titanic/test.csv" )
def resultado_final() :
df = pd.read_csv(tests)
 lista = df.values
print(lista.shape)
 clf = agrupados()
y_pred=clf.predict(lista)
 for i in range(0,len(lista)) :
if y_pred[i]>=.5:
y_pred[i]=1
else:
y_pred[i]=0
y_pred = y_pred.astype(int)
 df_test = pd.read_csv(test_csv)
 df_test['Winner'] = y_pred
df_test.to_csv(resultados_finales, index=False)
 df_sample = df_test[['battle_number', 'Winner']]
df_sample.to_csv(sample, index=False )<init_hyperparams>
attributes=["PassengerId", "Name", "Ticket", "Cabin"]
train_data.drop(attributes, axis=1,inplace=True)
 test_data.drop(attributes, axis=1,inplace=True )
params = {'num_iterations': 900,
'objective' :'binary',
'learning_rate' : 0.08,
'boosting_type' : 'goss',
'max_depth': 12,
'metric': 'binary_logloss',
'lambda_l1': 0.01,
'lambda_l2': 0.01,
'subsample': 0.75 }
juntar_csvs(False)
 juntar_csvs(True)
 resultado_final()<define_variables>
duplicate = train_data[train_data.duplicated() ]
n_test= 49999
fichero = 'datos_entrenamiento.csv'
tests = 'entrega_para_predecir.csv'
resultados_finales = 'resultados_finales_test.csv'
sample = 'resultados_finales_sampleSubmission.csv'
path_dir = '.. /input/'<load_from_csv>
train_data=train_data.drop_duplicates()
def get_data() :
df_pokemon = pd.read_csv(path_dir + 'pokemon.csv')
 df_pokemon = df_pokemon.fillna({'Name': 'None', 'Type 1': 'None', 'Type 2': 'None'})
 df_pokemon = df_pokemon.rename(index=str, columns={"
df_pokemon['Legendary'] = np.where(df_pokemon['Legendary'] == True, 1, 0)
 valores_type1 = df_pokemon['Type 1'].values
valores_type2 = df_pokemon['Type 2'].values
valores_name = df_pokemon['Name'].values
le1 = preprocessing.LabelEncoder()
le2 = preprocessing.LabelEncoder()
lename = preprocessing.LabelEncoder()
encoding1 = le1.fit_transform(valores_type1)
 encoding2 = le2.fit_transform(valores_type2)
 encodingName = lename.fit_transform(valores_name)
 df_pokemon['Type 1'] = encoding1
df_pokemon['Type 2'] = encoding2
df_pokemon['Name'] = encodingName
sum_speeds = np.sum(df_pokemon['Speed'].values)
 total_speeds = len(df_pokemon['Speed'])
 media_speeds = sum_speeds / total_speeds
df_pokemon['Rapidez'] = np.where(df_pokemon['Speed'] > media_speeds, 1, 0)
 df_battles = pd.read_csv(path_dir + 'battles.csv')
 df_battles = df_battles[['First_pokemon','Second_pokemon', 'Winner']]
print(df_battles.columns)
 print(df_pokemon.head())
 df_test = pd.read_csv(path_dir + 'test.csv')
 return df_pokemon, df_battles, df_test, le1, le2, lename 
<count_unique_values>
train_data[['Sex','Survived']].groupby(['Sex'], as_index=False ).mean()
def juntar_csvs() :
df_pokemon, df_battles, df_test, le1, le2, lename = get_data()
pokemon_values = df_pokemon.values
battles_values = df_battles.values
ids_pokemon = pokemon_values[:,0]
ids_pok1, inv1 = np.unique(battles_values[:, 0], return_inverse=True)
 ids_pok2, inv2 = np.unique(battles_values[:, 1], return_inverse=True)
 resultados_batallas = battles_values[:, 2]
indices1 = np.intersect1d(ids_pok1, ids_pokemon, return_indices=True)
 indices2 = np.intersect1d(ids_pok2, ids_pokemon, return_indices=True)
 vals_pok1 = pokemon_values[indices1[2], 1:]
vals_pok2 = pokemon_values[indices2[2], 1:]
sin_battles = pokemon_values[
np.where(
 np.logical_not(
 np.isin(ids_pokemon, ids_pok1)))]
print('Pokemons que no han peleado:', len(sin_battles))
lon_values = len(battles_values)
 pok1 = vals_pok1[inv1]
pok2 = vals_pok2[inv2]
columnas = pok2.shape[1] + 3
print(pok2.shape)
 pok_final = np.ones(( lon_values, columnas))
pok_final[:, :3] = pok1[:, :3]
pok_final[:, 3:6] = pok2[:, :3]
pok_final[:, 6:] = pok1[:, 3:] - pok2[:, 3:]
juntar_carac = pok_final
caracteristicas_y_resultados = np.ones(( lon_values, columnas + 1))
caracteristicas_y_resultados[:,:-1] = juntar_carac
caracteristicas_y_resultados[:,-1] = resultados_batallas
valores = np.array(( battles_values[:, 0], battles_values[:, 1], battles_values[:, 0]))
valores = valores.T
lista = np.concatenate(( valores, caracteristicas_y_resultados), axis=1)
 lista = lista.astype(int)
 df_lista = pd.DataFrame(lista, columns=['First_pokemon', 'Second_pokemon', 'id_primer_ataq',
'nombre1', 'tipo1_id1', 'tipo2_id1',
'nombre2', 'tipo1_id2', 'tipo2_id2',
'diff_HP','diff_Attack','diff_Defense','diff_Sp.Atk','diff_Sp.Def','diff_Speed',
'diff_Generation', 'diff_Legendary',
'diff_Rapidez',
'Winner'])
 df_lista['tipo1_id1'] = le1.inverse_transform(df_lista['tipo1_id1'])
 df_lista['tipo2_id1'] = le2.inverse_transform(df_lista['tipo2_id1'])
 df_lista['tipo1_id2'] = le1.inverse_transform(df_lista['tipo1_id2'])
 df_lista['tipo2_id2'] = le2.inverse_transform(df_lista['tipo2_id2'])
 df_lista['nombre1'] = lename.inverse_transform(df_lista['nombre1'])
 df_lista['nombre2'] = lename.inverse_transform(df_lista['nombre2'])
 df_lista = calculate_effectiveness(df_lista)
 winners = df_lista['Winner'].values
df_lista = df_lista.drop(['Winner'], axis=1)
 df_lista['Winner'] = winners
df_lista['tipo1_id1'] = le1.fit_transform(df_lista['tipo1_id1'])
 df_lista['tipo2_id1'] = le2.fit_transform(df_lista['tipo2_id1'])
 df_lista['tipo1_id2'] = le1.fit_transform(df_lista['tipo1_id2'])
 df_lista['tipo2_id2'] = le2.fit_transform(df_lista['tipo2_id2'])
 df_lista['nombre1'] = lename.fit_transform(df_lista['nombre1'])
 df_lista['nombre2'] = lename.fit_transform(df_lista['nombre2'])
 df_lista.to_csv(fichero, index=False)
 return lista<count_unique_values>
print("Train_data:",train_data.isnull().values.any())
 print("Test_data:",test_data.isnull().values.any() )
def preparar_test() :
df_pokemon, df_battles, df_test, le1, le2, lename = get_data()
pokemon_values = df_pokemon.values
tests_values = df_test.values
ids_pokemon = pokemon_values[:, 0]
ids_pok1, inv1 = np.unique(tests_values[:, 1], return_inverse=True)
 ids_pok2, inv2 = np.unique(tests_values[:, 2], return_inverse=True)
 indices1 = np.intersect1d(ids_pok1, ids_pokemon, return_indices=True)
 indices2 = np.intersect1d(ids_pok2, ids_pokemon, return_indices=True)
 vals_pok1 = pokemon_values[indices1[2], 1:]
vals_pok2 = pokemon_values[indices2[2], 1:]
sin_battles = pokemon_values[
np.where(
 np.logical_not(
 np.isin(ids_pokemon, ids_pok1)))]
print('Pokemons que no han peleado en test:', len(sin_battles))
lon_values = len(tests_values)
 pok1 = vals_pok1[inv1]
pok2 = vals_pok2[inv2]
columnas = pok2.shape[1] + 3
pok_final = np.ones(( lon_values, columnas))
pok_final[:, :3] = pok1[:, :3]
pok_final[:, 3:6] = pok2[:, :3]
pok_final[:, 6:] = pok1[:, 3:] - pok2[:, 3:]
juntar_carac = pok_final
valores = np.array(( tests_values[:, 1], tests_values[:, 2], tests_values[:, 1]))
valores = valores.T
lista = np.concatenate(( valores, juntar_carac), axis=1)
 lista = lista.astype(int)
 print(lista.shape)
 df_lista = pd.DataFrame(lista, columns=['First_pokemon', 'Second_pokemon', 'id_primer_ataq',
'nombre1', 'tipo1_id1', 'tipo2_id1',
'nombre2', 'tipo1_id2', 'tipo2_id2',
'HP','Attack','Defense','Sp.Atk','Sp.Def','Speed',
'Generation', 'Legendary',
'Rapidez'
])
 df_lista['tipo1_id1'] = le1.inverse_transform(df_lista['tipo1_id1'])
 df_lista['tipo2_id1'] = le2.inverse_transform(df_lista['tipo2_id1'])
 df_lista['tipo1_id2'] = le1.inverse_transform(df_lista['tipo1_id2'])
 df_lista['tipo2_id2'] = le2.inverse_transform(df_lista['tipo2_id2'])
 df_lista['nombre1'] = lename.inverse_transform(df_lista['nombre1'])
 df_lista['nombre2'] = lename.inverse_transform(df_lista['nombre2'])
 df_lista = calculate_effectiveness(df_lista)
 df_lista['tipo1_id1'] = le1.fit_transform(df_lista['tipo1_id1'])
 df_lista['tipo2_id1'] = le2.fit_transform(df_lista['tipo2_id1'])
 df_lista['tipo1_id2'] = le1.fit_transform(df_lista['tipo1_id2'])
 df_lista['tipo2_id2'] = le2.fit_transform(df_lista['tipo2_id2'])
 df_lista['nombre1'] = lename.fit_transform(df_lista['nombre1'])
 df_lista['nombre2'] = lename.fit_transform(df_lista['nombre2'])
 df_lista.to_csv(tests, index=False )<choose_model_class>
train_data.isnull().sum()
def GradientBoosting(train_x, train_y, test_x, test_y):
clf = GradientBoostingClassifier(n_estimators=100, learning_rate=0.08, subsample=0.75,max_depth=9, verbose = 1)
 clf.fit(train_x, train_y)
 y_pred=clf.predict(test_x)
 print(clf.feature_importances_)
 print("Accuracy random forest:",metrics.accuracy_score(test_y, y_pred))
return clf<compute_train_metric>
train_data[['Pclass','Age']].groupby(['Pclass'], as_index=False ).mean()
def agrupados() :
lista = pd.read_csv(fichero ).values
print(lista.shape)
 X = lista[:, :-1]
y = lista[:, -1]
train_x,train_y = X[:n_test], y[:n_test]
test_x, test_y = X[n_test:], y[n_test:]
rf = GradientBoosting(train_x, train_y, test_x, test_y)
 return rf
def resultado_final() :
lista = pd.read_csv(tests ).values
print(lista.shape)
 clf = agrupados()
y_pred = clf.predict(lista)
 print(y_pred)
 y_pred = y_pred.astype(int)
 df_test = pd.read_csv(path_dir + 'test.csv')
 df_test['Winner'] = y_pred
df_sample = df_test[['battle_number', 'Winner']]
df_sample.to_csv(sample, index=False )<import_modules>
train_data.loc[(train_data['Pclass'] == 1), 'Age'] = 38
train_data.loc[(train_data['Pclass'] == 2), 'Age'] = 30
train_data.loc[(train_data['Pclass'] == 3), 'Age'] = 25
train_data['Embarked'].fillna(train_data['Embarked'].mode() [0], inplace=True )
fastai.__version__<load_from_csv>
test_data.isnull().sum()
train = pd.read_csv('.. /input/train.csv' ).fillna(' ')
 valid = pd.read_csv('.. /input/valid.csv' ).fillna(' ')
 test = pd.read_csv('.. /input/test.csv' ).fillna(' ' )<save_to_csv>
test_data[['Pclass','Age']].groupby(['Pclass'], as_index=False ).mean()
pd.concat([train['text'], valid['text'], test['text']] ).to_csv(
 'unlabeled_news.csv', index=None, header=True )<save_to_csv>
test_data.loc[(test_data['Pclass'] == 1), 'Age'] = 38
test_data.loc[(test_data['Pclass'] == 2), 'Age'] = 30
test_data.loc[(test_data['Pclass'] == 3), 'Age'] = 25
test_data['Fare'].fillna(test_data['Fare'].mean() , inplace=True )
pd.concat([train[['text', 'label']],valid[['text', 'label']]] ).to_csv(
 'train_28k.csv', index=None, header=True)
 test[['text']].to_csv('test_5k.csv', index=None, header=True )<define_variables>
print("Train_data:",train_data.isnull().values.any())
 print("Test_data:",test_data.isnull().values.any() )
folder = '.'
unlabeled_file = 'unlabeled_news.csv'<load_from_csv>
train_data['Sex']=train_data['Sex'].map({'male': 1,'female':2})
 test_data['Sex']=test_data['Sex'].map({'male': 1,'female':2})
 train_data['Embarked']=train_data['Embarked'].map({'S':1, 'C':2, 'Q':3})
 test_data['Embarked']=test_data['Embarked'].map({'S':1, 'C':2, 'Q':3} )
%%time
data_lm = TextLMDataBunch.from_csv(folder, unlabeled_file, text_cols='text' )<load_pretrained>
X_train= train_data.iloc[:, 1:]
y_train= train_data['Survived'].values.reshape(-1,1)
 X_test= test_data
%%time
learn = language_model_learner(data_lm, drop_mult=0.3, arch=AWD_LSTM )<find_best_params>
lg= LogisticRegression()
lg.fit(X_train, y_train.ravel())
 Y_pred= lg.predict(X_test)
 print(lg.score(X_train, y_train))
%%time
learn.lr_find(start_lr = slice(10e-7, 10e-5), end_lr=slice(0.1, 10))<find_best_params>
rf = RandomForestClassifier()
rf.fit(X_train, y_train)
 Y_pred = rf.predict(X_test)
 print(rf.score(X_train, y_train))
best_lm_lr = learn.recorder.min_grad_lr
best_lm_lr<train_model>
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
 Y_pred = knn.predict(X_test)
 print(knn.score(X_train, y_train))
%%time
learn.fit_one_cycle(1, best_lm_lr )<train_model>
dtree = DecisionTreeClassifier()
dtree.fit(X_train, y_train)
 Y_pred = dtree.predict(X_test)
 print(dtree.score(X_train, y_train))
%%time
learn.fit_one_cycle(1, best_lm_lr )<predict_on_test>
passenger_id = pd.read_csv(".. /input/titanic/test.csv")[["PassengerId"]].values
learn.predict('An italian man was found dead in his yard due to', n_words=200 )<categorify>
submission = {'PassengerId': passenger_id.ravel() , 'Survived': Y_pred}
submission_pred = pd.DataFrame(submission ).set_index(['PassengerId'] )
learn.save_encoder('clickbait_news_enc' )<define_variables>
submission_pred.to_csv('titanic_prediction.csv' )