<jupyter_start><jupyter_text>imports<jupyter_code># imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics
%matplotlib inline<jupyter_output><empty_output><jupyter_text>data<jupyter_code># import the pg dataset
data=pd.read_csv("C:/Users/sonny/Desktop/pg.csv")
# see head of the pg dataset
data.head()
# check the shape of dataset
data.shape
data.columns
data.dtypes
# number of values 
data.nunique()
# check the missing values
data.isnull().sum()
#  view the data statistics
data.describe()
# find the correlation between the features
corr=data.corr()
corr.shape
# Plotting the heatmap of correlation between features
plt.figure(figsize=(20,20))
sns.heatmap(corr, cbar=True, square= True, fmt='.1f', annot=True, annot_kws={'size':15}, cmap='Greens')
# overlook
print("-"*40)
data.info()
print("-"*40)
#  look at the relation between obejective and explainotary; logpgv

figure=plt.figure()
sns.pairplot(x_vars=['Date','Mw','Distance','Depth','EqNo','StationNo'],y_vars=['logpgv'],data=data,dropna=True)
plt.show()
# look at the relation between obejective and explainotary; PGV*
figure=plt.figure()
sns.pairplot(x_vars=['Date','Mw','Distance','Depth','EqNo','StationNo'],y_vars=['PGV*'],data=data,dropna=True)
plt.show()
sns.distplot(data['PGV*'])
sns.distplot(data['logpgv'])<jupyter_output><empty_output><jupyter_text>the reason why logpgv, not PGV<jupyter_code># split objective variable and explainatory variables
column_sels=['Mw','Distance','Depth']
X=data.loc[:,column_sels]
y=data['logpgv']
# Splitting to training and testing data
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 = 4)
print(X_train.shape,
X_test.shape,
y_train.shape,
y_test.shape)<jupyter_output>(7787, 3) (1947, 3) (7787,) (1947,)<jupyter_text>Model: LM<jupyter_code># Import library for Linear Regression
from sklearn.linear_model import LinearRegression

# Create a Linear regressor
lm = LinearRegression()

# Train the model using the training sets 
lm.fit(X_train, y_train)
lm.intercept_
#Converting the coefficient values to a dataframe
coeffcients = pd.DataFrame([X_train.columns,lm.coef_]).T
coeffcients = coeffcients.rename(columns={0: 'Attribute', 1: 'Coefficients'})
coeffcients
# Model prediction on train data
y_pred = lm.predict(X_train)
print('R^2:',metrics.r2_score(y_train, y_pred))
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_train, y_pred))*(len(y_train)-1)/(len(y_train)-X_train.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_train, y_pred))
print('MSE:',metrics.mean_squared_error(y_train, y_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_train, y_pred)))
# Visualizing the differences between obs and pre; train data
plt.scatter(y_train, y_pred)
plt.xlabel("obs_logpgv")
plt.ylabel("Pre_logpgv")
plt.title("obs and pre")
plt.show()
# Checking residuals
plt.scatter(y_pred,y_train-y_pred)
plt.title("Predicted vs residuals")
plt.xlabel("Predicted")
plt.ylabel("Residuals")
plt.show()
# Checking Normality of errors
sns.distplot(y_train-y_pred)
plt.title("Histogram of Residuals")
plt.xlabel("Residuals")
plt.ylabel("Frequency")
plt.show()
# Predicting Test data with the model
y_test_pred = lm.predict(X_test)
# Visualizing the differences between obs and pre; test data
plt.scatter(y_test, y_test_pred)
plt.xlabel("obs_logpgv")
plt.ylabel("Pre_logpgv")
plt.title("obs and pre of test data")
plt.show()
# Model Evaluation
acc_linreg = metrics.r2_score(y_test, y_test_pred)
print('R^2:', acc_linreg)
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_test, y_test_pred))*(len(y_test)-1)/(len(y_test)-X_test.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_test, y_test_pred))
print('MSE:',metrics.mean_squared_error(y_test, y_test_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))<jupyter_output>R^2: 0.5715758259963973
Adjusted R^2: 0.5709143373077659
MAE: 0.25897643120910047
MSE: 0.10526698876091573
RMSE: 0.32444874596909096<jupyter_text>Model:RF<jupyter_code># Import Random Forest Regressor
from sklearn.ensemble import RandomForestRegressor

# Create a Random Forest Regressor
reg = RandomForestRegressor()

# Train the model using the training sets 
reg.fit(X_train, y_train)
# Model prediction on train data
y_pred = reg.predict(X_train)
# Model Evaluation
print('R^2:',metrics.r2_score(y_train, y_pred))
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_train, y_pred))*(len(y_train)-1)/(len(y_train)-X_train.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_train, y_pred))
print('MSE:',metrics.mean_squared_error(y_train, y_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_train, y_pred)))
# Visualizing the differences between obs and pre; train data
plt.scatter(y_train, y_pred)
plt.xlabel("obs_logpgv")
plt.ylabel("Pre_logpgv")
plt.title("obs and pre of train data")
plt.show()
# Checking residuals
plt.scatter(y_pred,y_train-y_pred)
plt.title("Predicted vs residuals")
plt.xlabel("Predicted")
plt.ylabel("Residuals")
plt.show()
# Predicting Test data with the model
y_test_pred = reg.predict(X_test)
# Model Evaluation
acc_rf = metrics.r2_score(y_test, y_test_pred)
print('R^2:', acc_rf)
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_test, y_test_pred))*(len(y_test)-1)/(len(y_test)-X_test.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_test, y_test_pred))
print('MSE:',metrics.mean_squared_error(y_test, y_test_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
# Visualizing the differences between obs and pre; test data
plt.scatter(y_test, y_test_pred)
plt.xlabel("obs_logpgv")
plt.ylabel("Pre_logpgv")
plt.title("obs and pre of test data")
plt.show()<jupyter_output><empty_output><jupyter_text>Model:XGBoost Regressor<jupyter_code># Import XGBoost Regressor
from xgboost import XGBRegressor

#Create a XGBoost Regressor
reg = XGBRegressor()

# Train the model using the training sets 
reg.fit(X_train, y_train)
# Model prediction on train data
y_pred = reg.predict(X_train)
# Model Evaluation
print('R^2:',metrics.r2_score(y_train, y_pred))
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_train, y_pred))*(len(y_train)-1)/(len(y_train)-X_train.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_train, y_pred))
print('MSE:',metrics.mean_squared_error(y_train, y_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_train, y_pred)))
# Visualizing the differences between obs and pre; train data
plt.scatter(y_train, y_pred)
plt.xlabel("obs_logpgv")
plt.ylabel("Pre_logpgv")
plt.title("obs and pre of train data")
plt.show()
# Checking residuals
plt.scatter(y_pred,y_train-y_pred)
plt.title("Predicted vs residuals")
plt.xlabel("Predicted")
plt.ylabel("Residuals")
plt.show()
#Predicting Test data with the model
y_test_pred = reg.predict(X_test)

# Model Evaluation
acc_xgb = metrics.r2_score(y_test, y_test_pred)
print('R^2:', acc_xgb)
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_test, y_test_pred))*(len(y_test)-1)/(len(y_test)-X_test.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_test, y_test_pred))
print('MSE:',metrics.mean_squared_error(y_test, y_test_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
# Visualizing the differences between obs and pre; test data
plt.scatter(y_test, y_test_pred)
plt.xlabel("obs_logpgv")
plt.ylabel("Pre_logpgv")
plt.title("obs and pre of test data")
plt.show()<jupyter_output><empty_output><jupyter_text>Model:SVM Regressor<jupyter_code># Creating scaled set to be used in model to improve our results
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Import SVM Regressor
from sklearn import svm

# Create a SVM Regressor
reg = svm.SVR()
# Train the model using the training sets 
reg.fit(X_train, y_train)
# Model prediction on train data
y_pred = reg.predict(X_train)
# Model Evaluation
print('R^2:',metrics.r2_score(y_train, y_pred))
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_train, y_pred))*(len(y_train)-1)/(len(y_train)-X_train.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_train, y_pred))
print('MSE:',metrics.mean_squared_error(y_train, y_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_train, y_pred)))
# Visualizing the differences between obs and pre; train data
plt.scatter(y_train, y_pred)
plt.xlabel("obs_logpgv")
plt.ylabel("Pre_logpgv")
plt.title("obs and pre of train data")
plt.show()
# Checking residuals
plt.scatter(y_pred,y_train-y_pred)
plt.title("Predicted vs residuals")
plt.xlabel("Predicted")
plt.ylabel("Residuals")
plt.show()
# Predicting Test data with the model
y_test_pred = reg.predict(X_test)
# Model Evaluation
acc_svm = metrics.r2_score(y_test, y_test_pred)
print('R^2:', acc_svm)
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_test, y_test_pred))*(len(y_test)-1)/(len(y_test)-X_test.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_test, y_test_pred))
print('MSE:',metrics.mean_squared_error(y_test, y_test_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
# Visualizing the differences between obs and pre; test data
plt.scatter(y_test, y_test_pred)
plt.xlabel("obs_logpgv")
plt.ylabel("Pre_logpgv")
plt.title("obs and pre of test data")
plt.show()<jupyter_output><empty_output><jupyter_text>R-squared score<jupyter_code>models = pd.DataFrame({
    'Model': ['Linear Regression', 'Random Forest', 'XGBoost', 'Support Vector Machines'],
    'R-squared Score': [acc_linreg*100, acc_rf*100, acc_xgb*100, acc_svm*100]})
models.sort_values(by='R-squared Score', ascending=False)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup