Kaggle competition: MLH - Pokemon Challenge
X_train['month'] = X_train["issue_d"].dt.month
X_test['month'] = X_test["issue_d"].dt.month
X_train['month'] = X_train['month'].astype("str")
 X_test['month'] = X_test['month'].astype("str")
 X_train.dtypes<feature_engineering>
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/'
X_train["zip_code"] = [ i[0:3] for i in X_train["zip_code"]]
X_test["zip_code"] = [ i[0:3] for i in X_test["zip_code"]]<load_from_csv>
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
df_zip = pd.read_csv(".. /input/homework-for-students4plus/free-zipcode-database.csv")
 df_zip["Zipcode"] = [str(i ).zfill(5)[0:3] for i in df_zip["Zipcode"]]
df_zip.head()<set_options>
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
del df_zip
gc.collect()<feature_engineering>
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 )
X_train["zip_code_null"] = X_train["zip_code"].where(X_train["zip_code"].isin(zip_list), "zip_null")
 X_test["zip_code_null"] = X_test["zip_code"].where(X_test["zip_code"].isin(zip_list), "zip_null" )<define_variables>
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
date_col = ['issue_d']
lang_col = ['emp_title','title']
cats_col = []
for col in X_train.columns:
if X_train[col].dtype == 'object':
cats_col.append(col)
 cats_col = list(set(cats_col)- set(lang_col))
num_col = list(X_train.columns)
 num_col = list(set(num_col)- set(date_col))
num_col = list(set(num_col)- set(lang_col))
num_col = list(set(num_col)- set(cats_col))
print("date_col : " + str(len(date_col)))
 print("lang_col : " + str(len(lang_col)))
 print("cats_col : " + str(len(cats_col)))
 print("num_col : " + str(len(num_col)))
 print("total : " + str(len(X_train.columns)) )<define_variables>
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 )