0% found this document useful (0 votes)
147 views24 pages

20AI16 - ML Record

The document describes implementing character recognition using a multilayer perceptron neural network. It involves the following steps: 1) Loading and preprocessing digit image data to split into training and test sets. 2) Defining a neural network class with an input, hidden, and output layer along with forward and backward pass functions. 3) Training the network by running the forward and backward passes to update weights using gradient descent. 4) Testing the trained network on new data and evaluating its performance using a softmax function.

Uploaded by

Menma
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)
147 views24 pages

20AI16 - ML Record

The document describes implementing character recognition using a multilayer perceptron neural network. It involves the following steps: 1) Loading and preprocessing digit image data to split into training and test sets. 2) Defining a neural network class with an input, hidden, and output layer along with forward and backward pass functions. 3) Training the network by running the forward and backward passes to update weights using gradient descent. 4) Testing the trained network on new data and evaluating its performance using a softmax function.

Uploaded by

Menma
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/ 24

EX.

NO: 03
FACIAL RECOGNITION APPLICATION WITH ARTIFICIAL NEURAL NETWORK
DATE:

AIM:

To implement facial recognition application with artificial neural network.

ALGORITHM:

Step 1: Start
Step 2: Import all the necessary modules.
Step 3: Create two directories for known and unknown pictures.

Step 4: Draw rectangle and name the person to recognize. Draw on the known images.
Step 5: Display the images

Step 6: Detect and recognize the person.

Step 7: Stop.

SOURCE CODE:

!pip3 install face_recognition

!mkdir known
!wget https://www.biography.com/.image/t_share/MTY2MzU3Nzk2OTM2MjMwNTkx/elon_musk_royal_s
ociety.jpg -O known/elon.jpg
!wget https://pbs.twimg.com/profile_images/988775660163252226/XpgonN0X_400x400.jpg -
O known/bill.jpg
!wget https://www.biography.com/.image/t_share/MTE4MDAzNDEwNzg5ODI4MTEw/barack-obama-
12782369-1-402.jpg -O known/obama.jpg

!mkdir unknown
!wget https://i.insider.com/5ddfa893fd9db26b8a4a2df7 -O unknown/1.jpg
!wget https://cdn-images-1.medium.com/max/1200/1*aEoYLgy4z1lT1kW7dqWzBg.jpeg -
O unknown/2.jpg
!wget https://media2.s-nbcnews.com/j/newscms/2017_46/2224911/171113-bill-gates-se-
247p_043bd413c63b2a97abf11bfad747538d.fit-760w.jpg -O unknown/3.jpg
!wget https://specials-images.forbesimg.com/imageserve/1184274010/960x0.jpg -
O unknown/4.jpg

import face_recognition
import cv2
import os
from google.colab.patches import cv2_imshow
KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016
def read_img(path):
img = cv2.imread(path)
(h, w) = img.shape[:2]
width = 500
ratio = width / float(w)
height = int(h * ratio)
return cv2.resize(img, (width, height))

known_encodings = []
known_names = []
known_dir = 'known'

for file in os.listdir(known_dir):


img = read_img(known_dir + '/' + file)
img_enc = face_recognition.face_encodings(img)[0]
known_encodings.append(img_enc)
known_names.append(file.split('.')[0])

unknown_dir = 'unknown'
for file in os.listdir(unknown_dir):
print("Processing", file)
img = read_img(unknown_dir + '/' + file)
img_enc = face_recognition.face_encodings(img)[0]

results = face_recognition.compare_faces(known_encodings, img_enc)

for i in range(len(results)):
if results[i]:
name = known_names[i]
(top, right, bottom, left) = face_recognition.face_locations(img)[0]
cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(img, name, (left+2, bottom+20), cv2.FONT_HERSHEY_PLAIN, 1, (255,
255, 255), 1)
cv2_imshow(img)

KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016


OUTPUT:

RESULT:

Thus, to implement facial recognition application with artificial neural network is executed and verified
successfully.

KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016


EX.NO: 04
AMAZON TOOLKIT: SAGEMAKER
DATE:

AIM:

To study and implement Amazon toolkit using Sagemaker machine learning tool.

PROCEDURE:

Step 1:Create an Amazon SageMaker Notebook Instance.

➢ Open the Amazon SageMaker console at https://console.aws.amazon.com/sagemaker/.

➢ Choose Notebook instances, and then choose Create notebook instance.

➢ On the Create notebook instance page, provide the following information.

KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016


Step 2: Create a Jupyter Notebook.

➢ To start scripting for training and deploying your model, create a Jupyter notebook in the SageMaker
notebook instance.

➢ Using the Jupyter notebook, you can conduct machine learning (ML) experiments for training and inference
while accessing the SageMaker features and the AWS infrastructure.

Step 3: Download, Explore, and Transform Data.

➢ In this step, you load the Adult Census dataset to your notebook instance using the SHAP (SHapley
Additive exPlanations)Library, review the dataset, transform it, and upload it to Amazon S3.

➢ Using Sklearn, split the dataset into a training set and a test set. The training set is used to train the model,
while the test set is used to evaluate the performance of the final trained model.

➢ Convert the train and validation dataframe objects to CSV files to match the input file format for the
XGBoost algorithm.

KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016


Step 4: Train a Model.
➢ Import the Amazon SageMaker Python SDK and start by retrieving the basic information from your current
SageMaker session.

➢ Create an XGBoost estimator using the sagemaker.estimator.Estimator class. In the following example
code, the XGBoost estimator is named xgb_model.

➢ Set the hyperparameters for the XGBoost algorithm by calling the set_hyperparameters method of the
estimator. For a complete list of XGBoost hyperparameters, see XGBoost Hyperparameters.

➢ Use the TrainingInput class to configure a data input flow for training.

➢ To start model training, call the estimator's fit method with the training and validation datasets.

KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016


KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016
Step 5: Implementation of program (decision tree with information gain).

➢ Start.

➢ Import all the necessary modules.

➢ Upload the csv file into colab.

➢ Do data preprocessing of the attributes of required.

➢ Split the data for testing and training.

➢ Fit the training data to a decision tree classifier and predict the test data.

➢ Use confusion matrix to see how well the model is performing.

➢ Stop.

SOURCE CODE:

#import modules

import pandas as pd

fromsklearn.model_selection import train_test_split

fromsklearn.preprocessing import StandardScaler

fromsklearn.tree import DecisionTreeClassifier

fromsklearn.metrics import confusion_matrix

fromsklearn.tree import export_graphviz

importgraphviz
KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016
#read the dataset

df = pd.read_csv('car_data.csv')

#convert string to int

df['Gender'].replace({'Male':1, 'Female':0}, inplace=True)

#split x and y

X = df[['Gender','AnnualSalary']]

Y = df[['Purchased']]

X_train, X_test, Y_train, Y_test=train_test_split(X,Y,test_size = 0.5,


random_state=0)

#standardize the data

a = StandardScaler()

X_train = a.fit_transform(X_train)

X_test = a.transform(X_test)

#train the model

clf = DecisionTreeClassifier(criterion = "entropy",random_state=0)

clf.fit(X_train,Y_train)

#predict the values of testing

Y_pred =clf.predict(X_test)

#calculate how well the prediction is

cm = confusion_matrix(Y_test,Y_pred)

cm

#visualize the decision tree

graphviz.Source(export_graphviz(clf,feature_names = X.columns,filled = True))

KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016


OUTPUT:

RESULT:

Thus, the concept of decision trees is used as an example in implementing Amazon toolkit using
Sagemaker machine learning tool has been executed and verified successfully.

KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016


EX.NO: 05
CHARACTER RECOGNITION USING MULTILAYER PERCEPTRON
DATE:

AIM:
To implement character recognition using multilayer perceptron.
ALGORITHM:
Step 1: Start the process.
Step 2: Import all the necessary modules.
Step 3: Upload the csv file into colab.
Step 4: Do data preprocessing of the attributes of required.
Step 5: Split the data for testing and training.
Step 6: Fit the training data to a handwritten text/image and predict the test data.
Step 7: Use softmax function to see how well the model is performing.
Step 8: Stop the process.
SOURCE CODE:
from sklearn.datasets import load_digits
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

X,y=load_digits().data,load_digits().target
y.shape
X.shape

plt.imshow(X[0].reshape(8,8))

def softmax(a):
e_a=np.exp(a)
ans=e_a/np.sum(e_a,axis=1,keepdims=True)
return ans

softmax([[90,10],[70,30]])

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016


class NN:
def __init__(self,input_size=64,layers=[500,1000],output=10):
np.random.seed(0)
model={}
model['w1']=np.random.randn(input_size,layers[0])
model['b1']=np.zeros((1,layers[0]))
model['w2']=np.random.randn(layers[0],layers[1])
model['b2']=np.zeros((1,layers[1]))
model['w3']=np.random.randn(layers[1],output)
model['b3']=np.zeros((1,output))
self.model=model

def forward(self,X):
z1=np.dot(X,self.model['w1'])+self.model['b1']
a1=np.tanh(z1)
z2=np.dot(a1,self.model['w2'])+self.model['b2']
a2=np.tanh(z2)
z3=np.dot(a2,self.model['w3'])+self.model['b3']
y_=softmax(z3)
self.activation_outputs=(a1,a2,y_)
return y_

def backward(self,X,y,learning_rate=0.01):
w1,w2,w3=self.model['w1'],self.model['w2'],self.model['w3']
b1,b2,b3=self.model['b1'],self.model['b2'],self.model['b3']
m=X.shape[0]
a1,a2,y_=self.activation_outputs

delta3=y_-y
dw3=np.dot(a2.T,delta3)
db3=np.sum(delta3,axis=0)/float(m)

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016


delta2=(1-np.square((a2))*np.dot(delta3,w3.T))
dw2=np.dot(a1.T,delta2)
db2=np.sum(delta2,axis=0)/float(m)

delta1=(1-np.square((a1))*np.dot(delta2,w2.T))
dw1=np.dot(X.T,delta1)
db1=np.sum(delta1,axis=0)/float(m)

self.model['w1'] -=learning_rate*dw1/m
self.model['b1'] -=learning_rate*db1

self.model['w2'] -=learning_rate*dw2/m
self.model['b2'] -=learning_rate*db2

self.model['w3'] -=learning_rate*dw3/m
self.model['b3'] -=learning_rate*db3

def predict(self,X):
y_out=self.forward(X)
return np.argmax(y_out,axis=1)

def loss(y_oht,y_):
l=-np.mean(y_oht*np.log(y_))
return l

def one_hot(y,depth):
m=y.shape[0]
y_oht=np.zeros((m,depth))
y_oht[np.arange(m),y]=1
return y_oht

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016


def train(X,y,model,epochs,learning_rate=0.01,logs=True):
training_loss=[]
classes=10
y_oht=one_hot(y,classes)
for ix in range(epochs):
y_=model.forward(X)
l=loss(y_oht,y_)
model.backward(X,y_oht,learning_rate)
training_loss.append(l)
if(logs):
print("Epoch %d Loss %.4f" %(ix,l))
return training_loss

model= NN()

loss=train(X,y,model,700)
ypred=model.predict(X)
np.mean(ypred==y)
plt.imshow(X[1].reshape(8,8))

ypred[1]
y[1]

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016


OUTPUT:

Predicted output:

RESULT:
Thus, to implement character recognition using multilayer perceptron is executed and verified
successfully.

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016


EX.NO: 06
NON-PARAMETRIC LOCALLY WEIGHTED REGRESSION ALGORITHM
DATE:

AIM:
To implement the non-parametric Locally Weighted Regression algorithm in order a fit data points.

ALGORITHM:
Step 1: Start the process.
Step 2: Import all the necessary modules.
Step 3: Upload the csv file into colab/Get the values of X and Y.
Step 4: Do data preprocessing of the attributes if required.
Step 5: Define functions for linear regression and weight calculation.
Step 6: Plot the graph and predict the line.
Step 7: Stop the process.

EXAMPLE 1:
SOURCE CODE:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(5,7,50)
print(X)
Y = np.sin(X)
print(Y)

plt.scatter(X,Y)
plt.show()
plt.scatter(X,Y,alpha = 0.3)
plt.show()

def local_regression(x0,X,Y,tau):
x0=np.r_[1,x0]
X=np.c_[np.ones(len(X)),X]

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016


xw=X.T*radial_kernel(x0,X,tau)
beta=np.linalg.pinv(xw@X)@xw@Y
return x0@beta

def radial_kernel(x0,X,tau):
formula = np.exp(np.sum((X-x0)**2,axis=1)/(-2*(tau**2)))
return formulaX += np.random.normal(scale=.1, size=n)

def plot_lwr(tau):
domain = np.linspace(5,7,50)
prediction=[local_regression(x0,X,Y,tau) for x0 in domain]
plt.scatter(X,Y,alpha = 0.2)
plt.plot(domain,prediction,color="red")
plt.show()
return prediction
plot_lwr(0.01)

OUTPUT:

EXAMPLE 2:
SOURCE CODE:
import pandas as pd
#df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-
learningdatabases/housing/housing.data',header=None, sep='\s+')
df = pd.read_csv("/content/boston.csv")
df.columns = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS',
'RAD','TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV']

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016


df.head(5)
X = df[['RM']].values # Number of Rooms
Y = df[['MEDV']].values
from math import ceil
import numpy as np
from scipy import linalg

def lowess(x, y, f= 2. / 3., iter=3):

n = len(x) # Number of x points


r = int(ceil(f * n)) # Computing the residual of smoothing functions
h = [np.sort(np.abs(x - x[i]))[r] for i in range(n)] #
w = np.clip(np.abs((x[:, None] - x[None, :]) / h), 0.0, 1.0) # Weight
Function
w = (1 - w ** 3) ** 3 # Tricube Weight Function
ypred = np.zeros(n) # Initialisation of predictor
delta = np.ones(n) # Initialisation of delta

for iteration in range(iter):


for i in range(n):
weights = delta * w[:, i] # Cumulative Weights
b = np.array([np.sum(weights * y), np.sum(weights * y * x)]) #
Matrix B
A = np.array([[np.sum(weights), np.sum(weights * x)],
[np.sum(weights * x), np.sum(weights * x * x)]]) #
Matrix A

beta = linalg.solve(A, b) # Beta,Solution of AX= B equation


ypred[i] = beta[0] + beta[1] * x[i]

residuals = y - ypred # Finding Residuals


s = np.median(np.abs(residuals)) # Median of Residuals
delta = np.clip(residuals / (6.0 * s), -1, 1) # Delta
delta = (1 - delta ** 2) ** 2 # Delta
return ypred

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016


if __name__ == '__main__': # Main Function

import math
n = 100 # Number of data points

#Case1: Sinusoidal Fitting


x = X[:100].ravel() # House Room Data for modeling
y = Y[:100].ravel() # Noisy House Price data
f = 0.66
ypred = lowess(x, y, f=f, iter=3) # Predicted House Price Data

import pylab as pl
pl.clf()
pl.plot(x, y, label='Y NOISY')
pl.plot(x, ypred, label='Y PREDICTED')
pl.legend()
pl.show()

OUTPUT:

RESULT:
Thus, to implement the non-parametric Locally Weighted Regression algorithm in order a fit data points
is executed and verified successfully.

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016


EX.NO: 07
SENTIMENTAL ANALYSIS USING RANDOM FOREST ALGORITHM
DATE:

AIM:
To implement sentimental analysis using random forest optimization algorithm.
ALGORITHM:
Step 1: Start the process.
Step 2: Import all the necessary modules.
Step 3: Upload the csv file into colab.
Step 4: Do data preprocessing of the attributes of required.
Step 5: Remove all the special characters and the user name form the file.
Step 6: Plot the remaining words to the word frequency.
Step 7: Fit the training data to count vectorization and predict the test data.
Step 8: Stop the process.
SOURCE CODE:
import pandas as pd
import numpy as np

#train data
data=pd.read_csv("train.csv")
data.head()

data.shape
data.value_counts('label')
import seaborn as sns
sns.countplot(x='label',hue = "label",data=data)

#test data
df=pd.read_csv("test.csv")
df.head()

data['tweet'] = data['tweet'].str.replace('@user',' ')


data['tweet'] = data['tweet'].str.replace('#',' ')
data['tweet'] = data['tweet'].str.replace("[^0-9a-zA-Z#]",' ')
data

df['tweet'] = df['tweet'].str.replace('@user',' ')


df['tweet'] = df['tweet'].str.replace('#',' ')
df['tweet'] = df['tweet'].str.replace("[^a-zA-Z#]",' ')
df

from collections import Counter

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016


all_words=[]
for line in list(data['tweet']):
words=line.split()
for word in words:
all_words.append(word.lower())

a=Counter(all_words).most_common(5)
a

data['tweet']=data['tweet'].apply(lambda x:x.split())
data.head()

all_words1=[]
for line in list(df['tweet']):
words=line.split()
for word in words:
all_words1.append(word.lower())

a=Counter(all_words1).most_common(5)
a

df['tweet']=df['tweet'].apply(lambda x:x.split())
df.head()

from nltk import PorterStemmer


stemmer=PorterStemmer()
data['tweet']=data['tweet'].apply(lambda x: [stemmer.stem(i) for i in x])
data.head()

from nltk import PorterStemmer


stemmer=PorterStemmer()
df['tweet']=df['tweet'].apply(lambda x: [stemmer.stem(i) for i in x ])
df.head()

import nltk
nltk.download('stopwords')

from nltk.corpus import stopwords


from nltk.tokenize import word_tokenize
stopwords=nltk.corpus.stopwords.words('english')

newStopWords=['u','go','got','via','or','ur','us','in','in','i','let','the','to
','is','amp','make','one','day','days','get']
stopwords.extend(newStopWords)

import string
def process(text):
nopunc=set(char for char in list(text) if char not in string.punctuation)
nonpunc= " ".join(nopunc)
return[word for word in nonpunc.lower().split() if word.lower() not in stopwo
rds]

data['tweet']=data['tweet'].apply(process)
KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016
data.head()
df['tweet']=df['tweet'].apply(process)
df.head()

from wordcloud import WordCloud


import matplotlib.pyplot as plt
word=[]
for line in data['tweet']:
word.extend(line)
wordfreq=Counter(word)
wordcloud=WordCloud(background_color='white',max_words=2000,stopwords=stopwords
).generate_from_frequencies(wordfreq)
plt.figure(figsize=(10,9))
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis("off")
plt.show()

from wordcloud import WordCloud


import matplotlib.pyplot as plt
word1=[]
for line in df['tweet']:
word1.extend(line)
wordfreq=Counter(word1)
wordcloud=WordCloud(background_color='white',max_words=2000,stopwords=stopwords
).generate_from_frequencies(wordfreq)
plt.figure(figsize=(10,9))
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis("off")
plt.show()

def string (text):


to_return=" "
for i in list(text):
to_return += str(i) + " "
to_return = to_return[:-1]

return to_return

data['tweet']=data['tweet'].apply(string)
data.head()

df['tweet']=df['tweet'].apply(string)
df.head()

positive=[r for r in data['tweet'][data['label']==0]]


pos=''.join(positive)

wordcloud=WordCloud(background_color='white',max_words=2000,stopwords=stopwords
).generate(pos)
plt.figure(figsize=(10,9))
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis("off")
plt.show()
KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016
negative=[r for r in data['tweet'][data['label']==1]]
neg=''.join(negative)

wordcloud=WordCloud(background_color='white',max_words=2000,stopwords=stopwords
).generate(neg)
plt.figure(figsize=(10,9))
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis("off")
plt.show()

data.head()
df.head()

from sklearn.feature_extraction.text import CountVectorizer


from sklearn.model_selection import train_test_split

x_train,x_test,y_train,y_test=train_test_split(data['tweet'],data['label'],rand
om_state=42,test_size=0.2)
print("training set:",x_train.shape,y_train.shape)
print("testing set:",x_test.shape,y_test.shape)

from sklearn.feature_extraction.text import CountVectorizer,TfidfTransformer


count_vect=CountVectorizer(stop_words='english')
transformer=TfidfTransformer(norm='l2',sublinear_tf=True)

test_x=data['tweet']
test_x

from nltk.parse.corenlp import transform


x_train_counts=count_vect.fit_transform(x_train)
x_train_tfidf=transformer.fit_transform(x_train_counts)
print(x_train_counts.shape)
print(x_train_tfidf.shape)

test_x_counts=count_vect.transform(test_x)
test_x_tfidf=transformer.transform(test_x_counts)
print(test_x_counts.shape)
print(test_x_tfidf.shape)

x_test_counts=count_vect.transform(x_test)
x_test_tfidf=transformer.transform(x_test_counts)
print(x_test_counts.shape)
print(x_test_tfidf.shape)

from sklearn.ensemble import RandomForestClassifier


model=RandomForestClassifier(n_estimators=200)
model.fit(x_train_tfidf,y_train)

predictions=model.predict(x_test_tfidf)

submission=model.predict(test_x_tfidf)
submission
KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016
from sklearn.metrics import accuracy_score
accuracy_score(y_test,predictions)*100

from sklearn.metrics import confusion_matrix,f1_score


confusion_matrix(y_test,predictions)

OUTPUT:

RESULT:
Thus, to implement sentimental analysis using random forest optimization algorithm is executed and
verified successfully.

KGiSL INSTITUTE OF TECHNOLOGY REG.NO: 711720243016

You might also like