Kaggle competition: Titanic - Machine Learning from Disaster
vectorizer = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 2), stop_words='english')
 X = vectorizer.fit_transform(data["Text"])
 print(len(vectorizer.get_feature_names()))
print(X.shape )<feature_engineering>
import seaborn as sns
import matplotlib.pyplot as plt
data["category_id"]=data["Category"].factorize() [0]<remove_duplicates>
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier
data = data[["Text", "category_id", "Category"]]
data
category_id_data = data[['Category', 'category_id']].drop_duplicates().sort_values('category_id')
 category_to_id = dict(category_id_data.values)
 id_to_category = dict(category_id_data[['category_id', 'Category']].values)
 category_id_data
id_to_category<categorify>
from sklearn.ensemble import ExtraTreesClassifier
tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 2), stop_words='english')
 features = tfidf.fit_transform(data.Text ).toarray()
print(features)
 labels = data.category_id
print(labels)
 features.shape<statistical_test>
df = pd.read_csv('.. /input/titanic/train.csv')
 test_df = pd.read_csv('.. /input/titanic/test.csv')
 combine = [df, test_df]
N = 5
for category, category_id in sorted(category_to_id.items()):
features_chi2 = chi2(features, labels == category_id)
 indices = np.argsort(features_chi2[0])
 feature_names = np.array(tfidf.get_feature_names())[indices]
unigrams = [v for v in feature_names if len(v.split(' ')) == 1]
bigrams = [v for v in feature_names if len(v.split(' ')) == 2]
print("
print(".Most correlated unigrams: 
.{}".format(' 
.'.join(unigrams[-N:])))
 print(".Most correlated bigrams: 
.{}".format(' 
.'.join(bigrams[-N:])) )<find_best_model_class>
df.isnull().sum()
X_train, X_test, y_train, y_test, indices_train, indices_test = train_test_split(features, labels, data.index, test_size=0.33, random_state=0)
 for model in models:
model.fit(X_train, y_train)
 y_pred_proba = model.predict_proba(X_test)
 y_pred = model.predict(X_test)
 accuracy = accuracy_score(y_test, y_pred)
 print(accuracy )<train_model>
for dataset in combine:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 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')
 df[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
model = models[2]
model.fit(features, labels)
 model.coef_<load_from_csv>
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 )
test_data = pd.read_csv(".. /input/bbc-test/BBC News Test.csv")
 test_data<predict_on_test>
emb = pd.get_dummies(df.Title)
 df = df.join(emb)
 df = df.rename(columns={1:"Mr", 2:"Miss", 3:"Mrs", 4:"Master", 5:"Rare"})
 df = df.drop(["Title"],axis=1)
 emb = pd.get_dummies(test_df.Title)
 test_df = test_df.join(emb)
 test_df = test_df.rename(columns={1:"Mr", 2:"Miss", 3:"Mrs", 4:"Master", 5:"Rare"})
 test_df = test_df.drop(["Title"],axis=1)
 combine = [df, test_df]
test_data.Text.tolist()
test_features = tfidf.transform(test_data.Text.tolist())
 Y_pred = model.predict(test_features)
 Y_pred
submission = []
for pred in Y_pred:
submission.append(id_to_category[pred])
 submission<create_dataframe>
freq_port = df.Embarked.dropna().mode() [0]
freq_port
submission = pd.DataFrame({
"ArticleId": test_data["ArticleId"],
"Category": submission
})
 submission<save_to_csv>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
 df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
submission.to_csv('submission.csv', index=False )<save_to_csv>
for dataset in combine:
dataset["Sex"] = dataset["Sex"].map({'male': 1,'female':0})
 dataset["Embarked"] = dataset["Embarked"].map({'S': 1,'C':2,'Q':3})
 emb = pd.get_dummies(df.Embarked)
 df = df.join(emb)
 df = df.rename(columns={1.0: "S", 2.0: "C", 3.0 : "Q"})
 emb = pd.get_dummies(test_df.Embarked)
 test_df = test_df.join(emb)
 test_df = test_df.rename(columns={1.0: "S", 2.0: "C", 3.0 : "Q"})
 df = df.drop(["Name","Ticket","Embarked","PassengerId"],axis = 1)
 test_df = test_df.drop(["Name","Ticket","Embarked"],axis = 1)
 combine = [df, test_df]
submission.to_csv('submission.csv', index=False )<load_from_csv>
def status(feature):
print('Processing', feature, ': ok')
TRAIN_PATH = os.path.join(".. /input/ai-academy-intermediate-class-competition-1", "BBC News Train.csv")
 df = pd.read_csv(TRAIN_PATH )<feature_engineering>
def process_cabin(combined):
combined.Cabin.fillna('U', inplace=True)
 combined['Cabin'] = combined['Cabin'].map(lambda c: c[0])
 cabin_dummies = pd.get_dummies(combined['Cabin'], prefix='Cabin')
 combined = pd.concat([combined, cabin_dummies], axis=1)
 combined.drop('Cabin', axis=1, inplace=True)
 status('cabin')
 return combined
df['category_id'] = df['Category'].factorize() [0]
df['category_id'][0:10]<remove_duplicates>
combine = [df,test_df]
category_id_df = df[['Category', 'category_id']].drop_duplicates().sort_values('category_id' )<define_variables>
df = df.drop('Cabin_T', axis=1)
 df.head()
category_to_id = dict(category_id_df.values)
 id_to_category = dict(category_id_df[['category_id', 'Category']].values )<count_values>
combine = [df,test_df]
df.groupby('Category' ).category_id.count() 
<categorify>
guess_ages = np.zeros(( 2,3))
guess_ages
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)
 df.head()
tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 2), stop_words='english')
 features = tfidf.fit_transform(df.Text ).toarray()
labels = df.category_id 
<sort_values>
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']
df.head()
sorted(category_to_id.items() )<statistical_test>
for dataset in combine:
dataset['Age*Class'] = dataset.Age * dataset.Pclass
N = 3
for Category, category_id in sorted(category_to_id.items()):
features_chi2 = chi2(features, labels == category_id)
 indices = np.argsort(features_chi2[0])
 feature_names = np.array(tfidf.get_feature_names())[indices]
unigrams = [v for v in feature_names if len(v.split(' ')) == 1]
bigrams = [v for v in feature_names if len(v.split(' ')) == 2]
print("
print(".Most correlated unigrams: 
.{}".format(' 
.'.join(unigrams[-N:])))
 print(".Most correlated bigrams: 
.{}".format(' 
.'.join(bigrams[-N:])) )<define_variables>
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
df[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
SAMPLE_SIZE = int(len(features)* 0.3)
 np.random.seed(0)
 indices = np.random.choice(range(len(features)) , size=SAMPLE_SIZE, replace=False)
 projected_features = TSNE(n_components=2, random_state=0 ).fit_transform(features[indices])
<filter>
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
df[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()
my_id = 0
projected_features[(labels[indices] == my_id ).values]<choose_model_class>
df = df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 test_df = test_df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 combine = [df, test_df]
df.head()
models = [
RandomForestClassifier(n_estimators=200, max_depth=3, random_state=0),
MultinomialNB() ,
LogisticRegression(random_state=0),
] 
<create_dataframe>
test_df['Fare'].fillna(test_df['Fare'].dropna().median() , inplace=True )
CV = 5
cv_df = pd.DataFrame(index=range(CV * len(models)))
 entries = []<find_best_model_class>
test_df.isnull().sum()
for model in models:
model_name = model.__class__.__name__
accuracies = cross_val_score(model, features, labels, scoring='accuracy', cv=CV)
 for fold_idx, accuracy in enumerate(accuracies):
entries.append(( model_name, fold_idx, accuracy))<create_dataframe>
df['FareBand'] = pd.qcut(df['Fare'], 4)
 df[['FareBand', 'Survived']].groupby(['FareBand'], as_index=False ).mean().sort_values(by='FareBand', ascending=True )
cv_df = pd.DataFrame(entries, columns=['model_name', 'fold_idx', 'accuracy'] )<groupby>
for dataset in combine:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 df = df.drop(['FareBand'], axis=1)
 combine = [df, test_df]
df.head(10 )
cv_df.groupby('model_name' ).accuracy.mean()<train_model>
X_train = df.drop("Survived", axis=1)
 Y_train = df["Survived"]
X_test = test_df.drop("PassengerId", axis=1 ).copy()
X_train.shape, Y_train.shape, X_test.shape
model = LogisticRegression(random_state=0)
 X_train, X_test, y_train, y_test, indices_train, indices_test = train_test_split(features, labels, df.index, test_size=0.33, random_state=0)
 model.fit(X_train, y_train)
 y_pred_proba = model.predict_proba(X_test)
 y_pred = model.predict(X_test )<compute_test_metric>
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
 Y_pred = logreg.predict(X_test)
 acc_log = round(logreg.score(X_train, Y_train)* 100, 2)
 acc_log
for predicted in category_id_df.category_id:
for actual in category_id_df.category_id:
if predicted != actual and conf_mat[actual, predicted] >= 2:
print("'{}' predicted as '{}' : {} examples.".format(id_to_category[actual], id_to_category[predicted], conf_mat[actual, predicted]))
display(df.loc[indices_test[(y_test == actual)&(y_pred == predicted)]]['Text'])
 print('' )<train_model>
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, Y_train)
 Y_pred = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, Y_train)* 100, 2)
 acc_knn
model.fit(features, labels )<features_selection>
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_pred = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, Y_train)* 100, 2)
 acc_gaussian
N = 5
for Category, category_id in sorted(category_to_id.items()):
indices = np.argsort(model.coef_[category_id])
 feature_names = np.array(tfidf.get_feature_names())[indices]
unigrams = [v for v in reversed(feature_names)if len(v.split(' ')) == 1][:N]
bigrams = [v for v in reversed(feature_names)if len(v.split(' ')) == 2][:N]
print("
print(".Top unigrams: 
.{}".format(' 
.'.join(unigrams)))
 print(".Top bigrams: 
.{}".format(' 
.'.join(bigrams)) )<predict_on_test>
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_pred = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, Y_train)* 100, 2)
 acc_gaussian
texts = ["Hooli stock price soared after a dip in PiedPiper revenue growth.",
"Captain Tsubasa scores a magnificent goal for the Japanese team.",
"Merryweather mercenaries are sent on another mission, as government oversight groups call for new sanctions.",
"Beyoncé releases a new album, tops the charts in all of south-east Asia!",
"You won't guess what the latest trend in data analysis is!"]
text_features = tfidf.transform(texts)
 predictions = model.predict(text_features)
 for text, predicted in zip(texts, predictions):
print('"{}"'.format(text))
print(" - Predicted as: '{}'".format(id_to_category[predicted]))
print("" )<load_from_csv>
perceptron = Perceptron()
perceptron.fit(X_train, Y_train)
 Y_pred = perceptron.predict(X_test)
 acc_perceptron = round(perceptron.score(X_train, Y_train)* 100, 2)
 acc_perceptron
TEST_PATH = os.path.join(".. /input/bbc-news-test", "BBC News Test.csv")
 test_df = pd.read_csv(TEST_PATH)
<predict_on_test>
linear_svc = LinearSVC()
linear_svc.fit(X_train, Y_train)
 Y_pred = linear_svc.predict(X_test)
 acc_linear_svc = round(linear_svc.score(X_train, Y_train)* 100, 2)
 acc_linear_svc
test_features = tfidf.transform(test_df.Text.tolist())
 Y_pred = model.predict(test_features)
 Y_pred<define_variables>
sgd = SGDClassifier()
sgd.fit(X_train, Y_train)
 Y_pred = sgd.predict(X_test)
 acc_sgd = round(sgd.score(X_train, Y_train)* 100, 2)
 acc_sgd
Y_pred_name =[]
for cat_id in Y_pred :
Y_pred_name.append(id_to_category[cat_id] )<create_dataframe>
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 Y_predt = decision_tree.predict(X_test)
 acc_decision_tree = round(decision_tree.score(X_train, Y_train)* 100, 2)
 acc_decision_tree
submission = pd.DataFrame({
"ArticleId": test_df["ArticleId"],
"Category": Y_pred_name
} )<save_to_csv>
ex = ExtraTreesClassifier(random_state = 6, bootstrap=True, oob_score=True)
 ex.fit(X_train, Y_train)
 y_predERT = ex.predict(X_test)
 ex.score(X_train, Y_train)
 score = round(ex.score(X_train, Y_train)* 100, 2)
 print('Extremely Randomized Trees', score )
submission.to_csv('submission.csv', index=False )<install_modules>
from sklearn.model_selection import GridSearchCV
<define_variables>
from sklearn.model_selection import GridSearchCV
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' 
<load_from_csv>
n_estimators = [int(x)for x in np.linspace(start = 10, stop = 80, num = 10)]
max_features = ['auto', 'sqrt']
max_depth = [2,4]
min_samples_split = [2, 5]
min_samples_leaf = [1, 2]
bootstrap = [True, False]
param_grid = {'n_estimators': n_estimators,
'max_features': max_features,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'bootstrap': bootstrap}
rf_Model = RandomForestClassifier()
rf_Grid = GridSearchCV(estimator = rf_Model, param_grid = param_grid, cv = 3, verbose=2, n_jobs = 4)
 rf_Grid.fit(X_train, Y_train)
 rf_Grid.best_params_
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<concatenate>
Y_pred = rf_Grid.predict(X_test)
 rf_Grid.score(X_train, Y_train)
 acc_random_forest = round(rf_Grid.score(X_train, Y_train)* 100, 2)
 acc_random_forest
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<compute_train_metric>
C=[0.05,0.1,0.2,0.3,0.25,0.4,0.5,0.6,0.7,0.8,0.9,1]
gamma=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
kernel=['rbf','linear']
hyper={'kernel':kernel,'C':C,'gamma':gamma}
gd=GridSearchCV(SVC() ,param_grid=hyper,verbose=True)
 gd.fit(X_train, Y_train)
 Y_preds = gd.predict(X_test)
 acc_svc = round(gd.score(X_train, Y_train)* 100, 2)
 acc_svc
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<choose_model_class>
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron',
'Stochastic Gradient Decent', 'Linear SVC',
'Decision Tree'],
'Score': [acc_svc, acc_knn, acc_log,
acc_random_forest, acc_gaussian, acc_perceptron,
acc_sgd, acc_linear_svc, acc_decision_tree]})
 sorted_model=models.sort_values(by='Score', ascending=False)
 sorted_model
<predict_on_test><EOS> 
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred
})
 submission.to_csv('submission2.csv', index=False )