Kaggle competition: Personality Profile Prediction
<SOS> metric: meancolumnwiselogloss Kaggle data source: personality-profile-prediction<normalization>
pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.width', 1000)
 %matplotlib inline
if not sys.warnoptions:
warnings.simplefilter("ignore")
saleprice_scaled = StandardScaler().fit_transform(train['SalePrice'][:,np.newaxis])
 low_range = saleprice_scaled[saleprice_scaled[:,0].argsort() ][:10]
high_range= saleprice_scaled[saleprice_scaled[:,0].argsort() ][-10:]
print('outer range(low)of the distribution:')
 print(low_range)
 print('
outer range(high)of the distribution:')
 print(high_range )<drop_column>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 test_id = test['id']
train_test_combined = pd.concat([train,test] )
train['TotalSF'] = train['TotalBsmtSF'] + train['1stFlrSF'] + train['2ndFlrSF']
print(train)
 train.drop(['TotalBsmtSF','1stFlrSF','2ndFlrSF'],axis=1 )<load_from_csv>
def remove_url(string):

 pattern_url = r'http[s]?://(?:[A-Za-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9A-Fa-f][0-9A-Fa-f])) +'
string = re.sub(pattern_url, " ", string)
 return string
train_test_combined['posts'] = train_test_combined['posts'].apply(remove_url )
test=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 test.isnull().sum()<count_missing_values>
def remove_numbers(string):

 pattern_number = r'[0-9]*'
string = re.sub(pattern_number, "", string)
 return string
train_test_combined['posts'] = train_test_combined['posts'].apply(remove_numbers )
miss_test=[col for col in test.columns if test[col].isnull().any() ]
print(miss_test )<data_type_conversions>
def remove_punc(strings):

 for punctuation in string.punctuation:
strings = strings.replace(punctuation, ' ')
 return strings
train_test_combined['posts'] = train_test_combined['posts'].apply(remove_punc )
for col in miss_test:
test[col]=test[col].fillna(test[col].mode() [0] )<categorify>
def remove_extra_spacing(string):

 string = re.sub('\s+', ' ', string)
 return string
train_test_combined['posts'] = train_test_combined['posts'].apply(remove_extra_spacing )
for col in test.select_dtypes(include=['object']):
test[col]=LE.fit_transform(test[col])
 test.head()<drop_column>
stopwords_list = set(stopwords.words("english"))
lemmatiser = WordNetLemmatizer()
def remove_stopwords(string):

 string = " ".join([lemmatiser.lemmatize(word)for word in string.split() if(word not in stopwords_list)&(len(word)< 15)])
 return string
train_test_combined['posts'] = train_test_combined['posts'].apply(remove_stopwords )
test['TotalSF'] = test['TotalBsmtSF'] + test['1stFlrSF'] + test['2ndFlrSF']
print(test)
 test.drop(['TotalBsmtSF','1stFlrSF','2ndFlrSF'],axis=1 )<prepare_x_and_y>
def generate_targets(data):

 df = data.copy()
df['mind'] = df['type'].apply(lambda x: x[0] == 'E' ).astype('int')
 df['energy'] = df['type'].apply(lambda x: x[1] == 'N' ).astype('int')
 df['nature'] = df['type'].apply(lambda x: x[2] == 'T' ).astype('int')
 df['tactics'] = df['type'].apply(lambda x: x[3] == 'J' ).astype('int')
 return df[['mind','energy','nature','tactics']]
y = generate_targets(train)
 y.head()
X_train=train.drop(["SalePrice"],axis=1)
 Y_train=train["SalePrice"]
print(X_train )<split>
tfidvectorizer = TfidfVectorizer(stop_words='english',min_df=1,max_df=0.95)
 tfidfvectorized_X = tfidvectorizer.fit_transform(train_test_combined['posts'] )
x_train , x_test , y_train , y_test = train_test_split(X_train , Y_train ,test_size = 0.3,random_state = 3 )<choose_model_class>
X = tfidfvectorized_X[:len(y)]
X_submission = tfidfvectorized_X[len(y):]
print(X.shape, y.shape, X_submission.shape)
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, stratify=y, random_state=22 )
lightgbm = LGBMRegressor(objective='regression',
num_leaves=8,
learning_rate=0.0385,
n_estimators=3500,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
verbose= 0,
)<choose_model_class>
LR_model = LogisticRegression()
results_index = ['Accuracy','Precision','Recall','F1 Score']
LR_model_results = pd.DataFrame(index = results_index, columns=list(y.columns))
LR_default_submission_dict = {'id':test['id']}
print(" Logistic Regression(Default Parameters)")
 print('-'*len(" Logistic Regression(Default Parameters)"))
for attribute in list(y.columns):
LR_model.fit(X_train, y_train[attribute])
 y_pred = LR_model.predict(X_test)
 LR_default_submission_dict[attribute] = LR_model.predict(X_submission)
 LR_model_results.loc['Accuracy',attribute] = accuracy_score(y_test[attribute],y_pred)
 LR_model_results.loc['Precision',attribute] = precision_score(y_test[attribute],y_pred)
 LR_model_results.loc['Recall',attribute] = recall_score(y_test[attribute],y_pred)
 LR_model_results.loc['F1 Score',attribute] = f1_score(y_test[attribute],y_pred)
 LR_model_results.loc['Log-loss',attribute] = log_loss(y_test[attribute],y_pred)
 LR_model_results.loc['ROC_AUC',attribute] = roc_auc_score(y_test[attribute],y_pred)
 print(f'Confusion Matrix({attribute}):
' + str(pd.DataFrame(confusion_matrix(y_test[attribute],y_pred,labels=[0,1]))))
print('
')
 print(" Logistic Regression Results(Default Parameters)")
 print("-"*len(" Logistic Regression Results(Default Parameters)"))
print(LR_model_results)
lightgbm1 = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=5000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
verbose=-1, 
)
<choose_model_class>
LR_model = LogisticRegression()
grid_parameters = {"C":[0.05,0.5,1,5,10,20,25,30],
"solver" : ['lbfgs', 'liblinear', 'sag', 'saga'],
"class_weight": ['balanced']}
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=22)
 results_index = ['Accuracy','Precision','Recall','F1 Score']
LR_model_GSCV_results = pd.DataFrame(index = results_index, columns=list(y.columns))
LR_submission_dict = {'id':test['id']}
print(" Logistic Regression(Grid Searched Parameters)")
 print("-"*len(" Logistic Regression(Grid Searched Parameters "))
print("(Best parameters per attribute)")
 print(" ")
 for attribute in list(y.columns):
LR_model_GSCV = GridSearchCV(LR_model, param_grid = grid_parameters, scoring = 'f1', cv = kfold)
 LR_model_GSCV.fit(X_train, y_train[attribute])
 print(f'{attribute}:',LR_model_GSCV.best_params_)
 print(" ")
 best_model = LR_model_GSCV.best_estimator_
y_pred = best_model.predict(X_test)
 LR_submission_dict[attribute] = best_model.predict(X_submission)
 LR_model_GSCV_results.loc['Accuracy',attribute] = accuracy_score(y_test[attribute],y_pred)
 LR_model_GSCV_results.loc['Precision',attribute] = precision_score(y_test[attribute],y_pred)
 LR_model_GSCV_results.loc['Recall',attribute] = recall_score(y_test[attribute],y_pred)
 LR_model_GSCV_results.loc['F1 Score',attribute] = f1_score(y_test[attribute],y_pred)
 LR_model_GSCV_results.loc['Log-loss',attribute] = log_loss(y_test[attribute],best_model.predict(X_test))
LR_model_GSCV_results.loc['ROC_AUC',attribute] = roc_auc_score(y_test[attribute],y_pred)
 print(f'Confusion Matrix({attribute}):
' + str(pd.DataFrame(confusion_matrix(y_test[attribute],y_pred,labels=[0,1]))))
print(" ")
 print("-"*len(" Logistic Regression Results(Grid Searched Parameters)"))
print(" Logistic Regression Results(Grid Searched Parameters)")
 print("-"*len(" Logistic Regression Results(Grid Searched Parameters)"))
print(LR_model_GSCV_results)
 LR_submission_df = pd.DataFrame(LR_submission_dict)
Catmodel= CatBoostRegressor(iterations=20, learning_rate=1, depth=3, verbose= 0 )<choose_model_class>
KNN_model = KNeighborsClassifier(weights='distance', n_neighbors=3)
 results_index = ['Accuracy','Precision','Recall','F1 Score']
KNN_model_results = pd.DataFrame(index = results_index, columns=list(y.columns))
KNN_submission_dict = {'id':test['id']}
print(" K-Nearest-Neighbors Results ")
 print('-'*len(" K-Nearest-Neighbors Results "))
for attribute in list(y.columns):
KNN_model.fit(X_train, y_train[attribute])
 y_pred = KNN_model.predict(X_test)
 KNN_submission_dict[attribute] = KNN_model.predict(X_submission)
 KNN_model_results.loc['Accuracy',attribute] = accuracy_score(y_test[attribute],y_pred)
 KNN_model_results.loc['Precision',attribute] = precision_score(y_test[attribute],y_pred)
 KNN_model_results.loc['Recall',attribute] = recall_score(y_test[attribute],y_pred)
 KNN_model_results.loc['F1 Score',attribute] = f1_score(y_test[attribute],y_pred)
 KNN_model_results.loc['Log-loss',attribute] = log_loss(y_test[attribute],y_pred)
 KNN_model_results.loc['ROC_AUC',attribute] = roc_auc_score(y_test[attribute],y_pred)
 print(f'Confusion Matrix({attribute}):
' + str(pd.DataFrame(confusion_matrix(y_test[attribute],y_pred,labels=[0,1]))))
print('
')
 print(" K-Nearest-Neighbors Results ")
 print("-"*49)
 print(KNN_model_results )
<train_model>
RFC_model = RandomForestClassifier()
grid_parameters = {"n_estimators":[100,200],
"class_weight": ['balanced']}
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=22)
 results_index = ['Accuracy','Precision','Recall','F1 Score']
RFC_model_GSCV_results = pd.DataFrame(index = results_index, columns=list(y.columns))
RFC_submission_dict = {'id':test['id']}
print(" Random Forest Classifier(Grid Searched Parameters)")
 print("-"*len(" Random Forest Classifier(Grid Searched Parameters)"))
print("(Best parameters per attribute)")
 print(" ")
 for attribute in list(y.columns):
RFC_model_GSCV = GridSearchCV(RFC_model, param_grid = grid_parameters, scoring = 'neg_log_loss', cv = kfold)
 RFC_model_GSCV.fit(X_train, y_train[attribute])
 print(f'{attribute}:',RFC_model_GSCV.best_params_)
 print(" ")
 best_model = RFC_model_GSCV.best_estimator_
y_pred = best_model.predict(X_test)
 RFC_submission_dict[attribute] = best_model.predict(X_submission)
 RFC_model_GSCV_results.loc['Accuracy',attribute] = accuracy_score(y_test[attribute],y_pred)
 RFC_model_GSCV_results.loc['Precision',attribute] = precision_score(y_test[attribute],y_pred)
 RFC_model_GSCV_results.loc['Recall',attribute] = recall_score(y_test[attribute],y_pred)
 RFC_model_GSCV_results.loc['F1 Score',attribute] = f1_score(y_test[attribute],y_pred)
 RFC_model_GSCV_results.loc['Log-loss',attribute] = log_loss(y_test[attribute],y_pred)
 RFC_model_GSCV_results.loc['ROC_AUC',attribute] = roc_auc_score(y_test[attribute],y_pred)
 print(f'Confusion Matrix({attribute}):
' + str(pd.DataFrame(confusion_matrix(y_test[attribute],y_pred,labels=[0,1]))))
print(" ")
 print("-"*len(" Random Forest Classifier Results(Grid Searched Parameters)"))
print(" Random Forest Classifier Results(Grid Searched Parameters)")
 print("-"*len(" Random Forest Classifier Results(Grid Searched Parameters)"))
print(RFC_model_GSCV_results )
<compute_test_metric><EOS> 
LR_submission_df = pd.DataFrame(LR_submission_dict)
 LR_submission_df.to_csv("LR_submission_df.csv",index=False )