0% found this document useful (0 votes)
10 views4 pages

Introduction To Neural Networks

The document outlines the implementation of three machine learning models: a Perceptron for classifying Iris flowers, a neural network for predicting California housing prices, and another neural network for classifying handwritten digits from the MNIST dataset. It includes code snippets for data loading, model training, and evaluation metrics such as accuracy and root mean squared error. Each section demonstrates the use of different libraries like Scikit-learn and TensorFlow for machine learning tasks.

Uploaded by

Jivesh Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Introduction To Neural Networks

The document outlines the implementation of three machine learning models: a Perceptron for classifying Iris flowers, a neural network for predicting California housing prices, and another neural network for classifying handwritten digits from the MNIST dataset. It includes code snippets for data loading, model training, and evaluation metrics such as accuracy and root mean squared error. Each section demonstrates the use of different libraries like Scikit-learn and TensorFlow for machine learning tasks.

Uploaded by

Jivesh Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Perceptron

In [1]:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import Perceptron
iris = load_iris(as_frame=True)
X = iris.data[["petal length (cm)", "petal width (cm)"]].values
y = (iris.target == 0) # Iris setosa
per_clf = Perceptron(random_state=42)
per_clf.fit(X, y)
X_new = [[2, 0.5], [3, 1]]
y_pred = per_clf.predict(X_new) # predicts True and False for these 2 flowers

In [2]:
iris

Out [2]: {'data': sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
.. ... ... ... ...
145 6.7 3.0 5.2 2.3
146 6.3 2.5 5.0 1.9
147 6.5 3.0 5.2 2.0
148 6.2 3.4 5.4 2.3
149 5.9 3.0 5.1 1.8

[150 rows x 4 columns],


'target': 0 0
1 0
2 0
3 0
4 0
..
145 2
146 2
147 2
148 2
149 2
Name: target, Length: 150, dtype: int32,
'frame': sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
.. ... ... ... ...
145 6.7 3.0 5.2 2.3
146 6.3 2.5 5.0 1.9
147 6.5 3.0 5.2 2.0
148 6.2 3.4 5.4 2.3
149 5.9 3.0 5.1 1.8

target
0 0
1 0
2 0
3 0
4 0
.. ...
145 2
146 2
147 2
148 2
149 2
[150 rows x 5 columns],
'target_names': array(['setosa', 'versicolor', 'virginica'], dtype='<U10'),
'DESCR': '.. _iris_dataset:\n\nIris plants dataset\n--------------------\n\n**Data Set
Characteristics:**\n\n :Number of Instances: 150 (50 in each of three classes)\n :Number of
Attributes: 4 numeric, predictive attributes and the class\n :Attribute Information:\n -
sepal length in cm\n - sepal width in cm\n - petal length in cm\n - petal width in
cm\n - class:\n - Iris-Setosa\n - Iris-Versicolour\n
- Iris-Virginica\n \n :Summary Statistics:\n\n ============== ==== ==== =======
===== ====================\n Min Max Mean SD Class Correlation\n
============== ==== ==== ======= ===== ====================\n sepal length: 4.3 7.9 5.84 0.83
0.7826\n sepal width: 2.0 4.4 3.05 0.43 -0.4194\n petal length: 1.0 6.9 3.76
1.76 0.9490 (high!)\n petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)\n
============== ==== ==== ======= ===== ====================\n\n :Missing Attribute Values: None\n
:Class Distribution: 33.3% for each of 3 classes.\n :Creator: R.A. Fisher\n :Donor: Michael
Marshall (MARSHALL%[email protected])\n :Date: July, 1988\n\nThe famous Iris database, first used
by Sir R.A. Fisher. The dataset is taken\nfrom Fisher\'s paper. Note that it\'s the same as in R, but
not as in the UCI\nMachine Learning Repository, which has two wrong data points.\n\nThis is perhaps the
best known database to be found in the\npattern recognition literature. Fisher\'s paper is a classic
in the field and\nis referenced frequently to this day. (See Duda & Hart, for example.) The\ndata set
contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant. One class
is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\n..
topic:: References\n\n - Fisher, R.A. "The use of multiple measurements in taxonomic problems"\n
Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n Mathematical Statistics"
(John Wiley, NY, 1950).\n - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene
Analysis.\n (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.\n - Dasarathy, B.V.
(1980) "Nosing Around the Neighborhood: A New System\n Structure and Classification Rule for
Recognition in Partially Exposed\n Environments". IEEE Transactions on Pattern Analysis and
Machine\n Intelligence, Vol. PAMI-2, No. 1, 67-71.\n - Gates, G.W. (1972) "The Reduced Nearest
Neighbor Rule". IEEE Transactions\n on Information Theory, May 1972, 431-433.\n - See also: 1988
MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II\n conceptual clustering system finds 3
classes in the data.\n - Many, many more ...',
'feature_names': ['sepal length (cm)',
'sepal width (cm)',
'petal length (cm)',
'petal width (cm)'],
'filename': 'iris.csv',
'data_module': 'sklearn.datasets.data'}

In [ ]:
X

In [3]:
y_pred

Out [3]: array([ True, False])

Predicting House Prices using Neural Network

In [6]:
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

housing = fetch_california_housing()
X_train_full, X_test, y_train_full, y_test = train_test_split(
housing.data, housing.target, random_state=42)
X_train, X_valid, y_train, y_valid = train_test_split(
X_train_full, y_train_full, random_state=42)
mlp_reg = MLPRegressor(hidden_layer_sizes=[50, 50, 50], random_state=42)
pipeline = make_pipeline(StandardScaler(), mlp_reg)
pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_valid)
rmse = mean_squared_error(y_valid, y_pred, squared=False) # about 0.505

In [7]:
rmse

Out [7]: 0.5053326657968648

In [ ]:
rms = mean_squared_error(y_valid, y_pred, squared=True)

In [ ]: rms

Classifying Hand Written Digits using Neural Network

In [1]:
from tensorflow.keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

In [ ]: train_images.shape

In [ ]:
len(train_labels)

In [2]:
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Dense(512, activation="relu"),
layers.Dense(10, activation="softmax")
])

In [3]:
#The compilation step

model.compile(optimizer="rmsprop",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])

In [4]:
#Preparing the image data
##Before training, we’ll preprocess the data by reshaping it into the shape the mode
##expects and scaling it so that all values are in the [0, 1] interval. Previously,
##images were stored in an array of shape (60000, 28, 28) of type uint8 with values
##in the [0, 255] interval. We’ll transform it into a float32 array of shape (60000,
##28) with values between 0 and 1.

train_images = train_images.reshape((60000, 28 * 28))


train_images = train_images.astype("float32") / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype("float32") / 255
In [5]:
#Fitting the model

model.fit(train_images, train_labels, epochs=5, batch_size=128)

Epoch 1/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 6s 5ms/step - accuracy: 0.8729 - loss: 0.4405
Epoch 2/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9658 - loss: 0.1165
Epoch 3/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9784 - loss: 0.0729
Epoch 4/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9849 - loss: 0.0500
Epoch 5/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 3s 5ms/step - accuracy: 0.9889 - loss: 0.0375

Out [5]: <keras.src.callbacks.history.History at 0x1cc66db7a90>

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

You might also like