Deep Learning PDF
Deep Learning PDF
III. Our graduates will exhibit team-work and leadership qualities to meet stakeholder
business objectives in their careers.
IV. Our graduates will evolve in ethical and professional practices and enhance
socioeconomic contributions to the society.
Program Outcomes (POs):
CO1: Understand and apply knowledge of computing and mathematics to machine learning problems, models and
algorithms
CO2: Understand the concepts of machine learning by applying different algorithms to create various models
CO3: Analyze machine learning algorithms to design and develop programs using python
CO4: Develop experiments and implement image recognition algorithms on various datasets using python
CO5: Understand and apply knowledge of neural network concepts for implementing speech recognition
algorithms using python.
PSO 1
PSO 2
PSO 3
PO 10
PO 11
PO 12
PO 3
PO 4
PO 5
PO 6
PO 7
PO 8
PO 9
PO2
PO 1
Attainment
x = 11 * [Link]((10, 1))
#y=a*x+b
y = 1.0 * x + 3.0
[Link](x_pred, y_pred)
ax.set_xlabel('X-Independent Variable')
ax.set_ylabel('Y-Dependent Variable')
[Link]('tight')
[Link]()
1
Output:
2
PROGRAM-2
Implement multiple Linear Regression
import numpy as np
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import [Link] as plt
def generate_dataset(n):
x = []
y = []
random_x1 = [Link]()
random_x2 = [Link]()
for i in range(n):
x1 = i
x2 = i/2 + [Link]()*n
[Link]([1, x1, x2])
[Link](random_x1 * x1 + random_x2 * x2 + 1)
return [Link](x), [Link](y)
x, y = generate_dataset(200)
[Link]['[Link]'] = 12
fig = [Link]()
ax = [Link](projection ='3d')
[Link]()
3
Result:
4
PROGRAM -3
Implementation of gradient descent in linear regression
import numpy as np
import [Link] as plt
class Linear_Regression:
def __init__(self, X, Y):
self.X = X
self.Y = Y
self.b = [0, 0]
return Y_pred
5
def compute_cost(self, Y_pred):
m = len(self.Y)
J = (1 / 2*m) * ([Link](Y_pred - self.Y)**2)
return J
def main():
X = [Link]([i for i in range(11)])
Y = [Link]([2*i for i in range(11)])
regressor = Linear_Regression(X, Y)
iterations = 0
steps = 100
learning_rate = 0.01
costs = []
while 1:
Y_pred = [Link]()
cost = regressor.compute_cost(Y_pred)
[Link](cost)
regressor.update_coeffs(learning_rate)
iterations += 1
if iterations % steps == 0:
print(iterations, "epochs elapsed")
print("Current accuracy is :",
regressor.get_current_accuracy(Y_pred))
6
break
if __name__ == '__main__':
main()
7
Output:
8
PROGRAM-4
Implementation of Logistic Regression
# imports
import numpy as np
import [Link] as plt
import pandas as pd
data = pd.read_csv("[Link]")
# plots
[Link]([Link][:, 0], [Link][:, 1], s=10, label='Admitted')
[Link](not_admitted.iloc[:, 0], not_admitted.iloc[:, 1], s=10, label='Not Admitted')
[Link]()
[Link]()
9
10
PROGRAM-5
Neural Networks Activation [Link] code shows briefly activation
functions used in neural networks' layers and compares their properties.
Currently supported:
- sigmoid
- tanh (hyperbolic tangent)
- relu (rectified linear unit)
"""
e = 2.7182818284590451
def sigmoid(x):
return (e**x)/((e**x)+1)
def tanh(x):
return ((e**x)-(e**(-x)))/((e**x)+(e**(-x)))
def relu(x):
return max(0, x)
start = -4.0
stop = 4.1
step = 0.1
11
Neural Networks Activation Functions
==================================
| x | sigmoid | tanh | relu |
==================================
| -4.0 | +0.0180 | -0.999 | +0.0 |
| -3.9 | +0.0198 | -0.999 | +0.0 |
| -3.8 | +0.0219 | -0.999 | +0.0 |
| -3.7 | +0.0241 | -0.999 | +0.0 |
| -3.6 | +0.0266 | -0.999 | +0.0 |
| -3.5 | +0.0293 | -0.998 | +0.0 |
| -3.4 | +0.0323 | -0.998 | +0.0 |
| -3.3 | +0.0356 | -0.997 | +0.0 |
| -3.2 | +0.0392 | -0.997 | +0.0 |
| -3.1 | +0.0431 | -0.996 | +0.0 |
| -3.0 | +0.0474 | -0.995 | +0.0 |
| -2.9 | +0.0522 | -0.994 | +0.0 |
| -2.8 | +0.0573 | -0.993 | +0.0 |
| -2.7 | +0.0630 | -0.991 | +0.0 |
| -2.6 | +0.0691 | -0.989 | +0.0 |
| -2.5 | +0.0759 | -0.987 | +0.0 |
| -2.4 | +0.0832 | -0.984 | +0.0 |
| -2.3 | +0.0911 | -0.980 | +0.0 |
| -2.2 | +0.0998 | -0.976 | +0.0 |
| -2.1 | +0.1091 | -0.970 | +0.0 |
| -2.0 | +0.1192 | -0.964 | +0.0 |
| -1.9 | +0.1301 | -0.956 | +0.0 |
| -1.8 | +0.1419 | -0.947 | +0.0 |
| -1.7 | +0.1545 | -0.935 | +0.0 |
| -1.6 | +0.1680 | -0.922 | +0.0 |
| -1.5 | +0.1824 | -0.905 | +0.0 |
| -1.4 | +0.1978 | -0.885 | +0.0 |
| -1.3 | +0.2142 | -0.862 | +0.0 |
| -1.2 | +0.2315 | -0.834 | +0.0 |
| -1.1 | +0.2497 | -0.800 | +0.0 |
| -1.0 | +0.2689 | -0.762 | +0.0 |
| -0.9 | +0.2891 | -0.716 | +0.0 |
| -0.8 | +0.3100 | -0.664 | +0.0 |
| -0.7 | +0.3318 | -0.604 | +0.0 |
| -0.6 | +0.3543 | -0.537 | +0.0 |
| -0.5 | +0.3775 | -0.462 | +0.0 |
| -0.4 | +0.4013 | -0.380 | +0.0 |
| -0.3 | +0.4256 | -0.291 | +0.0 |
| -0.2 | +0.4502 | -0.197 | +0.0 |
12
| -0.1 | +0.4750 | -0.100 | +0.0 |
| +0.0 | +0.5000 | +0.000 | +0.0 |
| +0.1 | +0.5250 | +0.100 | +0.1 |
| +0.2 | +0.5498 | +0.197 | +0.2 |
| +0.3 | +0.5744 | +0.291 | +0.3 |
| +0.4 | +0.5987 | +0.380 | +0.4 |
| +0.5 | +0.6225 | +0.462 | +0.5 |
| +0.6 | +0.6457 | +0.537 | +0.6 |
| +0.7 | +0.6682 | +0.604 | +0.7 |
| +0.8 | +0.6900 | +0.664 | +0.8 |
| +0.9 | +0.7109 | +0.716 | +0.9 |
| +1.0 | +0.7311 | +0.762 | +1.0 |
| +1.1 | +0.7503 | +0.800 | +1.1 |
| +1.2 | +0.7685 | +0.834 | +1.2 |
| +1.3 | +0.7858 | +0.862 | +1.3 |
| +1.4 | +0.8022 | +0.885 | +1.4 |
| +1.5 | +0.8176 | +0.905 | +1.5 |
| +1.6 | +0.8320 | +0.922 | +1.6 |
| +1.7 | +0.8455 | +0.935 | +1.7 |
| +1.8 | +0.8581 | +0.947 | +1.8 |
| +1.9 | +0.8699 | +0.956 | +1.9 |
| +2.0 | +0.8808 | +0.964 | +2.0 |
| +2.1 | +0.8909 | +0.970 | +2.1 |
| +2.2 | +0.9002 | +0.976 | +2.2 |
| +2.3 | +0.9089 | +0.980 | +2.3 |
| +2.4 | +0.9168 | +0.984 | +2.4 |
| +2.5 | +0.9241 | +0.987 | +2.5 |
| +2.6 | +0.9309 | +0.989 | +2.6 |
| +2.7 | +0.9370 | +0.991 | +2.7 |
| +2.8 | +0.9427 | +0.993 | +2.8 |
| +2.9 | +0.9478 | +0.994 | +2.9 |
| +3.0 | +0.9526 | +0.995 | +3.0 |
| +3.1 | +0.9569 | +0.996 | +3.1 |
| +3.2 | +0.9608 | +0.997 | +3.2 |
| +3.3 | +0.9644 | +0.997 | +3.3 |
| +3.4 | +0.9677 | +0.998 | +3.4 |
| +3.5 | +0.9707 | +0.998 | +3.5 |
| +3.6 | +0.9734 | +0.999 | +3.6 |
| +3.7 | +0.9759 | +0.999 | +3.7 |
| +3.8 | +0.9781 | +0.999 | +3.8 |
| +3.9 | +0.9802 | +0.999 | +3.9 |
| +4.0 | +0.9820 | +0.999 | +4.0 |
13
==================================
Legend:
14
# plot inputs and outputs
from matplotlib import pyplot as plt
15
[Link]['right'].set_color('none')
[Link]['top'].set_color('none')
[Link].set_ticks_position('bottom')
[Link].set_ticks_position('left')
# Create and show plot
# line plot of raw inputs to rectified outputs
[Link](series_in, series_out, color="#307EC7", linewidth=3, label="sigmoid")
[Link]()
16
PROGRAM -6
Implemetation of Polynomial Regression
import numpy as np
import [Link] as plt
import pandas as pd
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
17
# Fitting Polynomial Regression to the dataset
from [Link] import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=5)
X_poly = poly_reg.fit_transform(X)
pol_reg = LinearRegression()
pol_reg.fit(X_poly, y)
18
19
PROGRAM-7
CNN model: we will develop a one-dimensional convolutional neural
network model (1D CNN) for the human activity recognition dataset. The
data is provided as a single zip file that is about 58 megabytes in size. The
direct link for this download is below:
20
'total_acc_z_'+group+'.txt']
# body acceleration
filenames += ['body_acc_x_'+group+'.txt', 'body_acc_y_'+group+'.txt',
'body_acc_z_'+group+'.txt']
# body gyroscope
filenames += ['body_gyro_x_'+group+'.txt', 'body_gyro_y_'+group+'.txt',
'body_gyro_z_'+group+'.txt']
# load input data
X = load_group(filenames, filepath)
# load class output
y = load_file(prefix + group + '/y_'+group+'.txt')
return X, y
[Link](Dense(100, activation='relu'))
21
[Link](Dense(n_outputs,
activation='softmax'))
# summarize scores
def summarize_results(scores):
print(scores)
m, s = mean(scores), std(scores)
print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))
# run an PROGRAM
def run_PROGRAM(repeats=10):
# load data
trainX, trainy, testX, testy = load_dataset()
# repeat PROGRAM
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
[Link](score)
# summarize results
summarize_results(scores)
RESULT:
22
>p=False #4: 89.243
>p=False #5: 90.193
>p=False #6: 90.465
This allows the two samples of results to be compared in a nonparametric way, showing
the median and the middle 50% of each sample.
We can see that the distribution of results with standardization is quite different from the
distribution of results without standardization. This is likely a real effect.
23
24
PROGRAM-8
Develop a Long Short-Term Memory network model (LSTM) for the
human activity recognition dataset.
# lstm model
from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from [Link] import Sequential
from [Link] import Dense
from [Link] import Flatten
from [Link] import Dropout
from [Link] import LSTM
from [Link] import to_categorical
from matplotlib import pyplot
25
filenames += ['body_acc_x_'+group+'.txt', 'body_acc_y_'+group+'.txt',
'body_acc_z_'+group+'.txt']
# body gyroscope
filenames += ['body_gyro_x_'+group+'.txt', 'body_gyro_y_'+group+'.txt',
'body_gyro_z_'+group+'.txt']
# load input data
X = load_group(filenames, filepath)
# load class output
y = load_file(prefix + group + '/y_'+group+'.txt')
return X, y
26
return accuracy
# summarize scores
def summarize_results(scores):
print(scores)
m, s = mean(scores), std(scores)
print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))
# run an PROGRAM
def run_PROGRAM(repeats=10):
# load data
trainX, trainy, testX, testy = load_dataset()
# repeat PROGRAM
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
[Link](score)
# summarize results
summarize_results(scores)
Result:
>#1: 90.058
>#2: 85.918
>#3: 90.974
>#4: 89.515
>#5: 90.159
>#6: 91.110
>#7: 89.718
>#8: 90.295
>#9: 89.447
>#10: 90.024
27
[90.05768578215134, 85.91788259246692, 90.97387173396675, 89.51476077366813,
90.15948422124194, 91.10960298608755, 89.71835765184933, 90.29521547336275,
89.44689514760775, 90.02375296912113]
28
PROGRAM-9
K-means clustering on a sample random data using open-cv library.
# importing required tools
import numpy as np
from matplotlib import pyplot as plt
[Link]('Test Data')
[Link]('Z samples')
[Link](Z,256,[0,256])
[Link]()
RESULT
29
PROGRAM-10
Implementation of decision tree algorithm.
import numpy as np
import pandas as pd
from [Link] import confusion_matrix
from sklearn.cross_validation import train_test_split
from [Link] import DecisionTreeClassifier
from [Link] import accuracy_score
from [Link] import classification_report
30
# Creating the classifier object
clf_gini = DecisionTreeClassifier(criterion = "gini",
random_state = 100,max_depth=3, min_samples_leaf=5)
# Performing training
clf_gini.fit(X_train, y_train)
return clf_gini
# Performing training
clf_entropy.fit(X_train, y_train)
return clf_entropy
print("Report : ",
classification_report(y_test, y_pred))
# Driver code
31
def main():
# Building Phase
data = importdata()
X, Y, X_train, X_test, y_train, y_test = splitdataset(data)
clf_gini = train_using_gini(X_train, X_test, y_train)
clf_entropy = tarin_using_entropy(X_train, X_test, y_train)
# Operational Phase
print("Results Using Gini Index:")
RESULT:
Data Infomation:
32
Results Using Gini Index:
Predicted values:
['R' 'L' 'R' 'R' 'R' 'L' 'R' 'L' 'L' 'L' 'R' 'L' 'L' 'L' 'R' 'L' 'R' 'L'
'L' 'R' 'L' 'R' 'L' 'L' 'R' 'L' 'L' 'L' 'R' 'L' 'L' 'L' 'R' 'L' 'L' 'L'
'L' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'R' 'L' 'R'
'R' 'L' 'R' 'R' 'L' 'L' 'R' 'R' 'L' 'L' 'L' 'L' 'L' 'R' 'R' 'L' 'L' 'R'
'R' 'L' 'R' 'L' 'R' 'R' 'R' 'L' 'R' 'L' 'L' 'L' 'L' 'R' 'R' 'L' 'R' 'L'
'R' 'R' 'L' 'L' 'L' 'R' 'R' 'L' 'L' 'L' 'R' 'L' 'R' 'R' 'R' 'R' 'R' 'R'
'R' 'L' 'R' 'L' 'R' 'R' 'L' 'R' 'R' 'R' 'R' 'R' 'L' 'R' 'L' 'L' 'L' 'L'
'L' 'L' 'L' 'R' 'R' 'R' 'R' 'L' 'R' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R'
'L' 'L' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R' 'R' 'R' 'L' 'R' 'R' 'R' 'R' 'R'
'L' 'L' 'R' 'R' 'R' 'R' 'L' 'R' 'R' 'R' 'L' 'R' 'L' 'L' 'L' 'L' 'R' 'R'
'L' 'R' 'R' 'L' 'L' 'R' 'R' 'R']
Confusion Matrix: [[ 0 6 7]
[ 0 67 18]
[ 0 19 71]]
Accuracy : 73.4042553191
Report :
precision recall f1-score support
B 0.00 0.00 0.00 13
L 0.73 0.79 0.76 85
R 0.74 0.79 0.76 90
avg/total 0.68 0.73 0.71 188
Predicted values:
['R' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'R' 'R' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'L'
'L' 'R' 'L' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'L' 'L'
'L' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'R' 'R' 'L' 'L' 'R' 'L' 'L' 'R' 'L' 'L'
'R' 'L' 'R' 'R' 'L' 'R' 'R' 'R' 'L' 'L' 'R' 'L' 'L' 'R' 'L' 'L' 'L' 'R'
'R' 'L' 'R' 'L' 'R' 'R' 'R' 'L' 'R' 'L' 'L' 'L' 'L' 'R' 'R' 'L' 'R' 'L'
'R' 'R' 'L' 'L' 'L' 'R' 'R' 'L' 'L' 'L' 'R' 'L' 'L' 'R' 'R' 'R' 'R' 'R'
'R' 'L' 'R' 'L' 'R' 'R' 'L' 'R' 'R' 'L' 'R' 'R' 'L' 'R' 'R' 'R' 'L' 'L'
'L' 'L' 'L' 'R' 'R' 'R' 'R' 'L' 'R' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R'
'L' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'R' 'R' 'R' 'R' 'L' 'R' 'R' 'R' 'R' 'R'
'R' 'L' 'R' 'L' 'R' 'R' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'L' 'L' 'L' 'L' 'R'
'R' 'R' 'L' 'L' 'L' 'R' 'R' 'R']
33
Confusion Matrix: [[ 0 6 7]
[ 0 63 22]
[ 0 20 70]]
Accuracy : 70.7446808511
Report :
precision recall f1-score support
B 0.00 0.00 0.00 13
L 0.71 0.74 0.72 85
R 0.71 0.78 0.74 90
avg / total 0.66 0.71 0.68 188
34
PROGRAM-11
K-Nearest neighbors classifier algorithm
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
import [Link] as plt
import seaborn as sns
d C:\Users\Dev\Desktop\Kaggle\Breast_Cancer
# Changing the read file location to the location of the file
df = pd.read_csv('[Link]')
y = df['diagnosis']
X = [Link]('diagnosis', axis = 1)
X = [Link]('Unnamed: 32', axis = 1)
X = [Link]('id', axis = 1)
# Separating the dependent and independent variable
K = []
training = []
test = []
scores = {}
[Link](training_score)
[Link](test_score)
scores[k] = [training_score, test_score]
35
for keys, values in [Link]():
print(keys, ':', values)
ax = [Link](K, training);
[Link](xlabel ='values of k', ylabel ='Training Score')
[Link]()
# function to show plot
ax = [Link](K, test);
[Link](xlabel ='values of k', ylabel ='Test Score')
[Link]()
[Link](K, training, color ='k')
[Link](K, test, color ='g')
[Link]()
RESULT:
36
PROGRAM-12
Implementation of Multinomial Logisitc Regression using scikit-learn to
make predictions on digit dataset.
from sklearn import datasets, linear_model, metrics
# comparing actual response values (y_test) with predicted response values (y_pred)
print("Logistic Regression model accuracy(in %):",
metrics.accuracy_score(y_test, y_pred)*100)
37