text_plain_output_1.png
from sklearn.ensemble import RandomForestRegressor

from sklearn.linear_model import LinearRegression

from sklearn.metrics import r2_score

from sklearn.model_selection import train_test_split

from sklearn.model_selection import train_test_split

from sklearn.neighbors import KNeighborsRegressor

from sklearn.preprocessing import StandardScaler

from sklearn.preprocessing import StandardScaler, MinMaxScaler

from sklearn.svm import SVR

from xgboost import XGBRegressor

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

GM_df = pd.read_excel('/kaggle/input/juan-anyang/GM.xlsx')
Mining_df = pd.read_excel('/kaggle/input/juan-anyang/Mining.xlsx')
GM_df.fillna(0, inplace=True)
Mining_df.fillna(0, inplace=True)

GM_df['Reading No'] = GM_df['Reading No'].astype(str)

Mining_df['Reading No'] = Mining_df['Reading No'].astype(str)

dummies_GM = pd.get_dummies(GM_df['Type'])
GM_df = pd.concat([GM_df, dummies_GM], axis=1)
GM_df.columns

dummies_Mining = pd.get_dummies(Mining_df['Type'])
Mining_df = pd.concat([Mining_df, dummies_Mining], axis=1)
Mining_df.columns

Mining_df[['A', 'B', 'C', 'D']] = Mining_df[['A', 'B', 'C', 'D']].astype(int)

from sklearn.model_selection import train_test_split
y = Mining_df.Level
features = ['Cu', 'Sn', 'Pb', 'P', 'S', 'Cl']
X = Mining_df[features]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

from sklearn.linear_model import LinearRegression
M_lrmodel = LinearRegression()
M_lrmodel.fit(X_train, y_train)
y_lrpred = M_lrmodel.predict(X_test)

from sklearn.svm import SVR
M_svrmodel = SVR(kernel='rbf')
M_svrmodel.fit(X_train, y_train)
y_svrpred = M_svrmodel.predict(X_test)

from sklearn.neighbors import KNeighborsRegressor
M_knnmodel = KNeighborsRegressor(n_neighbors=1)
M_knnmodel.fit(X_train, y_train)
y_knnpred = M_knnmodel.predict(X_test)

from sklearn.ensemble import RandomForestRegressor
M_rfmodel = RandomForestRegressor(n_estimators=20, random_state=0)
M_rfmodel.fit(X_train, y_train)
y_rfpred = M_rfmodel.predict(X_test)

from xgboost import XGBRegressor
M_xgbmodel = XGBRegressor()
M_xgbmodel.fit(X_train, y_train)
y_xgbpred = M_xgbmodel.predict(X_test)

from sklearn.metrics import r2_score
svr_R = metrics.r2_score(y_test, y_svrpred)
svr_a_R = 1 - (1 - svr_R) * (len(y_test) - 1) / (len(y_test) - X_test.shape[1] - 1)
print('Adjusted R Squared Value for SVR: ', round(svr_a_R, 3))
lr_R = r2_score(y_test, y_lrpred)
lr_a_R = 1 - (1 - lr_R) * (len(y_test) - 1) / (len(y_test) - X_test.shape[1] - 1)
print('Adjusted R Squared Value for Linear Regression: ', round(lr_a_R, 3))
rf_R = metrics.r2_score(y_test, y_rfpred)
rf_a_R = 1 - (1 - rf_R) * (len(y_test) - 1) / (len(y_test) - X_test.shape[1] - 1)
print('Adjusted R Squared Value for Random Forest: ', round(rf_a_R, 3))
knn_R = metrics.r2_score(y_test, y_knnpred)
knn_a_R = 1 - (1 - knn_R) * (len(y_test) - 1) / (len(y_test) - X_test.shape[1] - 1)
print('Adjusted R Squared Value for KNN: ', round(knn_a_R, 3))
xgb_R = metrics.r2_score(y_test, y_xgbpred)
xgb_a_R = 1 - (1 - xgb_R) * (len(y_test) - 1) / (len(y_test) - X_test.shape[1] - 1)
print('Adjusted R Squared Value for XGBoost: ', round(xgb_a_R, 3))