Kaggle competition: Titanic - Machine Learning from Disaster
Ytest_pred2 = ridge.predict(Xtest)
 Ytest_pred2 = abs(Ytest_pred2 )<data_type_conversions>
train_dir = ".. /input/train.csv"
test_dir = ".. /input/test.csv"
result2 = np.vstack(( test['Id'], Ytest_pred2)).T.astype(int)
 x2 = ["Id","median_house_value"]
Resultado2 = pd.DataFrame(columns = x2, data = result2 )<compute_train_metric>
df = pd.read_csv(train_dir)
 test_df = pd.read_csv(test_dir )
lasso = linear_model.Lasso()
lasso.fit(Xtrain, Ytrain)
 total = 0
c_val = 10
scores = cross_val_score(lasso,Xtrain,Ytrain, cv = c_val,scoring = scorer_rmsle)
 total = 0
for j in scores:
total += j
acuracia_esperada = total/c_val
print(acuracia_esperada )<predict_on_test>
df.drop(["Ticket"], axis = 1, inplace = True)
 test_df.drop(["Ticket"], axis = 1, inplace = True)
 df.info()
df.head()
Ytest_pred3 = ridge.predict(Xtest)
 Ytest_pred3 = abs(Ytest_pred3 )<data_type_conversions>
print(df["Survived"].value_counts())
 df["Survived"].value_counts().plot(kind = "pie" )
result3 = np.vstack(( test['Id'], Ytest_pred3)).T.astype(int)
 x3 = ["Id","median_house_value"]
Resultado3 = pd.DataFrame(columns = x3, data = result3 )<train_on_grid>
df.drop("PassengerId", axis = 1, inplace = True)
 test_df.drop("PassengerId", axis = 1, inplace = True )
arvore = tree.DecisionTreeRegressor()
arvore.fit(Xtrain,Ytrain)
 scores = cross_val_score(arvore,Xtrain,Ytrain, cv = c_val,scoring = scorer_rmsle)
 total = 0
for j in scores:
total += j
acuracia_esperada = total/c_val
print(acuracia_esperada )<predict_on_test>
pclass_dum = pd.get_dummies(df["Pclass"])
 test_pclass_dum = pd.get_dummies(test_df["Pclass"])
 df = pd.concat([df, pclass_dum], axis = 1)
 test_df = pd.concat([test_df, test_pclass_dum], axis = 1)
 df.rename({1:"pclass1", 2:"pclass2", 3:"pclass3"}, axis = 1, inplace = True)
 test_df.rename({1:"pclass1", 2:"pclass2", 3:"pclass3"}, axis = 1, inplace = True)
 df.drop(["Pclass"], axis = 1, inplace = True)
 test_df.drop(["Pclass"], axis = 1, inplace = True )
Ytest_pred4 = arvore.predict(Xtest)
<data_type_conversions>
df["Sex"].replace("male", 0, inplace = True)
 test_df["Sex"].replace("male", 0, inplace = True)
 df["Sex"].replace("female", 1, inplace = True)
 test_df["Sex"].replace("female", 1, inplace = True)
 df["Embarked"].fillna("S", inplace = True)
 test_df["Embarked"].fillna("S", inplace = True)
 pclass_dum = pd.get_dummies(df["Embarked"])
 test_pclass_dum = pd.get_dummies(test_df["Embarked"])
 df = pd.concat([df, pclass_dum], axis = 1)
 test_df = pd.concat([test_df, test_pclass_dum], axis = 1)
 df.rename({"S":"embarked_s", "C":"embarked_c", "Q":"embarked_q"}, axis = 1, inplace = True)
 test_df.rename({"S":"embarked_s", "C":"embarked_c", "Q":"embarked_q"}, axis = 1, inplace = True)
 df.drop(["Embarked"], axis = 1, inplace = True)
 test_df.drop(["Embarked"], axis = 1, inplace = True )
result4 = np.vstack(( test['Id'], Ytest_pred4)).T.astype(int)
 x4 = ["Id","median_house_value"]
Resultado4 = pd.DataFrame(columns = x4, data = result4 )<train_on_grid>
def create_family_ranges(df):
familysize = []
for members in df["n_fam_mem"]:
if members == 1:
familysize.append(1)
 elif members == 2:
familysize.append(2)
 elif members>2 and members<=4:
familysize.append(3)
 elif members > 4:
familysize.append(4)
 return familysize
famsize = create_family_ranges(df)
 df["familysize"] = famsize
test_famsize = create_family_ranges(test_df)
 test_df["familysize"] = test_famsize
bot = BaggingRegressor(n_estimators = 200)
 bot.fit(Xtrain,Ytrain)
 total = 0
c_val = 10
scores = cross_val_score(bot,Xtrain,Ytrain, cv = c_val,scoring = scorer_rmsle)
 total = 0
for j in scores:
total += j
acuracia_esperada = total/c_val
print(acuracia_esperada )<predict_on_test>
fsizedummies = pd.get_dummies(df["familysize"])
 test_fsizedummies = pd.get_dummies(test_df["familysize"])
 df = pd.concat([df, fsizedummies], axis = 1)
 test_df = pd.concat([test_df, test_fsizedummies], axis = 1)
 df.rename({1:"fam_single",2:"fam_small",3:"fam_medium", 4:"fam_big"}, axis = 1, inplace = True)
 test_df.rename({1:"fam_single",2:"fam_small",3:"fam_medium", 4:"fam_big"}, axis = 1, inplace = True )
Ytest_pred5 = bot.predict(Xtest)
<create_dataframe>
reg_df = df.drop(["Survived", "Name", "Cabin"], axis = 1)
 reg_df_test = test_df.drop(["Name", "Cabin"], axis = 1)
 age_reg_df = reg_df[reg_df["Age"].isna() == False]
age_reg_df_test = reg_df_test[reg_df_test["Age"].isna() == False]
new_age_df = age_reg_df.append(age_reg_df_test)
 new_age_X = new_age_df.drop(["Age"], axis = 1)
 new_age_y = new_age_df["Age"]
new_age_X["Fare"].fillna(df["Fare"].median() , inplace = True)
 linear_reg_model = LinearRegression().fit(new_age_X, new_age_y )
result5 = np.vstack(( test['Id'], Ytest_pred5)).T.astype(int)
 x5 = ["Id","median_house_value"]
Resultado5 = pd.DataFrame(columns = x5, data = result5 )<find_best_model_class>
def get_age_indexes_to_replace(df):
age_temp_list = df["Age"].values.tolist()
indexes_age_replace = []
age_temp_list = [str(x)for x in age_temp_list]
for i, item in enumerate(age_temp_list):
if item == "nan":
indexes_age_replace.append(i)
 return indexes_age_replace
indexes_to_replace_main = get_age_indexes_to_replace(df)
 indexes_to_replace_test = get_age_indexes_to_replace(test_df)
 def linear_age_predictions(reg_df, indexes_age_replace):
reg_df_temp = reg_df.drop(["Age"], axis = 1)
 age_predictions = []
for i in indexes_age_replace:
x = reg_df_temp.iloc[i]
x = np.array(x ).reshape(1,-1)
 pred = linear_reg_model.predict(x)
 age_predictions.append(pred)
 return age_predictions
age_predictions_main = linear_age_predictions(reg_df, indexes_to_replace_main)
 age_predictions_test = linear_age_predictions(reg_df_test, indexes_to_replace_test)
 def fill_age_nan(df, indexes_age_replace, age_predictions):
for i, item in enumerate(indexes_age_replace):
df["Age"][item] = age_predictions[i]
return df
df = fill_age_nan(df, indexes_to_replace_main, age_predictions_main)
 df_test = fill_age_nan(test_df, indexes_to_replace_test, age_predictions_test )
rdf = RandomForestRegressor(n_estimators = 200, max_features = 'log2', min_samples_leaf = 1)
 rdf.fit(Xtrain, Ytrain)
 total = 0
c_val = 10
scores = cross_val_score(rdf,Xtrain,Ytrain, cv = c_val,scoring = scorer_rmsle)
 total = 0
for j in scores:
total += j
acuracia_esperada = total/c_val
print(acuracia_esperada )<predict_on_test>
def age_to_int(df):
agelist = df["Age"].values.tolist()
for i in range(len(agelist)) :
if agelist[i] < 14:
agelist[i] = 0
elif agelist[i] >= 14 and agelist[i] < 25:
agelist[i] = 1
elif agelist[i]>=25 and agelist[i]<60:
agelist[i] = 2
elif agelist[i]>=60:
agelist[i] = 3
ageint = pd.DataFrame(agelist)
 return ageint
Ytest_pred6 = rdf.predict(Xtest)
<save_to_csv>
ageint = age_to_int(df)
 df["Ageint"] = ageint
df.drop("Age", axis = 1, inplace = True)
 test_ageint = age_to_int(test_df)
 test_df["Ageint"] = test_ageint
test_df.drop("Age", axis = 1, inplace = True )
result6 = np.vstack(( test['Id'], Ytest_pred6)).T.astype(int)
 x6 = ["Id","median_house_value"]
Resultado = pd.DataFrame(columns = x6, data = result6)
 Resultado.to_csv("resultados_rdf.csv", index = False )<train_on_grid>
def conv_fare_ranges(df):
fare_ranges = []
for fare in df["actual_fare"]:
if fare < 7:
fare_ranges.append(0)
 elif fare >=7 and fare < 14:
fare_ranges.append(1)
 elif fare >=14 and fare < 30:
fare_ranges.append(2)
 elif fare >=30 and fare < 50:
fare_ranges.append(3)
 elif fare >=50:
fare_ranges.append(4)
 return fare_ranges
fare_ranges = conv_fare_ranges(df)
 df["fare_ranges"] = fare_ranges
test_fare_ranges = conv_fare_ranges(test_df)
 test_df["fare_ranges"] = test_fare_ranges
etr = ExtraTreesRegressor(n_estimators = 200)
 etr.fit(Xtrain,Ytrain)
 total = 0
c_val = 10
scores = cross_val_score(etr,Xtrain,Ytrain, cv = c_val,scoring = scorer_rmsle)
 total = 0
for j in scores:
total += j
acuracia_esperada = total/c_val
print(acuracia_esperada )<predict_on_test>
df["Cabin"].fillna("unknown", inplace = True)
 test_df["Cabin"].fillna("unknown", inplace = True )
Ytest_pred7 = rdf.predict(Xtest)
<create_dataframe>
cabins = [i[0] if i!= 'unknown' else 'unknown' for i in df['Cabin']]
test_cabins = [i[0] if i!= 'unknown' else 'unknown' for i in test_df['Cabin']]
df.drop(["Cabin"], axis = 1, inplace = True)
 test_df.drop(["Cabin"], axis = 1, inplace = True)
 df["cabintype"] = cabins
test_df["cabintype"] = test_cabins
result7 = np.vstack(( test['Id'], Ytest_pred7)).T.astype(int)
 x7 = ["Id","median_house_value"]
Resultado = pd.DataFrame(columns = x7, data = result7 )<import_modules>
df.drop(["cabintype"], axis = 1, inplace = True)
 test_df.drop(["cabintype"], axis = 1, inplace = True )
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier<load_from_csv>
def name_to_int(df):
name = df["Name"].values.tolist()
namelist = []
for i in name:
index = 1
inew = i.split()
if inew[0].endswith(","):
index = 1
elif inew[1].endswith(","):
index = 2
elif inew[2].endswith(","):
index = 3
namelist.append(inew[index])
 titlelist = []
for i in range(len(namelist)) :
titlelist.append(namelist[i])
 return titlelist
PATH = '.. /input/dm-and-pr-ws1920-machine-learning-competition/'
df_train = pd.read_csv(PATH+'train.csv')
 df_test = pd.read_csv(PATH+'test.csv')
 sample_sub = pd.read_csv(PATH+'sampleSubmission.csv' )<prepare_x_and_y>
titlelist = name_to_int(df)
 df["titles"] = titlelist
df["titles"].value_counts()
testtitlelist = name_to_int(test_df)
 test_df["titles"] = testtitlelist
df["titles"].value_counts()
X = df_train.profession.values
y = df_train.target.values
X_test = df_test.profession.values<predict_on_test>
df["titles"].replace(["Mr.", "Miss.", "Mrs.", "Master.","sometitle"],[0,1,2,3,4], inplace = True)
 df["titles"].astype("int64")
 test_df["titles"].replace(["Mr.", "Miss.", "Mrs.", "Master.", "sometitle"],[0,1,2,3,4], inplace = True)
 test_df["titles"].astype("int64")
 df.drop(["Name"], axis = 1, inplace = True)
 test_df.drop(["Name"], axis = 1, inplace = True )
model = DecisionTreeClassifier(max_depth=4)
 model.fit(X.reshape(-1,1),y)
 y_hat = model.predict_proba(X_test.reshape(-1,1)) [:,1]<prepare_output>
df.drop(["Fare","n_fam_mem","actual_fare"], axis = 1, inplace = True)
 test_df.drop(["Fare","n_fam_mem","actual_fare"], axis = 1, inplace = True )
sample_sub['target'] = y_hat
sample_sub.head() 
<save_to_csv>
labels = df["Survived"]
data = df.drop("Survived", axis = 1 )
sample_sub.to_csv('estimation_01.csv', index=False )<set_options>
final_clf = None
clf_names = ["Logistic Regression", "KNN(3)", "XGBoost Classifier", "Random forest classifier", "Decision Tree Classifier",
"Gradient Boosting Classifier", "Support Vector Machine"]
%matplotlib inline
warnings.filterwarnings('ignore')
<load_from_csv>
classifiers = []
scores = []
for i in range(10):
X_train, X_test, Y_train, Y_test = train_test_split(data, labels, test_size = 0.1)
 tempscores = []
lr_clf = LogisticRegression()
lr_clf.fit(X_train, Y_train)
 tempscores.append(( lr_clf.score(X_test, Y_test)) *100)
 knn3_clf = KNeighborsClassifier(n_neighbors = 3)
 knn3_clf.fit(X_train, Y_train)
 tempscores.append(( knn3_clf.score(X_test, Y_test)) *100)
 xgbc = XGBClassifier(n_estimators=15, seed=41)
 xgbc.fit(X_train, Y_train)
 tempscores.append(( xgbc.score(X_test, Y_test)) *100)
 rf_clf = RandomForestClassifier(n_estimators = 100)
 rf_clf.fit(X_train, Y_train)
 tempscores.append(( rf_clf.score(X_test, Y_test)) *100)
 dt_clf = DecisionTreeClassifier()
dt_clf.fit(X_train, Y_train)
 tempscores.append(( dt_clf.score(X_test, Y_test)) *100)
 gb_clf = GradientBoostingClassifier()
gb_clf.fit(X_train, Y_train)
 tempscores.append(( gb_clf.score(X_test, Y_test)) *100)
 svm_clf = SVC(gamma = "scale")
 svm_clf.fit(X_train, Y_train)
 tempscores.append(( svm_clf.score(X_test, Y_test)) *100)
 scores.append(tempscores )
teste_base = pd.read_csv('.. /input/dataset_teste.csv')
 teste = teste_base.copy()
teste.info()<define_variables>
scores = np.array(scores)
 clfs = pd.DataFrame({"Classifier":clf_names})
 for i in range(len(scores)) :
clfs['iteration' + str(i)] = scores[i].T
means = clfs.mean(axis = 1)
 means = means.values.tolist()
clfs["Average"] = means
featuresTeste = [
"Postal Code",
"Latitude",
"Longitude",
"DOF Gross Floor Area",
"Year Built",
"Number of Buildings - Self-reported",
"Occupancy",
"Site EUI(kBtu/ft²)",
"Property GFA - Self-Reported(ft²)",
"Source EUI(kBtu/ft²)",
"Community Board",
"Council District",
"Census Tract",
"Weather Normalized Site EUI(kBtu/ft²)",
"Weather Normalized Site Electricity Intensity(kWh/ft²)",
"Weather Normalized Source EUI(kBtu/ft²)",
"Weather Normalized Site Natural Gas Use(therms)",
"Weather Normalized Site Electricity(kWh)",
"Water Use(All Water Sources )(kgal)",
"Water Intensity(All Water Sources )(gal/ft²)",
"Total GHG Emissions(Metric Tons CO2e)",
"Direct GHG Emissions(Metric Tons CO2e)",
"Indirect GHG Emissions(Metric Tons CO2e)",
"Electricity Use - Grid Purchase(kBtu)",
"Natural Gas Use(kBtu)",
"Manhattan", "Queens", "Brooklyn", "Staten Island"]
featuresTreino = featuresTeste + ["ENERGY STAR Score"]<data_type_conversions>
clfs.set_index("Classifier", inplace = True)
 print("Accuracies : ")
 clfs["Average"].head(10 )
def setCity(df):
lista = df["Borough"].value_counts()
for item in lista.index:
df[item] = df["Borough"] == item
df[item] = df[item].astype(int)
 return df<data_type_conversions>
def create_multiple() :
ensembles = []
ensemble_scores = []
for i in range(5):
X_train, X_test, Y_train, Y_test = train_test_split(data, labels, test_size = 0.07)
 svm_clf = SVC(gamma = "scale")
 svm_clf = svm_clf.fit(X_train, Y_train)
 ensemble_scores.append(( svm_clf.score(X_test, Y_test)) *100)
 ensembles.append(svm_clf)
 return ensembles, ensemble_scores
SVM_ensembles, SVM_ensemble_scores = create_multiple()
def setPostalCode(df):
df["Postal Code"] = df["Postal Code"].str.replace("-", "")
 df["Postal Code"] = df["Postal Code"].astype(int)
 return df<data_type_conversions>
def print_ensemble_score(ensemble_scores, model_name):
e_score = 0
for i in range(len(ensemble_scores)) :
e_score = e_score + ensemble_scores[i]
print("SCORE(ENSEMBLE MODELS)" +str(model_name)+ " : " + str(e_score/len(ensemble_scores)))
 return
print_ensemble_score(SVM_ensemble_scores, "SVM" )
def setMean(df, features):
df = df.replace('Not Available',np.nan, regex=True)
 for item in features:
if df[item].dtype == "object":
df[item] = df[item].astype(float)
 for item in features:
df[item] = df[item].fillna(df[item].mean())
 return df<feature_engineering>
def per_model_prediction(ensembles):
test_data = test_df
predictions_ensembles = []
for clf in ensembles:
temppredictions = clf.predict(test_data)
 predictions_ensembles.append(temppredictions)
 return predictions_ensembles
def setGeneralData(df, features):
df["Number of Buildings - Self-reported"][df["Number of Buildings - Self-reported"] > 30 ] = 30
df["Number of Buildings - Self-reported"][df["Number of Buildings - Self-reported"] <= 0] = 1
df["Occupancy"][df["Occupancy"] <= 0] = 1
df["Site EUI(kBtu/ft²)"][df["Site EUI(kBtu/ft²)"] <= 0] = 1
df["Property GFA - Self-Reported(ft²)"][df["Property GFA - Self-Reported(ft²)"] >= 2500000] = 2500000
df["Source EUI(kBtu/ft²)"][df["Source EUI(kBtu/ft²)"] < 1] = 1
df["Year Built"][df["Year Built"] < 1800] = 1800
df["Year Built"][df["Year Built"] > 2015] = 2015
df = df.round(2)
 return df<categorify>
def get_predictions_modes(predictions_ensembles):
final_predictions_list = []
for i in range(len(predictions_ensembles[0])) :
temp = [predictions_ensembles[0][i], predictions_ensembles[1][i], predictions_ensembles[2][i], predictions_ensembles[3][i], predictions_ensembles[4][i]]
final_predictions_list.append(temp)
 final_predictions_list = np.array(final_predictions_list)
 pred_modes = stats.mode(final_predictions_list, axis = 1)
 final_predictions = []
for i in pred_modes[0]:
final_predictions.append(i[0])
 return final_predictions
treino = setPostalCode(treino)
 teste = setPostalCode(teste)
 treino = setCity(treino)
 teste = setCity(teste)
 treino = setMean(treino, featuresTreino)
 teste = setMean(teste, featuresTeste)
 treino = setGeneralData(treino, featuresTreino)
 teste = setGeneralData(teste, featuresTeste)
 print(treino.shape)
 print(teste.shape)
 treino = treino.filter(items=featuresTreino)
 teste = teste.filter(items=featuresTeste)
 print(treino.shape)
 print(teste.shape)
 showCorr(treino )<split>
SVM_predictions_ensembles = per_model_prediction(SVM_ensembles)
 SVM_final_predictions = get_predictions_modes(SVM_predictions_ensembles )
X_train, X_test, y_train, y_test = train_test_split(treino.drop(columns=['ENERGY STAR Score']), pd.DataFrame(treino["ENERGY STAR Score"]))<choose_model_class>
passengerid = [892 + i for i in range(len(SVM_final_predictions)) ]
sub = pd.DataFrame({'PassengerId': passengerid, 'Survived':SVM_final_predictions})
 sub.to_csv('submission.csv', index = False )