<jupyter_start><jupyter_text>Objective:• Understand and practice principal component analysis using scikit learn. Questions:1. Scikit learn comes with pre-loaded dataset, load the digits dataset from that collection and write a helper function to plot the image using matplotlib.[Hint: Explore datasets module from scikit learn]2. Make a train -test split with 20% of the data set aside for testing. Fit a logistic regression model and observe the accuracy.3. Using scikit learn perform a PCA transformation such that the transformed dataset can explain 95% of the variance in the original dataset. Find out the number of components in the projected subspace.[Hint: Refer to decomposition module of scikit learn]4. Transform the dataset and fit a logistic regression and observe the accuracy. Compare it with the previous model and comment on the accuracy.[Hint: Project both the train and test samples to the new subspace]5. Compute the confusion matrix and count the number of instances that has gone wrong. For each of the wrong sample, plot the digit along with predicted and original label.<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_digits
digits = load_digits()
type(digits.data)
digits.data.shape
plt.gray()
plt.matshow(digits.images[101])
plt.matshow(digits.images[1179])
plt.show()
dfdigits = pd.DataFrame(digits.data)
dfdigits.sample(10)
dfdigits.iloc[1179]
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
digits.target.shape
digits.target_names
X = digits.data
y = digits.target
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state = 1 )
X_train.shape
X_test.shape
logisticReg = LogisticRegression()
logisticReg.fit(X_train,y_train)
prediction1 = logisticReg.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy_score(y_test,prediction1)
df = pd.DataFrame({"Actual":y_test,"Prediction":prediction1})
df.shape
from sklearn.decomposition import PCA
pca = PCA(n_components=20)
pca.fit(digits.data)
pca.explained_variance_ratio_*100
transformeddf = pd.DataFrame(pca.transform(digits.data))
transformeddf.head()
plt.plot(pca.explained_variance_ratio_*100)
Xtrans = transformeddf
ytrans = digits.target
xtrain, xtest, ytrain, ytest = train_test_split(Xtrans ,digits.target, random_state=1)
logisticReg.fit(xtrain,ytrain)
newprediction = logisticReg.predict(xtest)
accuracy_score(ytest,newprediction)
from sklearn.metrics import confusion_matrix<jupyter_output><empty_output><jupyter_text>Confusion Matrix<jupyter_code>confusion_matrix(ytest,newprediction)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07320698625046451, 'fraction_numerical': 0.013749535488665924, 'mean_word_length': 6.848396501457726, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '206540', 'license': 'MIT', 'n_tokens_mistral': 857, 'n_tokens_neox': 812, 'n_words': 265, 'path': 'Cls5-Dimentionality Reduction/DimensionalityReduction-CaseStudy1-solution.ipynb', 'repo_name': 'tuhinssam/MLResources', 'size': '2691'}