PCa $ Image processing
PCa $ Image processing
x, y = data.data, data.target
plt.show()
pca = PCA(n_components=50)
x_reduced = pca.fit_transform(x)
lg.fit(x_train, y_train)
predicted = lg.predict(x_test)
print(accuracy)
1. Importing Libraries
python
Copy
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
fetch_openml: Used to download datasets from OpenML
train_test_split: For splitting data into training and test sets
LogisticRegression: The classification model we'll use
accuracy_score: To evaluate model performance
PCA: For dimensionality reduction
matplotlib.pyplot: For visualizing the digits
Expected Output
The code will:
import numpy as np
image = Image.open(image_path)
grayscale_image = resized_image.convert("L")
image_df = pd.DataFrame(image_array)
plt.title("Original Image")
plt.imshow(image)
plt.show()
plt.imshow(grayscale_image, cmap="gray")
plt.show()
Copy
import pandas as pd
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
pandas (pd): For creating DataFrames (though not strictly
necessary for this operation)
Copy
image_path = "C:/Users/GeeKs/Desktop/dagi/pictures/dagi.jpg"
Defines the path to the image file (note there's a duplicate
assignment)
Uses Windows path format with forward slashes (also works with
raw strings or double backslashes)
Copy
image = Image.open(image_path)
Opens the image file using PIL's Image.open()
Copy
5. Converting to Grayscale
python
Copy
grayscale_image = resized_image.convert("L")
Converts the color image to 8-bit grayscale (mode "L")
Copy
image_array = np.array(grayscale_image)
Converts the PIL Image object to a NumPy array
Creates a 28×28 2D array where each element is a pixel intensity
value
Copy
image_df = pd.DataFrame(image_array)
Converts the NumPy array to a pandas DataFrame
Copy
plt.title("Original Image")
plt.imshow(image)
plt.show()
Second block:
o Shows the processed grayscale image
Expected Output
When you run this code, you'll see two popup windows showing:
Possible Improvements
1. Normalization: Add pixel value normalization (divide by 255)