Kaggle competition: Homework for Students
<SOS> metric: auc Kaggle data source: homework-for-students<save_to_csv>
plt.style.use('ggplot')
 %matplotlib inline
MODEL_6.fit(X_train[FEATURES], y_train)
 prediction_6 = MODEL_6.predict_proba(X_test[FEATURES])[:,1]
submit = pd.DataFrame({"PassengerId": X_test.index,"Survived": np.round(prediction_6, 0 ).astype(int)})
 submit.to_csv("MODEL_6.csv",index=False )<save_to_csv>
xgb.XGBClassifier()
MODEL_7.fit(X_train[FEATURES_4], y_train)
 prediction_7 = MODEL_7.predict_proba(X_test[FEATURES_4])[:,1]
submit = pd.DataFrame({"PassengerId": data_test.index,"Survived": np.round(prediction_7, 0 ).astype(int)})
 submit.to_csv("MODEL_7.csv",index=False )<save_to_csv>
df_train = pd.read_csv('.. /input/homework-for-students3/train.csv', index_col=0)
 df_test = pd.read_csv('.. /input/homework-for-students3/test.csv', index_col=0)
 print(len(df_test))
print(len(df_train))
MODEL_8.fit(X_train[FEATURES], y_train)
 prediction_8 = MODEL_8.predict_proba(X_test[FEATURES])[:,1]
submit = pd.DataFrame({"PassengerId": data_test.index,"Survived": np.round(prediction_8, 0 ).astype(int)})
 submit.to_csv("MODEL_8.csv",index=False )<import_modules>
gdp=pd.read_csv('.. /input/homework-for-students3/US_GDP_by_State.csv')
 zipdata=pd.read_csv('.. /input/homework-for-students3/free-zipcode-database.csv')
 drop_col = ['WorldRegion',
'Country', 'LocationText', 'Location', 'Decommisioned',
'TaxReturnsFiled', 'EstimatedPopulation', 'TotalWages', 'Notes']
zipdata=zipdata.drop(drop_col,axis=1)
 state=pd.read_csv('.. /input/homework-for-students3/statelatlong.csv')
 spi=pd.read_csv('.. /input/homework-for-students3/spi.csv')
 spi['date']=pd.to_datetime(spi['date'])
 spi= spi.set_index("date")
 spi=spi.asfreq('d', method='ffill')
 spi = spi.reset_index()
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
from sklearn.preprocessing import LabelBinarizer,LabelEncoder,StandardScaler,MinMaxScaler<load_from_csv>
df_train["issue_d"]=pd.to_datetime(df_train["issue_d"])
 df_test["issue_d"]=pd.to_datetime(df_test["issue_d"])
 df_train = df_train[df_train.issue_d.dt.year >= 2015]
df_train = df_train[df_train['annual_inc'] < df_train['annual_inc'].quantile(0.999)]
df_train['IDdami']=df_train.index
df_test['IDdami']=df_test.index
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-3/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-3/test.csv")
 submission_df = pd.read_csv(".. /input/covid19-global-forecasting-week-3/submission.csv" )<categorify>
df_train["earliest_cr_line"]=pd.to_datetime(df_train["earliest_cr_line"])
 df_test["earliest_cr_line"]=pd.to_datetime(df_test["earliest_cr_line"])
 df_train["issue_d_unix"] = df_train["issue_d"].view('int64')// 10**9
df_test["issue_d_unix"] = df_test["issue_d"].view('int64')// 10**9
df_train["earliest_cr_line_unix"] = df_train["earliest_cr_line"].view('int64')// 10**9
df_test["earliest_cr_line_unix"] = df_test["earliest_cr_line"].view('int64')// 10**9
df_train["period"]=df_train["issue_d_unix"]-df_train["earliest_cr_line_unix"]
df_test["period"]=df_test["issue_d_unix"]-df_test["earliest_cr_line_unix"]
df_train["period"]=df_train["period"].fillna(0)
 df_test["period"]=df_test["period"].fillna(0)
 df_train['aaa']=round(df_train['loan_amnt']/df_train['installment'],5)
 df_test['aaa']=round(df_test['loan_amnt']/df_test['installment'],5)
 df_train['bbb']=round(df_train['loan_amnt']/df_train['annual_inc'],5)
 df_test['bbb']=round(df_test['loan_amnt']/df_test['annual_inc'],5)
 df_train['ddd']=round(df_train['revol_bal']/df_train['revol_util'],5)
 df_test['ddd']=round(df_test['revol_bal']/df_test['revol_util'],5)
 df_train['eee']=round(df_train['revol_bal']/df_train['total_acc'],5)
 df_test['eee']=round(df_test['revol_bal']/df_test['total_acc'],5)
 df_train['fff']=round(df_train['revol_util']/df_train['total_acc'],5)
 df_test['fff']=round(df_test['revol_util']/df_test['total_acc'],5)
 df_train['aaa_open_acc']=round(df_train['loan_amnt']/df_train['open_acc'],5)
 df_test['aaa_open_acc']=round(df_test['loan_amnt']/df_test['open_acc'],5)
lb = LabelEncoder()
train_df['Country_Region'] = lb.fit_transform(train_df['Country_Region'])
 test_df['Country_Region'] = lb.transform(test_df['Country_Region'])
 lb1 = LabelEncoder()
train_df['Province_State'] = lb.fit_transform(train_df['Province_State'])
 test_df['Province_State'] = lb.transform(test_df['Province_State'] )<feature_engineering>
df_train = df_train.reset_index()
df_test = df_test.reset_index()
kari_df_train=pd.merge(df_train, state, how='left',left_on='addr_state',right_on='State')
 kari_df_test=pd.merge(df_test, state, how='left',left_on='addr_state',right_on='State')
 df_train = kari_df_train.set_index("ID")
 df_test =kari_df_test.set_index("ID")
 df_train=df_train.drop('State',axis=1)
 df_test=df_test.drop('State',axis=1 )
def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
train_df.Date = train_df.Date.apply(split_date)
 test_df.Date = test_df.Date.apply(split_date )<feature_engineering>
df_train['dami_year']=df_train.issue_d.dt.year
df_test['dami_year']=int(2015)
 df_train = df_train.reset_index()
df_test = df_test.reset_index()
kari_df_train=pd.merge(df_train, gdp, how='left',left_on=['City','dami_year'],right_on=['State','year'])
 kari_df_test=pd.merge(df_test, gdp, how='left',left_on=['City','dami_year'],right_on=['State','year'])
 df_train = kari_df_train.set_index("ID")
 df_test =kari_df_test.set_index("ID")
 df_train=df_train.drop(['State','dami_year','year'],axis=1)
 df_test=df_test.drop(['State','dami_year','year'],axis=1 )
year = []
month = []
day = []
for i in train_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2])
 train_df['Year'] = year
train_df['Month'] = month
train_df['Day'] = day
del train_df['Date']<feature_engineering>
df_train = df_train.reset_index()
df_test = df_test.reset_index()
kari_df_train=pd.merge(df_train, spi, how='left',left_on=['issue_d'],right_on=['date'])
 kari_df_test=pd.merge(df_test, spi, how='left',left_on=['issue_d'],right_on=['date'])
 df_train = kari_df_train.set_index("ID")
 df_test =kari_df_test.set_index("ID")
 df_train=df_train.drop(['date'],axis=1)
 df_test=df_test.drop(['date'],axis=1 )
year = []
month = []
day = []
for i in test_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2])
 test_df['Year'] = year
test_df['Month'] = month
test_df['Day'] = day
del test_df['Date']
del train_df['Id']
del test_df['ForecastId'] 
<data_type_conversions>
zipdata["Zipcode"]=zipdata["Zipcode"].astype(str)
 zipdata["Zipcode"]=zipdata["Zipcode"].str[:3]
zipdata=zipdata[['Zipcode','State','Xaxis', 'Yaxis', 'Zaxis']]
zipdata=zipdata.groupby(['Zipcode','State'],as_index=False ).mean()
train_df['ConfirmedCases'] = train_df['ConfirmedCases'].apply(int)
 train_df['Fatalities'] = train_df['Fatalities'].apply(int)
 cases = train_df.ConfirmedCases
fatalities = train_df.Fatalities
del train_df['ConfirmedCases']
del train_df['Fatalities']<normalization>
df_train['zip_code']=df_train['zip_code'].str[:3]
df_test['zip_code']=df_test['zip_code'].str[:3]
df_train["zip_code"]=df_train["zip_code"].astype(str)
 df_test["zip_code"]=df_test["zip_code"].astype(str)
scaler = MinMaxScaler()
x_train = scaler.fit_transform(train_df.values)
 x_test = scaler.transform(test_df.values )<predict_on_test>
zipdata[zipdata.duplicated() ]
rf = XGBRegressor(n_estimators = 1500 , random_state = 0 , max_depth = 15)
 rf.fit(x_train,cases)
 cases_pred = rf.predict(x_test)
 cases_pred = np.around(cases_pred,decimals = 0)
 cases_pred
x_train_cas = []
for i in range(len(x_train)) :
x = list(x_train[i])
 x.append(cases[i])
 x_train_cas.append(x)
 x_train_cas = np.array(x_train_cas )<predict_on_test>
df_train = df_train.reset_index()
df_test = df_test.reset_index()
kari_df_train=pd.merge(df_train, zipdata, how='left',left_on=['zip_code','addr_state'],right_on=['Zipcode','State'])
 kari_df_test=pd.merge(df_test, zipdata, how='left',left_on=['zip_code','addr_state'],right_on=['Zipcode','State'])
 df_train = kari_df_train.set_index("ID")
 df_test =kari_df_test.set_index("ID")
 df_train=df_train.drop(['Zipcode','State'],axis=1)
 df_test=df_test.drop(['Zipcode','State'],axis=1 )
rf = XGBRegressor(n_estimators = 1500 , random_state = 0 , max_depth = 15)
 rf.fit(x_train_cas,fatalities)
 x_test_cas = []
for i in range(len(x_test)) :
x = list(x_test[i])
 x.append(cases_pred[i])
 x_test_cas.append(x)
 x_test_cas = np.array(x_test_cas)
 fatalities_pred = rf.predict(x_test_cas)
 fatalities_pred = np.around(fatalities_pred,decimals = 0)
 fatalities_pred<prepare_output>
encoder = OrdinalEncoder()
enc_train = encoder.fit_transform(df_train['zip_code'].values)
 enc_test = encoder.transform(df_test['zip_code'].values)
 df_train = df_train.reset_index()
df_test = df_test.reset_index()
df_train['zip_code_la']=enc_train.iloc[:,0]
df_test['zip_code_la']=enc_test.iloc[:,0]
df_train = df_train.set_index("ID")
 df_test =df_test.set_index("ID" )
submission_df['ConfirmedCases'] = cases_pred
submission_df['Fatalities'] = fatalities_pred<save_to_csv>
zi_cal1='zip_code'
zi_summary1 = df_train[zi_cal1].value_counts()
df_train['zip_code_co'] = df_train[zi_cal1].map(zi_summary1)
 df_test['zip_code_co'] = df_test[zi_cal1].map(zi_summary1 )
submission_df.to_csv("submission.csv" , index = False )<import_modules>
encoder = OrdinalEncoder()
enc_train = encoder.fit_transform(df_train['addr_state'].values)
 enc_test = encoder.transform(df_test['addr_state'].values)
 df_train = df_train.reset_index()
df_test = df_test.reset_index()
df_train['addr_state_la']=enc_train.iloc[:,0]
df_test['addr_state_la']=enc_test.iloc[:,0]
df_train = df_train.set_index("ID")
 df_test =df_test.set_index("ID" )
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
from sklearn import preprocessing
from xgboost import XGBRegressor
from sklearn.tree import DecisionTreeRegressor
from lightgbm import LGBMRegressor
from catboost import CatBoostRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor<load_from_csv>
zi_cal2='addr_state'
zi_summary2 = df_train[zi_cal2].value_counts()
df_train['addr_state_co'] = df_train[zi_cal2].map(zi_summary2)
 df_test['addr_state_co'] = df_test[zi_cal2].map(zi_summary2 )
path = '/kaggle/input/covid19-global-forecasting-week-3/'
train = pd.read_csv(path+'train.csv')
 test = pd.read_csv(path+'test.csv' )<count_missing_values>
train.isnull().sum()<count_missing_values>
test.isnull().sum()<set_options>
%matplotlib inline 
<data_type_conversions>
df_train=df_train.drop(['issue_d','earliest_cr_line'],axis=1)
 df_test=df_test.drop(['issue_d','earliest_cr_line'],axis=1)
 drop_col=['City','acc_now_delinq']
df_train=df_train.drop(drop_col,axis=1)
 df_test=df_test.drop(drop_col,axis=1 )
train["Date"] = train["Date"].apply(lambda x: x.replace("-",""))
train["Date"] = train["Date"].astype(int)
 test["Date"] = test["Date"].apply(lambda x: x.replace("-",""))
test["Date"] = test["Date"].astype(int )<categorify>
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state<feature_engineering>
train['Province_State'].fillna(EMPTY_VAL, inplace=True)
 train['Province_State'] = train.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 test['Province_State'].fillna(EMPTY_VAL, inplace=True)
 test['Province_State'] = test.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1 )<categorify>
ce_cal2='initial_list_status'
ce_summary2 = df_train[ce_cal2].value_counts()
df_train['initial_list_status'] = df_train[ce_cal2].map(ce_summary2)
 df_test['initial_list_status'] = df_test[ce_cal2].map(ce_summary2 )
le = LabelEncoder()
train['Country_Region'] = le.fit_transform(train['Country_Region'])
 train['Province_State'] = le.fit_transform(train['Province_State'])
 test['Country_Region'] = le.fit_transform(test['Country_Region'])
 test['Province_State'] = le.fit_transform(test['Province_State'] )<choose_model_class>
ce_cal2='application_type'
ce_summary2 = df_train[ce_cal2].value_counts()
df_train['application_type'] = df_train[ce_cal2].map(ce_summary2)
 df_test['application_type'] = df_test[ce_cal2].map(ce_summary2 )
def build_model_1() :
model = RandomForestRegressor(n_estimators = 100, random_state = 0)
 return model
def build_model_2() :
model = XGBRegressor(n_estimators=1000)
 return model
def build_model_3() :
model = DecisionTreeRegressor(random_state=1)
 return model
def build_model_4() :
model = LogisticRegression()
return model
def build_model_5() :
model = LinearRegression()
return model
def build_model_6() :
model = LGBMRegressor(random_state=5)
 return model
def build_model_7() :
model = LGBMRegressor(iterations=2)
 return model<create_dataframe>
df_train['grade'].unique()
df_train=df_train.replace({'grade':{'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7}})
 df_test=df_test.replace({'grade':{'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7}})
 df_train["grade"]=df_train["grade"].astype(int)
 df_test["grade"]=df_test["grade"].astype(int )
out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []} )<data_type_conversions>
df_train=df_train.replace({'sub_grade':{'A1':1,'A2':2,'A3':3,'A4':4,'A5':5,
'B1':6,'B2':7,'B3':8,'B4':9,'B5':10,
'C1':11,'C2':12,'C3':13,'C4':14,'C5':15,
'D1':16,'D2':17,'D3':18,'D4':19,'D5':20,
'E1':21,'E2':22,'E3':23,'E4':24,'E5':25,
'F1':26,'F2':27,'F3':28,'F4':29,'F5':30,
'G1':31,'G2':32,'G3':33,'G4':34,'G5':35}})
 df_test=df_test.replace({'sub_grade':{'A1':1,'A2':2,'A3':3,'A4':4,'A5':5,
'B1':6,'B2':7,'B3':8,'B4':9,'B5':10,
'C1':11,'C2':12,'C3':13,'C4':14,'C5':15,
'D1':16,'D2':17,'D3':18,'D4':19,'D5':20,
'E1':21,'E2':22,'E3':23,'E4':24,'E5':25,
'F1':26,'F2':27,'F3':28,'F4':29,'F5':30,
'G1':31,'G2':32,'G3':33,'G4':34,'G5':35}})
 df_train["sub_grade"]=df_train["sub_grade"].astype(int)
 df_test["sub_grade"]=df_test["sub_grade"].astype(int )
for country in range(len(countries)) :
country_train = train.loc[train['Country_Region'] == countries[country]]
country_test = test.loc[test['Country_Region'] == countries[country]]
print("Current Country: ", countries[country])
 xtrain = country_train[['Country_Region', 'Province_State', 'Date']].to_numpy()
y1train = country_train[['ConfirmedCases']].to_numpy()
y2train = country_train[['Fatalities']].to_numpy()
xtest = country_test[['Country_Region', 'Province_State', 'Date']].to_numpy()
y1train = y1train.reshape(-1)
 y2train = y2train.reshape(-1)
 model1 = build_model_2()
model1.fit(xtrain, y1train)
 res_cnf_cls = model1.predict(xtest)
 model2 = build_model_2()
model2.fit(xtrain, y2train)
 res_fac = model2.predict(xtest)
 country_test_Id = country_test.loc[:, 'ForecastId']
country_test_Id = country_test_Id.astype(int)
 ans = pd.DataFrame({'ForecastId': country_test_Id, 'ConfirmedCases': res_cnf_cls, 'Fatalities': res_fac})
 out = pd.concat([out, ans], axis=0)
<data_type_conversions>
in_0=df_train[df_train.loan_condition==0].installment.median()
df_train['in_0_sa'] =df_train['installment']-in_0
df_test['in_0_sa'] =df_test['installment']-in_0
lo_0=df_train[df_train.loan_condition==0].loan_amnt.median()
df_train['lo_0_sa'] =df_train['loan_amnt']-lo_0
df_test['lo_0_sa'] =df_test['loan_amnt']-lo_0
dti_0=df_train[df_train.loan_condition==0].dti.median()
df_train['dti_0_sa'] =df_train['dti']-dti_0
df_test['dti_0_sa'] =df_test['dti']-dti_0
tot_0=df_train[df_train.loan_condition==0].tot_cur_bal.median()
df_train['tot_0_sa'] =df_train['tot_cur_bal']-tot_0
df_test['tot_0_sa'] =df_test['tot_cur_bal']-tot_0
rev_0=df_train[df_train.loan_condition==0].revol_bal.median()
df_train['rev_0_sa'] =df_train['revol_bal']-rev_0
df_test['rev_0_sa'] =df_test['revol_bal']-rev_0
pe_0=df_train[df_train.loan_condition==0].period.median()
df_train['pe_0_sa'] =df_train['period']-pe_0
df_test['pe_0_sa'] =df_test['period']-pe_0
in_1=df_train[df_train.loan_condition==1].installment.median()
df_train['in_1_sa'] =df_train['installment']-in_1
df_test['in_1_sa'] =df_test['installment']-in_1
lo_1=df_train[df_train.loan_condition==1].loan_amnt.median()
df_train['lo_1_sa'] =df_train['loan_amnt']-lo_1
df_test['lo_1_sa'] =df_test['loan_amnt']-lo_1
dti_1=df_train[df_train.loan_condition==1].dti.median()
df_train['dti_1_sa'] =df_train['dti']-dti_1
df_test['dti_1_sa'] =df_test['dti']-dti_1
tot_1=df_train[df_train.loan_condition==1].tot_cur_bal.median()
df_train['tot_1_sa'] =df_train['tot_cur_bal']-tot_1
df_test['tot_1_sa'] =df_test['tot_cur_bal']-tot_1
rev_1=df_train[df_train.loan_condition==1].revol_bal.median()
df_train['rev_1_sa'] =df_train['revol_bal']-rev_1
df_test['rev_1_sa'] =df_test['revol_bal']-rev_1
pe_1=df_train[df_train.loan_condition==1].period.median()
df_train['pe_0_sa'] =df_train['period']-pe_1
df_test['pe_0_sa'] =df_test['period']-pe_1
out["ForecastId"] = out["ForecastId"].astype(int )<save_to_csv>
df_train['home_ownership'].unique()
df_train=df_train.replace({'home_ownership':{'MORTGAGE':3,'RENT':2,'OWN':4,'ANY':1}})
 df_test=df_test.replace({'home_ownership':{'MORTGAGE':3,'RENT':2,'OWN':4,'ANY':1}})
 df_train["home_ownership"]=df_train["home_ownership"].astype(int)
 df_test["home_ownership"]=df_test["home_ownership"].astype(int)
 print(len(df_train.columns))
print(df_test.columns )
out.to_csv('submission.csv', index=False )<import_modules>
summary = df_train['purpose'].value_counts()
summary
df_train['purpose_co'] = df_train['purpose'].map(summary)
 df_test['purpose_co'] = df_test['purpose'].map(summary )
import numpy as np
import pandas as pd
import os
import pickle
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, Embedding, SpatialDropout1D, add, concatenate
from tensorflow.keras.layers import LSTM, Bidirectional, GlobalMaxPooling1D, GlobalAveragePooling1D
from tensorflow.keras.preprocessing import text, sequence
from tensorflow.keras.models import Model 
<define_variables>
df_train=df_train.replace({'emp_length':{'< 1 year':0.5,'1 year':1,'2 years':2,'3 years':3,
'4 years':4,'5 years':5,'6 years':6,'7 years':7,
'8 years':8,'9 years':9,'10+ years':10}})
 df_test=df_test.replace({'emp_length':{'< 1 year':0.5,'1 year':1,'2 years':2,'3 years':3,
'4 years':4,'5 years':5,'6 years':6,'7 years':7,
'8 years':8,'9 years':9,'10+ years':10}})
 df_train["emp_length"].head()
CRAWL_EMBEDDING_PATH = '.. /input/pickled-crawl300d2m-for-kernel-competitions/crawl-300d-2M.pkl'
GLOVE_EMBEDDING_PATH = '.. /input/pickled-glove840b300d-for-10sec-loading/glove.840B.300d.pkl'<load_pretrained>
df_train['emp_length_iii']=round(df_train['annual_inc']*df_train['emp_length'],5)
 df_test['emp_length_iii']=round(df_test['annual_inc']*df_test['emp_length'],5)
def get_coefs(word, *arr):
return word, np.asarray(arr, dtype='float32')
 def load_embeddings(path):
with open(path,'rb')as f:
emb_arr = pickle.load(f)
 return emb_arr
def build_matrix(word_index, path):
embedding_index = load_embeddings(path)
 embedding_matrix = np.zeros(( len(word_index)+ 1, 300))
unknown_words = []
for word, i in word_index.items() :
try:
embedding_matrix[i] = embedding_index[word]
except KeyError:
unknown_words.append(word)
 return embedding_matrix, unknown_words<load_from_csv>
df_train['ggg_mths_since_last_delinq']=round(df_train['installment']*df_train['mths_since_last_delinq'],5)
 df_test['ggg_mths_since_last_delinq']=round(df_test['installment']*df_test['mths_since_last_delinq'],5 )
sub = pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv')
 sub.head()<load_from_csv>
df_train['ggg']=round(df_train['loan_amnt']*df_train['sub_grade'],5)
 df_test['ggg']=round(df_test['loan_amnt']*df_test['sub_grade'],5)
 df_train['hhh']=round(df_train['installment']*df_train['sub_grade'],5)
 df_test['hhh']=round(df_test['installment']*df_test['sub_grade'],5)
 df_train['iii']=round(df_train['annual_inc']*df_train['sub_grade'],5)
 df_test['iii']=round(df_test['annual_inc']*df_test['sub_grade'],5)
 df_train['jjj']=round(df_train['dti']*df_train['sub_grade'],5)
 df_test['jjj']=round(df_test['dti']*df_test['sub_grade'],5)
 df_train['kkk']=round(df_train['open_acc']*df_train['sub_grade'],5)
 df_test['kkk']=round(df_test['open_acc']*df_test['sub_grade'],5)
 df_train['lll']=round(df_train['revol_bal']*df_train['sub_grade'],5)
 df_test['lll']=round(df_test['revol_bal']*df_test['sub_grade'],5)
 df_train['mmm']=round(df_train['revol_util']*df_train['sub_grade'],5)
 df_test['mmm']=round(df_test['revol_util']*df_test['sub_grade'],5)
 df_train['nnn']=round(df_train['total_acc']*df_train['sub_grade'],5)
 df_test['nnn']=round(df_test['total_acc']*df_test['sub_grade'],5)
 df_train['ooo']=round(df_train['tot_cur_bal']*df_train['sub_grade'],5)
 df_test['ooo']=round(df_test['tot_cur_bal']*df_test['sub_grade'],5 )
train = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test = pd.read_csv('.. /input/nlp-getting-started/test.csv' )<define_variables>
df_train[df_train.loan_condition==1].loan_amnt.mean()
TEXT_COLUMN = 'text'
TARGET_COLUMN = 'target'
CHARS_TO_REMOVE = '!"
“”’'∞θ÷α•à−β∅³π‘₹´°£€\×™√²—'<prepare_x_and_y>
df_train[df_train.loan_condition==0].loan_amnt.mean()
x_train = train[TEXT_COLUMN].astype(str)
 y_train = train[TARGET_COLUMN].values
x_test = test[TEXT_COLUMN].astype(str )<string_transform>
f = 'purpose'
df_train[f].value_counts() / len(df_train )
tokenizer = text.Tokenizer(filters=CHARS_TO_REMOVE, lower=False)
 tokenizer.fit_on_texts(list(x_train)+ list(x_test))<concatenate>
df_test[f].value_counts() / len(df_test )
embedding_matrix = np.concatenate([crawl_matrix, glove_matrix], axis=-1)
 embedding_matrix.shape<define_variables>
cats = []
for col in df_train.columns:
if df_train[col].dtype == 'object':
cats.append(col)
 print(col, df_train[col].nunique())
 print(cats )
MAX_LEN = 300
LSTM_UNITS = 128
DENSE_HIDDEN_UNITS = 4 * LSTM_UNITS<string_transform>
print(df_train['title'].unique())
 print(len(df_test.columns))
print(len(df_train.columns))
x_train = tokenizer.texts_to_sequences(x_train)
 x_test = tokenizer.texts_to_sequences(x_test)
 x_train = sequence.pad_sequences(x_train, maxlen=MAX_LEN)
 x_test = sequence.pad_sequences(x_test, maxlen=MAX_LEN )<choose_model_class>
encoder = OrdinalEncoder()
enc_train = encoder.fit_transform(df_train['emp_title'].values)
 enc_test = encoder.transform(df_test['emp_title'].values)
 df_train = df_train.reset_index()
df_test = df_test.reset_index()
df_train['emp_title_lab']=enc_train.iloc[:,0]
df_test['emp_title_lab']=enc_test.iloc[:,0]
df_train = df_train.set_index("ID")
 df_test =df_test.set_index("ID" )
def build_model(embedding_matrix):
words = Input(shape=(None,))
x = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False )(words)
 x = SpatialDropout1D(0.2 )(x)
 x = Bidirectional(LSTM(LSTM_UNITS, return_sequences=True))(x)
 x = Bidirectional(LSTM(LSTM_UNITS, return_sequences=True))(x)
 hidden = concatenate([
GlobalMaxPooling1D()(x),
GlobalAveragePooling1D()(x),
])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 result = Dense(1, activation='sigmoid' )(hidden)
 model = Model(inputs=words, outputs=result)
 model.compile(loss='binary_crossentropy', optimizer='adam')
 return model<define_variables>
ce_cal1='emp_title'
ce_summary1 = df_train[ce_cal1].value_counts()
df_train['emp_title_co'] = df_train[ce_cal1].map(ce_summary1)
 df_test['emp_title_co'] = df_test[ce_cal1].map(ce_summary1 )
NUM_MODELS = 2
BATCH_SIZE = 512
EPOCHS = 5<train_model>
encoder = OrdinalEncoder()
enc_train = encoder.fit_transform(df_train['title'].values)
 enc_test = encoder.transform(df_test['title'].values)
 df_train = df_train.reset_index()
df_test = df_test.reset_index()
df_train['title_la']=enc_train.iloc[:,0]
df_test['title_la']=enc_test.iloc[:,0]
df_train = df_train.set_index("ID")
 df_test =df_test.set_index("ID")
checkpoint_predictions = []
weights = []
for model_idx in range(NUM_MODELS):
model = build_model(embedding_matrix)
 for global_epoch in range(EPOCHS):
model.fit(
 x_train,y_train,
batch_size=BATCH_SIZE,
epochs=1,
verbose=2 
)
 checkpoint_predictions.append(model.predict(x_test, batch_size=BATCH_SIZE))
weights.append(2 ** global_epoch )<prepare_output>
ce_cal2='title'
ce_summary2 = df_train[ce_cal2].value_counts()
df_train['title_co'] = df_train[ce_cal2].map(ce_summary2)
 df_test['title_co'] = df_test[ce_cal2].map(ce_summary2 )
predictions = np.average(checkpoint_predictions, weights=weights, axis=0 )<data_type_conversions>
df_train['NaN']=df_train.isnull().sum(axis=1)
 df_test['NaN']=df_test.isnull().sum(axis=1)
 df_train['NaN']=df_train["NaN"].fillna(0)
 df_test['NaN']=df_test["NaN"].fillna(0)
sub.iloc[:, 1] =(predictions > 0.5 ).astype(int )<save_to_csv>

 df_train=df_train.drop("pub_rec",axis=1)
 df_test=df_test.drop("pub_rec",axis=1)
 df_train=df_train.drop("annual_inc",axis=1)
 df_test=df_test.drop("annual_inc",axis=1)
sub.to_csv('submission.csv', index=False )<save_to_csv>
y_train = df_train.loan_condition
X_train = df_train.drop(['loan_condition'], axis=1)
 X_test = df_test
sub.to_csv('submission.csv', index=False )<count_values>
col='title'
target = 'loan_condition'
X_temp = pd.concat([X_train, y_train], axis=1)
 summary = X_temp.groupby([col])[target].mean()
enc_test = X_test[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train['title']=enc_train
X_test['title']=enc_test
Counter(sub['target'] )<define_search_model>
col='emp_title'
target = 'loan_condition'
X_temp = pd.concat([X_train, y_train], axis=1)
 summary = X_temp.groupby([col])[target].mean()
enc_test = X_test[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train['emp_title']=enc_train
X_test['emp_title']=enc_test
def simple_pipeline() :
print("Load data")
 train, test = load_data()
data = pd.concat([train, test], axis=0, ignore_index=True)
 print("Vectorization")
 X = vectorization(data.drop('target', axis=1))
if type(X)== scipy.sparse.coo_matrix:
X = X.tocsr()
test_mask = data.is_test.values
X_train = X[~test_mask]
y_train = data['target'][~test_mask]
X_test = X[test_mask]
if scipy.sparse.issparse(X):
X_train.sort_indices()
X_test.sort_indices()
model = build_model(X_train, y_train)
 print("Prediction with model")
 p = model.predict(X_test)
 print("Generate submission")
 make_submission(data[test_mask], p)
 def load_data() :
train = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 train['is_test'] = False
test = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 test['target'] = -1
test['is_test'] = True
return train, test
def calculate_validation_metric(model, X, y, metric):
folds = StratifiedKFold(n_splits=4, shuffle=True, random_state=0)
 score = cross_val_score(model, X, y, scoring=metric, cv=folds, n_jobs=4)
 return np.mean(score), model
def select_model(X, y):
models = [
LinearSVC(C=30),
LinearSVC(C=10),
LinearSVC(C=3),
LinearSVC(C=1),
LinearSVC(C=0.3),
LinearSVC(C=0.1),
LinearSVC(C=0.03),
RidgeClassifier(alpha=30),
RidgeClassifier(alpha=10),
RidgeClassifier(alpha=3),
RidgeClassifier(alpha=1),
RidgeClassifier(alpha=0.3),
RidgeClassifier(alpha=0.1),
RidgeClassifier(alpha=0.03),
LogisticRegression(C=30),
LogisticRegression(C=10),
LogisticRegression(C=3),
LogisticRegression(C=1),
LogisticRegression(C=0.3),
LogisticRegression(C=0.1),
LogisticRegression(C=0.03),
]
results = [calculate_validation_metric(
 model, X, y, 'f1_macro', 
)for model in models]
best_result, best_model = max(results, key = lambda x: x[0])
 print("Best model validation result: {:.4f}".format(best_result))
print("Best model: {}".format(best_model))
return best_model
def build_model(X, y):
print("Selecting best model")
 best_model = select_model(X, y)
 print("Refit model to full dataset")
 best_model.fit(X, y)
 return best_model
def make_submission(data, p):
submission = data[['id']].copy()
submission['target'] = p
submission.to_csv('submission.csv', index=False )<categorify>
col='zip_code'
target = 'loan_condition'
X_temp = pd.concat([X_train, y_train], axis=1)
 summary = X_temp.groupby([col])[target].mean()
enc_test = X_test[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train['zip_code']=enc_train
X_test['zip_code']=enc_test
def vectorization(data):

 vectorizer_chars = TfidfVectorizer(analyzer='char', ngram_range=(3, 6), max_df=0.7, min_df=0.0011)
 vectorizer_words = TfidfVectorizer(max_df=0.6)
 text = data['text'].fillna('')
 keyword = data['keyword'].fillna('')
 vect_words = vectorizer_words.fit_transform(text + keyword)
 vect_chars = vectorizer_chars.fit_transform(text)
 vectors = hstack(( vect_words, vect_chars))
return vectors<set_options>
col='addr_state'
target = 'loan_condition'
X_temp = pd.concat([X_train, y_train], axis=1)
 summary = X_temp.groupby([col])[target].mean()
enc_test = X_test[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train['addr_state']=enc_train
X_test['addr_state']=enc_test
%matplotlib inline
color = sns.color_palette()
sns.set_style('darkgrid')
 def ignore_warn(*args, **kwargs):
pass
warnings.warn = ignore_warn
pd.set_option('display.float_format', lambda x: '{:.3f}'.format(x))
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
col='purpose'
target = 'loan_condition'
X_temp = pd.concat([X_train, y_train], axis=1)
 summary = X_temp.groupby([col])[target].mean()
enc_test = X_test[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train['purpose']=enc_train
X_test['purpose']=enc_test
print(check_output(["ls", ".. /input"] ).decode("utf8"))
df_train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 df_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' )<prepare_x_and_y>
col='application_type'
target = 'loan_condition'
X_temp = pd.concat([X_train, y_train], axis=1)
 summary = X_temp.groupby([col])[target].mean()
enc_test = X_test[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train['application_type_ta']=enc_train
X_test['application_type_ta']=enc_test
df_train = df_train.drop(df_train[(df_train['GrLivArea']>4000)&(df_train['SalePrice']<300000)].index)
 ntrain = df_train.shape[0]
ntest = df_test.shape[0]
y_train = df_train.SalePrice.values
all_data = pd.concat(( df_train, df_test)).reset_index(drop=True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape))<create_dataframe>
X_train['Yaxis'].isnull().sum()
NA=all_data[['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond','GarageYrBlt','BsmtFinType2','BsmtFinType1','BsmtCond', 'BsmtQual','BsmtExposure', 'MasVnrArea','MasVnrType','Electrical','MSZoning','BsmtFullBath','BsmtHalfBath','Utilities','Functional','Exterior1st','BsmtUnfSF','Exterior2nd','TotalBsmtSF','GarageArea','GarageCars','KitchenQual','BsmtFinSF2','BsmtFinSF1','SaleType']]<create_dataframe>
X_train['Yaxis']=X_train['Yaxis'].replace([np.inf, -np.inf,np.nan], -9999)
 X_test['Yaxis']=X_test['Yaxis'].replace([np.inf, -np.inf,np.nan], -9999)
 X_train['Yaxis'].astype(str)
 X_test['Yaxis'].astype(str)
 col='Yaxis'
target = 'loan_condition'
X_temp = pd.concat([X_train, y_train], axis=1)
 summary = X_temp.groupby([col])[target].mean()
enc_test = X_test[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train['Yaxis_ta']=enc_train
X_test['Yaxis_ta']=enc_test
X_train['Yaxis'].astype(int)
 X_test['Yaxis'].astype(int )
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head(20 )<data_type_conversions>
X_train=X_train.replace([np.inf, -np.inf,np.nan], -9999)
 X_test=X_test.replace([np.inf, -np.inf,np.nan], -9999)
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
all_data[col] = all_data[col].fillna('None' )<data_type_conversions>
scores = []
y_pred_test=np.zeros(len(X_test))
df = pd.DataFrame(index=[], columns=[])
 df['feature']=X_train.columns
n=10
for i in range(n):
X_train_,X_val,y_train_,y_val=train_test_split(X_train,y_train,test_size=0.3,random_state=i*10)
 clf = LGBMClassifier(boosting_type='gbdt', class_weight=None, colsample_bytree=1,
importance_type='split', learning_rate=0.05, max_depth=-1,
min_child_samples=20, min_child_weight=0.001, min_split_gain=0.0,
n_estimators=100, n_jobs=-1, num_leaves=50, objective=None,
random_state=None, reg_alpha=0.0, reg_lambda=0.0, silent=True,
subsample=1.0, subsample_for_bin=200000, subsample_freq=0)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
score = roc_auc_score(y_val, y_pred)
 scores.append(score)
 df[i]=Series(clf.booster_.feature_importance(importance_type='gain'))
y_pred_test+=clf.predict_proba(X_test)[:,1]
df['ave']=df.mean(axis=1)
 df['std']=df.std(axis=1)
 df=df.sort_values('ave',ascending=False)
 ykai=y_pred_test/n
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
all_data[col] = all_data[col].fillna(0 )<data_type_conversions>
scores_xg=[]
y_pred_test_xg=np.zeros(len(X_test))
df_xg = pd.DataFrame(index=[], columns=[])
 df_xg['feature']=X_train.columns
n=10
for i in range(n):
X_train_,X_val,y_train_,y_val=train_test_split(X_train,y_train,test_size=0.3,random_state=i*10)
 xg=xgb.XGBClassifier()
xg.fit(X_train_, y_train_,early_stopping_rounds=100, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred_xg = xg.predict_proba(X_val)[:,1]
score_xg = roc_auc_score(y_val, y_pred_xg)
 print(score_xg)
 scores_xg.append(score_xg)
 y_pred_test_xg+=xg.predict_proba(X_test)[:,1]
ykai_xg=y_pred_test_xg/n
for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_data[col] = all_data[col].fillna(0 )<data_type_conversions>
scores_cb = []
y_pred_test_cb=np.zeros(len(X_test))
df_cb = pd.DataFrame(index=[], columns=[])
 df_cb['feature']=X_train.columns
n=10
for i in range(n):
X_train_,X_val,y_train_,y_val=train_test_split(X_train,y_train,test_size=0.3,random_state=i*10)
 cb = catboost.CatBoostClassifier(eval_metric='AUC')
 cb.fit(X_train_, y_train_, early_stopping_rounds=200,eval_set=[(X_val, y_val)])
 y_pred_cb = cb.predict_proba(X_val)[:,1]
y_pred_test_cb+=cb.predict_proba(X_test)[:,1]
ykai_cb=y_pred_test_cb/n
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_data[col] = all_data[col].fillna('None')
 all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0)
 all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0])
 all_data = all_data.drop(['Utilities'], axis=1)
 all_data["Functional"] = all_data["Functional"].fillna("Typ")
 all_data['Electrical'] = all_data['Electrical'].fillna(all_data['Electrical'].mode() [0])
 all_data['KitchenQual'] = all_data['KitchenQual'].fillna(all_data['KitchenQual'].mode() [0])
 all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0])
 all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0])
 all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None")
<create_dataframe>
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head(20 )<sort_values>
y_pred=(ykai+ykai_xg+ykai_cb)/3
total = df_train.isnull().sum().sort_values(ascending=False)
 percent =(df_train.isnull().sum() /df_train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )<categorify>
submission = pd.read_csv('.. /input/homework-for-students3/sample_submission.csv', index_col=0)
 submission.loan_condition = y_pred
submission.to_csv('submission.csv' )