0% found this document useful (0 votes)
1 views

digits_classification_ml

The document outlines a process for training a neural network using the MNIST dataset to classify handwritten digits, including data preprocessing steps such as flattening and scaling the images. It also describes the creation and training of a Support Vector Machine (SVM) classifier for the Iris dataset, allowing for user input to predict flower types based on measurements. The document includes visualizations and model evaluation methods for both classifiers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

digits_classification_ml

The document outlines a process for training a neural network using the MNIST dataset to classify handwritten digits, including data preprocessing steps such as flattening and scaling the images. It also describes the creation and training of a Support Vector Machine (SVM) classifier for the Iris dataset, allowing for user input to predict flower types based on measurements. The document includes visualizations and model evaluation methods for both classifiers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import tensorflow as tf

from tensorflow import keras


import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
(x_train,y_train),(x_test,y_test)=keras.datasets.mnist.load_data()
len(x_train) #no of images in training set
len(x_test) #no of images in test set
x_train[0].shape # output is (6000,28,28) 6000 sample each imsge is stored as 2d
array28*28 #how the image is stored
x_train[0]# a 2d array consists from 0 to 255 values
plt.matshow(x_train[0])# visulises the dataset

# we are flattening the dataset 28 rows and 28 cols to 1-d array


x_train_flattened=x_train.reshape(len(x_train),28*28)
x_train_flattened.shape
x_test_flattened=x_test.reshape(len(x_test),28*28)
x_test_flattened.shape
# create a neural network with 2 layers input and output layer
keras.sequential([
keras.layers.Dense[input_shape=(784, ),activation='sigmoid')
])
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
model.fit(x_train_flattened,y_train,epochs=5) # actually trainning happens
supplying the training set

# here accuracy is low


# values have to be scaled the values are between 0 to 255 the values are between
0 to 1
#each individual in the range of 0 to 255 divide each value with 255
x_train=x_train/255
x_test=x_test/255
# training of dataset is done
# now testing dataset

model.evaluate(x_test_flattened,y_test)
plt.matshow(x_test[0])
y_predicted=model.predict(x_test_flattened)
y_predicted[0]
#prints the possibilty of value from 0 to 10
np.argmax(y_predicted[0])#real prediction
#build confusion matrix
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Reshape the data to flatten the images


x_train_flattened = x_train.reshape(len(x_train), 28 * 28)
x_test_flattened = x_test.reshape(len(x_test), 28 * 28)

# Scale the pixel values to be between 0 and 1


x_train_flattened = x_train_flattened / 255
x_test_flattened = x_test_flattened / 255

model = keras.Sequential([
keras.layers.Dense(10, input_shape=(784,), activation='sigmoid')
])

model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

model.fit(x_train_flattened, y_train, epochs=5)

model.evaluate(x_test_flattened, y_test)

# Visualize the first test image


plt.matshow(x_test[0])
plt.show()

# Make a prediction for the first test image


y_predicted = model.predict(x_test_flattened)
predicted_label = np.argmax(y_predicted[0])
print("Predicted label:", predicted_label)
#iris classifier

from sklearn import datasets


from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC

# Load the Iris dataset


iris = datasets.load_iris()
X = iris.data
y = iris.target

# Standardize the features


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Create an SVM classifier


svm = SVC(kernel='rbf', C=1.0, gamma='scale', random_state=42)

# Train the classifier


svm.fit(X_scaled, y)

# Input for petal length, sepal length, sepal width, and petal width
petal_length = float(input("Enter petal length: "))
sepal_length = float(input("Enter sepal length: "))
sepal_width = float(input("Enter sepal width: "))
petal_width = float(input("Enter petal width: "))

# Make a prediction
input_data = scaler.transform([[petal_length, sepal_length, sepal_width,
petal_width]])
predicted_class = svm.predict(input_data)[0]
predicted_flower = iris.target_names[predicted_class]

print("Predicted Flower Type:", predicted_flower)

You might also like