<jupyter_start><jupyter_text>Importing all the required libraries<jupyter_code>import numpy as np # for linear algebra
import pandas as pd # for data processing and CSV file input/output (like pd.read_csv, pd.DataFrame ...)
import seaborn as sns # another library
import matplotlib.pyplot as plt # to make plots from the data
sns.set(style="white", color_codes=True) # using white style with color codes in seaborn
import warnings # to ignore any version related warnings generated by seaborn
warnings.filterwarnings("ignore")
from sklearn.datasets import load_iris
iris = load_iris() #loading dataset
X = iris.data[:,:4]
y = iris.target #updating species data into y
X
y
# transform the raw data from the dataset into a dataframe
df = pd.DataFrame(iris.data, columns = iris.feature_names)
df['species'] = iris.target
df.head(100) #printing first few records
df.shape
df.isnull().sum()
df.corr() #identifying correlation
df.describe() #description of data
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   sepal length (cm)  150 non-null    float64
 1   sepal width (cm)   150 non-null    float64
 2   petal length (cm)  150 non-null    float64
 3   petal width (cm)   150 non-null    float64
 4   species            150 non-null    int64  
dtypes: float64(4), int64(1)
memory usage: 6.0 KB<jupyter_text>Data Visualization<jupyter_code>## from sklearn docucmentation

from mpl_toolkits.mplot3d import Axes3D
from sklearn.decomposition import PCA

x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5

plt.figure(2, figsize=(8, 6))
plt.clf()

# Plot the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Set1,
            edgecolor='k')
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')

plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())

# To getter a better understanding of interaction of the dimensions
# plot the first three PCA dimensions
fig = plt.figure(1, figsize=(8, 6))
ax = Axes3D(fig, elev=-150, azim=110)
X_reduced = PCA(n_components=3).fit_transform(iris.data)
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=y,
           cmap=plt.cm.Set1, edgecolor='k', s=40)
ax.set_title("First three PCA directions")
ax.set_xlabel("1st eigenvector")
ax.w_xaxis.set_ticklabels([])
ax.set_ylabel("2nd eigenvector")
ax.w_yaxis.set_ticklabels([])
ax.set_zlabel("3rd eigenvector")
ax.w_zaxis.set_ticklabels([])

plt.show()

# a scatter plot for IRIS features

df.plot(kind="scatter", x="sepal length (cm)", y="sepal width (cm)")
# making use of seaborn jointplot to make bivariate scatterplots and univariate histograms and join them in the same figure

sns.jointplot(x="sepal length (cm)", y="sepal width (cm)", data=df, size=8)<jupyter_output><empty_output><jupyter_text>Seperating the data into X and y<jupyter_code>X=df.iloc[:, :-1]
X.head()
y=df.iloc[:,-1]
y.head()
# (Standardizing) Scaling the data to be between -1 and 1
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X)
X = scaler.transform(X)<jupyter_output><empty_output><jupyter_text>Train-test split on the data<jupyter_code>from sklearn.model_selection import train_test_split as tts
X_train, X_test, y_train, y_test = tts(X, y, test_size=0.3, random_state=0)<jupyter_output><empty_output><jupyter_text>Using a KNN model to fit and then make predictions<jupyter_code>from sklearn.neighbors import KNeighborsClassifier as knc
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.model_selection import cross_val_score
# Instantiate learning model (k = 9)
classifier = knc(n_neighbors=9)

# Fitting the model
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)
con_matrix = confusion_matrix(y_test, y_pred)
con_matrix
accuracy = accuracy_score(y_test, y_pred)*100
print('The model accurace is ' + str(round(accuracy, 2)) + ' %.')<jupyter_output>The model accurace is 97.78 %.<jupyter_text>Cross validation for model validation<jupyter_code># creating list of K for KNN
k_list = list(range(1,50,2))
# creating list of cv scores
cv_scores = []

# perform 10-fold cross validation
for k in k_list:
    knn = knc(n_neighbors=k)
    scores = cross_val_score(knn, X_train, y_train, cv=10, scoring='accuracy')
    cv_scores.append(scores.mean())
# changing to misclassification error
MSE = [1 - x for x in cv_scores]

plt.figure()
plt.figure(figsize=(15,10))
plt.title('The optimal number of neighbors', fontsize=20, fontweight='bold')
plt.xlabel('Number of Neighbors K', fontsize=15)
plt.ylabel('Misclassification Error', fontsize=15)
sns.set_style("whitegrid")
plt.plot(k_list, MSE)

plt.show()

# finding best k
best_k = k_list[MSE.index(min(MSE))]
print("The optimal number of neighbors is %d." % best_k)
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from scipy.stats import uniform, truncnorm, randint<jupyter_output><empty_output><jupyter_text>Randomized Search CV in KNN Classifier<jupyter_code>from sklearn.model_selection import RandomizedSearchCV
from sklearn.datasets import load_iris
iris = load_iris()
X=iris.data
y=iris.target
k_range=list(range(1,31))
options=['uniform', 'distance']
param_dist = dict(n_neighbors=k_range, weights=options)
knn = knc(n_neighbors = 1)
rand = RandomizedSearchCV(knn, param_dist, cv=10, scoring='accuracy', n_iter=10, random_state=5)
rand.fit(X, y)
print(rand.best_score_)
print(rand.best_params_)
best_scores=[]
for i in range(20):
    rand = RandomizedSearchCV(knn, param_dist, cv=10, scoring='accuracy', n_iter=10)
    rand.fit(X, y)
    best_scores.append(round(rand.best_score_,3))
print(best_scores)<jupyter_output>0.9800000000000001
{'weights': 'uniform', 'n_neighbors': 18}
[0.98, 0.98, 0.973, 0.973, 0.973, 0.98, 0.973, 0.98, 0.98, 0.973, 0.98, 0.98, 0.98, 0.98, 0.973, 0.973, 0.98, 0.98, 0.973, 0.98]<jupyter_text>SVM Classifier to make predictions<jupyter_code>from sklearn.metrics import mean_squared_error
from math import sqrt
from sklearn.svm import SVC
s1 = SVC(kernel='rbf', random_state=0, gamma=.10, C=1.0)
s1.fit(X_train,y_train)
from sklearn.model_selection import cross_val_score
svc=SVC(kernel='rbf',gamma=0.01)
scores = cross_val_score(svc, X, y, cv = 10, scoring = 'accuracy')
print(scores)
print(scores.mean())
svc=SVC(kernel='rbf')
scores = cross_val_score(svc, X, y, cv=10, scoring='accuracy') #cv is cross validation
print(scores)
print(scores.mean())
print(s1.score(X_train,y_train)) #accuracy of training data
print(s1.score(X_test,y_test)) #accuracy of test data
p=s1.predict(X_test)
#calculation of root mean square error
rmse= sqrt(mean_squared_error(p, y_test)) 
print(rmse)
from sklearn.metrics import classification_report
print(classification_report(y_test,p))
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_test,p))<jupyter_output>[[16  0  0]
 [ 0 17  1]
 [ 0  0 11]]<jupyter_text>Grid Search CV in SVM Classifier for hyperparamter optimization<jupyter_code>#Tuning SVM hyperparameters by using GridSearchCV with cross validation
from sklearn.model_selection import GridSearchCV
param_grid = {'C':[0.1,1,10,100], 'gamma':[1,0.1,0.01,0.001]}
g1 = GridSearchCV(SVC(), param_grid, refit = True, verbose=3)
g1.fit(X_train, y_train)
p2= g1.predict(X_test)
print(classification_report(y_test, p2))
#default RBF kernel
from sklearn import metrics
svc=SVC(kernel='rbf')
svc.fit(X_train,y_train)
p3=svc.predict(X_test)
print('Accuracy Score:')
print(metrics.accuracy_score(y_test,p3))
#taking different gamma values for rbf kernel
gamma_range=[0.0001,0.001,0.01,0.1,1,10,100]
acc_score=[]
for g in gamma_range:
    svc = SVC(kernel='rbf', gamma=g)
    scores = cross_val_score(svc, X, y, cv=10, scoring='accuracy')
    acc_score.append(scores.mean())
print(acc_score)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13003868713340822, 'fraction_numerical': 0.037813552976413325, 'mean_word_length': 5.70066889632107, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '512447', 'license': 'MIT', 'n_tokens_mistral': 3186, 'n_tokens_neox': 2943, 'n_words': 748, 'path': 'Assignments/KNN_and_SVM_with_IRIS_dataset.ipynb', 'repo_name': 'ritvik-chebolu/RIT-DSCI-633-FDS', 'size': '8013'}