|
| 1 | +#Understanding the dataset |
| 2 | +# Importing the libraries |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +import pandas as pd |
| 6 | +import matplotlib |
| 7 | +# Importing the dataset |
| 8 | +dataset = pd.read_csv('/content/iris data.csv') |
| 9 | +print(dataset) |
| 10 | +print(dataset.shape) |
| 11 | +x=dataset.iloc[:,0:4] |
| 12 | +y=dataset['iris_class'] |
| 13 | +print(x) |
| 14 | +print(y) |
| 15 | + |
| 16 | +#Train test split for dummy classifiers |
| 17 | +from sklearn.linear_model import LogisticRegression |
| 18 | +from sklearn import metrics |
| 19 | +from sklearn.model_selection import train_test_split |
| 20 | + |
| 21 | +x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=10) |
| 22 | + |
| 23 | +#Using dummy classifiers |
| 24 | +from sklearn.dummy import DummyClassifier |
| 25 | +from sklearn.metrics import confusion_matrix |
| 26 | +from sklearn.metrics import classification_report |
| 27 | +clf_dummy = DummyClassifier(random_state=42) |
| 28 | + |
| 29 | +clf_dummy.fit(x_train, y_train) |
| 30 | +y_pred = clf_dummy.predict(x_test) #uniform parameter |
| 31 | +print('Accuracy of dummy classifier on test set: {:.2f}'.format(clf_dummy.score(x_test, y_test)*100)+ '%') |
| 32 | + |
| 33 | +confusion_matrix=confusion_matrix(y_test, y_pred) |
| 34 | +print(confusion_matrix) |
| 35 | + |
| 36 | +report = classification_report(y_test, y_pred) |
| 37 | +print(report) |
| 38 | + |
| 39 | +#Train test split for SVM |
| 40 | +from sklearn.linear_model import LogisticRegression |
| 41 | +from sklearn import metrics |
| 42 | +from sklearn.model_selection import train_test_split |
| 43 | + |
| 44 | +x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=10) |
| 45 | + |
| 46 | +#Applying 4 kernal functions |
| 47 | +from sklearn.svm import SVC |
| 48 | +from sklearn.metrics import classification_report, confusion_matrix |
| 49 | +import matplotlib.pyplot as plt |
| 50 | + |
| 51 | +kernels = ['Polynomial', 'RBF', 'Sigmoid','Linear']#A function which returns the corresponding SVC model |
| 52 | +def getClassifier(ktype): |
| 53 | + if ktype == 0: |
| 54 | + # Polynomial kernal |
| 55 | + return SVC(kernel='poly', degree=8, gamma="auto") |
| 56 | + elif ktype == 1: |
| 57 | + # Radial Basis Function kernal |
| 58 | + return SVC(kernel='rbf', gamma="auto") |
| 59 | + elif ktype == 2: |
| 60 | + # Sigmoid kernal |
| 61 | + return SVC(kernel='sigmoid', gamma="auto") |
| 62 | + elif ktype == 3: |
| 63 | + # Linear kernal |
| 64 | + return SVC(kernel='linear', gamma="auto") |
| 65 | + |
| 66 | +#SVC model |
| 67 | +for i in range(4): |
| 68 | + # Separate data into test and training sets |
| 69 | + x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state=10)# Train a SVC model using different kernal |
| 70 | + svclassifier = getClassifier(i) |
| 71 | + svclassifier.fit(x_train, y_train)# Make prediction |
| 72 | + y_pred = svclassifier.predict(x_test)# Evaluate our model |
| 73 | + print('Accuracy of SVM classifier on test set: {:.2f}'.format(svclassifier.score(x_test, y_test)*100)+ '%') |
| 74 | + print("Evaluation:", kernels[i], "kernel") |
| 75 | + print(classification_report(y_test,y_pred)) |
| 76 | + |
| 77 | +#GridsearchCV for Hyperparameter tuning |
| 78 | +from sklearn.model_selection import GridSearchCV |
| 79 | +param_grid = {'C': [0.1,1, 10, 100], 'gamma': [1,0.1,0.01,0.001],'kernel': ['rbf', 'poly', 'sigmoid']} |
| 80 | +grid = GridSearchCV(SVC(),param_grid,refit=True,verbose=2) |
| 81 | +grid.fit(x_train,y_train) |
| 82 | + |
| 83 | +print(grid.best_estimator_) #finding the optimal parameters |
| 84 | +print(grid.best_params_) |
| 85 | + |
| 86 | +grid_predictions = grid.predict(x_test) |
| 87 | +print(confusion_matrix(y_test,grid_predictions)) |
| 88 | +print(classification_report(y_test,grid_predictions)) |
0 commit comments