<jupyter_start><jupyter_text>Installing specfied Libraries<jupyter_code>pip install xgboost
# Essentials
import numpy as np
import pandas as pd
import datetime
import random

# Plots
import seaborn as sns
import matplotlib.pyplot as plt

# Models
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import mean_absolute_error as mae
from sklearn.metrics import r2_score

from math import sqrt

from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV

from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.model_selection import KFold
train=pd.read_csv("../input/traindata/train_0OECtn8.csv")
test=pd.read_csv("../input/testdata/test_1zqHu22.csv")
train.shape, test.shape
train<jupyter_output><empty_output><jupyter_text>EDA ON DATA  Data type and Null values<jupyter_code>train.dtypes
train.isnull().sum()
train["user_id"].value_counts()
#train['user_id'].value_counts().plot(kind='bar');
train["category_id"].value_counts()
train["video_id"].value_counts()<jupyter_output><empty_output><jupyter_text>Engagement_score: the variable we're trying to predict<jupyter_code>sns.set_style("white")
sns.set_color_codes(palette='deep')
f, ax = plt.subplots(figsize=(8, 7))
#Check the new distribution 
sns.distplot(train['engagement_score'], color="b");
ax.xaxis.grid(False)
ax.set(ylabel="Frequency")
ax.set(xlabel="engagement_score")
ax.set(title="ES distribution")
sns.despine(trim=True, left=True)
plt.show()
# Skew and kurt
print("Skewness: %f" % train['engagement_score'].skew())
print("Kurtosis: %f" % train['engagement_score'].kurt())<jupyter_output>Skewness: -0.855630
Kurtosis: 0.640584<jupyter_text>Converting category data to numerical<jupyter_code>print(train['gender'].value_counts())
dummies = pd.get_dummies(train['gender'],drop_first=True)
train_data = pd.concat([train.drop('gender',axis=1),dummies],axis=1)
train_data.head()    # 1- male ,0 - Female
print(train_data['profession'].value_counts())
dummies = pd.get_dummies(train_data['profession'],drop_first=True)
train_data = pd.concat([train_data.drop('profession',axis=1),dummies],axis=1)
train_data.tail()    # 10 -student,01-wp,00-others
sns.histplot(data = train_data, x = "age", kde = True)
sns.histplot(data = train_data, x = "followers", kde = True)
sns.histplot(data = train_data, x = "views", kde = True)<jupyter_output><empty_output><jupyter_text>Dropping unnecessary coloumns<jupyter_code>train_data.drop(['row_id'], axis=1, inplace=True)
#test.drop(['row_id','user_id'], axis=1, inplace=True)
train_data.shape, test.shape
train_data.tail()    
test.tail()<jupyter_output><empty_output><jupyter_text>Correlations<jupyter_code>corr = train_data.corr()
plt.subplots(figsize=(15,12))
dataplot = sns.heatmap(corr, cmap="YlGnBu", annot=True) 
# displaying heatmap
plt.show()<jupyter_output><empty_output><jupyter_text>Training a model<jupyter_code>X=train_data.drop(['engagement_score'], axis=1)
y=train_data['engagement_score']
X.head()
y.head()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)
print("The size of training input is", X_train.shape)
print("The size of training output is", y_train.shape)
print(50 *'*')
print("The size of testing input is", X_test.shape)
print("The size of testing output is", y_test.shape)<jupyter_output>The size of training input is (71357, 9)
The size of training output is (71357,)
**************************************************
The size of testing input is (17840, 9)
The size of testing output is (17840,)<jupyter_text>Ridge Regressor<jupyter_code>params ={'alpha' :[0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000, 100000]}
ridge_regressor =GridSearchCV(Ridge(), params ,cv =5,scoring = 'neg_mean_absolute_error', n_jobs =-1)
ridge_regressor.fit(X_train ,y_train)
y_train_pred =ridge_regressor.predict(X_train) ##Predict train result
y_test_pred =ridge_regressor.predict(X_test) ##Predict test result
print("Train Results for Ridge Regressor Model:")
print(50 * '-')
print("Root mean squared error: ", sqrt(mse(y_train.values, y_train_pred)))
print("Mean absolute error: ",mae(y_train.values, y_train_pred))
print("R-squared: ", r2_score(y_train.values, y_train_pred))
print("Test Results for Ridge Regressor Model:")
print(50 * '-')
print("Root mean squared error: ", sqrt(mse(y_test, y_test_pred)))
print("Mean absolute error: ",mae(y_test, y_test_pred))
print("R-squared: ", r2_score(y_test, y_test_pred))<jupyter_output>Test Results for Ridge Regressor Model:
--------------------------------------------------
Root mean squared error:  0.7386171417905056
Mean absolute error:  0.5687148260816715
R-squared:  0.2778788616815382<jupyter_text>Lasso Regressor<jupyter_code>params ={'alpha' :[0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000, 100000]}
lasso_regressor =GridSearchCV(Lasso(), params ,cv =15,scoring = 'neg_mean_absolute_error', n_jobs =-1)
lasso_regressor.fit(X_train ,y_train)
y_train_pred =lasso_regressor.predict(X_train) ##Predict train result
y_test_pred =lasso_regressor.predict(X_test) ##Predict test result
print("Train Results for lasso Regressor Model:")
print(50 * '-')
print("Root mean squared error: ", sqrt(mse(y_train.values, y_train_pred)))
print("Mean absolute error: ",mae(y_train.values, y_train_pred))
print("R-squared: ", r2_score(y_train.values, y_train_pred))
print("Test Results for lasso Regressor Model:")
print(50 * '-')
print("Root mean squared error: ", sqrt(mse(y_test, y_test_pred)))
print("Mean absolute error: ",mae(y_test, y_test_pred))
print("R-squared: ", r2_score(y_test, y_test_pred))<jupyter_output>Test Results for lasso Regressor Model:
--------------------------------------------------
Root mean squared error:  0.7386187907991228
Mean absolute error:  0.5687363668822549
R-squared:  0.27787563731740805<jupyter_text>K Neighbors Regressor<jupyter_code>k_range = list(range(1, 30))
params = dict(n_neighbors = k_range)
knn_regressor = GridSearchCV(KNeighborsRegressor(), params, cv =10, scoring = 'neg_mean_squared_error')
knn_regressor.fit(X_train, y_train)
y_train_pred =knn_regressor.predict(X_train) ##Predict train result
y_test_pred =knn_regressor.predict(X_test) ##Predict test result
print("Train Results for knn Regressor Model:")
print(50 * '-')
print("Root mean squared error: ", sqrt(mse(y_train.values, y_train_pred)))
print("Mean absolute error: ",mae(y_train.values, y_train_pred))
print("R-squared: ", r2_score(y_train.values, y_train_pred))
print("Test Results for knn Regressor Model:")
print(50 * '-')
print("Root mean squared error: ", sqrt(mse(y_test, y_test_pred)))
print("Mean absolute error: ",mae(y_test, y_test_pred))
print("R-squared: ", r2_score(y_test, y_test_pred))<jupyter_output>Test Results for knn Regressor Model:
--------------------------------------------------
Root mean squared error:  0.8553706077899511
Mean absolute error:  0.6823642338023814
R-squared:  0.0315438999996932<jupyter_text>Decision Tree Regressor<jupyter_code>depth  =list(range(3,30))
param_grid =dict(max_depth =depth)
tree =GridSearchCV(DecisionTreeRegressor(),param_grid,cv =10)
tree.fit(X_train,y_train)
y_train_pred =tree.predict(X_train) ##Predict train result
y_test_pred =tree.predict(X_test) ##Predict test result
print("Train Results for Decision Tree Regressor Model:")
print(50 * '-')
print("Root mean squared error: ", sqrt(mse(y_train.values, y_train_pred)))
print("Mean absolute error: ", mae(y_train.values, y_train_pred))
print("R-squared: ", r2_score(y_train.values, y_train_pred))

print("Test Results for Decision Tree Regressor Model:")
print(50 * '-')
print("Root mean squared error: ", sqrt(mse(y_test, y_test_pred)))
print("Mean absolute error: ", mae(y_test, y_test_pred))
print("R-squared: ", r2_score(y_test, y_test_pred))<jupyter_output>Test Results for Decision Tree Regressor Model:
--------------------------------------------------
Root mean squared error:  0.7086241287145016
Mean absolute error:  0.5332242322782851
R-squared:  0.33533445695674513<jupyter_text>RF REGRESSOR<jupyter_code>tuned_params = {'n_estimators': [100, 200, 300, 400, 500], 'min_samples_split': [2, 5, 10], 'min_samples_leaf': [1, 2, 4]}
random_regressor = RandomizedSearchCV(RandomForestRegressor(), tuned_params, n_iter = 20, scoring = 'neg_mean_absolute_error', cv = 5, n_jobs = -1)
random_regressor.fit(X_train, y_train)
y_train_pred = random_regressor.predict(X_train)
y_test_pred = random_regressor.predict(X_test)
print("Train Results for Random Forest Regressor Model:")
print(50 * '-')
print("Root mean squared error: ", sqrt(mse(y_train.values, y_train_pred)))
print("Mean absolute error: ", mae(y_train.values, y_train_pred))
print("R-squared: ", r2_score(y_train.values, y_train_pred))
print("Test Results for Random Forest Regressor Model:")
print(50 * '-')
print("Root mean squared error: ", sqrt(mse(y_test, y_test_pred)))
print("Mean absolute  error: ", mae(y_test, y_test_pred))
print("R-squared: ", r2_score(y_test, y_test_pred))<jupyter_output>Test Results for Random Forest Regressor Model:
--------------------------------------------------
Root mean squared error:  0.6745914680459376
Mean absolute  error:  0.5043107231742436
R-squared:  0.3976443597641398<jupyter_text>Checking on test data<jupyter_code>test.dtypes
test.isnull().sum()
test.head()
print(test['gender'].value_counts())
dummies = pd.get_dummies(test['gender'],drop_first=True)
test_data = pd.concat([test.drop('gender',axis=1),dummies],axis=1)
test_data.head()
print(test_data['profession'].value_counts())
dummies = pd.get_dummies(test_data['profession'],drop_first=True)
test_data = pd.concat([test_data.drop('profession',axis=1),dummies],axis=1)
test_data=test_data.drop(['row_id'],axis=1)
test_data.head()
y_test_ridge_pred = ridge_regressor.predict(test_data)
y_test_lasso_pred =lasso_regressor.predict(test_data)
y_test_knn_pred = knn_regressor.predict(test_data)
y_test_dt_pred = tree.predict(test_data)
y_test_rf_pred = random_regressor.predict(test_data)
# y_test_xgb_pred = model_xgb.predict(test_data)
y_test_pred
test_data['engagement_score_ridge'] = y_test_ridge_pred
test_data['engagement_score_lasso'] = y_test_lasso_pred
test_data['engagement_score_knn'] = y_test_knn_pred
test_data['engagement_score_dt'] = y_test_dt_pred
test_data['engagement_score_rf'] = y_test_rf_pred
# test_data['engagement_score_xgb'] = y_test_xgb_pred
test_data
test=pd.read_csv("../input/testdata/test_1zqHu22.csv")
test
test_data['row_id']=test['row_id']
test_data
test_data.to_csv("predictions.csv")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15684113865932048, 'fraction_numerical': 0.04361799816345271, 'mean_word_length': 6.943836615609045, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '193891', 'license': 'Apache-2.0', 'n_tokens_mistral': 4341, 'n_tokens_neox': 3888, 'n_words': 770, 'path': 'Hackthon Notebook.ipynb', 'repo_name': 'sai-123-code/JOB-A-THON---Feb22', 'size': '10890'}