<jupyter_start><jupyter_text>**Importing all the necessary libraries**<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns

import matplotlib.pyplot as plt

%matplotlib inline<jupyter_output><empty_output><jupyter_text>**Download iris dataset from sklearn**<jupyter_code>from sklearn.datasets import load_iris
data = load_iris()
df = pd.DataFrame(data.data, columns = data.feature_names)
df['target'] = data.target
df['label'] = df.target.replace(dict(enumerate(data.target_names)))
df.head()<jupyter_output><empty_output><jupyter_text>**Checking the correlation between each feature**<jupyter_code>plt.figure(1)
sns.heatmap(df.corr())
plt.title("Correlation of iris classes") 
# shows correlation between and all features
df.describe() # statistical summary for the given dataset<jupyter_output><empty_output><jupyter_text>**Checking for null values**<jupyter_code>df.isnull().sum()
''' Removing duplicates '''
df = df.drop_duplicates()
''' Distribution of each class is found '''
df.value_counts('label')
''' Comparing petal length with petal width '''
sns.scatterplot(x = 'petal length (cm)', y = 'petal width (cm)', hue = 'label', data = df)
'''The plot below shows that 'Setosa' has smaller petal length and petal width
The 'Versicolor' lies in the middle with petal length(3 - 5 cm) and petal width (1 - 1.5 cm)
The 'Viginica' has larger petal length and petal width
'''
''' Comparing sepal length with sepal width '''
sns.scatterplot(x = 'sepal length (cm)', y = 'sepal width (cm)', hue = 'label', data = df )
'''The plot below shows that 'Setosa' has lower sepal length but higher sepal width
Its harder to comment about the other species
'''<jupyter_output><empty_output><jupyter_text>**Data Standardisation**Standardization refers to shifting the distribution of each attribute to have a mean of zero and a standard deviation of one (unit variance). It is useful to standardize attributes for a model. Standardization of datasets is a common requirement for many machine learning estimators implemented in scikit-learn; they might behave badly if the individual features do not more or less look like standard normally distributed data.<jupyter_code>from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
'''Dataset is split into X and y (input and output respectively)'''
X = df[['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']]
y = df.target


print(X.shape[0], y.shape[0])
'''Applying standard scaling on features'''
scalar = StandardScaler()
X_scaled = scalar.fit_transform(X)<jupyter_output><empty_output><jupyter_text>**Splitting dataset into training set and testing set for better generalisation**<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<jupyter_output><empty_output><jupyter_text>**SVM classification**<jupyter_code>from sklearn import svm
clf = svm.SVC(kernel = 'rbf')
clf.fit(X_train, y_train)
print("Accuracy on training {:.2f}".format(clf.score(X_train, y_train)))
print("Accuracy on testing {:.2f}".format(clf.score(X_test, y_test)))<jupyter_output>Accuracy on training 0.97
Accuracy on testing 1.00<jupyter_text>**Root mean squared Error (RMSE) is used to compute the error**<jupyter_code>from sklearn import metrics
from math import sqrt
y_pred = clf.predict(X_test)
rmse = sqrt(metrics.mean_squared_error(y_test,y_pred))
print("RMSE: ", rmse)<jupyter_output>RMSE:  0.0<jupyter_text>**Cross validation**<jupyter_code>from sklearn.model_selection import cross_val_score
scores = cross_val_score(clf, X, y, cv = 5)
print(scores)<jupyter_output>[0.96666667 0.96666667 0.96666667 0.9        1.        ]<jupyter_text>**Tune SVM hyperparameters by using GridSearchCV**<jupyter_code>from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
param_grid = {'C': [0.1, 1, 10, 100, 1000], 'gamma': [1, 0.1, 0.01, 0.001, 0.0001]}
grid = GridSearchCV(clf, param_grid, verbose = 2, cv = 5)
grid.fit(X_train, y_train)
grid_pred = grid.predict(X_test)
print(classification_report(y_test, grid_pred))
rmse_grid = sqrt(metrics.mean_squared_error(y_test,grid_pred))
print('RMSE on GridSearchCV:', rmse_grid)
print(grid.best_params_)
print(grid.best_estimator_)<jupyter_output>SVC(C=1000, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma=0.001, kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)<jupyter_text>Hyperparameter value for C: 1000Hyperparameter value for gamma: 0.001Accuracy on training 0.97Accuracy on testing 1.00   KNN classification<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn_clf = KNeighborsClassifier(n_neighbors = 9)
knn_clf.fit(X_train, y_train)
print("Accuracy on training {:.2f}".format(knn_clf.score(X_train, y_train)))
print("Accuracy on testing {:.2f}".format(knn_clf.score(X_test, y_test)))
y_pred_knn=knn_clf.predict(X_test)
print(metrics.accuracy_score(y_test, y_pred_knn))
scores_knn = cross_val_score(knn_clf, X, y, cv = 5, scoring='accuracy')
print(scores_knn.mean())
from sklearn.model_selection import RandomizedSearchCV
k_range = list(range(1,31))
param_grid_knn = dict(n_neighbors = k_range)
print(param_grid_knn)
options = ['uniiform', 'distance']
random_search = RandomizedSearchCV(knn_clf, param_grid_knn, cv = 10, scoring = 'accuracy', random_state=42)
random_search.fit(X_train, y_train)
print(random_search.best_score_)
print(random_search.best_params_)<jupyter_output>{'n_neighbors': 16}
{'fraction_non_alphanumeric': 0.11836734693877551, 'fraction_numerical': 0.022892635314995562, 'mean_word_length': 7.251830161054173, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '534495', 'license': 'MIT', 'n_tokens_mistral': 2060, 'n_tokens_neox': 1922, 'n_words': 503, 'path': 'Assignments/Assignment_4.ipynb', 'repo_name': 'niranjana1997/RIT-DSCI-633-FDS', 'size': '5635'}