# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + id="RcBNimbbsjMx"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler 
from sklearn import datasets 
import warnings
warnings.filterwarnings('ignore')

# + id="FwEgnVEBzclV"
from sklearn.tree import  DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score, roc_auc_score
from six import StringIO 
from sklearn.tree import export
import sklearn
from sklearn.datasets import load_boston

# + id="nsoqKmX_zXiK"
boston = load_boston()
bos = pd.DataFrame(boston.data)

# + colab={"base_uri": "https://localhost:8080/", "height": 195} id="rENJkOnV0NgV" outputId="5f4fa533-4b6c-48a3-ce63-030422cb2b29"
bos.head()

# + colab={"base_uri": "https://localhost:8080/"} id="Dw_d4Op-0VST" outputId="fcfdf46e-8573-4e96-fb23-a2137d6f3654"
boston.feature_names

# + colab={"base_uri": "https://localhost:8080/"} id="VCNoj7Ao0Yis" outputId="00bf6fbb-5ab6-4a8e-8205-a197ac30df60"
boston.target

# + id="14mBlVqG0a7y"
bos.columns = boston.feature_names

# + colab={"base_uri": "https://localhost:8080/"} id="DSSuhSD30fxe" outputId="d3f94fbe-dac5-46b1-9fc7-96ddbe34d199"
bos.columns

# + id="8z9BRTzB0hns"
bos['Target'] = boston.target

# + colab={"base_uri": "https://localhost:8080/", "height": 402} id="Uf9UPLC70jDE" outputId="ac59d950-09e4-48d0-f8fa-ee38df2077e8"
bos

# + colab={"base_uri": "https://localhost:8080/", "height": 195} id="MaH1Hmgl0mup" outputId="31fd4993-71e2-4f74-dfe7-5256b50f1f69"
bos.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 195} id="8KpPKMWM0o-P" outputId="17b58c6d-bacd-4543-cdb1-a607736f7389"
bos.tail()

# + colab={"base_uri": "https://localhost:8080/", "height": 284} id="GVAcCwge0rol" outputId="6edb018e-fe86-43b1-d902-629457d852cb"
bos.describe()

# + colab={"base_uri": "https://localhost:8080/"} id="s4H47wtM0tn_" outputId="8e4075f5-ae42-449a-840b-b350f68e2a48"
bos.info()

# + colab={"base_uri": "https://localhost:8080/"} id="Ifp5RuY30vY4" outputId="4e212a29-bfe6-4fcb-d494-02236e97b603"
bos.isnull().sum()

# + colab={"base_uri": "https://localhost:8080/"} id="Ay2rHRXh0xFa" outputId="669758be-6af2-4827-9165-ab44cfb90aee"
bos.columns

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="LTHhnf-m0yu0" outputId="86f69f59-368d-42fb-8f0a-b0df4c447f09"
plt.figure(figsize=(20,25),facecolor='white')
plotnumber=1
for column in bos:
    if plotnumber<=14: 
        ax=plt.subplot(4,4,plotnumber)
        sns.distplot(bos[column])
        plt.xlabel(column,fontsize=20)
    plotnumber+=1
plt.tight_layout()

# + id="jW3v2um200UZ"
x = bos.drop(columns=['Target'])

# + colab={"base_uri": "https://localhost:8080/", "height": 402} id="-wRcogXE02iK" outputId="34ca3361-9a35-49bd-80d4-d88f93120acb"
x

# + id="cQsBCbZM04HL"
y = bos['Target']

# + colab={"base_uri": "https://localhost:8080/"} id="1wPpomLu05fX" outputId="67195a53-9606-4c5f-c288-ba06578796d7"
y

# + colab={"base_uri": "https://localhost:8080/"} id="ro2-K90y065I" outputId="f5878416-8b17-4f9b-8ada-f54a19cb10fa"
x.columns

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="IwbhAPtI0820" outputId="2e7bad97-6d2d-4b4d-a22b-dfa2d3bc9dad"
plt.figure(figsize=(20,30),facecolor='white')
plotnumber=1
for col in x:
    if plotnumber <=13:
        ax=plt.subplot(5,3,plotnumber)
        plt.scatter(x[col],y)
        plt.xlabel(col,fontsize=20)
        plt.ylabel('Target',fontsize=10)
    plotnumber+=1
plt.tight_layout()

# + id="9m8IlaSg0-6H"
scaler = StandardScaler()

# + id="O4l-2rA_2H3W"
x_scaled = scaler.fit_transform(x)

# + colab={"base_uri": "https://localhost:8080/"} id="oIsY48882Mdr" outputId="48a4b6fc-2c8f-4aab-f8aa-439bd107dbb3"
x_scaled

# + id="uvEgN7kj2NoU"
from statsmodels.stats.outliers_influence import variance_inflation_factor

# + id="ldKyHenP2PdC"
variables = x_scaled
vif=pd.DataFrame()
vif['VIF'] = [variance_inflation_factor(variables,i) for i in range(variables.shape[1])]
vif['Features'] = x.columns

# + colab={"base_uri": "https://localhost:8080/", "height": 432} id="iUeed4_12SJM" outputId="23b49533-353a-4d93-d52f-cebbfcf51ae4"
vif

# + id="wEjZ472L2UGD"
x = bos.drop(columns=['Target','RAD','TAX'])

# + id="eRBeQ8qU2XDh"
y = bos['Target']

# + id="gNfsahUP2Yiz"
scaler = StandardScaler()
x_scaled = scaler.fit_transform(x)
variables = x_scaled
vif=pd.DataFrame()
vif['VIF'] = [variance_inflation_factor(variables,i) for i in range(variables.shape[1])]
vif['Features'] = x.columns

# + colab={"base_uri": "https://localhost:8080/", "height": 373} id="CGQX4D3p2aj3" outputId="b5e4f5ac-5c46-4d39-ebc8-20dcd1cf3eb0"
vif

# + id="Dm-KxkVfAo17"
from sklearn.tree import  DecisionTreeClassifier, export_graphviz

# + id="ahjJm848Ao18"
x_train,x_test,y_train,y_test = train_test_split(x_scaled,y,test_size = 0.25,random_state = 355)

# + id="p0eWdOM0Ao19"
from sklearn.ensemble import RandomForestRegressor

# + id="pKhnBoWoAo19"
Random_clf = RandomForestRegressor(n_estimators=50,random_state=0)

# + colab={"base_uri": "https://localhost:8080/"} id="ZJaytuGMAo1-" outputId="98ab3dd2-87cf-4e83-eaf7-a288edfce382"
Random_clf.fit(x_train,y_train)

# + id="bXrlstS9Ao2H"
y_pred = Random_clf.predict(x_test)

# + colab={"base_uri": "https://localhost:8080/"} id="c0sW4UNKAo2I" outputId="18712584-88f0-44a1-a4ed-d2529a544345"
y_pred

# + colab={"base_uri": "https://localhost:8080/"} id="bQuoEiKaAo2J" outputId="724aefcb-6ef8-4d72-9a88-d83fe06f0847"
Random_clf.score(x_train,y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="QrEjOvsUAo2J" outputId="d69695e5-a85d-4314-d687-d3a718c64a1c"
Random_clf.score(x_test,y_test)

# + id="cEmTXSaBAo2L"
grid_param = {
    "n_estimators" : [90,100,115,130],
    'criterion': ["mse", "mae"],
    'max_depth' : range(2,20,1),
    'min_samples_leaf' : range(1,10,1),
    'min_samples_split': range(2,10,1),
    'max_features' : ['auto','log2']
}

# + id="Nh2WKTnQC7z6"
grid_search = GridSearchCV(estimator=Random_clf,param_grid=grid_param,cv=5,n_jobs=-1,verbose=3)

# + colab={"base_uri": "https://localhost:8080/"} id="mx9viM6UAo2M" outputId="072237da-da1f-4481-a68b-00789f9c27b3"
grid_search.fit(x_train,y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="LgubbcimI13q" outputId="f925ec63-f483-471b-e7f8-ed39a47ffe4f"
grid_search.best_params_

# + id="jFevIzxlBLdp"
Random_clf = RandomForestRegressor(criterion = 'mse',max_depth = 14,max_features = 'log2',min_samples_leaf = 1,min_samples_split=2
                                 ,n_estimators=115,random_state=6)

# + colab={"base_uri": "https://localhost:8080/"} id="Bu9hupFeJBZx" outputId="f03e63f5-878a-4671-c003-2dfecf3f915d"
Random_clf.fit(x_train,y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="EirKC_U2JEUF" outputId="2b37fc4b-5c48-4746-bd5e-a504f960cdea"
Random_clf.score(x_train,y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="7Qe5tE2bJJVq" outputId="3088ef8f-38a4-4c19-d4ff-b7c1e20ae407"
Random_clf.score(x_test,y_test)

# + id="zCBBn1_rJQ11"
y_pred = Random_clf.predict(x_test)

# + colab={"base_uri": "https://localhost:8080/"} id="Iiq4elRVJajs" outputId="b4a53933-7a44-4c66-e8e5-861b1ffd9346"
y_pred

# + id="UOjnpQBeKFSc"
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score, roc_auc_score,mean_absolute_error

# + id="_B6XSx3HKY7h"
from sklearn import metrics

# + colab={"base_uri": "https://localhost:8080/"} id="1CqB96umJ5Qz" outputId="04b1abb7-26de-4ea4-eac3-bc7bb3c9af69"
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))  
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))  
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))

# + id="btY5YvhKJ_oH"


StarcoderdataJupyterScriptsDedupFiltered