20AI16 - ML Record
20AI16 - ML Record
NO: 03
FACIAL RECOGNITION APPLICATION WITH ARTIFICIAL NEURAL NETWORK
DATE:
AIM:
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 7: Stop.
SOURCE CODE:
!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'
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]
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)
RESULT:
Thus, to implement facial recognition application with artificial neural network is executed and verified
successfully.
AIM:
To study and implement Amazon toolkit using Sagemaker machine learning tool.
PROCEDURE:
➢ 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.
➢ 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.
➢ 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.
➢ Start.
➢ Fit the training data to a decision tree classifier and predict the test data.
➢ Stop.
SOURCE CODE:
#import modules
import pandas as pd
importgraphviz
KGiSL INSTITUTE OF TECHNOLOGY REG. NO: 711720243016
#read the dataset
df = pd.read_csv('car_data.csv')
#split x and y
X = df[['Gender','AnnualSalary']]
Y = df[['Purchased']]
a = StandardScaler()
X_train = a.fit_transform(X_train)
X_test = a.transform(X_test)
clf.fit(X_train,Y_train)
Y_pred =clf.predict(X_test)
cm = confusion_matrix(Y_test,Y_pred)
cm
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.
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]])
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)
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
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]
Predicted output:
RESULT:
Thus, to implement character recognition using multilayer perceptron is executed and verified
successfully.
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]
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']
import math
n = 100 # Number of data points
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.
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()
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()
import nltk
nltk.download('stopwords')
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()
return to_return
data['tweet']=data['tweet'].apply(string)
data.head()
df['tweet']=df['tweet'].apply(string)
df.head()
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()
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)
test_x=data['tweet']
test_x
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)
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
OUTPUT:
RESULT:
Thus, to implement sentimental analysis using random forest optimization algorithm is executed and
verified successfully.