How to combine results of machine learning models without using voting classifier in python?

Description

To build a model for cloth classification using keras and CNN in python.

Input

Iris data set.

Output

 Classification report and accuracy of final combined model

  Import necessary libraries.

  Load the data set.

  Fix independent and dependent variables.

  Build the individual machine learning models.

  Train the individual model using train data.

  Test the model using test data.

  Combine the results of individual model results.

  Print the final model results.

#import libraries
import warnings
warnings.filterwarnings(“ignore”)
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import model_selection
from sklearn.model_selection import cross_val_score
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

#load data
data = pd.read_csv(‘……../iris.csv’)

#check missing values
print(“Checking missing values\n\n”,data.isnull().sum())

#make it as a data frame
df = pd.DataFrame(data)

#print data shape
print(“\n”)
print(“Shape of data\n\n”,df.shape)

#Feature extraction
#Define X and y variable
X = df.iloc[:,0:4]
y = df.iloc[:,4]

#Training and testing data using 80:20 rule
#Split train and test data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

#training set and testing set
print(“\n”)
print(“Training data set\n\n”,X_train,”\n”,y_train)
print(“\n”)
print(“Testing data set\n\n”,X_test)

#Building the model
#Naive-bayes
naive_bayes = MultinomialNB()
naive_bayes.fit(X_train,y_train)
y_pred1 = naive_bayes.predict(X_test)

#Evaluate the model
print(“\n”)
print(“Classification report for Naive_bayes\n”)
print(classification_report(y_test, y_pred1))
print(“Confusion matrix\n”)
print(confusion_matrix(y_test, y_pred1))
print(“\n”)

#SVM
svm = SVC(kernel=’linear’,probability=True)
svm.fit(X_train,y_train)
y_pred2 = svm.predict(X_test)

#Evaluate the model
print(“\n”)
print(“Classification report for SVM\n”)
print(classification_report(y_test, y_pred2))
print(“Confusion matrix\n”)
print(confusion_matrix(y_test, y_pred2))
print(“\n”)

#Random forest
rf = RandomForestClassifier()
#create a dictionary of all values we want to test for n_estimators
params_rf = {‘n_estimators': [50, 100, 200]}
#use gridsearch to test all values for n_estimators
rf_gs = GridSearchCV(rf, params_rf, cv=5)
#fit model to training data
rf_gs.fit(X_train, y_train)
rf_best = rf_gs.best_estimator_
y_pred3 = rf_best.predict(X_test)

#Evaluate the model
print(“\n”)
print(“Classification report for Random Forest\n”)
print(classification_report(y_test, y_pred3))
print(“Confusion matrix\n”)
print(confusion_matrix(y_test, y_pred3))
print(“\n”)

#Score for individual model
print(“Accuracy score for individual model\n”)
print(‘Naive Bayes: {}’.format(naive_bayes.score(X_test, y_test)))
print(‘SVM: {}’.format(svm.score(X_test, y_test)))
print(‘Random forest: {}’.format(rf_best.score(X_test, y_test)))

#################################Ensemble model######################

#store all predictions
frame = [y_pred1,y_pred2,y_pred3]
df3 = pd.DataFrame(frame)

#find prediction mode from all three classifiers
En_pred = df3.mode().loc[0]

print(“Confusion matrix of ensemble model\n”)
print(confusion_matrix(y_test, En_pred))
print(“\n”)

print(“Classification report of Ensemble model\n”)
print(classification_report(y_test, En_pred))

#test our model on the test data
print(“Accuracy score of ensemble model : “,accuracy_score(y_test, En_pred)*100)
print(“\n”)

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit