Kaggle competition: Titanic - Machine Learning from Disaster
submit_pred(d_dset.drop('latitude', axis = 1),
d_test.drop('latitude', axis = 1),
model = linear_model.BayesianRidge() ,
name = 'linear_model_bayes' )<predict_on_test>
%matplotlib inline
submit_pred(d_dset,
d_test,
model = ensemble.RandomForestRegressor(n_estimators = 100),
name = 'random_forest_gps' )<compute_train_metric>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 gen_sub = pd.read_csv('/kaggle/input/titanic/gender_submission.csv' )
submit_pred(d_dset.drop('latitude', axis = 1),
d_test.drop('latitude', axis = 1),
model = ensemble.RandomForestRegressor(n_estimators = 100),
name = 'random_forest' )<predict_on_test>
train.columns = [x.lower() for x in train.columns]
test.columns = [x.lower() for x in test.columns]
submit_pred(d_dset,
d_test,
model = ensemble.ExtraTreesRegressor(n_estimators = 100),
name = 'extra_trees_gps' )<predict_on_test>
train.drop(['passengerid','name','ticket'],axis=1,inplace=True)
 train.head(2 )
submit_pred(d_dset.drop('latitude', axis = 1),
d_test.drop('latitude', axis = 1),
model = ensemble.ExtraTreesRegressor(n_estimators = 100),
name = 'extra_trees' )<choose_model_class>
train.isnull().sum()
submit_pred(d_dset,
d_test,
model = ensemble.AdaBoostRegressor(base_estimator =
ensemble.RandomForestRegressor(max_depth=20, n_estimators=10), n_estimators=50),
name = 'ada_boost_gps' )<load_from_csv>
train.isnull().sum()
train_data = pd.read_csv(".. /input/atividade-3-pmr3508/train.csv")
 test_data = pd.read_csv(".. /input/atividade-3-pmr3508/test.csv")
 train_Id = train_data.loc[:,'Id']
test_Id = test_data.loc[:,'Id']
test_data = test_data.drop('Id',axis = 'columns')
 train_data = train_data.drop('Id',axis = 'columns')
 train_data 
<feature_engineering>
train.drop('cabin',axis=1,inplace=True)
 train.head()
def adding_new_features(df):
df.loc[:,'mean_rooms'] = df.loc[:,'total_rooms']/df.loc[:,'households']
df.loc[:,'rooms_per_person'] = df.loc[:,'total_rooms']/df.loc[:,'population']
df.loc[:,'mean_bedrooms'] = df.loc[:,'total_bedrooms']/df.loc[:,'households']
df.loc[:,'bedrooms_per_person'] = df.loc[:,'total_bedrooms']/df.loc[:,'households']
df.loc[:,'persons_per_household'] = df.loc[:,'population']/df.loc[:,'households']
df.loc[:, 'median_income_per_person'] = df.loc[:,'median_income']/df.loc[:,'persons_per_household']
adding_new_features(train_data)
 adding_new_features(test_data)
 train_data 
<load_from_csv>
train.isnull().sum()
dist2coast = pd.read_csv(".. /input/distance-to-coast/dist2coast.txt",delim_whitespace = True)
<create_dataframe>
train.isnull().sum()
dist2coast = pd.DataFrame(dist2coast)
 dist2coast = dist2coast.rename({'-179.98': 'longitude', '89.98': 'latitude', '712.935': 'dist2coast'}, axis='columns')
 dist2coast = dist2coast.query('-114.00 > longitude > -126.00')
 dist2coast = dist2coast.query('28.00 < latitude < 42.00')
 dist2coast 
<save_to_csv>
age_mean = train['age'].mean()
train['age'] = train['age'].fillna(age_mean)
 train.isnull().sum()
def saving(name,y_predict):
df = pd.DataFrame()
df['Id'] = test_Id
df.set_index('Id', inplace=True)
 df['median_house_value'] =y_predict
print(df)
 return df.to_csv(name )<find_best_model_class>
train.isnull().sum()
x_train_data = train_data.drop('median_house_value', axis = 'columns')
 y_train_data = train_data.loc[:,'median_house_value']
reg1 = tree.DecisionTreeRegressor(max_depth = 1)
 reg1 = reg1.fit(x_train_data, y_train_data)
 DTR_y = reg1.predict(test_data)
 saving("DecisionTreeRegression_1.csv",DTR_y)
<create_dataframe>
train.isnull().sum()
Scores1 = []
for i in range(1,10):
reg1 = tree.DecisionTreeRegressor(max_depth = i)
 scores1 = cross_val_score(reg1, x_train_data, y_train_data, cv=10)
 Scores1.append(scores1.mean())
 Scores1_df = pd.DataFrame()
Scores1_df['Regression Tree Scoring']= Scores1
Scores1_df<find_best_model_class>
train['embarked'].value_counts()
reg2 = KNeighborsRegressor(n_neighbors=50)
 reg2 = reg2.fit(x_train_data, y_train_data)
 knnR_y = reg2.predict(test_data)
 saving("50nn Regressor.csv", knnR_y )<train_on_grid>
train[train['embarked'].isnull() ]
reg3 = linear_model.LassoLars(alpha=.1, positive = True)
 reg3.fit(x_train_data, y_train_data)
 print(reg3.coef_)
 LASSO_y = reg3.predict(test_data)
 saving("LASSO LARS.csv", LASSO_y)
<train_model>
reg4 = MLPRegressor()
reg4.fit(x_train_data, y_train_data)
 MLP_y = reg4.predict(test_data)
 saving("MultiLayerPerceptrons Regressor.csv", MLP_y )<find_best_model_class>
train['embarked'] = train['embarked'].fillna('S' )
reg5 = linear_model.BayesianRidge()
reg5.fit(x_train_data, y_train_data)
 BRR_y = reg5.predict(test_data)
 scores5 = cross_val_score(reg5, x_train_data, y_train_data, cv = 10)
 saving("Bayesian Ridge Regressor.csv", BRR_y )<find_best_model_class>
train.isnull().sum()
reg6 = RandomForestRegressor()
scores6 = cross_val_score(reg6, x_train_data, y_train_data, cv=10)
 scores6 = scores6.mean()
print(scores6)
 reg6.fit(x_train_data, y_train_data)
 RF_y = reg6.predict(test_data)
 saving("RandomForestsRegressor20.csv", RF_y)
 print(scores6 )<choose_model_class>
train.isnull().sum()
reg7 = ExtraTreesRegressor()
reg7.fit(x_train_data, y_train_data)
 ET_y = reg7.predict(test_data)
 saving("ExtraTreesRegressor.csv", ET_y)
<train_model>
train['survived'].value_counts()
reg8 = AdaBoostRegressor(base_estimator = RandomForestRegressor(max_depth=20), n_estimators=50)
 reg8.fit(x_train_data, y_train_data)
 ADA_y = reg8.predict(test_data)
 saving("ADA Boost Regression.csv",ADA_y )<find_best_model_class>
train[['pclass','survived']].groupby(['pclass'],as_index=False ).mean().sort_values(by='survived',ascending=False )
dist2coast_reg = KNeighborsRegressor(n_neighbors=1)
 scores_dist = cross_val_score(dist2coast_reg,dist2coast.loc[:,:'latitude'],dist2coast.loc[:,'dist2coast':],cv=10)
 scores_dist = scores_dist.mean()
dist2coast_reg.fit(dist2coast.loc[:,:'latitude'],dist2coast.loc[:,'dist2coast':])
 train_pred_dist= dist2coast_reg.predict(train_data.loc[:,:'latitude'])
 test_pred_dist= dist2coast_reg.predict(test_data.loc[:,:'latitude'])
 train_data['dist2coast'] = train_pred_dist
test_data['dist2coast']= test_pred_dist
print(scores_dist)
 x_train_data = train_data.drop('median_house_value', axis = 'columns')
 y_train_data = train_data.loc[:,'median_house_value']
train_data<train_model>
train[['sex','survived']].groupby(['sex'],as_index=False ).mean().sort_values(by='survived',ascending=False )
reg_F = AdaBoostRegressor(base_estimator = RandomForestRegressor(max_depth=20), n_estimators=50)
 reg_F.fit(x_train_data, y_train_data)
 final = reg_F.predict(test_data)
 saving("Final ADA Boost Regression.csv",final )<import_modules>
train.sibsp.value_counts()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score,classification_report,confusion_matrix,fbeta_score
from sklearn import linear_model
from sklearn.ensemble import RandomForestRegressor<load_from_csv>
train[["sibsp", "survived"]].groupby(['sibsp'], as_index=False ).mean().sort_values(by='survived', ascending=False )
trainDb = pd.read_csv(".. /input/mycalifdb/train.csv")
 testDb = pd.read_csv(".. /input/mycalifdb/test.csv" )<compute_test_metric>
train.parch.value_counts()
def RMSLE(Y, Y_hat):
sumError = 0.0
assert len(Y)== len(Y_hat)
 for i in range(len(Y)) :
if Y[i] < 0: Y[i] = 0
sumError +=(np.log(Y[i] + 1)- np.log(Y_hat[i] + 1)) **2
return np.sqrt(sumError/len(Y))<prepare_x_and_y>
train[["parch", "survived"]].groupby(['parch'], as_index=False ).mean().sort_values(by='survived', ascending=False )
Xtrain = trainDb.drop("Id", axis = 1 ).drop("median_house_value", axis = 1)
 Xtest = testDb.drop("Id", axis = 1)
 Ytrain = trainDb["median_house_value"]<compute_train_metric>
sex = pd.get_dummies(train['sex'],drop_first=True)
 sex.head()
kNNMethod = KNeighborsRegressor(n_neighbors=10)
 kNNMethod.fit(Xtrain, Ytrain)
 scores = cross_val_score(kNNMethod, Xtrain, Ytrain, cv=3)
 Y = kNNMethod.predict(Xtrain)
 print('Pontuação média kNN: ' + str(scores.mean()))
print('RMSLE kNN: ' + str(RMSLE(Y, Ytrain)) )<compute_train_metric>
pclass = pd.get_dummies(train['pclass'],drop_first= True)
 pclass.head()
lassoMethod = linear_model.Lasso(alpha = 0.1)
 lassoMethod.fit(Xtrain, Ytrain)
 scores = cross_val_score(lassoMethod, Xtrain, Ytrain, cv=3)
 Y = lassoMethod.predict(Xtrain)
 print('Pontuação média Lasso: ' + str(scores.mean()))
print('RMSLE Lasso: ' + str(RMSLE(Y, Ytrain)) )<find_best_model_class>
embarked = pd.get_dummies(train['embarked'],drop_first= True)
 embarked.head()
forestMethod = RandomForestRegressor(max_depth=25, random_state=0, n_estimators=100)
 forestMethod.fit(Xtrain, Ytrain)
 scores = cross_val_score(forestMethod, Xtrain, Ytrain, cv=3)
 Y = forestMethod.predict(Xtrain)
 print('Pontuação média Forest: ' + str(scores.mean()))
print('RMSLE Forest: ' + str(RMSLE(Y, Ytrain)) )<save_to_csv>
final_train = pd.concat([train,sex,pclass,embarked],axis = 1)
 final_train.head()
forestMethod = RandomForestRegressor(max_depth=25, random_state=0, n_estimators=100)
 forestMethod.fit(Xtrain, Ytrain)
 Ytest = forestMethod.predict(Xtest)
 predictionTest = pd.DataFrame({"Id":testDb.Id, "median_house_value":Ytest})
 predictionTest.to_csv("predictTable.csv", index=False)
 predictionTest<load_from_csv>
final_train = final_train.drop(['sex','embarked','pclass'],axis = 1 )
data = pd.read_csv('/kaggle/input/girlsgoit-competition-2020/train.csv')
 data.head()<load_from_csv>
final_train.columns = ['survived','age','sibsp','parch','fare','sex_male','pclass_2','pclass_3','embarked_q','embarked_s']
final_train.head()
test = pd.read_csv('/kaggle/input/girlsgoit-competition-2020/test.csv')
 test.head()<load_from_csv>
test.isnull().sum()
submit = pd.read_csv('/kaggle/input/girlsgoit-competition-2020/sampleSubmission.csv')
 submit.info()<groupby>
test.isnull().sum()
data[["year", "popularity"]].groupby(["year",'popularity'] ).count().max(level=0 )<groupby>
test.drop(['passengerid','name','ticket'],axis=1,inplace=True)
 test.drop('cabin',axis=1,inplace=True)
 age_mean = test['age'].mean()
test['age'] = test['age'].fillna(age_mean)
 test.isnull().sum()
max_per_year = data.groupby(['year'])['popularity'].apply(lambda x: x.value_counts().index[0] ).reset_index()
max_per_year<data_type_conversions>
test['fare'] = test['fare'].fillna(test['fare'].mean() )
toPopularity = {i:j for [i,j] in max_per_year.to_numpy() }
toPopularity[2010]<categorify>
test.isnull().sum()
def pred_for_year(x):

 return x['year'].apply(lambda x: toPopularity[x])
 pred_for_year(data )<define_variables>
test.isnull().sum()
columns = [ 'acousticness', 'danceability', 'duration_ms',
'energy', 'explicit', 'instrumentalness', 'key', 'liveness', 'loudness',
'mode', 'speechiness', 'tempo', 'valence',
'year']
columns = ['year']
X = data[columns]
X.head()<prepare_x_and_y>
sex = pd.get_dummies(test['sex'],drop_first=True)
 pclass = pd.get_dummies(test['pclass'],drop_first= True)
 embarked = pd.get_dummies(test['embarked'],drop_first= True )
Y = data[['popularity']]
Y.head()<split>
final_test = pd.concat([test,sex,pclass,embarked],axis = 1)
 final_test.head()
dataX, dataY = X, Y
train_ratio = 0.70
validation_ratio = 0.15
test_ratio = 0.15
x_train, x_test, y_train, y_test = train_test_split(dataX, dataY, test_size=1 - train_ratio)
 x_val, x_test, y_val, y_test = train_test_split(x_test, y_test, test_size=test_ratio/(test_ratio + validation_ratio))
print(x_train.shape, x_val.shape, x_test.shape )<import_modules>
final_test = final_test.drop(['sex','embarked','pclass'],axis = 1)
 final_test.head()
from sklearn.tree import DecisionTreeClassifier 
<import_modules>
final_test.columns = ['age','sibsp','parch','fare','sex_male','pclass_2','pclass_3','embarked_q','embarked_s']
final_test.head()
plt.figure(figsize=(20, 20)) 
<predict_on_test>
X_train = final_train.drop('survived',axis=1)
 y_train = final_train['survived']
Y_predict = pred_for_year(x_val )<compute_test_metric>
X_test = final_test
print("Accuracy score:", accuracy_score(y_val, Y_predict))
print("F1 score:", f1_score(y_val, Y_predict, average='micro'))
print("Recall score:", recall_score(y_val, Y_predict, average='micro'))<compute_test_metric>
from sklearn.preprocessing import StandardScaler
cm = confusion_matrix(y_val, Y_predict)
 cm<split>
scaler = StandardScaler()
columns = ['year' , 'acousticness', 'danceability', 'energy', 'duration_ms',
'explicit', 'instrumentalness', 'key', 'liveness', 'loudness',
'mode', 'speechiness', 'tempo', 'valence'
]
info = []
for c in range(1,4):
break
for leaf in range(3, 30,3):
for deapth in range(3, 30,3):
cs = columns[:c]
X = data[cs]
x_train, x_test, y_train, y_test = train_test_split(X, dataY, test_size=1 - train_ratio)
 x_val, x_test, y_val, y_test = train_test_split(x_test, y_test, test_size=test_ratio/(test_ratio + validation_ratio))
clf = DecisionTreeClassifier(random_state=1, max_leaf_nodes=leaf, max_depth=deapth)
 clf.fit(x_train, y_train)
 Y_predict = clf.predict(x_val)
 info.append([','.join(cs), leaf, deapth , accuracy_score(y_val, Y_predict), clf])
<sort_values>
scaled_X_train = scaler.fit_transform(X_train)
 scaled_X_test = scaler.transform(X_test)
so = sorted(info, key=lambda x:x[-2])[-5:]<save_to_csv>
y_test = gen_sub['Survived']
test_data = pd.read_csv('https://girlsgoitpublic.z6.web.core.windows.net/test.csv')
 test_data['popularity'] = pred_for_year(test_data[columns])
 test_data[['ID', 'popularity']].to_csv('submission.csv',index=False )<import_modules>
from sklearn.linear_model import LogisticRegression
warnings.filterwarnings('ignore')
 %matplotlib inline<load_from_csv>
from sklearn.model_selection import GridSearchCV
dataset_treino = pd.read_csv(".. /input/dataset_treino.csv", index_col = "Order")
 dataset_teste = pd.read_csv(".. /input/dataset_teste.csv", index_col = "OrderId" )<drop_column>
from sklearn.model_selection import GridSearchCV
dataset_treino = dataset_treino.drop(["Property Id","Property Name","Parent Property Id",
"Parent Property Name", "NYC Borough, Block and Lot(BBL)self-reported",
"NYC Building Identification Number(BIN)","Postal Code",
"Street Number","Street Name",
"Borough", "Address 1(self-reported)", 'Address 2'],
axis = 1 )<drop_column>
log_model = LogisticRegression(solver='saga',multi_class="ovr",max_iter=5000 )
dataset_treino = dataset_treino.drop(["2nd Largest Property Use Type",
"2nd Largest Property Use - Gross Floor Area(ft²)","3rd Largest Property Use Type",
"3rd Largest Property Use Type - Gross Floor Area(ft²)", "Fuel Oil
"Fuel Oil
"Diesel<drop_column>
penalty = ['l1', 'l2']
C = np.logspace(0, 4, 10 )
dataset_treino = dataset_treino.drop(["Largest Property Use Type","List of All Property Use Types at Property"], axis = 1 )<data_type_conversions>
grid_model = GridSearchCV(log_model,param_grid={'C':C,'penalty':penalty} )
dataset_treino['BBL - 10 digits'] = dataset_treino['BBL - 10 digits'].astype('category')
 dataset_treino['BBL - 10 digits'] = dataset_treino['BBL - 10 digits'].apply(lambda x: x[0] ).astype('category')
 dataset_teste['BBL - 10 digits'] = dataset_teste['BBL - 10 digits'].astype('category')
 dataset_teste['BBL - 10 digits'] = dataset_teste['BBL - 10 digits'].apply(lambda x: x[0] ).astype('category' )<categorify>
grid_model.fit(scaled_X_train,y_train )
dataset_treino = dataset_treino.where(( pd.notnull(dataset_treino)) , 0.001)
 d = {'Not Available': 0.001}
df = dataset_treino.replace(d)
 dataset_teste = dataset_teste.where(( pd.notnull(dataset_teste)) , 0.001)
 df_teste = dataset_teste.replace(d )<define_variables>
grid_model.best_params_
colunas = ["Year Built", "Site EUI(kBtu/ft²)","Weather Normalized Site EUI(kBtu/ft²)",
"Weather Normalized Site Electricity Intensity(kWh/ft²)","Weather Normalized Site Natural Gas Intensity(therms/ft²)",
"Natural Gas Use(kBtu)","Weather Normalized Site Electricity(kWh)","Total GHG Emissions(Metric Tons CO2e)",
"Direct GHG Emissions(Metric Tons CO2e)","Indirect GHG Emissions(Metric Tons CO2e)",
"Water Use(All Water Sources )(kgal)","Water Intensity(All Water Sources )(gal/ft²)","Source EUI(kBtu/ft²)",
"Weather Normalized Site Natural Gas Use(therms)", "Electricity Use - Grid Purchase(kBtu)",
"Natural Gas Use(kBtu)", "Weather Normalized Site Natural Gas Use(therms)","Weather Normalized Source EUI(kBtu/ft²)",
"DOF Gross Floor Area","Number of Buildings - Self-reported", "Census Tract",
"Largest Property Use Type - Gross Floor Area(ft²)","Property GFA - Self-Reported(ft²)","Occupancy",
"Council District","Community Board"]
array = df[colunas].values
array_teste = df_teste[colunas].values<data_type_conversions>
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report,plot_confusion_matrix
array = array.astype(float)
 array_teste = array_teste.astype(float )<categorify>
y_lr_pred = grid_model.predict(scaled_X_test )
imp = SimpleImputer(missing_values = 0.001, strategy = 'median' ).fit(array)
 dataset_treino[colunas] = imp.transform(array)
 dataset_teste[colunas] = imp.transform(array_teste )<drop_column>
accuracy_score(y_test,y_lr_pred )
dataset_treino = dataset_treino.drop(["DOF Gross Floor Area", "Largest Property Use Type - Gross Floor Area(ft²)",
"Weather Normalized Site EUI(kBtu/ft²)","Number of Buildings - Self-reported",
"Occupancy","Electricity Use - Grid Purchase(kBtu)",
"Weather Normalized Site Electricity(kWh)","Indirect GHG Emissions(Metric Tons CO2e)",
'Property GFA - Self-Reported(ft²)',"Water Use(All Water Sources )(kgal)",
"Water Intensity(All Water Sources )(gal/ft²)","Latitude","Longitude",
"Community Board", "Council District","Census Tract"],
axis = 1 )<create_dataframe>
confusion_matrix(y_test,y_lr_pred )
colunas_num = ["ENERGY STAR Score", "Site EUI(kBtu/ft²)", "Year Built",
"Weather Normalized Site Electricity Intensity(kWh/ft²)", "Weather Normalized Site Natural Gas Intensity(therms/ft²)",
"Natural Gas Use(kBtu)", "Weather Normalized Site Natural Gas Use(therms)", "Total GHG Emissions(Metric Tons CO2e)",
"Direct GHG Emissions(Metric Tons CO2e)", "Source EUI(kBtu/ft²)", "Weather Normalized Source EUI(kBtu/ft²)"]
vif = pd.DataFrame()
vif["VIF Factor"] = [variance_inflation_factor(dataset_treino[colunas_num].values, i)for i in range(dataset_treino[colunas_num].shape[1])]
vif["features"] = dataset_treino[colunas_num].columns
vif<drop_column>
print(classification_report(y_test,y_lr_pred))
dataset_treino = dataset_treino.drop(["Source EUI(kBtu/ft²)", "Weather Normalized Source EUI(kBtu/ft²)","Natural Gas Use(kBtu)",
"Year Built","Direct GHG Emissions(Metric Tons CO2e)"],
axis = 1 )<create_dataframe>
from sklearn.metrics import roc_curve, auc
colunas_num = ["ENERGY STAR Score", "Site EUI(kBtu/ft²)",
"Weather Normalized Site Electricity Intensity(kWh/ft²)", "Weather Normalized Site Natural Gas Intensity(therms/ft²)",
"Weather Normalized Site Natural Gas Use(therms)", "Total GHG Emissions(Metric Tons CO2e)"]
vif = pd.DataFrame()
vif["VIF Factor"] = [variance_inflation_factor(dataset_treino[colunas_num].values, i)for i in range(dataset_treino[colunas_num].shape[1])]
vif["features"] = dataset_treino[colunas_num].columns
vif<feature_engineering>
from sklearn.metrics import precision_recall_curve,plot_precision_recall_curve,plot_roc_curve
coluna_out = "Weather Normalized Site Natural Gas Use(therms)"
mean = np.mean(dataset_treino[coluna_out])
 sd = np.std(dataset_treino[coluna_out])
 out = mean + 2*sd
dataset_treino = dataset_treino.drop(dataset_treino[dataset_treino[coluna_out] > out].index)
 coluna_out = "Weather Normalized Site Natural Gas Intensity(therms/ft²)"
mean = np.mean(dataset_treino[coluna_out])
 sd = np.std(dataset_treino[coluna_out])
 out = mean + 2*sd
dataset_treino = dataset_treino.drop(dataset_treino[dataset_treino[coluna_out] > out].index)
 coluna_out = "Weather Normalized Site Electricity Intensity(kWh/ft²)"
mean = np.mean(dataset_treino[coluna_out])
 sd = np.std(dataset_treino[coluna_out])
 out = mean + 2*sd
dataset_treino = dataset_treino.drop(dataset_treino[dataset_treino[coluna_out] > out].index)
 coluna_out = "Site EUI(kBtu/ft²)"
mean = np.mean(dataset_treino[coluna_out])
 sd = np.std(dataset_treino[coluna_out])
 out = mean + 2*sd
dataset_treino = dataset_treino.drop(dataset_treino[dataset_treino[coluna_out] > out].index)
 coluna_out = "Total GHG Emissions(Metric Tons CO2e)"
mean = np.mean(dataset_treino[coluna_out])
 sd = np.std(dataset_treino[coluna_out])
 out = mean + 2*sd
dataset_treino = dataset_treino.drop(dataset_treino[dataset_treino[coluna_out] > out].index )<categorify>
from sklearn.neighbors import KNeighborsClassifier
coluna_cat = ["Primary Property Type - Self Selected", "Metered Areas(Energy)", 'BBL - 10 digits','Water Required?']
dummies = pd.get_dummies(dataset_treino[coluna_cat])
 dummies_teste = pd.get_dummies(dataset_teste[coluna_cat])
 final_train, final_test = dummies.align(dummies_teste,join='left',axis=1 )<define_variables>
from sklearn.neighbors import KNeighborsClassifier
coluna_num = ["Site EUI(kBtu/ft²)",
"Weather Normalized Site Electricity Intensity(kWh/ft²)", "Weather Normalized Site Natural Gas Intensity(therms/ft²)",
"Weather Normalized Site Natural Gas Use(therms)", "Total GHG Emissions(Metric Tons CO2e)"]
target = "ENERGY STAR Score"<normalization>
from sklearn.neighbors import KNeighborsClassifier
X = dataset_treino[coluna_num].values
Y_target = dataset_treino[target].values
X_teste = dataset_teste[coluna_num].values
scaler = MinMaxScaler(feature_range =(-1, 1)).fit(X)
 rescaledX = scaler.transform(X)
 rescaledX_teste = scaler.transform(X_teste )<merge>
knn_model = KNeighborsClassifier(n_neighbors=1 )
data_modelo = dataset_treino[coluna_num].copy()
data_modelo[coluna_num] = rescaledX
data_modelo = data_modelo.join(final_train)
 data_modelo_teste = dataset_teste[coluna_num].copy()
data_modelo_teste[coluna_num] = rescaledX_teste
data_modelo_teste = data_modelo_teste.join(final_test )<feature_engineering>
knn_model.fit(scaled_X_train,y_train )
data_modelo_teste[np.isnan(data_modelo_teste)] = 0<prepare_x_and_y>
y_knn_pred = knn_model.predict(scaled_X_test )
X_treino = data_modelo.values
Y_treino = Y_target<choose_model_class>
print(classification_report(y_test,y_knn_pred))
modelos = []
modelos.append(( 'GBM', GradientBoostingRegressor(n_estimators = 270, loss = 'huber', max_depth = 4,
learning_rate = 0.05, subsample = 0.7)))
 modelos.append(( 'XG', XGBRegressor(n_estimators = 300, max_depth = 4, learning_rate = 0.05,
gamma = 0.3, subsample= 0.75, min_child_weight = 0.5)))
 resultados = []
nomes = []
for nome, modelo in modelos:
kfold = model_selection.KFold(50, True, random_state = 7)
 cross_val_result = model_selection.cross_val_score(modelo,
X_treino,
Y_treino,
cv = kfold, scoring = 'neg_mean_absolute_error')
 resultados.append(cross_val_result)
 nomes.append(nome)
 texto = "%s: %f(%f)" %(nome, cross_val_result.mean() , cross_val_result.std())
 print(texto )<train_model>
knn = KNeighborsClassifier()
model_gbm = GradientBoostingRegressor(n_estimators = 270, loss = 'huber', max_depth = 4,
learning_rate = 0.05, subsample = 0.7)
 model_gbm.fit(X_treino,Y_treino)
 previsoes_final_gbm = model_gbm.predict(data_modelo_teste.values )<feature_engineering>
k_values = list(range(1,20))
params_grid = {'n_neighbors':k_values}
for i in range(0,len(previsoes_final_gbm)) :
if previsoes_final_gbm[i] > 100:
previsoes_final_gbm[i] = 100<feature_engineering>
knn_grid_model = GridSearchCV(knn,param_grid=params_grid,cv=5,scoring='accuracy' )
for i in range(0,len(previsoes_final_gbm)) :
if previsoes_final_gbm[i] < 1:
previsoes_final_gbm[i] = 1<data_type_conversions>
knn_grid_model.fit(scaled_X_train,y_train )
previsoes_final = previsoes_final_gbm.round(0 ).astype('int' )<load_from_csv>
knn_grid_model.best_params_
dataset_test_submission = pd.read_csv(".. /input/dataset_teste.csv", index_col = 'Property Id')
 dataset_test_submission['score'] = previsoes_final
Submission = dataset_test_submission['score']
Submission = pd.DataFrame(Submission);Submission.head(10 )<save_to_csv>
y_knn_grid_pred = knn_grid_model.predict(scaled_X_test )
Submission.to_csv('Submission.csv', header = True )<import_modules>
print(classification_report(y_test,y_knn_grid_pred))
import pandas as pd
import numpy as np
import seaborn as sns
import missingno as msno
import gc<set_options>
from sklearn.svm import SVC
%matplotlib inline
set_matplotlib_formats('pdf', 'png')
 pd.options.display.float_format = '{:.2f}'.format
rc={'savefig.dpi': 75, 'figure.autolayout': False, 'figure.figsize': [12, 8], 'axes.labelsize': 18,\
'axes.titlesize': 18, 'font.size': 18, 'lines.linewidth': 2.0, 'lines.markersize': 8, 'legend.fontsize': 16,\
'xtick.labelsize': 16, 'ytick.labelsize': 16}
sns.set(style='dark',rc=rc )<set_options>
param_grid = {'C':[0.001,0.01,0.1,0.5,1],'gamma':['scale','auto']}
grid_svc = GridSearchCV(svc,param_grid )
default_color = '
colormap = plt.cm.cool<define_variables>
grid_svc.fit(scaled_X_train,y_train )
path = '.. /input/'
path_result = '.. /output/'<load_from_csv>
grid_svc.best_params_
train = pd.read_csv(path + 'train_data.csv')
 test = pd.read_csv(path + 'teste_data.csv')
 train = train.rename(columns={"default": "target", "ids":"id"})
 test = test.rename(columns={"ids":"id"} )<define_variables>
y_svc_grid_pred = grid_svc.predict(scaled_X_test )
test_id = test.id<count_missing_values>
print(classification_report(y_test,y_svc_grid_pred))
def missing_values_table(df):
mis_val = df.isnull().sum()
mis_val_percent = 100 * df.isnull().sum() / len(df)
 mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1)
 mis_val_table_ren_columns = mis_val_table.rename(
 columns = {0 : 'Missing Values', 1 : '% of Total Values'})
 mis_val_table_ren_columns = mis_val_table_ren_columns[
mis_val_table_ren_columns.iloc[:,1] != 0].sort_values(
 '% of Total Values', ascending=False ).round(1)
 print("Your selected dataframe has " + str(df.shape[1])+ " columns.
 "
"There are " + str(mis_val_table_ren_columns.shape[0])+
" columns that have missing values.")
 return mis_val_table_ren_columns<count_missing_values>
from sklearn.tree import DecisionTreeClassifier
missing_values_table(train )<drop_column>
dt_model.fit(X_train,y_train )
missingValueColumns = train.columns[train.isnull().any() ].tolist()
df_null = train[missingValueColumns]<correct_missing_values>
y_dt_pred = dt_model.predict(X_test )
train = train.dropna(subset=['target'] )<drop_column>
pd.DataFrame(index=X_train.columns,data=dt_model.feature_importances_,columns=['Feature Importance'])
missingValueColumns = train.columns[train.isnull().any() ].tolist()
df_null = train[missingValueColumns]<define_variables>
from sklearn.tree import plot_tree
def get_meta(train):
data = []
for col in train.columns:
if col == 'target':
role = 'target'
elif col == 'id':
role = 'id'
else:
role = 'input'
if col == 'target' or 'facebook' in col or col == 'gender' or 'bin_' in col:
level = 'binary'
elif train[col].dtype == np.object or col == 'id':
level = 'nominal'
elif train[col].dtype == np.float64:
level = 'interval'
elif train[col].dtype == np.int64:
level = 'ordinal'
keep = True
if col == 'id':
keep = False
dtype = train[col].dtype
col_dict = {
'varname': col,
'role' : role,
'level' : level,
'keep' : keep,
'dtype' : dtype
}
data.append(col_dict)
 meta = pd.DataFrame(data, columns=['varname', 'role', 'level', 'keep', 'dtype'])
 meta.set_index('varname', inplace=True)
 return meta<define_variables>
max_depth = list(range(1,30))
params_grid = {'max_depth':max_depth,'max_leaf_nodes':[2,3,4,5,6,7,8]}
dt_grid_model = GridSearchCV(dt_model,param_grid=params_grid)
meta_data = get_meta(train)
 meta_data<groupby>
dt_grid_model.fit(X_train,y_train)
 y_dt_grid_pred = dt_grid_model.predict(X_test )
meta_counts = meta_data.groupby(['role', 'level'] ).agg({'dtype': lambda x: x.count() } ).reset_index()
meta_counts<filter>
dt_grid_model.best_params_
col_ordinal = meta_data[(meta_data.level == 'ordinal')&(meta_data.keep)].index
col_nominal = meta_data[(meta_data.level == 'nominal')&(meta_data.keep)&(meta_data.role != 'target')&(meta_data.role != 'id')].index
col_interval = meta_data[(meta_data.level == 'interval')&(meta_data.keep)].index
col_binary = meta_data[(meta_data.level == 'binary')&(meta_data.keep)&(meta_data.role != 'target')].index<feature_engineering>
from sklearn.ensemble import RandomForestClassifier
def new_missing_columns(df):
for i in missingValueColumns:
if 'target' not in i:
new_col = 'bin_missing_'+ i
df[new_col] = np.where(df[i].isnull() , True, False)
 return df<drop_column>
rf_model = RandomForestClassifier()
train = new_missing_columns(train)
 test = new_missing_columns(test )<merge>
rf_model.fit(X_train,y_train )
def count_label_encoding(train, test,col):
for i in col:
df1 = train[i].value_counts().reset_index(name='freq_'+ i ).rename(columns={'index': 'lc_'+ i})
 train = pd.merge(train,df1,left_on=i, right_on='lc_'+ i, how='left')
 test = pd.merge(test,df1,left_on=i, right_on='lc_'+ i, how='left')
 for i in list(train):
if 'lc_' in i:
train = train.drop(i, axis = 1)
 test = test.drop(i, axis = 1)
 return train, test<categorify>
y_rf_pred = rf_model.predict(X_test )
train, test = count_label_encoding(train, test,col_nominal)
 train, test = count_label_encoding(train, test,col_binary )<filter>
pd.DataFrame(index=X_train.columns,data=rf_model.feature_importances_,columns=['Feature Importance'])
meta_data = get_meta(train)
 col_ordinal = meta_data[(meta_data.level == 'ordinal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_nominal = meta_data[(meta_data.level == 'nominal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_interval = meta_data[(meta_data.level == 'interval')&(meta_data.keep)&(meta_data.role != 'target')].index
col_binary = meta_data[(meta_data.level == 'binary')&(meta_data.keep)&(meta_data.role != 'target')].index<groupby>
no_of_trees = list(range(10,50,500))
max_depth = list(range(1,30))
param_grid_rf = {'n_estimators':no_of_trees,'criterion':['gini','entropy'],
'max_depth':max_depth,'max_leaf_nodes':[2,3,4,5,6,7,8]}
meta_counts = meta_data.groupby(['role', 'level'] ).agg({'dtype': lambda x: x.count() } ).reset_index()
meta_counts<import_modules>
rf_grid_model = GridSearchCV(rf_model,param_grid=param_grid_rf )
from sklearn.model_selection import train_test_split<import_modules>
rf_grid_model.fit(X_train,y_train)
 y_rf_grid_pred = rf_grid_model.predict(X_test )
from sklearn.model_selection import train_test_split<split>
rf_grid_model.best_params_
X = pd.concat([train[col_interval],train[col_ordinal],pd.get_dummies(train[col_binary])], axis=1)
 y = pd.DataFrame(train.target)
 X.fillna(-1, inplace=True)
 y.fillna(-1, inplace=True)
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42 )<train_model>
print('Logistic_Regression_best_accuracy: ',accuracy_score(y_test,y_lr_pred))
print('
')
 print('KNN_best_accuracy: ',accuracy_score(y_test,y_knn_grid_pred))
print('
')
 print('SVM_best_accuracy: ',accuracy_score(y_test,y_svc_grid_pred))
print('
')
 print('Decision_tree_best_accuracy: ',accuracy_score(y_test,y_dt_grid_pred))
print('
')
 print('Random_forest_best_accuracy: ',accuracy_score(y_test,y_rf_grid_pred))
rf = RandomForestClassifier(n_estimators=150, max_depth=8, min_samples_leaf=30, max_features=0.2, n_jobs=-1, random_state=0)
 rf.fit(X_train, y_train['target'])
 features = X_train.columns.values
print("----- Training Done -----" )<import_modules>
test_pred = dt_grid_model.predict(X_test )
from sklearn.metrics import accuracy_score, roc_auc_score<compute_train_metric>
test2 = pd.read_csv('/kaggle/input/titanic/test.csv')
 test2.head()
acc = accuracy_score(y_train, rf.predict(X_train))
auc = roc_auc_score(y_train, rf.predict(X_train))
print("Accuracy: %.4f" % acc)
 print("AUC: %.4f" % auc )<compute_test_metric>
passengerid = test2['PassengerId']
acc = accuracy_score(y_test, rf.predict(X_test))
auc = roc_auc_score(y_test, rf.predict(X_test))
print("Accuracy: %.4f" % acc)
 print("AUC: %.4f" % auc )<create_dataframe>
submission = pd.DataFrame({"PassengerId": passengerid,"Survived": test_pred})
 submission.PassengerId = submission.PassengerId.astype(int)
 submission.Survived = submission.Survived.astype(int)
 submission.to_csv("titanic1_submission.csv", index=False )