Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<train_model>
%matplotlib inline
pd.set_option('max_columns', 200 )
classifier.train(cleaned_train )<load_from_csv>
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )
!git clone https://github.com/mitramir55/Kaggle_NLP_competition.git
perfection = pd.read_csv('Kaggle_NLP_competition/perfect_submission.csv' )<predict_on_test>
drop_columns = ['FireplaceQu','PoolQC','Fence','MiscFeature','BsmtUnfSF']
train.drop(drop_columns, axis = 1, inplace = True)
 test.drop(drop_columns, axis = 1, inplace = True )
y_pred = np.round(classifier.predict(cleaned_test))
print('The score of prediction: ', sklearn.metrics.f1_score(perfection.target, y_pred, average = 'micro'))<save_to_csv>
train['LotFrontage'].fillna(train['LotFrontage'].median() , inplace = True)
 train['MasVnrArea'].fillna(train['MasVnrArea'].median() , inplace = True )
sample_sub = pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv')
 ids = sample_sub.id
final_submission = pd.DataFrame(np.c_[ids, y_pred.astype('int')], columns = ['id', 'target'])
 final_submission.to_csv('final_submission.csv', index = False)
 final_submission.head()<import_modules>
fill_col = ['Alley','MasVnrType','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2',
'GarageType','GarageFinish','GarageCond']
both_col = [train,test]
for col in both_col:
col[fill_col] = col[fill_col].fillna('None' )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from wordcloud import WordCloud 
<load_from_csv>
colfil = ['LotFrontage','MasVnrArea','BsmtFinSF1','BsmtFinSF2','TotalBsmtSF','BsmtFullBath','BsmtHalfBath','GarageCars',
'GarageArea']
for coll in colfil:
test[coll].fillna(test[coll].median() , inplace = True )
df =pd.read_csv('/kaggle/input/nlp-getting-started/train.csv' , encoding='ISO-8859-1')
 df.head()<define_variables>
both_col = [train, test]
for col in both_col:
col['YrBltAndRemod'] = col['YearBuilt'] + col['YearRemodAdd']
col['TotalSF'] = col['TotalBsmtSF'] + col['1stFlrSF'] + col['2ndFlrSF']
col['Total_sqr_footage'] =(col['BsmtFinSF1'] + col['BsmtFinSF2'] +
col['1stFlrSF'] + col['2ndFlrSF'])
 col['Total_Bathrooms'] =(col['FullBath'] +(0.5 * col['HalfBath'])+
col['BsmtFullBath'] +(0.5 *col['BsmtHalfBath']))
col['Total_porch_sf'] =(col['OpenPorchSF'] + col['3SsnPorch'] +
col['EnclosedPorch'] + col['ScreenPorch'] +
col['WoodDeckSF'] )
df_tweets = df[['text','target']]<remove_duplicates>
both_col = [train,test]
for col in both_col:
col['haspool'] = col['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 col['has2ndfloor'] = col['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 col['hasgarage'] = col['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 col['hasbsmt'] = col['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 col['hasfireplace'] = col['Fireplaces'].apply(lambda x: 1 if x > 0 else 0 )
df_tweets.drop_duplicates(subset=['text'],keep='first',inplace=True)
 df_tweets.info()<split>
drop_col = ['Exterior2nd','GarageYrBlt','Condition2','RoofMatl','Electrical','HouseStyle','Exterior1st',
'Heating','GarageQual','Utilities','MSZoning','Functional','KitchenQual']
train.drop(drop_col, axis = 1,inplace = True)
 test.drop(drop_col, axis = 1,inplace = True )
train_df,eval_df = train_test_split(df_tweets,test_size = 0.01 )<install_modules>
x_t = train.drop(['SalePrice'], axis = 1)
 y_t = train['SalePrice']
!pip install simpletransformers==0.32.3 
<choose_model_class>
hot_one= pd.get_dummies(x_t )
model = ClassificationModel('bert', 'bert-base-cased', num_labels=2, args={'reprocess_input_data': True, 'overwrite_output_dir': True},use_cuda=False)
<create_dataframe>
x_train ,x_test ,y_train ,y_test = train_test_split(hot_one, y_t, test_size = 0.3, random_state = 0 )
train_df2 = pd.DataFrame({
'text': train_df['text'].replace(r'
', ' ', regex=True),
'label': train_df['target']
})
 eval_df2 = pd.DataFrame({
'text': eval_df['text'].replace(r'
', ' ', regex=True),
'label': eval_df['target']
} )<train_model>
model = RandomForestRegressor(n_estimators = 300, random_state = 0)
 model.fit(x_train,y_train )
model.train_model(train_df2 )<predict_on_test>
dtr_pred = model.predict(x_test )
result, model_outputs, wrong_predictions = model.eval_model(eval_df2 )<concatenate>
Image(".. /input/kaggle/Screenshot-2019-05-16-at-6.43.11-PM-850x262.png" )
lst = []
for arr in model_outputs:
lst.append(np.argmax(arr))<define_variables>
print('RMSE:', np.sqrt(mean_squared_log_error(y_test, dtr_pred)) )
true = eval_df2['label'].tolist()
predicted = lst<compute_test_metric>
hot_two = pd.get_dummies(test )
mat = sklearn.metrics.confusion_matrix(true , predicted)
 mat<compute_test_metric>
test_prediction = model.predict(hot_two )
print(sklearn.metrics.classification_report(true,predicted,target_names=['fake','real']))<load_from_csv>
test_pred = pd.DataFrame(test_prediction, columns=['SalePrice'] )
test_df =pd.read_csv('/kaggle/input/nlp-getting-started/test.csv' , encoding='ISO-8859-1')
 test_df.head()<predict_on_test>
modell = ensemble.GradientBoostingRegressor(n_estimators = 3000, max_depth = 5,max_features='sqrt', min_samples_split = 10,
learning_rate = 0.005,loss = 'huber',min_samples_leaf=15,random_state =42)
 modell.fit(x_train, y_train )
final_prediction = model.predict(list(test_df.text))<save_to_csv>
pred = modell.predict(x_test )
print('Loading in Submission File...')
 submit_df = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 submit_df['target'] = final_prediction[0]
submit_df.to_csv('bert_submit.csv', index=False )<train_model>
print('RMSE:', np.sqrt(mean_squared_log_error(y_test, pred)) )
print("Finished" )<set_options>
test_pred_2 = modell.predict(hot_two )
if torch.cuda.is_available() :
device = torch.device("cuda")
 print('We will use the GPU:', torch.cuda.get_device_name(0))
else:
print('No GPU available, using the CPU instead.')
 device = torch.device("cpu" )<load_from_csv>
test_pred = pd.DataFrame(test_pred_2, columns=['SalePrice'] )
<categorify><EOS> 
out = pd.DataFrame({'Id': hot_two['Id'], 'SalePrice': test_pred_2})
 out.to_csv('submission.csv', index=False,header=True )