Kaggle competition: MLH - Pokemon Challenge
<SOS> metric: categorizationaccuracy Kaggle data source: mlh-pokemon-challenge<train_model>
clf_dt = DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='entropy',
max_depth=None, max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=10, min_samples_split=35,
min_weight_fraction_leaf=0.0, presort='deprecated',
random_state=None, splitter='best')
 clf_dt.fit(X_train, Y_train)
 Y_pred = clf_dt.predict(X_test)
 scores = cross_val_score(clf_dt, X_train, Y_train, cv=10, scoring = "accuracy")
 print("Scores:", scores)
 print("Mean:", scores.mean())
 print("Standard Deviation:", scores.std() )<save_to_csv>
n_test= 49999
fichero = 'datos.csv'
tests = 'entrega_para_predecir.csv'
resultados_finales = 'resultados_finales_test.csv'
sample = 'resultados_finales_sampleSubmission.csv'
path_dir = 'pokemon-challenge-mlh/'
pokemon_csv = '.. /input/pokemon.csv'
battles_csv = '.. /input/battles.csv'
test_csv = '.. /input/test.csv'
output = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': Y_pred})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<set_options>
def get_data() :
df_pokemon = pd.read_csv(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_pokemon['total_stats'] = df_pokemon['HP'] + df_pokemon['Attack'] + df_pokemon['Defense'] + df_pokemon['Sp.Atk'] + df_pokemon['Sp.Def'] + df_pokemon['Speed']
df_battles = pd.read_csv(battles_csv)
 df_battles = df_battles[['First_pokemon','Second_pokemon', 'Winner']]
df_test = pd.read_csv(test_csv)
 return df_pokemon, df_battles, df_test, le1, le2, lename
sns.set()
%matplotlib inline 
<load_from_csv>
def diff_combates(df_lista):
lista_ata_pok1 = df_lista[['Attack_id1']].values
lista_ata_pok2 = df_lista[['Attack_id2']].values
lista_def_pok1 = df_lista[['Defense_id1']].values
lista_def_pok2 = df_lista[['Defense_id2']].values
lista_diff_ataDef_pok1 = lista_ata_pok1 - lista_def_pok1
lista_diff_ataDef_pok2 = lista_ata_pok2 - lista_def_pok1
df_lista['diff_ata_def_pok1'] = lista_diff_ataDef_pok1
df_lista['diff_ata_def_pok2'] = lista_diff_ataDef_pok2
efec_pok1 = df_lista[['P1_type1', 'P1_type2']].values
efec_pok2 = df_lista[['P2_type1', 'P2_type2']].values
sumatorio_efectividad_pok1 = np.sum(efec_pok1, axis=1)
 sumatorio_efectividad_pok2 = np.sum(efec_pok2, axis=1)
 lista_ataESP_pok1 = df_lista[['Sp.Atk_id1']].values
lista_ataESP_pok2 = df_lista[['Sp.Atk_id2']].values
lista_defESP_pok1 = df_lista[['Sp.Def_id1']].values
lista_defESP_pok2 = df_lista[['Sp.Def_id2']].values
lista_ataESP_pok1_final = np.zeros(( len(df_lista)))
 lista_ataESP_pok2_final = np.zeros(( len(df_lista)))
 lista_diff_ataDefESP_pok1 = np.zeros(( len(df_lista)))
 lista_diff_ataDefESP_pok2 = np.zeros(( len(df_lista)))
 for i in range(0, len(df_lista)) :
lista_ataESP_pok1_final[i] = lista_ataESP_pok1[i] * sumatorio_efectividad_pok1[i]
lista_ataESP_pok2_final[i] = lista_ataESP_pok2[i] * sumatorio_efectividad_pok2[i]
lista_diff_ataDefESP_pok1[i] = lista_ataESP_pok1_final[i] - lista_defESP_pok2[i]
lista_diff_ataDefESP_pok2[i] = lista_ataESP_pok2_final[i] - lista_defESP_pok1[i]
i+=1
df_lista['diff_ata_def_ESP_pok1'] = lista_diff_ataDef_pok1
df_lista['diff_ata_def_ESP_pok2'] = lista_diff_ataDef_pok2
return df_lista
df_train = pd.read_csv('.. /input/homework-for-students4plus/train.csv', index_col=0, parse_dates=['issue_d'])
 df_test = pd.read_csv('.. /input/homework-for-students4plus/test.csv' , index_col=0, parse_dates=['issue_d'] )<prepare_x_and_y>
def juntar_csvs(es_conjunto_de_test):
df_pokemon, df_battles, df_test, le1, le2, lename = get_data()
pokemon_values = df_pokemon.values
if es_conjunto_de_test == False:
battles_values = df_battles.values
indice_pok1 = 0
indice_pok2 = 1
else:
battles_values = df_test.values
indice_pok1 = 1
indice_pok2 = 2
ids_pokemon = pokemon_values[:,0]
ids_pok1, inv1 = np.unique(battles_values[:, indice_pok1], return_inverse=True)
 ids_pok2, inv2 = np.unique(battles_values[:, indice_pok2], return_inverse=True)
 if es_conjunto_de_test == False:
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] + 13 + 6 + 6
pok_final = np.ones(( lon_values, columnas))
print(pok_final.shape)
 pok_final[:, :11] = pok1[:, :11]
pok_final[:, 11:22] = pok2[:, :11]
pok_final[:, 22:30] = pok1[:, 3:11] - pok2[:, 3:11]
pok1_ratios = pok1[:, 3:9]
pok2_ratios = pok2[:, 3:9]
pok_final[:, 30:36] = pok1_ratios/pok2_ratios
valores = np.array(( battles_values[:, indice_pok1], battles_values[:, indice_pok2], battles_values[:, indice_pok1]))
valores = valores.T
if es_conjunto_de_test == False:
caracteristicas_y_resultados = np.ones(( lon_values, columnas + 1))
caracteristicas_y_resultados[:,:-1] = pok_final
caracteristicas_y_resultados[:,-1] = resultados_batallas
lista = np.concatenate(( valores, caracteristicas_y_resultados), axis=1)
 else:
lista = np.concatenate(( valores, pok_final), axis=1)
 lista = lista.astype(int)
 columnas = ['First_pokemon', 'Second_pokemon', 'id_primer_ataq',
'nombre1', 'tipo1_id1', 'tipo2_id1',
'HP_id1','Attack_id1','Defense_id1','Sp.Atk_id1','Sp.Def_id1','Speed_id1', 'Generation_id1', 'Legendary_id1',
'nombre2', 'tipo1_id2', 'tipo2_id2',
'HP_id2','Attack_id2','Defense_id2','Sp.Atk_id2','Sp.Def_id2','Speed_id2', 'Generation_id2', 'Legendary_id2',
'diff_HP','diff_Attack','diff_Defense','diff_Sp.Atk','diff_Sp.Def','diff_Speed',
'diff_Generation', 'diff_Legendary',
'diff_Rapidez', 'diff_stats',
'ratio_HP','ratio_Attack','ratio_Defense','ratio_Sp.Atk','ratio_Sp.Def','ratio_Speed']
if es_conjunto_de_test == False:
columnas.append('Winner')
 df_lista = pd.DataFrame(lista, columns=columnas)
 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['diff_HPDefense_SpDef'] = df_lista['diff_HP'] + df_lista['diff_Defense'] + df_lista['diff_Sp.Def']
df_lista['ratio_HPDefense_SpDef'] = df_lista['ratio_HP'] + df_lista['ratio_Defense'] + df_lista['ratio_Sp.Def']
efec_pok1 = df_lista['P1_type1'].values + df_lista['P1_type2'].values
efec_pok2 = df_lista['P2_type1'].values + df_lista['P2_type2'].values
df_lista['diff_efectividad'] = np.subtract(efec_pok1,efec_pok2)
 if es_conjunto_de_test == False:
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 = df_lista.drop(['Legendary_id1', 'Legendary_id2', 'Generation_id1', 'Generation_id2'], axis=1)
 df_lista = df_lista.drop(['nombre1', 'nombre2'], axis = 1)
 print(df_lista.shape)
 if es_conjunto_de_test == False:
df_lista.to_csv(fichero, index=False)
 else:
df_lista.to_csv(tests, index=False)
 return lista
y_train = df_train.loan_condition
X_train = df_train.drop(['loan_condition'], axis=1)
 X_test = df_test<feature_engineering>
def GradientBoostingCl(train_x, train_y, test_x, test_y):
clf = GradientBoostingClassifier(n_estimators=800, learning_rate=0.05, subsample=0.75,max_depth=9)
 clf.fit(train_x, train_y)
 y_pred=clf.predict(test_x)
 print(clf.feature_importances_)
 print("Accuracy random forest1:",metrics.accuracy_score(test_y, y_pred))
return clf
X_train['loan_amnt'] = X_train['loan_amnt'].apply(np.log1p)
 X_test['loan_amnt'] = X_test['loan_amnt'].apply(np.log1p )<data_type_conversions>
def lightgbm_model(train_x, train_y, test_x, test_y, columnas):
d_train = lgbm.Dataset(train_x, label= train_y)
 d_valid = lgbm.Dataset(test_x, label= test_y)
 bst = lgbm.train(params, d_train, valid_sets= [d_valid], verbose_eval=100, feature_name=(columnas.tolist())[:-1])
 y_pred=bst.predict(test_x)
 for i in range(0,len(test_y)) :
if y_pred[i]>=.5:
y_pred[i]=1
else:
y_pred[i]=0
accuracy=accuracy_score(y_pred,test_y)
 print(accuracy)
 lgbm.plot_importance(bst, max_num_features = 15)
 return bst
def feature_search(X, y, columnas):
gridParams = {
'learning_rate': [0.05, 0.07, 0.08, 0.1],
'num_iterations': [900, 1000, 1100],
'boosting_type' : ['goss'],
'objective' : ['binary'],
'subsample' : [0.7,0.75],
'lambda_l1' : [0.005, 0.01],
'lambda_l2' : [0.005, 0.01],
'max_depth': [9, 10, 11, 12, -1]
}
model = lgbm.LGBMClassifier(params)
 grid = RandomizedSearchCV(estimator = model,
param_distributions = gridParams,
scoring='roc_auc',
n_jobs=-1,
iid=False,
verbose=2,
cv=3)
 grid.fit(X, y)
 print(grid.best_params_)
 print(grid.best_score_)
 return grid
X_train['earliest_cr_line_day'] = pd.to_datetime(X_train['earliest_cr_line'], format='%b-%Y')
 X_test['earliest_cr_line_day'] = pd.to_datetime(X_test['earliest_cr_line'] , format='%b-%Y' )<feature_engineering>
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
X_train["date_from_earliest_cr_line"] =(X_train["issue_d"] - X_train["earliest_cr_line_day"] ).dt.days
X_test["date_from_earliest_cr_line"] =(X_test["issue_d"] - X_test["earliest_cr_line_day"] ).dt.days
X_train["date_from_earliest_cr_line"] = X_train["date_from_earliest_cr_line"].fillna(X_train["date_from_earliest_cr_line"].median())
 X_test["date_from_earliest_cr_line"] = X_test["date_from_earliest_cr_line"].fillna(X_train["date_from_earliest_cr_line"].median())
 X_train["date_from_earliest_cr_line"] = X_train["date_from_earliest_cr_line"].astype("int")
 X_test["date_from_earliest_cr_line"] = X_test["date_from_earliest_cr_line"].astype("int")
 X_train.drop(['earliest_cr_line_day'],axis=1,inplace=True)
 X_test.drop(['earliest_cr_line_day'],axis=1,inplace=True)
 X_train.dtypes<feature_engineering>
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 )
X_train["sub_grade_num"] = [int(i[1])for i in X_train["sub_grade"]]
X_test["sub_grade_num"] = [int(i[1])for i in X_test["sub_grade"]]<data_type_conversions>
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()