<jupyter_start><jupyter_text>LDA  plot for IRIS dataset<jupyter_code>'''Linear Discriminant Analysis or Normal Discriminant Analysis or Discriminant Function Analysis is a dimensionality reduction 
technique which is commonly used for the supervised classification problems.'''

# Importing Datasets From Sklearn

import matplotlib.pyplot as plt

from sklearn import datasets

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA

# Loading IRIS Dataset 

iris = datasets.load_iris()

X = iris.data
y = iris.target
target_names = iris.target_names
# fitting the LDA model
lda = LDA(n_components=2)
lda_X = lda.fit(X,y).transform(X)<jupyter_output><empty_output><jupyter_text>LDA Cluster plot<jupyter_code>plt.scatter(lda_X[y == 0, 0], lda_X[y == 0, 1], s =80, c = 'orange', label = 'Iris-setosa')
plt.scatter(lda_X[y == 1, 1], lda_X[y == 1, 0], s =80,  c = 'yellow', label = 'Iris-versicolour')
plt.scatter(lda_X[y == 2, 0], lda_X[y == 2, 1], s =80,  c = 'green', label = 'Iris-virginica')
plt.title('LDA plot for Iris Dataset')
plt.legend()
'''Assigning colors for graph'''

plt.figure()
colors = ['orange', 'yellow', 'green'] 
lw = 2
# Plotting the graph for LDA (IRIS dataset)

for color, i, target_name in zip(colors, [0, 1, 2], target_names):
    plt.scatter(lda_X[y == i, 0], lda_X[y == i, 1], alpha=.8, color=color,
                label=target_name)
plt.legend(loc='best', shadow=False, scatterpoints=1)
plt.title('LDA of IRIS dataset')

plt.show()
from sklearn.model_selection import train_test_split
from sklearn import neighbors, datasets, preprocessing
Xtrain, Xtest, y_train, y_test = train_test_split(X, y)
scaler = preprocessing.StandardScaler().fit(Xtrain)
Xtrain = scaler.transform(Xtrain)
Xtest = scaler.transform(Xtest)

clf = LDA()
clf.fit(Xtrain,y_train)
y_pred=clf.predict(Xtest)

y_pred

from sklearn.metrics import accuracy_score

print('Accuracy Score:', accuracy_score(y_test, y_pred))
                                   
from sklearn.metrics import cohen_kappa_score
cluster = cohen_kappa_score(y_test, y_pred)
cluster

# Using knn model for accuracy
knn = neighbors.KNeighborsClassifier(n_neighbors=5)
knn.fit(Xtrain, y_train)
y_pred = knn.predict(Xtest)
y_pred
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

print('Accuracy Score:', accuracy_score(y_test, y_pred))
print('Confusion matrix \n',  confusion_matrix(y_test, y_pred))
print('Classification \n', classification_report(y_test, y_pred))

from sklearn.metrics import cohen_kappa_score
cluster = cohen_kappa_score(y_test, y_pred)
cluster

Xtrain, Xtest, y_train, y_test = train_test_split(X, y)<jupyter_output><empty_output><jupyter_text>Logistic Regression Accuracy<jupyter_code>#Logistic Regression
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score

from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(Xtrain,y_train)
y_pred = classifier.predict(Xtest)
cm = confusion_matrix(y_test,y_pred)
accuracy = accuracy_score(y_test,y_pred)
print("Logistic Regression :")
print("Accuracy = ", accuracy)
print(cm)<jupyter_output>Logistic Regression :
Accuracy =  0.9210526315789473
[[13  0  0]
 [ 0 12  3]
 [ 0  0 10]]<jupyter_text>Cohen Kappa Accuracy for LR<jupyter_code>from sklearn.metrics import cohen_kappa_score
cluster = cohen_kappa_score(y_test, y_pred)
cluster<jupyter_output><empty_output><jupyter_text>K-Nearest Neighbors Accuracy<jupyter_code>#K Nearest Neighbors
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=5, p=2, metric='minkowski')
classifier.fit(Xtrain,y_train)
y_pred = classifier.predict(Xtest)
cm = confusion_matrix(y_test,y_pred)
accuracy = accuracy_score(y_test,y_pred)
print("K Nearest Neighbors :")
print("Accuracy = ", accuracy)
print(cm)<jupyter_output>K Nearest Neighbors :
Accuracy =  0.9473684210526315
[[13  0  0]
 [ 0 14  1]
 [ 0  1  9]]<jupyter_text>Cohen Kappa Accuracy for KNN<jupyter_code>from sklearn.metrics import cohen_kappa_score
cluster = cohen_kappa_score(y_test, y_pred)
cluster<jupyter_output><empty_output><jupyter_text>Support Vector Machine Accuracy<jupyter_code>#Support Vector Machine
from sklearn.svm import SVC
classifier = SVC()
classifier.fit(Xtrain,y_train)
y_pred = classifier.predict(Xtest)
cm = confusion_matrix(y_test,y_pred)
accuracy = accuracy_score(y_test,y_pred)
print("Support Vector Machine:")
print("Accuracy = ", accuracy)
print(cm)<jupyter_output>Support Vector Machine:
Accuracy =  0.9736842105263158
[[13  0  0]
 [ 0 14  1]
 [ 0  0 10]]<jupyter_text>Cohen Kappa Accuracy for SVM<jupyter_code>from sklearn.metrics import cohen_kappa_score
cluster = cohen_kappa_score(y_test, y_pred)
cluster<jupyter_output><empty_output><jupyter_text>Gaussian Naive Bayes Accuracy<jupyter_code>#Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(Xtrain,y_train)
y_pred = classifier.predict(Xtest)
cm = confusion_matrix(y_test,y_pred)
accuracy = accuracy_score(y_test,y_pred)
print("Gaussian Naive Bayes :")
print("Accuracy = ", accuracy)
print(cm)<jupyter_output>Gaussian Naive Bayes :
Accuracy =  0.8947368421052632
[[13  0  0]
 [ 0 12  3]
 [ 0  1  9]]<jupyter_text>Cohen Kappa Accuracy for GNB<jupyter_code>from sklearn.metrics import cohen_kappa_score
cluster = cohen_kappa_score(y_test, y_pred)
cluster<jupyter_output><empty_output><jupyter_text>Decision Tree Classifier Accuracy<jupyter_code>#Decision Tree Classifier
from sklearn.model_selection import train_test_split
Xtrain, Xtest, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

from sklearn.tree import DecisionTreeClassifier as DT
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score

classifier = DT(criterion='entropy', random_state=0)
classifier.fit(Xtrain,y_train)
y_pred = classifier.predict(Xtest)
cm = confusion_matrix(y_test,y_pred)
print("Decision Tree Classifier :")
print("Accuracy = ", accuracy)
print(cm)<jupyter_output>Decision Tree Classifier :
Accuracy =  0.8947368421052632
[[16  0  0]
 [ 0 17  1]
 [ 0  0 11]]<jupyter_text>Cohen Kappa Accuracy for DTC<jupyter_code>from sklearn.metrics import cohen_kappa_score
cluster = cohen_kappa_score(y_test, y_pred)
cluster<jupyter_output><empty_output><jupyter_text>Random Forest Classifier Accuracy<jupyter_code>#Random Forest Classifier
from sklearn.ensemble import RandomForestClassifier as RF
classifier = RF(n_estimators=10, criterion='entropy', random_state=0)
classifier.fit(Xtrain,y_train)
y_pred = classifier.predict(Xtest)
cm = confusion_matrix(y_test,y_pred)
print("Random Forest Classifier :")
print("Accuracy = ", accuracy)
print(cm)<jupyter_output>Random Forest Classifier :
Accuracy =  0.8947368421052632
[[16  0  0]
 [ 0 17  1]
 [ 0  2  9]]<jupyter_text>Cohen Kappa Accuracy for RFC<jupyter_code>from sklearn.metrics import cohen_kappa_score
cluster = cohen_kappa_score(y_test, y_pred)
cluster<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10331632653061225, 'fraction_numerical': 0.02947845804988662, 'mean_word_length': 7.046750285062714, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '578054', 'license': 'MIT', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2550, 'n_words': 564, 'path': 'Mtech_Iris_LDA.ipynb', 'repo_name': 'Ayantika22/LDA-Linear-discriminant-Analysis--clustering-visualization-for-Iris-dataset', 'size': '7056'}