regression method code example
Example 1: regression model
knn=KNeighborsRegressor()
svr=SVR()
lr=LinearRegression()
dt=DecisionTreeRegressor()
gbm=GradientBoostingRegressor()
ada=AdaBoostRegressor()
rfr=RandomForestRegressor()
xgb=XGBRegressor()
------------------------------------------------------------------------
models=[]
models.append(('KNeighborsRegressor',knn))
models.append(('SVR',svr))
models.append(('LinearRegression',lr))
models.append(('DecisionTreeRegressor',dt))
models.append(('GradientBoostingRegressor',gbm))
models.append(('AdaBoostRegressor',ada))
models.append(('RandomForestRegressor',rfr))
models.append(('XGBRegressor',xgb))
-------------------------------------------------------------------------
from sklearn.metrics import r2_score,mean_squared_error
from sklearn.model_selection import train_test_split,cross_val_score
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=42)
-------------------------------------------------------------------------
Model=[]
r2score=[]
rmse=[]
cv=[]
for name,model in models:
print('*****************',name,'*******************')
print('\n')
Model.append(name)
model.fit(x_train,y_train)
print(model)
pre=model.predict(x_test)
print('\n')
score=r2_score(y_test,pre)
print('R2score -',score)
r2score.append(score*100)
print('\n')
sc=cross_val_score(model,x,y,cv=5,scoring='r2').mean()
print('cross_val_score -',sc)
cv.append(sc*100)
print('\n')
rmsescore=np.sqrt(mean_squared_error(y_test,pre))
print('rmse_score -',rmsescore)
rmse.append(rmsescore)
print('\n')
------------------------------------------------------------------------
result=pd.DataFrame({'Model':Model,'R2_score':r2score,'RMSEscore':rmse,'Cross_val_score':cv})
result
Example 2: regression functions
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import AdaBoostRegressor
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.externals import joblib