﻿1
00:00:00,150 --> 00:00:00,600
‫Hello.

2
00:00:00,870 --> 00:00:01,650
‫in this video

3
00:00:01,940 --> 00:00:10,500
‫We will create a very simple single Perceptron model to classify flower species depending on their petal

4
00:00:10,800 --> 00:00:11,880
‫length and petal width.

5
00:00:15,030 --> 00:00:19,650
‫We will be using a sklearn to create this single Perceptron model.

6
00:00:20,730 --> 00:00:28,080
‫In the later part of this course, we will use Keras and TensorFlow to create multi-level Perceptron

7
00:00:28,080 --> 00:00:34,770
‫models, Sklearn is a very popular machine learning library for Python.

8
00:00:36,510 --> 00:00:46,650
‫It is the go to library to create regression classification decision trees or SVM models, we

9
00:00:46,650 --> 00:00:50,160
‫have separate lectures on all this machine learning models.

10
00:00:50,640 --> 00:00:57,000
‫So if you are interested in learning any one of these, you can go ahead and check our courses

11
00:00:57,090 --> 00:01:01,350
‫on regression classification, decision trees and SVM.

12
00:01:03,620 --> 00:01:06,260
‫So let's start first.

13
00:01:06,470 --> 00:01:08,750
‫We will import numpy and pandas.

14
00:01:10,010 --> 00:01:12,530
‫Now, if you have installed Anaconda.

15
00:01:14,190 --> 00:01:17,550
‫There is no need to install a sklearn separately.

16
00:01:18,300 --> 00:01:19,860
‫You just have to import sklearn.

17
00:01:20,970 --> 00:01:29,400
‫But in case you are facing any error in importing a sklearn, you can install it using pip or conda

18
00:01:29,400 --> 00:01:29,910
‫install.

19
00:01:30,600 --> 00:01:32,550
‫So just run this

20
00:01:32,550 --> 00:01:36,270
‫Command, Pip, install sklearn in your command prompt.

21
00:01:37,680 --> 00:01:46,080
‫You can open your command prompt by pressing Windows R and write cmd in the run command and hit

22
00:01:46,080 --> 00:01:49,110
‫enter there in the command prompt

23
00:01:49,260 --> 00:01:53,790
‫You can write this code and execute it to install sklearn.

24
00:01:55,110 --> 00:01:57,620
‫Or else you can directly run this code

25
00:01:58,090 --> 00:02:03,030
‫in Jupyter notebook and this will also install sklearn for you.

26
00:02:04,440 --> 00:02:06,540
‫So if you have installed a sklearn.

27
00:02:07,770 --> 00:02:09,990
‫First, we will load iris data.

28
00:02:11,880 --> 00:02:14,220
‫There are various csv files out there.

29
00:02:14,400 --> 00:02:17,640
‫You can also import those csv file to load this data.

30
00:02:19,230 --> 00:02:25,140
‫But as Skillern have some predefined datasets and we will load our iris data from there.

31
00:02:26,730 --> 00:02:29,650
‫So just write from sklearn.datasets.

32
00:02:29,820 --> 00:02:30,830
‫Import load

33
00:02:31,110 --> 00:02:32,250
‫Underscore iris.

34
00:02:32,880 --> 00:02:37,220
‫And then we are saving our iris data into this variable iris.

35
00:02:38,930 --> 00:02:40,200
‫So just run this.

36
00:02:42,240 --> 00:02:49,040
‫Now, let's take a look at our data, you can see there are four columns.

37
00:02:51,230 --> 00:02:59,780
‫the columns are sepal length, sepal width, petal length and petal width, as i have said earlier this is a

38
00:02:59,790 --> 00:03:01,950
‫data of different types of flowers.

39
00:03:02,760 --> 00:03:06,140
‫There are three different types of flowers, Setosa.

40
00:03:06,920 --> 00:03:08,860
‫versicolour, virginica

41
00:03:09,420 --> 00:03:13,600
‫And for each of these flowers, we have their sepal length, sepal width

42
00:03:13,890 --> 00:03:15,120
‫petal length and petal width

43
00:03:16,860 --> 00:03:25,680
‫So here we have this four variables and that category is stored in Iris.Target.

44
00:03:26,970 --> 00:03:34,740
‫So in our example, we want to create a Perceptron model which would identify whether the flower is

45
00:03:34,740 --> 00:03:42,210
‫Setosa or not using petal width and petal length as the independent variables.

46
00:03:44,400 --> 00:03:47,310
‫If you want, you can take all the four variables.

47
00:03:47,840 --> 00:03:54,480
‫But for our example, we are just taking these two variables to predict where the flower is setosa or not.

48
00:03:56,130 --> 00:04:01,650
‫So we want our independent variable to be this petal length and petal width.

49
00:04:02,550 --> 00:04:07,300
‫We will store this information in another variable which we are calling X.

50
00:04:08,460 --> 00:04:11,250
‫We are defining X as iris.data

51
00:04:11,640 --> 00:04:15,270
‫And here we just want the third and fourth column.

52
00:04:15,360 --> 00:04:17,510
‫That's why we have written 2,3.

53
00:04:18,450 --> 00:04:21,060
‫If you remember, the indexing starts with zero.

54
00:04:21,540 --> 00:04:24,930
‫So the last two columns are two and three.

55
00:04:25,560 --> 00:04:26,640
‫Just run this.

56
00:04:27,780 --> 00:04:29,970
‫So our X variable is now ready.

57
00:04:31,040 --> 00:04:34,070
‫Now let's just look at the target variable.

58
00:04:36,240 --> 00:04:44,820
‫Here you can see we have different categories, zero one and two zero stand for Setosa.

59
00:04:45,390 --> 00:04:50,070
‫One stands for versicolor and two stands for Virginica.

60
00:04:51,760 --> 00:04:57,270
‫Now, if you have some machine learning knowledge, you may know that to create classification more

61
00:04:57,270 --> 00:05:00,780
‫than what y variable should be in the form of zero and one.

62
00:05:02,190 --> 00:05:11,910
‫So ideally we want one in all these records where the flower is serosa zero in all this records

63
00:05:12,270 --> 00:05:15,420
‫where the flower is versicolor or virginica.

64
00:05:16,760 --> 00:05:23,000
‫So let's just convert this target variable using some basic operations first.

65
00:05:25,130 --> 00:05:29,660
‫Let's convert this target variable in the form of true and false.

66
00:05:30,450 --> 00:05:34,430
‫We want true where the flower is setosa.

67
00:05:34,820 --> 00:05:37,400
‫That is the value of target is zero.

68
00:05:38,090 --> 00:05:42,770
‫And we want false where the flower is Virginica or versicolor.

69
00:05:43,400 --> 00:05:47,690
‫That is the numerical value is one or two.

70
00:05:49,850 --> 00:05:52,300
‫So let's just run this

71
00:05:52,310 --> 00:05:52,730
‫Command.

72
00:05:53,060 --> 00:05:57,640
‫We are just checking whether the target is equal to zero or not.

73
00:05:58,280 --> 00:06:00,610
‫If the target is zero, we will get true.

74
00:06:00,800 --> 00:06:03,950
‫And if the target is not equal to zero, we will get false.

75
00:06:04,040 --> 00:06:05,000
‫Let's run this.

76
00:06:08,180 --> 00:06:09,980
‫Now let's look at our y variable.

77
00:06:11,650 --> 00:06:18,250
‫You can see we have converted zeros to true and one or two to false.

78
00:06:20,810 --> 00:06:25,820
‫Now, in the next step, we want to convert this true and falls to one zero.

79
00:06:26,330 --> 00:06:28,610
‫We want one in place of true.

80
00:06:28,910 --> 00:06:32,760
‫And we want zero in place of false.

81
00:06:34,670 --> 00:06:39,730
‫So we will just use as type method and we will convert it to int.

82
00:06:39,990 --> 00:06:45,670
‫And so just run and look at our y variable.

83
00:06:47,000 --> 00:06:54,560
‫So as you can see now, our y variable is an array with one and zero.

84
00:06:55,070 --> 00:06:58,900
‫One means setosa and zero means not setosa.

85
00:07:02,390 --> 00:07:07,250
‫Now let's look at our X variable before creating a Perceptron model.

86
00:07:09,350 --> 00:07:16,340
‫You can see our X variable is a two dimensional array with petal length and petal width.

87
00:07:20,360 --> 00:07:22,880
‫No our x and y variables are ready.

88
00:07:23,960 --> 00:07:30,260
‫We just have to create our Perceptron model and train that model using this X and Y variables.

89
00:07:33,230 --> 00:07:40,110
‫We can create a single Perceptron model using sklearn, but to create MLP on multi level perceptron

90
00:07:40,110 --> 00:07:42,690
‫model, we have to use Keras and TensorFlow

91
00:07:44,150 --> 00:07:48,260
‫We will be looking at mlp in the later part of this course.

92
00:07:49,560 --> 00:07:54,920
‫But now we will create this perceptron model using sklearn only.

93
00:07:58,270 --> 00:08:02,950
‫Now first we need to import Perceptron from sklearn.linear model

94
00:08:02,990 --> 00:08:06,330
‫Then let's just import it.

95
00:08:06,990 --> 00:08:10,950
‫And you can also look at the documentation using this link.

96
00:08:11,850 --> 00:08:16,380
‫This Jupyter notebook is also shared in the resources section of this video.

97
00:08:16,890 --> 00:08:20,940
‫So you can download it and run it if you want to practice.

98
00:08:23,100 --> 00:08:27,090
‫So this is the official document of Perceptron in sklearn.

99
00:08:28,360 --> 00:08:34,380
‫You can look at all the parameters which we can give, first one is the penalty.

100
00:08:35,500 --> 00:08:42,400
‫If you remember, in linear regression, there are regularization terms, L1, L2 also known as

101
00:08:42,460 --> 00:08:43,550
‫LASO and Ridge.

102
00:08:45,490 --> 00:08:50,390
‫You can also give alpha, that is the variable we use for regularization.

103
00:08:51,970 --> 00:08:54,880
‫Then there are different other hyper parameters.

104
00:08:54,970 --> 00:08:59,230
‫We will just stick to basic default hyper parameters.

105
00:08:59,860 --> 00:09:01,870
‫But you can look at it if you want.

106
00:09:02,350 --> 00:09:09,130
‫These are just some very basic hyper parameters that we get with most of the machine learning algorithms.

107
00:09:11,240 --> 00:09:19,640
‫Now, as with any other sklearn machine learning model,  we first have to create an object of that algorithm,

108
00:09:20,150 --> 00:09:29,300
‫then we have to fit our X and Y variables into that object and then we can use that object to predict

109
00:09:29,330 --> 00:09:30,910
‫the future values of y.

110
00:09:32,240 --> 00:09:37,430
‫So first, let's create an object that is Perceptron classifier.

111
00:09:37,970 --> 00:09:47,510
‫We are naming our variable is per_clf and then we are using Perceptron that we have

112
00:09:47,510 --> 00:09:51,450
‫just imported and we are giving only one hyper parameter.

113
00:09:51,560 --> 00:09:54,020
‫That is a random state equal to 42.

114
00:09:54,770 --> 00:09:59,330
‫This is basically to reproduce the same result whenever we run this model.

115
00:09:59,840 --> 00:10:04,550
‫So if you give random state, you will always going to get the same result.

116
00:10:06,320 --> 00:10:09,050
‫Giving this hyper parameter is not mandatory.

117
00:10:09,080 --> 00:10:11,840
‫You can skip that also if you want.

118
00:10:12,620 --> 00:10:19,610
‫And in the next line, we are fitting our X and Y variables into this object.

119
00:10:20,030 --> 00:10:21,380
‫So let's run this.

120
00:10:23,480 --> 00:10:24,800
‫So our model is trained

121
00:10:25,400 --> 00:10:27,500
‫You can get the values of default hyperparameter

122
00:10:27,540 --> 00:10:29,300
‫here if you want.

123
00:10:32,030 --> 00:10:35,090
‫Now, let's predict the value using our classifier object.

124
00:10:35,820 --> 00:10:37,940
‫We can just use dot predict method.

125
00:10:38,430 --> 00:10:45,870
‫So this is our object and we are using dot predict and we are giving our independent and variable,

126
00:10:46,020 --> 00:10:49,020
‫which is X as an input.

127
00:10:51,060 --> 00:10:52,170
‫Let's do that.

128
00:10:55,620 --> 00:10:58,350
‫You can see this are the predicted values.

129
00:10:58,740 --> 00:11:02,970
‫We can compare this predicted values with the actual values.

130
00:11:04,200 --> 00:11:06,390
‫There is no need to manually do that.

131
00:11:07,020 --> 00:11:08,580
‫That is an another function.

132
00:11:08,760 --> 00:11:18,270
‫That is accuracy score available in sklearn, which will give us the accuracy of our prediction. Accuracy

133
00:11:18,270 --> 00:11:25,830
‫score varies between zero and one. Zero means zero percent accuracy, which means all the predictions are

134
00:11:25,830 --> 00:11:26,130
‫wrong.

135
00:11:26,820 --> 00:11:31,340
‫And one means hundred percent accuracy, which mean all the predictions are right.

136
00:11:32,970 --> 00:11:39,750
‫So let's just first import the accuracy score from sklearn metrics

137
00:11:40,350 --> 00:11:42,960
‫And then we are going to use accuracy score.

138
00:11:43,920 --> 00:11:46,020
‫And here there are two arguments.

139
00:11:46,050 --> 00:11:48,330
‫First, we have to provide the actual values.

140
00:11:48,570 --> 00:11:52,020
‫And in the next argument, we have to provide the predicted values.

141
00:11:52,770 --> 00:11:55,590
‫So our actual values are stored in just y variable

142
00:11:56,220 --> 00:11:57,570
‫And the predicted values

143
00:11:57,750 --> 00:12:00,120
‫We have restored in y_pred

144
00:12:00,780 --> 00:12:05,550
‫So let's get the accuracy score of our predictions.

145
00:12:06,840 --> 00:12:11,820
‫So the accuracy here is one that is hundred percent accuracy. Our perceptron

146
00:12:11,820 --> 00:12:18,720
‫was able to identify the speci of the floert with 100 percent accuracy.

147
00:12:20,490 --> 00:12:22,290
‫Now, this is a very simple model.

148
00:12:22,350 --> 00:12:28,320
‫Usually we don't use Perceptron for any regression or classification tasks.

149
00:12:29,590 --> 00:12:34,530
‫We usually go for machine learning techniques where the data is not following

150
00:12:34,700 --> 00:12:38,680
‫any complex pattern and we go for nlp

151
00:12:38,960 --> 00:12:39,340
‫where

152
00:12:40,440 --> 00:12:42,810
‫The data is following a very complex pattern.

153
00:12:43,740 --> 00:12:49,030
‫Usually you will never find yourself using Perceptron in business settings.

154
00:12:49,470 --> 00:12:51,360
‫But this is just an introduction.

155
00:12:51,690 --> 00:12:58,100
‫And we want you to give just some basic idea about running Perceptron models using sklearn.

156
00:12:59,910 --> 00:13:03,900
‫Now, after training your model, you will get these two attributes.

157
00:13:04,200 --> 00:13:05,250
‫That is the coefficient.

158
00:13:05,360 --> 00:13:06,510
‫And the intercepts.

159
00:13:08,270 --> 00:13:13,820
‫Basically our Perceptron is dividing this space using a linear regression line.

160
00:13:14,360 --> 00:13:16,130
‫So this is the coefficient.

161
00:13:17,030 --> 00:13:20,270
‫And this is the intercept of that line.

162
00:13:21,440 --> 00:13:31,130
‫So your equation of that line will be four minus one point four times petal length minus two point

163
00:13:31,130 --> 00:13:31,790
‫two times.

164
00:13:31,810 --> 00:13:32,500
‫petal width.

165
00:13:34,750 --> 00:13:43,990
‫So if you want to create that line, you can use coefficient and intercept values to do that, and you

166
00:13:43,990 --> 00:13:49,510
‫can also see the impact of our different variables on the Y variable.

167
00:13:50,410 --> 00:13:55,360
‫So this coefficient is giving you the impact of each of those variables.

168
00:13:55,570 --> 00:13:59,350
‫This is the impact of petal length and this the impact of petal width.

169
00:14:01,210 --> 00:14:02,650
‫That's all for this lecture.

170
00:14:03,970 --> 00:14:09,880
‫Next, we will look at tensor flow and Keras to create our MLP model.

171
00:14:10,210 --> 00:14:10,600
‫Thank you.

