<jupyter_start><jupyter_text>IAML LAB 4: CLUSTERING, PCA AND EVALUATION<jupyter_code>from scipy.io import arff
import pandas as pd
import numpy as np

from utils import update_plot_params, cv2df

from sklearn.cluster import KMeans
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import MultinomialNB, GaussianNB
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split, cross_validate
from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix, precision_recall_fscore_support

from IPython.display import HTML

%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns
sns.set(palette = "Set2")
update_plot_params()
cm = ListedColormap(sns.color_palette("RdYlGn", 30).as_hex())
styles = [
    dict(selector="th", props=[("font-size", "100%"),
                               ("text-align", "center")]),
    dict(selector="caption", props=[("caption-side", "bottom")])
]<jupyter_output><empty_output><jupyter_text>CLUSTERING<jupyter_code>data, _ = arff.loadarff('./data/raw/landsat.arff.txt')
landsat = pd.DataFrame(data)
landsat['class'] = landsat['class'].str.decode('utf-8')
landsat['class'].value_counts()
y = landsat.pop('class')
X = landsat
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)<jupyter_output><empty_output><jupyter_text>K-means<jupyter_code>kmeans = KMeans(n_clusters=6, random_state=0)
kmeans.fit(x_train)
kmeans_pred = kmeans.predict(x_test)
df = pd.DataFrame({'Labels': y_test, 'Clusters': kmeans_pred})
kmeans_conf_matrix = pd.crosstab(df['Labels'], df['Clusters'])

sns.heatmap(kmeans_conf_matrix, annot=True, fmt="d", cmap='Pastel1_r')
plt.ylabel('True label', size=20)
plt.xlabel('Predicted label', size=20)
plt.title('Confusion matrix', size=26)
plt.show()
noise1 = np.random.normal(0,0.1,df.shape[0])
noise2 = np.random.normal(0,0.1,df.shape[0])
noise = [[x, y] for x, y in zip(noise1, noise2)]

df = df.astype(int)
df = pd.DataFrame(df.values + noise, columns=df.columns, index=df.index)
plt.plot(df['Labels'], df['Clusters'], 'ko')
plt.title('Class against Cluster')
plt.show()<jupyter_output><empty_output><jupyter_text>PCA<jupyter_code>pca = PCA(n_components=2).fit(X)
components = pca.transform(X)
results = (pd.DataFrame(pca.components_,columns=X.columns,index = ['PC1','PC2']).T.style\
        .set_table_styles(styles)
        .background_gradient(cmap=cm))
print('Correlation between a particular pair of component and feature')
display(results)
df = pd.DataFrame(data=components, columns=['PC1', 'PC2'])
df = pd.concat([df, y], axis=1)

sns.lmplot(x='PC1', y='PC2', data=df, hue='class', fit_reg=False, size=10)
plt.title('PCA components against each other')
plt.show()<jupyter_output><empty_output><jupyter_text>Naive Bayes  Train on PC:<jupyter_code>naive_bayes = GaussianNB()
scores = cross_validate(naive_bayes, pd.DataFrame(components), y, 
                        scoring=['accuracy', 'precision_macro', 
                                 'recall_macro', 'f1_macro'], 
                        cv=5, return_train_score=True)                 
cv2df(scores, True, 6).T.style.set_table_styles(styles)<jupyter_output><empty_output><jupyter_text>Train on original data frame:<jupyter_code>naive_bayes = GaussianNB()
scores = cross_validate(naive_bayes, X, y, 
                        scoring=['accuracy', 'precision_macro', 
                                 'recall_macro', 'f1_macro'], 
                        cv=5, return_train_score=True)    
df = pd.DataFrame(scores)
df = df.append(df.mean(axis=0), ignore_index=True)
df.index.name = 'n_folds'
df.index = df.index + 1
df = df.rename(index={6: 'Mean'})
cv2df(scores, True, 6).T.style.set_table_styles(styles)<jupyter_output><empty_output><jupyter_text>PERFORMANCE ASSESSMENT 2<jupyter_code>train = pd.read_csv('./data/preprocessed/splice_train.csv')
test = pd.read_csv('./data/preprocessed/splice_test.csv')
y_train = train.pop('class')
x_train = train

y_test = test.pop('class')
x_test = test<jupyter_output><empty_output><jupyter_text>K-nearest neighbour<jupyter_code>knn_model = KNeighborsClassifier(n_neighbors=1000)
knn_model.fit(x_train, y_train) 
knn_pred = knn_model.predict(x_test)
knn_conf_matrix = confusion_matrix(y_test, knn_pred)
sns.heatmap(knn_conf_matrix, annot=True, fmt="d", cmap='Pastel1_r')
plt.ylabel('True label', size=20)
plt.xlabel('Predicted label', size=20)
plt.title('Confusion matrix', size=26)
plt.show()
knn_prfs_score = precision_recall_fscore_support(y_test, knn_pred)
knn_df = pd.DataFrame(data={'Precision': knn_prfs_score[0], 'Recall': knn_prfs_score[1], 
                            'F1 score': knn_prfs_score[2], 'Support': knn_prfs_score[3]},
                      columns=['Precision', 'Recall', 'F1 score', 'Support'], index=[0, 1, 2])

results = (knn_df.style.set_table_styles(styles))
results<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14539579967689822, 'fraction_numerical': 0.015145395799676898, 'mean_word_length': 5.41580310880829, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '167091', 'license': 'Apache-2.0', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1816, 'n_words': 306, 'path': 'Introductory Applied Machine Learning/Problems/Problem 4.ipynb', 'repo_name': 'roaffix/University', 'size': '4952'}