<jupyter_start><jupyter_text>PCA Clustering For Iris Dataset<jupyter_code>#Importing libraries from SKLEARN
import matplotlib.pyplot as plt

from sklearn import datasets
from sklearn.decomposition import PCA
iris = datasets.load_iris()

X = iris.data
y = iris.target
target_names = iris.target_names

pca = PCA(n_components=3)
X_r = pca.fit(X).transform(X)<jupyter_output><empty_output><jupyter_text>PCA Cluster plot<jupyter_code>plt.scatter(X_r[y == 0, 0], X_r[y == 0, 1], s =80, c = 'orange', label = 'Iris-setosa')
plt.scatter(X_r[y == 1, 1], X_r[y == 1, 0], s =80,  c = 'yellow', label = 'Iris-versicolour')
plt.scatter(X_r[y == 2, 0], X_r[y == 2, 1], s =80,  c = 'green', label = 'Iris-virginica')
plt.title('PCA plot for Iris Dataset')
plt.legend()
from sklearn.model_selection import train_test_split
from sklearn import neighbors, datasets, preprocessing
X = iris.data
y = iris.target
Xtrain, Xtest, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
scaler = preprocessing.StandardScaler().fit(Xtrain)
Xtrain = scaler.transform(Xtrain)
Xtest = scaler.transform(Xtest)
# 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, test_size=0.2, random_state=0)<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.9666666666666667
[[11  0  0]
 [ 0 12  1]
 [ 0  0  6]]<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.9666666666666667
[[11  0  0]
 [ 0 12  1]
 [ 0  0  6]]<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
Xtrain, Xtest, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)<jupyter_output><empty_output><jupyter_text>Support Vector Machine Accuracy<jupyter_code>#Support Vector Machine
from sklearn.svm import SVC
classifier = SVC(kernel='linear',random_state=123)
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.9733333333333334
[[21  0  0]
 [ 0 29  1]
 [ 0  1 23]]<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.9466666666666667
[[21  0  0]
 [ 0 30  0]
 [ 0  4 20]]<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


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

classifier = DT(criterion='entropy',max_depth=3,  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.9466666666666667
[[21  0  0]
 [ 0 29  1]
 [ 0  2 22]]<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.9466666666666667
[[21  0  0]
 [ 0 29  1]
 [ 0  7 17]]<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.10371692906995618, 'fraction_numerical': 0.03424768706378835, 'mean_word_length': 7.815450643776824, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, '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': '46950', 'license': 'MIT', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2260, 'n_words': 473, 'path': 'Mtech PCA iris.ipynb', 'repo_name': 'Ayantika22/PCA-Iris-Clustering-Visualization', 'size': '6161'}