-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomForestClassifier.py
More file actions
86 lines (84 loc) · 3.11 KB
/
RandomForestClassifier.py
File metadata and controls
86 lines (84 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from warnings import filterwarnings
filterwarnings('ignore')
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.metrics import mean_absolute_error,r2_score,accuracy_score,classification_report,confusion_matrix
import pickle
df=sns.load_dataset('tips')
print(df.head())
# we will predict time based on other info
print(df.time.unique())
# Feature Engineering: Handling Missing values, categorical features, outliers, feature scaling, Automate this process
print(df.info())
from sklearn.preprocessing import LabelEncoder
encoder=LabelEncoder()
df['time']=encoder.fit_transform(df[['time']])
print(df.head())
print(df.time.value_counts())
# Independent and dependent features
X=df.drop(labels=['time'],axis=1)
y=df['time']
print(X.head())
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
categorical_cols=['sex', 'smoker', 'day']
numerical_cols=['total_bill','tip','size']
# Feature ENgineering Pipeline
num_pipeline=Pipeline(
steps=[
('imputer',SimpleImputer(strategy='median')),#missing value
('scaler',StandardScaler())#feature scaling
]
)
cat_pipeline=Pipeline(
steps=[
('imputer',SimpleImputer(strategy='most_frequent')),#missing value
('onehotencoder',OneHotEncoder())#categorical feature to numerical
]
)
preprocessor=ColumnTransformer([
('num_pipeline',num_pipeline,numerical_cols),
('cat_pipeline',cat_pipeline,categorical_cols)
])
X_train=preprocessor.fit_transform(X_train)
X_test=preprocessor.transform(X_test)
from sklearn.ensemble import RandomForestClassifier
# Automate Model training process
models={
'Random Forest':RandomForestClassifier(),
'Decision Tree': DecisionTreeClassifier(),
'SVC': SVC()
}
from sklearn.metrics import accuracy_score
def evaluate_model(X_train,y_train,X_test,y_test,models):
report={}
for i in range(len(models)):
model=list(models.values())[i]
# Train model
model.fit(X_train,y_train)
y_pred=model.predict(X_test)
test_model_score=accuracy_score(y_test,y_pred)
report[list(models.keys())[i]]=test_model_score
return report
print(evaluate_model(X_train,y_train,X_test,y_test,models))
classifier=RandomForestClassifier()
# Hyperparameter tuning
params={'max_depth':[3,5,10,'None'],
'n_estimators':[100,200,300],
'criterion':['gini','entropy']}
from sklearn.model_selection import RandomizedSearchCV
cvf=RandomizedSearchCV(classifier,param_distributions=params,cv=5,scoring='accuracy',verbose=3)
cvf.fit(X_train,y_train)
print(cvf.best_params_)