Predictive analytics one of the most important modules that is a prerequisite in your data AI journey be it data analytics data science or AI engineering this is Satijit Patnak welcoming you all to my channel which is famous for such end toend tutorials around data analytics data science and AI and welcome you back to another beautiful end to end series on predictive analytics this is going to be a 5hour offers one-stop solution for predictive analytics in case you are getting started with your journey in data analytics into data science or even AI engineering. Predictive analytics is one of the most important modules and in this video we are going to talk about various aspects of predictive analytics. If you want the codes used in this particular tutorial, just write down P and at 100 likes and 50 comments, we will be sending you all the codes used in this tutorial. Apart from this, if you have a requirement on any such topic, be it anything around data analytics, data science, AI, agentic AI, let me know what should be our next end toend tutorial and we will also come up with that. Without further ado, let's get started. Enjoy this video. Hi, welcome to this module on predictive analytics. Before getting into predictive analytics, let's try to understand the different types of analytics. Analytics are majorly of four types. One is descriptive analytics. One is diagnostic analytics, one is prescriptive analytics and one is predictive analytics and all these combined together to form the analytics domain. Descriptive is basically what happened. If you have a data, if you are able to find out what has happened, what are the people that are churning, what are the possible people that are marked as fraudulent, what are the characteristics. So the what part is the descriptive analytics. Diagnostic is your diagnosing the problem. What could be the problem? Why it happened? If 20% of the customers have left the company, why they have left? Prescriptive is nothing but how can we make it happen? The how part and predictive is what will happen. That means the future part. Now let's say you have a data set of customers data where 20% is your churn rate. That means 20% of your customers are churning on a monthly basis or on a yearly basis. The what will happen part basically means if by taking the data you are able to create a predictive model. The model will be able to understand the pattern and for a new customer it will be able to predict that is basically a predictive analytics part. So going forward let's try to understand more about predictive analytics. Predictive analytics is the branch of the advanced analytics which is used to make predictions about unknown future events. Predictive analytics uses many techniques using data mining, statistics, modeling, machine learning, AI to analyze current data to make predictions. So in simple layman's term predictive analytics is that field of analytics is that branch of analytics which is used to make predictions. Now let's say you have data customers data and 20% customers have joined 80% customers are active. If you are able to create a model, let's consider it as just a model. The model will be able to read and understand the pattern, understand the behavior and we'll be able to predict the future. That means for a given new customer, will it be able to predict it as active or churned? What will be the customer after 6 months? Will he stay in the company or will he leave the company? That is what the predictive analytics part is. Another use case of predictive analytics is let's say you are analyzing your country's temperature data. Let's say you are from Malaysia and you are analyzing your country's temperature data. So you have columns like date, temperature, rainfall, precipitation and so on. You need to predict temperature. So you have existing 24 months of data let's say starting from 2021 till end of 2022. If somebody asks you can you do a prediction for the entire January month 2023 the answer is yes it is quite possible and how it is possible by using predictive analytics. Further inside predictive analytics there are multiple other subbranches or subm modules which we will be covering in the future. The subm modules includes classification, regression, sometimes series analysis and so on. But I hope you got a fair amount of idea about the predictive analytics parts. It's simply nothing but a data is available when you are analyzing the data. That's the descriptive part. When you analyze the data and make a model which is able to understand the data, understand the pattern, understand the behavior and is able to predict something for the future or predict something for a record that is not a part of your original data set. That is your predictive analytics part. That is your predictive model. Let's talk about the predictive analytics process. It all starts with data. Data is the core. Without data, nothing is possible. Be it your predictive analytics, your descriptive analytics or be anything. Data is mandatory. Without data, nothing is possible. Data needed for predictive analytics is usually a mixture of historical data and real-time data. Historical data is that kind of data that you have already been capturing from the past couple of months, couple of years or couple of weeks. And real time data is real time. Now for example, you are analyzing let's say temperature. You are you are you are analyzing the temperature data of a particular area let's say Tokyo. I'm not pretty sure about the temperature in Tokyo right now, but let's assume from the last 5 days or 10 days or 20 days, the temperature is somewhere around uh uh 10 15° C. Let's say 10° then 11° then 12° then 13° then 11° and so on. So from Jan 21 till December 2022 you have the historical data. You create a model on January 1st 2023. Now right now your model is using historical data which is from January 2021 till December 2022 which is almost 2 years data. But the model after January 1st when it will read the January 2nd data obviously it is using the historical data and also the real time existing data or else a realtime example could be stock market. Everybody knows that stock market values changes every minute or every 5 minutes. That means when you are building some models, the model is working on a real-time data, a real-time streaming data, right? So that is basically real-time data and historical data is the data that we have been capturing from the past couple of years. Reporting and analysis. Reporting includes creating, configuring, consolidating, organizing, formatting and summarizing. detailed examination of the elements or structure of something. Monitoring data monitoring is a business practice in which critical business data is routinely checked against quality control rules. And then we have predictive analytics which is the branch of advanced analytics that makes predictions about future outcomes using historical data combined with statistical modeling, data mining techniques and machine learning. Now this particular screen will help you out in understanding more about the predictive modeling part. Now before talking about all these points, let me just quickly refresh your memory on what we studied when we were talking about how to build an end toend data analytics pipeline. I hope everybody remembers it all starts with business understanding. Then comes data understanding. Then comes data collection. After your data is collected, then comes your data preparation. After your data preparation is done, which is basically clean data, then you perform EDA, which is your descriptive part. After EDA is done, your next step is usually to create a model and then once your model is done, you have to deploy it. Basically, a data analytics solution is limited here. But sometimes when you are solving a data science problem or a predictive modeling problem in that case you can also use these two steps. Predictive modeling or predictive analytics however is a part of data analytics but in the industry we treat predictive analytics as a part of data science because predictive analytics or predictive modeling is simply nothing but machine learning. Now when it comes to model, now model is just one step. Inside the model, there are further substeps and they are creating a model, testing the model, validating the model and evaluating the model. And once everything is done, then we have to deploy the model. What do you mean by creating the model? Creating the model simply means you have an existing data 10,000 records. Some customers are churned let's say 2,000 are churned which is 20%. Rest 8,000 are active which is 80%. And using that data you create a model which is able to read these records understand which customer is churned which is active and so on and it has created the model. How do you test the model? Normally while testing the model it is always recommended to create a model on training data. Now what do you mean by training data? Imagine your entire data set is of 10,000 records. If you use the entire data set to create a model, your model will be trained on 10,000 records. That means the model understands and knows all the patterns inside the 10,000 records. How will the model be tested? The model cannot be tested if it is trained on the entire population. But instead of 10,000 records, if you randomly split them into two books or two chunks, let's say 7.5K in one group and 2.5K in another group. Now this 7.5K could have again 80/20 ratio of churned and active customers. Same with this Okay. Now what happens here is we treat this as training data and treat this as testing data. Now this training data goes and the model is created using 7.5,000 records or 7,500 records. And then the model is tested on 2500 records. Now for these 2500 records, we already know which of them are active, which of them are churned. But here what happens is the model predicts which records are active, which records are churned. Then what we do is we compare the predicted values with the actual test values. And this is basically the validating step. and then we evaluate the model whether the model is fine or not. In real time scenarios we create multiple models. So in real time scenario how it happens probably I'll take another example for better understanding. I'll take a similar example uh but I I'll take a similar example but the use case will be different so that you can understand what's going on. Let's say I'm talking about fraud detection. Fraud detection. I have a data set of 10,000 records. Out of them, 9,000 records are not fraud and 1,000 records are fraud. So what is the fraud ratio? 10 is to 90 or 1 is to 9. So 10% of my records are fraudulent records. The very first step that we do in machine learning or predictive analytics is we divide the data set into training and test. Now the division into training and test the split into training and test happens randomly. There is a concept there is a library that we import input skarn library. Inside skarn library there is a method called as train test split and then we split the records. Usually we split it into 75% and 25%. Sometimes we also do 80/20. There is no thumb rule for this. So what do you mean by 75% records? That means 7,500 records are here and 2,500 records are here. So out of 75,000 records, how many are fraudulent? You can equally distribute them based on this or randomly distribute them. Again, that is depending on your sampling technique. Let's assume that we are doing a stratified sampling. I hope everybody knows about stratified sampling. We are doing a stratified sampling and out of this 7,500 records 75 are fraudulent. Uh 1,910 10% 10% is 750 750 are fraudulent and rest 6 uh 750 are not fraudulent. And out of this 2,500 250 are fraudulent and rest 2,250 are not fraudulent. Now what happens is from this 7,500 records we kind of create models. We create multiple models. Model one, model 2, model n dot dot dot. That means multiple models we create and all the models are created on training data. I will mark it as TR and T. All the models are created on training data. Then what happens? Then models are tested on testing data. Then we do some predictions. Predictions. Now there are 2500 testing records. So 2500 predictions will be done. Now I cannot show you by taking 2500 records. Imagine there are only five records as part of testing data. Out of those five records, the first one is fraudulent, second is not fraudulent, not fraudulent, not fraudulent and not fraudulent. Imagine now this model has predicted this as fraudulent. This as fraudulent and others are not fraudulent. The second model has predicted this as fraudulent. Not fraudulent, not fraudulent, not fraudulent, fraudulent. And the the nth record and the nth model has predicted this as fraudulent. Not fraudulent, not fraudulent, not fraudulent, not fraudulent. What is the error here? There is one error here. So the accuracy is 80%. What is the error here? The error here is here. So accuracy is 80%. Here there are no errors. So accuracy is 100%. Now I'm just taking an example with five records. Imagine this thing happens with 2500 records and finally we get accuracy and different other metrics from each model. Let's say this model is giving me 81% accuracy. This is giving 82 dot dot dot and this is giving you 78%. Then what we do is we finalize one of them models that this is the best model. Then what we do is we go ahead and again there are some further steps in predictive analytics which includes feature engineering hyperparameter optimization and then we fine-tune the model to boost the accuracy. As a data scientist or as a data analyst our first focus is to boost this. If you are able to get a 90% accuracy on your testing data, then the model is good enough for the deployment part and then the model goes and it is deployed and that's the entire picture of how a predictive modeling or a predictive model is created. How machine learning really works. That's all about this particular video. In the next video, we shall be talking more about the different use cases in predictive analytics and various other things. In this video, we shall be talking about how does the model work. What is a model? I hope everybody is quite clear with this. But how does the model work? In predictive modeling, data is collected for the relevant predictors. A statistical model is formulated, predictions are made and the model is validated or revised as additional data becomes available. The model may employ a simple linear equation or a complex neural network mapped out by sophisticated software. In this particular slide, you will learn what a predictive model is and how by actively guiding marketing campaigns, it constitutes a key form of business intelligence and data analytics. We'll take a look inside to see how a model works. Predictors rank your customers to guide your marketing. Combined predictors means smarter rankings. The computer makes your model from your customer data. A simple curve shows how well your model works. So normally in simple layman's term, a model is something that is being created from the historical data. Using your historical data that model is created which has learned something from the data that we have based on the combinations and permutations possible in the data set. The model has already acquired a lot of knowledge and has been trained. Now when your model sees a new data point or for a new customer, the model will be able to formulate some equations inside it based on the type of model that is being created and based on that the model will be able to predict whether that customer is going to be a fraudulent or non-fraudulent and that's how your model works. In simple layman's term, let me just quickly talk about something called as neural networks. We all know human brains are created in a similar fashion. Our brain constitutes of neurons. Imagine we have a small baby. Let's say there is a small baby and the baby is just a newborn baby 2 to 3 months old. That's it. Now when the baby spots a cat, I'm pretty much sure the baby will not be able to recognize it's a cat because her memory is empty. Her memory is not aware of an object or an animal called as cat. But her mother or her father, her parents teach the baby this is a cat. This is a cat and it goes on for 6 months or 7 months. Now in that duration of 6 months or 7 months, there are thousand instances where the baby has already spotted multiple cats. Maybe several times the baby has also encountered dogs. Several instances the baby has also encountered other animals. Now which basically means from the last six to seven months the baby's brain is fed with so many images of cats, dogs and different animals. And who is training the baby's brain? Her parents. So her parents are the source of data. Her parents are helping her to understand who is a cat and who is a dog. After 6 months, if a baby sees a cat, there is a higher chance that the baby will be able to recognize. Why? Because thousand images has already been trained for cats and thousand images for dogs and thousand images for other animals has already gone to the baby's brain. Baby's brain is nothing but a concept similar to a machine learning or a neural network model. Her brain has already processed thousands and thousands of images and that is because of because of the training part. The baby is able to recognize a cat or a dog and that is exactly how a model works. You feed the data to the model. The model automatically understands the pattern with human eyes. It will be very difficult to understand the pattern. Somehow using ADA or doing predict uh descriptive analytics, you will be able to come across some patterns what the data is telling you. But it's very complex. Just by looking at the data, you will not be able to do that. But predictive model is something which has been fed with different types of historical data and the model is good enough to do a prediction on an unknown object or an unknown data point or for a new customer. That is how your model works. The next big question is why predictive modeling? Why there is a need of predictive modeling? Honestly speaking, predictive modeling is one of the most important and critical aspects in the field of data analytics and data science. It is very much needed. Nearly every business in competitive markets will eventually need to do predictive modeling to remain ahead of the curve. Predictive modeling also known as predictive analytics is the process of automatically detecting patterns in data and then using those patterns to foretell some event. Imagine you are running a company and you are able to predict let's say you are running a telecom company. There are certain amount of people those are leaving the company every month. 20% 25% of the people are leaving. Everybody's reason of leaving will not be same. Let's let's just accept that fact. If every customer's reason is not same, we will it's nearly impossible to predict who is going to churn, who's not going to churn. But using the help of predictive modeling, however, you will be able to predict whether a new customer or an existing customer is likely to predict in the next 6 months, 12 months or 18 months. And that likelihood because of that if you are able to understand the reason why they're leaving by doing some predictive modeling somehow if you know the reason you can probably pitch them with some offers and try to retain them. Even if you retain one out of thousand customers it is a win-win situation for the company. And our ultimate goal as a data scientist or as a data analyst is to retain the customers. So our model will be able to predict the customers who who is having a higher chance of getting churned and if we get those list of customers we can probably offer them something to kind of retain them. So predictive modeling has a lots and lots of use cases. We will be talking about each and every use case in the next particular video. Let's talk about the applications of predictive modeling. Now there are multiple applications of predictive modeling. Any model that is able to predict something is basically considered as a predictive model. So there are hundreds and hundreds of use cases across multiple domains to predict. If you talk about healthcare, in health care you can potentially predict whether a customer is having COVID or not by just scanning their X-ray images. Or else you can analyze the blood sugar level or multiple other factors of different patients and probably can understand or predict whether a customer is likely to have cancer or not or any other disease or not. So disease prediction is one of the classical examples in the healthcare domain, COVID detection, disease detection. If you talk about uh some leaves, whether the leaves are having disease or not, multiple use cases are there in healthcare which will be able to be a part of predictive modeling. Coming back to insurance. Yes, there are again a lot of use cases in insurance. Cross-selling, upselling, upselling basically means a customer. There are two types of customer. Two customers, let's say A and B. And both are kind of similar in nature. A has bought two insurance policies. Let's say policy 1 and policy 2. B has bought policy one. As A and B are similar in nature, if you recommend P2 to this user, there are high chances that the user will take. This is also an application of predictive modeling. So be it analytical, customer relationship management, healthcare, collection analytics, cross-selling, fraud detection, risk management, all these are different use cases of predictive modeling. Apart from them there are use cases like churn prediction whether a customer is going to be churned or not churned. Upselling whether a customer will take an offer or not. Disease prediction by analyzing the voice data. If you are able to predict whether a customer is smoker or non-smoker, well that is again a use case for predictive modeling. And there are thousands and thousands of use cases. If you are interested in a particular domain, let's say healthcare, banking, telecom, shipping, any kind of industry, just go to the website, go to google.com, search your domain and just search predictive modeling in banking use cases or predictive modeling in telecom use cases or machine learning use cases in banking. Just simply Google it out. There will be lots and lots of blogs that talks about various use cases and you'll be having a good experience and knowledge from that. Hi, let's talk about machine learning otherwise called as predictive analytics. Machine learning is an application of artificial intelligence that involves algorithms and data that automatically analyze and make decision by itself without human intervention. It describes how computer perform tasks on their own by previous experiences. Therefore we can say in machine learning language artificial intelligence is generated on the basis of experience. So before getting into the next topic which is all about the types of machine learning and the further topics let's try to talk about one of the most important question that everybody's confused with and the question is what is the difference between AI, machine learning, deep learning and data science. These are the four jarens that are mostly used and people are still not clear with each and every concept. Now imagine I'll draw a very big circle. Now this big circle is called as AI. AI which basically means artificial intelligence. That means a type of intelligence that is achieved artificially. Whatever you have right now in your brain is your natural intelligence that you have inherited from your parents genetically and somehow by reading and studying you also make your intelligence more stronger but that is still your natural intelligence. Artificial intelligence is achieved artificially. There are multiple areas of researches that basically leads to AI and those areas of researches are these small circles and one of these is basically your machine learning. So machine learning is one of the area of research that leads to AI. What are the other circles? They could be natural language processing. They could be robot process automation. They could be neural networks. They could be speech to text, text to speech and so on. So there are multiple areas of researches that basically leads to AI. Now what is DL? Deep learning. Deep learning is nothing but advanced machine learning. However, I have drawn NN here, neural networks, but it basically comes under ML. Probably I'll rub it off and I will write it down as computer vision. Computer vision is another area of research. So, deep learning is nothing but a part of machine learning. again. So if I draw machine learning which is a zoomed version of this one, there is something called as deep learning. So anything that is not possible with your traditional machine learning algorithms, you can take help of neural networks and deep learning concepts to solve those problems. And what do we study inside deep learning? Concepts like how to analyze images, how to analyze voice data, how to analyze video data, all these things are not possible for ML algorithms. So we take help of DL algorithms. But ultimately they are same are part of machine learning. So DL is a subset of ML which is again a subset of AI. But everything is fine. What is data science? Data science is nothing but the study of data. Some of you might already be be a student of computer science or IT or electronics engineering or civil engineering. Now I'll take example of computer science engineering. Imagine you are a computer science engineer. What do you study in your bachelor's degree? You study about computers. You study about various computer programming languages. You study about C, C++, Java and so on. So computer science is a major subject in your bachelor's degree. If you are doing computer science engineering, so computer science engineering is simply nothing but the study of computers. Similarly, the study of data is called as data science. So it could use ML, it could use DL, it could be NLP, it could be robotics, RPA, it could be anything else. If you are trying to study data, that is basically your data science. So that's all about the overall concept behind machine learning otherwise known as predictive analytics for a data analyst. In the next video, we shall be further talking about various types of machine learning and I will tell you which type of machine learnings we should focus more on because they are the predictive analytics part. There is also a non-predictive analytics part inside machine learning which we will be covering in the next video as well. Machine learning is a subset of AI which enables the machine to automatically learn from data, improve performance from past experiences and make predictions. Machine learning contains a set of algorithms that work on a huge amount of data be it historical or real time. Data is fed to these algorithms to train them and on the basis of training they build the model and perform a specific task. These machine learning algorithms help to solve different business problems like regression, classification, forecasting etc etc. Based on the methods and way of learning, machine learning is majorly divided into four categories. But on paper, there are three supervised learning, unsupervised learning and reinforcement learning. We also have the fourth type which is a combination of supervised plus unsupervised otherwise called as semi-supervised learning. Now what do you mean by supervised learning? When it comes to machine learning, these are the three or four different types of machine learning. But when it comes to predictive analytics or the predictive model building, we will majorly be focusing on the supervised learning part. However, in this module, I will also talk about some of the unsupervised learning techniques like clustering. But usually clustering is not studied under predictive analytics because the reason will be talked in the clustering session itself but in this next couple of videos we will be majorly focusing on the supervised learning part. Supervised learning is further into multiple categories. One of them being classification regression. Time series forecasting also comes under supervised learning. So as the name suggests supervised machine learning is based on supervision. It means in the supervised learning technique we train the machines using labeled data. So supervised learning is all about labeled data. The data is labeled. Here the labeled data specifies that some of the inputs are already mapped to the output. More precisely we can say first we train the machine with the input and corresponding output and then we ask the machine to predict the output using the test data. I'll give you a quick example on what exactly do we mean by labeled data. I have a data set 10,000 records, 9,000 fraudulent and 1,000 not fraud. Sorry, my bad. 9,000 not fraudulent and 1,000 fraudulent records. So almost 10% fraudulent transactions. I will divide the data into training and test. Let's say training has 8,000 records and testing has 2,000 records. Now when you're training the when you're creating the model which is being trained on the training data it knows that 8 8,000 customers data is fed. It knows how many of them are fraudulent and how many of them are not fraudulent. So the labels are known. When the model is ready it predicts on the testing data. The testing data is also known but only the information only the transaction information is passed here. We don't pass which transaction is fraudulent which is not fraudulent. While testing you just pass rest all the columns. Let's say imagine you have a data set. There are multiple columns column 1 column 2 column 3 column 4 and this column tells whether it is a fraud or a not fraud. fraud, not fraud. You don't pass this column while predicting. But once your predictions are done, you compare the predictions value with this actual value. That is what is called as labeled data. So we usually deal with labeled data. We know that some of these transactions are fraudulent and some of them are not fraudulent. So they are called as supervised learning. What do you mean by regression? Now this is an example of classification because we are classifying. What do you mean by regression? Regression is another type of supervised learning. Well, classification algorithms are used to solve the classification problems in which the output variable is categorized as fraud not fraud, yes or no, churned or not churned, diseased or non-dised and so on. Some of the classic examples in classification are spam detection, email filtering, fraud detection, churn analysis. All these are different use cases. disease prediction and so on. There are multiple algorithms inside classification. We will be focusing on some of the important ones in this particular module but there are more than 20 30 40 algorithms. Some of the algorithms are K nearest neighbors, decision and trees, random forest, logistic regression, knife based classifiers, XG boost, cat boost, ADA boost and so on. Many many algorithms are there but we will be focusing on some of the important ones in this module. After classification comes regression. What do you mean by regression? Regression algorithms are used to solve regression problems in which there is a linear relationship between input and output and based on that you use to predict the continuous output variable. For example, I'll give an example. Let's say you have multiple customers height and weight. Given certain height, you have some weights. Now what you do is you start plotting them first, second, third data point, fourth data point. Now if somebody asks what will be the weight at a certain height let's say at this height what will be the weight? Now using the existing data what happens here is you create a linear relationship once you create a linear relationship and then you kind of plot this data point on top of this line wherever it kind of deviates or dissects the line that becomes your predicted weight variable. predicted weight. That means based on one particular variable, you are predicting the second variable. House price prediction is an example of regression. Let's say there are different types of houses, different areas and you're predicting the price. Let's say in the existing data for a 500 ft² let's say the cost is 2 million. For 1,000 square ft it is 4 million. for 600 square ft² it is 3 million and so on. Somebody asks you what will be the price at 200 square ft. So using this data let's say you try to plot area and price 2 4 6 8 10 price is 1 2 3 4 5 6. So at 500 it is 2 million. So 2 4 5 it is 2 million somewhere here. At 1,000 it is 4 million somewhere here. At 600 it is 3 million somewhere here. So if you draw a line that goes through these points not exactly through all the points but you have to find that line which is closest to these data points. Now there are techniques to find that line and that's all about your regression problem. I'm just explaining you from a very overall point of view from from a very layman's point of view. Now once you get this best fit line you have to find what is the price at 200. So 200 price will be somewhere here. So let's say the value is around uh I don't know 1.5 million. So you predict that the value at 200 square ft² is around 1.5 million. So that is a regression problem. So problems like house price prediction, rainfall prediction, temperature prediction and all those things, weather forecasting comes under your regression problem. Forecasting is an extension of regression. Forecasting is an extension of regression. But majorly we consider a problem as a forecasting problem, time series forecasting problem when it has a date component in it. So let's say you also have a date component. That means on this date the price was this. On this date the price was this. On this date the price deviated. Now if somebody asks at this particular day what will be the price? In that case it becomes a forecasting problem. time series forecasting problem. So in time series forecasting problem, time plays the most vital role. So time is the most important feature in that. So once we jump into regression and forecasting, you will have better understanding on that. What are some of the advantages of supervised learning? Since supervised learning work with the labeled data set, so we can have an exact idea about the classes of objects and these algorithms are helpful in predicting the output on the basis of prior experience. These algorithms are not able to solve complex tasks though it may predict the wrong output if the test data is different from the training data. Now there are some drawbacks of supervised learning as well. Let's say one of the examples I have a data set 10,000 records 9,000 not fraud and 1,000 fraud. Imagine while creating the training and test samples. Imagine all these 1,000 records comes into the training data or maybe just 100 records comes into this training data. For example, just very few records. Now, when you are creating model, out of the 8,000 records, 7,900 are not fraudulent. Only 100 are fraudulent. That means the model is not learning properly. Imagine the model is being trained with 7,900 cat images and only 100 dog images. Will the model be able to learn? No. because there are very few records from one particular class. So the model will not be able to learn. So dividing the data into a proper training and test is very important. So sometimes your predictions may go wrong. So when the predictions goes wrong, you always have to come back to step number one and revise everything. And that is one of the reasons why predictive models or machine learning models are always an iterative process. Another drawback of supervised learning will be it requires a lot of computational time to train the algorithms. So summarizing everything the advantages of supervised learning will be it will be able to predict the classes if you have the distributed the classes properly. It takes help of the prior experience. Some of the disadvantages will be. It might not be able to solve complex tasks. You always have to worry about your split on how your training and test split happens and computational time is high. So memory consumption will also be high in order to run machine learning or supervised learning problem. Some of the applications of supervised learning will be image segmentation, medical diagnosis, fraud detection, spam detection and so on. Now talking about unsupervised learning. Unsupervised learning is a type of learning where you deal with unlabeled data. It is different from the supervised learning technique and deals with unlabelled data. Imagine a similar example. Similar example can also be treated as an unsupervised learning technique. Imagine you have a data set 10,000 transactions. You don't know which of them is a non which of them is a fraud and which of them is a not fraud. So there are two solutions to it. Either you convert it into a supervised learning problem by manually labeling them using your domain expertise. If you're able to do that, it becomes a supervised problem. Otherwise, treat it as an unsupervised problem and try to do some clustering techniques to understand which transactions are similar in nature. So that is also possible. So unsupervised learning is a particular type of learning where the data is not labeled. If somebody gives you that hey you know what these are the list of news can you create groups out of them. If you know how many groups do we have to create let's say we have to create three groups entertainment sports and politics. And you know that existing news that has been given has those labels. In that case you will be able to create groups and you will be able to create models. If a new news comes in you will be able to predict whether it falls in this category or that category. But if you don't know how many groups then in that case it becomes a unsupervised learning problem. Some of the classic examples of unsupervised learning problem is again fraud detection. if it does not have labels. Another classic example is if somebody gives you a list of millions of customers and tells can you do some customer segmentation. I want to find which customers are similar in nature. Again in that case unsupervised learning will be used. Some of the algorithms under unsupervised learning are C means clustering, mean shift, DB scan, hierarchical clustering otherwise called as uh aglumerative clustering concepts like PCA, LDA, all these things comes under your unsupervised learning. In this particular module, we will be covering some of the clustering techniques though they are not part of predictive analytics. Lastly, another type of learning is reinforcement learning which basically means that it is a feedback based learning. That means you take feedbacks and try to rectify your mistakes. For example, you are talking to a chatbot. A chatbot usually learns with every new um communication. If you are making new communications with chatbot, most of the chatbots try to learn by themselves automatically. Those type of chatbots are created using reinforcement learning. So in simple terms, reinforcement learning works on a feedback based process in which an AI agent or a computer software component automatically explores its surrounding by hit and trial, some taking some actions, learning from experiences and improving its performance. Again, these are some advanced topics which is again not required. So we'll be majorly focusing on supervised. And the last type is semi-supervised which is a combination of supervised and unsupervised. This could be an example of semosupervised. When you have an unsupervised data set, you are manually labeling them, making a supervised data set and then using supervised models, you are solving them. That could be a use case of semi-supervised learning problems. So that's all about types of machine learning in a nutshell. In the next video, we shall be jumping into supervised learning and we'll be trying to learn more and more concepts related to them. As we discussed in the last session that supervised learning is further having multiple types, one of them being classification and one of them being regression. We'll be talking about regression in one of our future videos. Let's try to talk about classification. As the name suggests, classification is a technique where you deal with classes. Classification algorithms used in machine learning utilize input training data for the purpose of predicting the likelihood or probability that the data that follows will fall into one of the predetermined categories. One of the most common applications of classification is for filtering emails into spam or non-spam as used by today's top email service providers. So we'll try to talk about spam and non-spam example. Let's say you have multiple emails. Some of these emails are tagged as spam and some of them are tagged as not spam. Now obviously when an email is tagged as spam could be because of multiple functionalities or multiple characteristics because of which it is marked as spam. Let's say their body has a word called as offer or their title has something called as collaboration or lottery. So these kind of words are important words and they basically help you identify spams. So basically based on the data that you have if you train it obviously you divide it into training and test. Once you train it the model will be able to understand why this is treated as a spam and why this is treated as a not spam. So when a new email comes in based on the existing data that has been trained, it will be able to predict whether a new email is a spam or a not spam. And based on that all the email providers work in Gmail also you might see some of your mails going into spam. That could be because of the inbuilt spam detector or spam filtering technique used by Gmail. Sometimes some of these emails are not spam. They are tagged as spam by Gmail. If you go ahead and mark them as safe, next time automatically it will be rectified and they will not push those kind of emails to your spam folder. Another example of classification will be let's say fraud and not fraud and obviously the data set having the labels. Let's say I am having millions of transactions. I know some of them are fraud and some of them are not fraud. I divide the data into training and test. Training data, I create a model. Now the model knows the characteristics of fraud and not fraud. So when you test it from the testing data, it basically uses its own knowledge from the training data and predicts for the testing data. And whatever it predicts, we compare predictions with the actual labels and then come up to a conclusion whether the model is performing well or not. And similarly we also create multiple models and whichever is performing good we kind of consider that for the next step and then do further fine-tuning and then finally create our final model which is ready for deployment. So that's all about classification in nutshell. As I already discussed in classification there are multiple algorithms. One of them is K and N. K nearest neighbors. One of them being decision tree random forest. Knive base classification base. Logistic regression. Logistic regression even though the name is regression but it is considered as a classifier. Apart from that support vector machines, XG boost, CAD boost, ada boost, extra trace classifier and so on. There are many many such algorithms. Learning each and every algorithm is not possible. But each and every algorithm somehow does the same task. They understands the training data and based on that they understand the patterns and predict on the testing data and once your final model is ready when a new transaction an alien transaction comes in it will be able to predict whether it is fraud or not fraud that's the task of all these algorithms but the way these algorithms work the concepts the mathematics the statistics behind the algorithms are completely different. In the next video, we shall be talking about some of these algorithms which are one of the most al most important algorithms for classification. And once we are good with the explanation part of the algorithms, we will also take a public data set similar to this one like a fraud detection or a churn prediction data set and we will be implementing these algorithms in Python using some of the libraries in Python. In this video, we shall be talking about the first algorithm inside classification which is K nearest neighbors. Now, what do you mean by K nearest neighbors. It simply means how many nearest neighbors do we have for a particular data point and based on that we will be assigning that data point to a particular class. In simple terms, it is a supervised machine learning algorithm and K nearest neighbors algorithm can be used for both classification and regression. It is one of the simplest machine learning algorithms based on supervised learning technique. But in this particular case, we will be using this algorithm for classification. And we will see how K nearest neighbors algorithm works and how is it able to classify. The number of nearest neighbors to a new unknown variable that has to be predicted or classified is denoted by the symbol K. Now let's take an example of this particular diagram. Before that we'll try to we'll try to draw a diagram from scratch. So let's say this is my predictor one. So I'll take a small example. I'm doing the same churn analysis data set. One of the columns I have is churn which says whether a customer has churned or not churned. Churned or not churned. Churned or not churned. And there are multiple features. Now as we are drawing a two-dimensional graph, I can only pick two features because it is visually not possible to plot 3D, 4D, 5D or n dimensional graphs, right? So instead of multiple predictors, I will be using two of them. Let's say one of them is age and one of them is uh salary. Age and salary. Now based on my age and salary I will try to plot which of them are churned and which of them are not churned. So for churned probably I will plot this churned churned churned churned churned churned churned for not churned that means active probably active active active active active active. Now these are my six data points from active and 1 2 3 4 5 6 seven data points from my churn list. What does this mean? This means this graph has been plotted by using 13 data points. Now the question is very simple. If I have a particular age of an employee and salary of an employee, I should be able to predict whether that customer is going to be churned or not churned. As simple as that. So how does KN&N algorithm solve this problem? So we'll try to plot this data point here. Let's say arbitrarily the data point is I'll use a separate color. Let's say this is the data point. Now my task is to find the K nearest neighbors to it. Now what is K? K value depends on the type of classification problem. In this case we are using two classes. We are having two classes which is churned or active. Churned or not churned. So this type of classification problem is called as a binary classification problem. You could also come across multiple other classification problems where there will be more classes. For example, you are having multiple images. Some of them are cats, some of them are dogs, some of them are horses. Now, this is a multiclass classification problem. Anything that has more than two classes are called as multiclass. In this case, it's a binary problem. That means we have two classes. So normally in case of binary problems your k can be any odd number starting from three or n + 1. So three, it could be five, it could be seven, it could be 9 dot dot dot. What is the optimum value of k? You can use as per your expertise, as per your knowledge. It's a hit and trial process. Let's start with three. What do you mean by k equals to 3? That means three nearest neighbors. That means for this particular data point, I need to find the three nearest neighbors. And how to find the three nearest neighbors? What I can do is I'll probably draw a line from each of these data points to that final data point. 1 2 3 4 5 6. this this this this this this right so what I'm doing is I'm trying to find the distance between multiple points to this particular data point and I want to find what what are the three nearest neighbors that means which data points are the three nearest data points to the cross object or to the new data point for this particular data point Right. Let me erase it a little bit so that it does not create confusion. Yeah. Now before getting into this, I'll try to talk about another mathematical concept which is how to find the distance between two different points. I'll try to take help of this graph. Let's say there is a point here. Its coordinates are 2 and 3. There is another point here. Its coordinates are 8 and 6. Let's say I will give it as A. I'll give it as B. Now mathematically what will be the distance from A and B? Mathematically the distance from a and b will be x1 - x2 squared plus y1 - y2 square roo over that is the mathematical formula to find the distance between two lines. Okay. So what is my x1? My x1 is 2. 2 - 8 squared. Your x1 can be 2, it can be 8 because 2 - 8 square and 8 - 2 square are same, right? - 6 square is also 36 + 6 whole square is also 36. So it can be either way around and y1 - y2 will be 3 - 6 3 - 6 square roo over which will be 36 + 3² 9 over which will be 45 roo over and roo over of 45 should be somewhere around 6 7 or 6.8. You can cross check it with your calculator. 45 roo over is 6.7. So that is the distance between A and B. Another technique is also there using the hypotenus technique right angle triangle. Let's say I will name it as C. Obviously this will be in the Xaxis it will be 8. In the y-axis it will be three. So what is the distance here? The distance here is 6 units and the distance here is 3 units. Now my hypotenus square is nothing but my base square plus my height square right and ab will be the root over of that which is 6 square + 3 square roo over which is again 45 roo over which is 6.7 same. So we will be using this technique. This particular technique in mathematics is called as ukidian distance. So if somebody asks you between two points what is the formula of ukidian distance you can talk about this formula. So coming back to this example, what happens here is each and every data point till this X X is our new data point which our KN algorithm is predicting going to predict. So I'll be calculating the distance between all the points to this X. And let's say this, this and this are the three nearest neighbors or these are the three data points having the lowest ukidian distance from the new data point. Now out of these three, one belongs to class A and two belongs to class B. And as per voting this becomes class B whatever the class is it could be churned or active right and this is how it works if you want to consider K equals to 5 again let's say the fourth lowest is this and the fifth lowest is this now as per K equals to 5 it basically belongs to class A so a lot of hit and trial processes goes on internally and you will be able to get the optimum K value and you will also be able to predict the out predict the output predict the class of your unknown data point. That is all about K nearest neighbors. In the next example, I will take an example using an Excel sheet using a small use case and I will be explaining you how to calculate K nearest neighbors, how to use K nearest neighbors to solve that problem and further we will jump into Python code where we will be implementing this algorithm and the Python code is going to be one of the easiest things to implement because we will be using an existing library and we will be calling this algorithm and solving a small problem. That's all about this particular video on the basics of KNN. In the next videos, we'll be talking more about KN&N using other examples. So we'll jump onto an example where we will try to solve this particular example using KNN algorithm using the K nearest neighbor algorithm. Now let's try to explain this particular use case. I have list of multiple players their respective ages and their respective genders and based on their age and gender what type of game they prefer whether it is cricket or football. Now the question here is for a new data point for a person named Satya Jit whose age is 30 whose gender is male which type of game he or she will prefer that is the question. So let me write down the problem statement for a given player which type of game he or she will prefer. we need to predict the game. That is my problem statement. Now in the previous example I showed a simple example taking two variables right this and this multiple data points were there and for a new data point we had to calculate the nearest neighbors right now here I had taken two features feature one and feature two and we were able to visualize it very properly right so in a two-dimensional graph if you have a data point like this and another data point like this and its coordinates are 2 and 3 and 8 and 6. The formula of AB is very simple. X1 - X2² + Y1 - Y2² roo over but if this goes into three dimensions which is little bit difficult to plot but imagine I have a and I have b let's say the coordinates of A are 2 3 and 1 the coordinates of B are 8 6 and 5 because there are three dimensions right so the formula here changes is from this to this x1 - x2 square 2 + y1 - y2 square 2 + z 1 - z2 square. So the number of dimension increases the formula becomes complicated and complicated right but the formula is easy enough to remember in this particular example. If you look at this example, we have feature like age and we have a feature like gender. Name can be ignored because name has no relevance in solving a classification problem. And I have a feature called as game which is my y variable. We need to predict this. What is the problem in the data right now? Age is a numerical attribute but gender is a categorical attribute. So what we need to do is we need to convert it into numerical using some encoding techniques. Now which type of nu uh encoding techniques is most preferable in this case you can go for dummy encoding or a label encoding. But again label encoding is not used for X variables. So we will be using a dummy encoding. Before getting into a dummy encoding, let's try to convert it into one hot encoding first. So how can we convert gender M? Two new columns. Gender F. Wherever I see M, it will be one. This will be zero. Same for all. Same for this. Zero and one. Same for all this. Right? Now these are my two dummy variables. What is the difference between dummy dummy encoding and one hot encoding? In one hot encoding, you will have n number of dummy variables. But in dummy encoding, you will have n minus one number of dummy variables. Why? Because you are getting redundant information from n variables. So it is better to remove one of them. So I'll randomly remove one of them. Delete column. And this becomes my final column. And I will strike this down. So my problem statement is clear. I have age, I have gender. And we need to find which person is playing which game. And based on that, we'll be able to predict for this new customer. So I'll try to draw this. On my y-axis, I will have the gender M. On my X-axis, I will have age. Let's say this is my zero and this is my one. First one is 251. Let's say here 291 sorry 251 291 11 1 181 181 is somewhere here. 291 again 351 somewhere here 180 180 21 0 220 0 31 1 something like that these are my multiple data points now the first data point belongs to cricket this basically belongs to cricket cricket cricket Cricket cricket 18 is football. Okay, it's a little bit confusing. Let me just do it from scratch. We'll try to use cricket as red and football as blue. So 251 cricket 251 cricket 291 cricket 291 cricket 11 cricket 181 football so this will be football 291 cricket 351 football 180 cricket 21 football 220 football and 311 football. Now these are my multiple data points. Now my new data point which is in purple color is 31 somewhere around here right 31. Now what we need to do based on my k value let's say my k value is three. It's a binary classification problem. Let's say my k value is three. I need to find the distance between all these points. Right? And we need to find which three of them are nearest neighbors. Now for example, visually these two and this one are three nearest neighbors. So this becomes cricket. Right? That is all about K nearest neighbors. So let's try to solve this problem for the first one. And what will be the ukidian distance from this point? From 251 from 251 till 31 what will be the distance? It will be 25 - 30² + 1 - 1 square roo over right 1 - 1 square cancel 25 - 30 square will be 5. So here my uklidian distance will be 5. Similarly it will be 1 it will be 19. This will be 12. This will be 1. This will be five. Now this is class 0. 18 and 0. So what it will be? 18 - 30 squared + 0 - 1 square roo over. So 12 square is 144 + 1 over 145 roo over should be around 12.05 or something like that 145 root over is 12.04 okay so I'll write down it as 12.04 similarly this one will be let's say 9.05 and this one will be around 8.05 05 and this one will be again one. Right? These are my multiple ukidian distances from this point to all these points. Which are the nearest three points? So the nearest ones are the ones having the lowest ukidian distance. Let's say this one, this one and this one. Now you take here it is cricket here it is cricket and here it is football. Now there are three cases of cricket uh two cases of cricket and one cases of football. So if you take a voting it becomes cricket. So my final output will be this person with age 30 and gender as male will be preferring cricket as per my k nearest neighbors algorithm. If you talk about k equals to 5 this is again football and this is cricket. So again three cricket and two football it will again be cricket. And this is how your k nearest k nearest neighbors algorithm works. That's all about this particular small example. In the next video, we shall be jumping into the Python part. We'll be implementing one particular example. We'll be using KN&N to build our predictive model. Build our predictive analytics model. Hi, in this video we shall be talking about how to implement our first predictive model, our first classification related predictive model using KN&N algorithm. So I have used Google Collab and I have mounted my Google Drive so that I can access the files from my Google Drive and I can start working on my model. In case you are working on a local Jupyter notebook or spider notebook, in that case you have to place your files in your local machine and pass the entire file path while reading the file. So in this case as I told I am using Google Collab and I have mounted my Google Drive. So I'm using a location from my Google Drive which is this AI data science and analytics and this is the file name. So I have just passed this. Press shift enter and our data frame will be ready. You can get started with doing some minor AD activities by printing the head, printing the info which gives you an initial idea about the data set. Now in this case we have 7,043 records. Customer ID, gender, senior citizen, all these are different columns. I can spot one mistake here. Customer ID, gender and senior citizen, partner, dependence. If you look at total charges, now total charges is nothing but a numerical attribute. If you see total charges, it's a numerical attribute. But in the information, it is showing this as an object. Now what we will do is we will convert this total charges to a numerical attribute. So I will do df dot total charges equals to df uh pd dot two numeric and I will be passing the same total charges. So my total charges is now a numerical attribute. If you just print info again, you will be able to see total charges is now a float object. And you can see that there are 7,32 records, which means there are 11 missing values in the total charges column. Now again, as per your missing values logic, you can imputee them or you can get rid of them. In this particular scenario, there is almost 11% almost 11 records which is 0.15%. Which is less than 1% of missing values. So I will simply get rid of them. I will do df.drop na how equals to any and then in place equals to true. That means wherever there is a null value drop it. That is what I'm doing. Now once that is done that's how your data frame looks like right now I'll try to draw a simple architecture which will help you to understand how to create your classification model you have your data set in this data set if I look at the distribution df churn do value counts my distribution is almost 5,1800 if I calculate the percentage almost 73 or let's say almost 73% of active customers 27% of churn customers this is what the data has what we did now was a little bit of ADA and finally we are with a clean data now what we will do is divide the data into training and test. This is what we will do. And once that is done, your training data will be used to create models. Model one, model 2, model N. And once models are created, your testing data will be passed. We will be getting predictions, predictions, predictions. And we will be comparing the p predictions to check which model is best. Right now we are only using KN&N classifier. So we will be creating only one model. In the next session when we are studying about decision trees after that we will be creating decision trees. After that we will be creating random forest and so on. And at the end of the classification, you will be able to see a similar architecture like this. And we will be able to compare multiple models. Okay. So let's go ahead and divide the data into training and test. Before that, I will be dividing the data into X variables and Y variables. So what is my Y variable in the data? My y variable is this churn column which talks about which customer is active which is churned which is this column right. So that will be my y and x will be others. So I will do df dot drop customer id is not important so I will drop them and I will drop the churn. Once that is done, x is equals to 1. Done. Y equals to df.churn dot valvalues. I'm just taking the churn values in y. Right? Right now I have my x and y. If you want to print your x and y, you can go ahead and print your x and y. x is your same data frame without the churn column and y is your churn column. Right? Now if you want to print the columns of X you can just do X doc columns. Now what I need to do is if you look at X gender, partner, dependence, phone service, multiple phone lines, all these are categorical attributes, right? So what we need to do is we need to convert categorical features to numericals. And what we need to do here if you recall your previous module we studied about feature encoding. So I will be performing feature encoding. So x equals to pd.get dummies. I will be calling my X and then I will be calling all the columns. Columns equals to. So I will call everything and then I will delete it. I will delete the unnecessary columns and after that I will be doing drop first equals to true. Why? Because I am going to do the dummy encoding. If you remember if there is a column like location Malaysia, Indonesia, Korea, Malaysia, Malaysia, Malaysia and so on. What we do is we create multiple columns like this right? Wherever you see Malaysia that will be one, others will be zero. Wherever you see Korea that will be one, others will be zero and so on. And these are all dummy features. So when you are creating dummy features that's basically called as one hot encoding but in dummy encoding we basically get rid of one of these features because it's passing redundant information. So you either you delete this you delete this or delete this anything is fine. So here we are deleting the first column. So gender senior citizen is not required. Partner dependence phone service tenure is also a numerical attribute. Phone service multiple lines internet service online security online backup device protections tech support streaming this contract uh yes contract paperless billing payment method and these two are numerical so I'll delete it. Okay now I'll run this. Once that is run, you can also check your X. I will just do head of one so that I can just check one column. And you can see everything is numerical. Now partner, yes. Dependence, yes. And that is how the data frame looks like. Now your data is well ready to be passed to your models. Before that I will be performing this step of dividing the data into training and test split. Splitting the data into training and test. So here I will be using sklearn libraries model selection and then I will be calling X train X test Y train Y test equals to this and pass your X and Y and test size equals to something. Now this something basically means that when you are dividing the training and test samples your training should be 75% and your test should be 25%. Okay, this is what is happening here. I'll run this. Now if you want to check the X train data you can see 5,000 records and length of X test somewhere around 1,000 records you can see. And if you want to check they will be divided into 3 is to four ratio. Three portions training data and one portion testing data. So our next step is doing feature scaling. Feature scaling is basically done in order to scale down the features. As you can see when you have when you are printing the data frame some are in zeros and ones format some are in 10 format some are in hundreds format something like that. So in order to get everything to a single shape or to a single scale we have to perform feature scaling. I hope everybody remembers about the two most important feature scaling technique. One of them is standardization and one of them is normalization. Normalization is xdash = to x - uh x - uh x min divided by x max - x min. Standardization will be x - mu by sigma. Right? So I will be performing standardization from sklearn dot pre-processing import standard scaler SC equals to standard scalar transform Xray X test equals to sc.t transform test. Now I would like to explain this piece of code. Why did we perform fit transform on x train and only transform on train? Let's uh on only transform on test. Let's try to understand. You have a data set you divide it into training and test. So when you are performing fit transform basically you are using your training data to fit into your models. So only training data information will be passed. That means whenever you are doing this standardization xdash equals to x - mu by sigma out of these records let's say mu is 10 and sigma is three imagine. So based on this your data will be scaled, your training data will be scaled. Again based on this your testing data is also scaled. That means you are only fitting the training data out of this 75% records. Whatever mean or sigma you are getting from this data, you are using the same mu and sigma for this data also. So when you are converting your testing data, you are also using the same formula where mu and sigma remains the same from the training data. You are not recreating the mu and sigma from the testing data. So that's why we are only doing transform for testing data and doing fit transform for training data. Okay. Now our next step is you can just print your training and test data and you can see they are all scaled values. So that's all about your you know basic things till here we have kind of cleaned the data deleted some of the null values. We have divided the data into training and test samples and we have also performed feature scaling step here. In the next video we shall be creating our canine classifier and going forward we'll also be creating decision tree random forest and so on. In the last video, we talked about how did we create how did we use our data set, how did we performed some null value imputation, how did we also kind of divided the data into X and Y and then further into training and test samples. We also performed some feature encoding technique especially dummy encoding to ultimately get all the columns into numericals and then we also performed some standardization technique some feature scaling technique. Now so far our data is almost ready. If you want to go ahead and check it out you can check it out that my training transform data and my testing transform data looks like this. Now our next step is to create our classifier. So I'll call my classifier KN&N classifier. Now simply if you don't remember any classifier's name, it's super simple. Just try to search sklearn KN&N. You will be getting the link on top where your scychitlearn.org website will be shown. And here is the details about the K neighbors classifier. Classifier implementing the K nearest neighbors vote. You can see whenever you are calling a classifier there are some attributes there are some parameters that are also needed. Now these parameters are basically called as hyperparameters. If you don't pass anything the default values will be gone and if you pass something the the values will be overridden. So let's try to call our classifier from sklearn dot neighbors from skarn.t neighbors import k nearest classifiers. So I will name it as call the KN&N classifier initiating the classifier. So I will start my classifier model equals to this. Now if you pass this that means you are calling the default classifier that means all these parameters are not changing. But let's say you want to override this n neighbors equals to 5. Instead of that you want to pass n neighbors equals to 7. In that case these values will be overridden. Now one thing you might be asking that what is the best value? Is five the best value or seven the best value or three the best value? So the best value in order to get the best value what we do is hyperparameter optimization which is again a very advanced machine learning topic which we are not covering here. So if you go back to your diagram, what really happens is you clean the data, you get this training and test, prepare your models and then let's say this model is performing best. Model 2 is performing best. In realtime scenarios, what happens is you take this model and try to recreate the model by doing some feature engineering steps. I will name it as mod model 2's first version or model 2's second version and so on. Let's say out of all these feature engineering step this is the best model then you again go ahead and do some hyperparameter optimization step to increase the accuracy to increase the to enhance the models. So this is a very advanced step where we arbit arbitrarily go and change all these parameters run the models again and again and again and again and we see which of these models are running the best. In simple terms, hyperparameter optimization is imagine you are running the first classifier like this, second classifier like this and third classifier like this and weights equals to distance. Let's say that means these are the three different versions of your classifier. This is giving you 80. This is giving you 81. This is giving you 78. That means this is the best classifier. That's basically your hyperparameter optimization. Coming back to the code, I will be using the default configuration and then passing the data to classifier. Model.fit. You have to use the fit function to pass the data and then run this and your classifier is ready. Once your classifier is ready, going back to the model, your classifier is ready with training data. What we need to do right now is pass the testing data for predictions. So I will call my model dotpredict function and I will pass my testing data and whatever predictions I'm doing on testing data. Let's say testing data has 100 records. So I will be getting 100 predictions, right? So I will be storing them in a variable called as yred. Run this and and your yred is ready. If you want to check Y red and white test that's how your wipe red and white test looks like. For initial few records they have done a good job. You can see here for one record it is no but it is predicting as yes. So there are mistakes of of course so we'll try to check the mistakes and once your model is ready and once your model has predicted our next step is to check out of these 100 testing records how many of them are correct predictions how many of them are wrong predictions based on that you will be calculating the accuracy right let's say out of 100 there are 80 active customers and 20 churned But out of this 20 churned, our model has predicted 16 correctly. And out of this 80 active, our model has predicted 70 accurately. That means 86 is the total correct predictions and total records are 100. So your accuracy is 86%. Right? So in order to calculate accuracy simply what we need to do is classification matrix to check how the model is behaving. So I will be calling from sklearn.metric metrics import accuracy score and then import accuracy score. Accuracy score and then I will pass my testing data and pass my prediction data. My accuracy score is I'll just So my accuracy score is around 75.6%. Or else if you simply print it also should be okay. My accuracy is 75.6%. Which is obviously not bad because we have not performed a lot of steps. I have just simply created a basic classifier with some basic you know feature encoding and feature scaling. There are a lot of things that can be done in order to take this accuracy from 75 to 85 or 90. Now the most important thing is that is fine training you have already trained the data you have already predicted from the testing data. What if you have a customer coming in and for that new customer you want to predict whether the customer is going to be churned or not churned for a new customer. So I will mark it as new data prediction. That means a data that is not a part of your entire data set. Let's say I'll quickly take one of these examples. Now this is my testing data. Let's say B. Okay. So let's say I'll take some random data sets here. data equals to okay let me just print my text test head one. So based on this I will be passing some information. Okay. So let's say my data my customer is senior citizen like it's not he's not a senior citizen. Let's say tenure is 2 monthly charges is let's say 87 178 gender is 0 0 0 1 0. So 30 columns right now everything is yes or no. Yes or no? Yes or no? Yes or no. And I think last two columns everything is yes or no. Okay. Yeah. Everything is yes or no. So I will just quickly pass some random values. 0 0 0 1 0 0 1 0 0 0 1 0 0 1 1 0 0 1 0 0 1 0 0 How many columns? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Okay, so that's my data. That's my new data. So whatever the customer when whenever you are testing your model for an unknown customer which is not a part of your original data set that means somebody has to enter the information about the customer right so when you enter all this information about the customer whether he's a senior citizen or not whether what is the tenure what is the monthly charges what is the total charges whether he is a male or not whether he has partners or not if he has no partners then it will be zero. If he has dependence, it will be one. If no dependence, it will be zero. So, you have to pass everything here. Now look at this. When you are getting your original data, what you are doing here is you are doing the standard scaling, right? That means you are transforming the data. So, obviously you cannot pass this raw data to your model. So, we'll also have to transform it. So I will mark it as data sc= to sc dot transform data and once that is done single equals to model.tpredict data sc and then I will print my single I'll run this and you can see for this particular record the model is predicting as yes that means the customer is likely to be churned. You also need to do one thing. For example, you want to understand the probability. In that case, you can also run the probability predict proba which basically says that there are chances there are 80% chances that this customer will get churned. That is what the data is telling. And of course, there are a lot of things that can be done here. As I told feature engineering, hyperparameter optimization, all these things are very advanced topics that's not required for at least for building your first predictive model. And of course, that's all about KN&N. So what we have learned so far is how to build our classification model using KN classifier. In the next module, in the next video, once we learn about decision trees, the rest of the codes are going to be very simple because I'm going to take help of this piece of code to create the code for decision trees, random forest and so on. Hi, welcome back. In the previous video, we got started into the practicals of creating our classification model. We started analyzing the data. We did minutely little bit of feature encoding processes. We also did some sort of normalization or standardization. And right now our data is ready to get started with creating the first model. So now jumping on to VIP coding where we will be leveraging AI to write down codes. Now that's the whole agenda of this entire program on how you can leverage heavily on AI to get started with programming, get started with SQL, get started with PowerBI, etc., etc. Right? So moving on for this exercise I'm going to use grock.com and the reason is known I have already talked about this thing in many of my previous videos that this is my first preference. Uh Chad is something I usually don't use and that's a very personal choice. Uh because I have used Chad GPT heavily. I have used Croc heavily. I have used some other tools somehow. I like grock. That's a very personal call. But in case you want to use something else, it's completely up to you, right? Eventually, it's going to be a similar tool, right? Where you will be asking some questions. So, in case you want to choose Grock, I will recommend you this is how you can choose. The very first thing that you can do is let's say you are going to work on this project right. Let us first import the libraries. Now this is something which is very basic. So I'm not taking the help of chat gpt or grock for writing down these few lines of code. And after importing let's say df is equals to pd read csv. And then I'm going to pass this copy path and done. And this is how my data looks like. Right? So the problem statement is pretty much understood. This is a churn prediction data set. Basically a data set from a telecom company of multiple customers. And there is a column called as churn which basically tells whether the customer is churned or not churned. Simple eventually it's a project right. So how do we get started with coding or sorry uh get started with AI tool like this? Well there are two options either you start a new chat or you start a new project. I always recommend to start a new project because inside the project the instructions are already there with Grock. So nothing to worry about and chats sometimes could be lost. As you can see on on my screen I have three different projects and I cannot talk more about these projects but this is how I work and whenever the project is over I just delete it. That's it. If I want it, I will save it forever. But if I don't want it, I can delete it. For example, this one. Let's say this is a dummy project that I'm creating. Maybe after the project is created, once I'm satisfied with the results, I might potentially delete that. So, let's get started with a project. So, if you click on a project, let's say here is something that you need to name your project. Let's say churn prediction. Okay. And I will write it down as uh let's say keep it like this. Churn prediction. Now there are multiple things that you need to add here. First of all, it's asking you to start a conversation in the project. Well, I can write down few things. But even if you want to write down something which is generated by AI model then what you can do is you can quickly open a new chat and ask this is what I'm trying to do. Can you tell me how to start a conversation? The bot will also be able to tell you that. So everything can be done using AI. Right? Even prompt engineering if you want to write a prompt for something you can also ask chat GPT or Grock this is what I need to do can you help me write a prompt and then once you get the prompt you can use that prompt to get a better result so that can also be done right so there are multiple things here start a project start a conversation instructions attachments so attachment is very simple I just need to attach this particular file file right which is wa fn. So let's try to attach that first. So this is the file I'm going to attach it and attachment is done. But I don't know what to add here and what to add here. So I will be taking the help of another gro chat window. So let's say here I'm going to ask uh let's say I'm going to ask in a very simple layman's term when creating a new project on grock.com what should we mention in the box start a conversation in the project and what project instructions to provide. Let me give you some context. I will be uploading a CSV file in the project and would need to eventually create a machine learning model like a classification model uh on customer churn data set. So I need to interact with the bot, upload the CSV file and ask questions accordingly. Accordingly you need to behave as a ML expert or you can also give this here but this is also fine right uh maybe what we can do is we'll shuffle this up so that first is the context that we are providing and this is what we need to do and then I will post it. I'm not going to provide this exact prompt as a deliverable of this course, but I think you can type it out while I am doing all these exercises. Right? So, you can see the only drawback with Grock is sometimes it gives you unnecessarily too long output. But I think I love it. So, so here starting a conversation suggested prompt for starting a conversation can just try to copy it. Go here and paste it. That's it. And then instructions. Instructions. You can also check why this prompt it has clarity, structure, actionable, technical. These are the project instructions. Just copy them and add it up. That's it. Okay. Save. Anything else to take care of? Done. So, let's try to create. Okay. Um, let's see what Grock is responding. I have received the uploaded CSV file. Wow. You you have the entire piece of code. Okay. Let let me first go through it line by line and then obviously we'll go through this code. Okay. So, let me validate the instructions. I am starting a machine learning project to build a classification model for predicting customer churn using a CSV data set. I will upload which is correct. I have uploaded the file. The data set contains customer information such as demograph. This is also correct. Indicating churn example one for churn zero for no churn. This is also correct. You are providing as much information as you can. I need assistance with the following. loading the loading and exploring the uploaded CSV. Performing exploration of data, identifying patterns, pre-processing the data, including handling missing values, encoding categorical variables, and addressing class imbalance issues. Suggesting and implementing a suitable classification algorithm. Da da da. and evaluating its performance using okay so this is where I need to change because we have not studied about a lot of algorithms we have just studied about KN&N classification model uh but once you get started with KN&N classification model you can quickly jump over to other models as well with a simple line of code change that's it and providing code snippets this is good so this This is what we need. Maybe what I'll try to do is implement the ML model using KNN classifier. That's it. Because we have not talked about any other classification models. I'm just going to stick to KNN classification model. That's it. So this is what the new code is. Um one more thing also to add is whenever you are doing vibe coding you cannot I mean this is dicey to talk about but I usually recommend people not to follow tools like charg blindly right you you can only use these tools as the coding help. So this is this tool is helping you to code. This tool is not going to teach you something, right? So never 100% rely on these codes, right? So so that that is the only thing you need to take care of. Now let me go through the code and I'll tell you what flaws this code has. Uh it has read the file displaying the basic information which is correct. Exploration of data which is also correct. Categorical columns. Okay. There is a small change or small problem that we have already identified. So what it is doing is it is by default checking the normal data frame. It is not checking whether any of the columns are of different data types. If you remember in the last video we talked about total charges right total charges was maybe I'll try to show it here directly. If I do info you can basically see that total charges is an object type. But if you look at the data right total charges is a numerical attribute and as I know this data set I know that this particular column is basically having some missing values. Okay. So that is what the problem is here. It has just checked the data frame. It has not checked individual column data types and has not changed them. Right? So this is the first problem. The second problem is um I think all good here they have provided some EDA. Okay. So I'm just going to reprompt it because we want to do whatever we have done so far, right? Can you please okay let's not write please and all which um the creators of openAI has requested not to use such words because it increases their uh you know memory and all those things. So let's stick to very pure a good prompt. Whatever we have done so far I'm just going to prompt that and take that code. Okay, I don't want to perform EDA or plot any graphs. Nor do I need smart or any other class imbalancing steps. I just need to read the file as DF. Change the data type of total charges to numeric. handle the missing values of total charges and then perform one hot encoding and standardize the data and finally create a KN&N classification model and print me the classification report. That's it. So very clear statements. I just need this right. So here it has created pipeline and all which is not needed. I don't want to implement pipeline. Can you restructure the code or can you rewrite the code? Okay, this is good. Very simple piece of code. Um, some of them are little bit questionable but it's okay. I'll try to copy this and then I'm going to go to my Google Collab and here I'm going to paste it up. So let's try to go through this piece of code. Okay, let me try to write down uh Y coding and I will also make it bold. That's it. I have all the libraries. So everybody knows that sklearn is scikit learning where you have train test plate you have standard scaler one hot encoder multiple things here I'm reading the file very simple here is when I am changing the total charges from object to numeric categorical columns df do select okay we are dropping customer ID and churn numerical columns are including this and this to list. Then I'm doing a handling missing values and numerical columns. Strategy is median. You can use mean, median, whatever you want to. And then I'm imputing it. Then I'm handling missing values in categorical columns. So eventually there are no uh numeric uh missing values in the categorical column. So this piece of code is not that important. And then I have converted the data into one hot encoding. And then I have dropped categorical columns. So pretty much this is good. X is customer ID and churn. And churn is this and this. I think all good. And uh okay, I will stick to 0.25 whatever we have done in the previous class. And then standard scaler all good. Fit transform and transform. For those who don't know about fit transform and transform just try to go through the previous video or the previous lectures you will understand. Then we have the KN&N classifier and then the predict. I think all good. Let's try to run this. If there is an error of course we will be again going back and we'll try to ask right. Uh so there is an error. No such file. Okay, I will just try to copy this path and I will try to paste it. That's it. Okay, you can see the results are here, right? The classification report is this. We can also do a simple printing the accuracy accuracy score I just need to okay so accuracy score is 100 uh 76% which is not bad. Now if you deep dive into this classification report. So I this is my personal favorite. I usually print classification report because it gives you like a detailed analysis of everything. So it gives you precision recall and F1 score also support for both the classes. And you can see for class zero the values are really good. for class one the values are little bit worrisome and the reason behind that is I can tell you the reason you can also go to the chat to ask the reason right you let's say for class one the recall precision in classification report is around 0.57. Why is that? So let's try to ask Groc. The reason is very simple. The reason is the distribution of Y variable. If you go to this particular column, you will be able to see that the distribution is bit off. Right? If you divide it with the length of data frame and multiply it with 100, you will see that the distribution is 73 inclined towards no and 26% is inclined towards yes. Right? That basically means your model sees more of these records than these records. And what does that mean? Imagine a baby, a newborn baby. is getting to know about cats and dogs, right? The baby sees more cats, let's say thousand cats over the period of 4 to 5 months and let's say 200 dogs. So the baby's memory has more cats information than dogs. Now if the baby sees a tiger which the baby has never seen in in the past potentially the baby will not be able to identify the tiger and that's because tiger's images are not inside baby's brain as simple as that the more data you train the more understanding the model will have right so how to fix this problem the problem can be fixed by reshuffleling the data by upsampling down sampling so basically we call it as class imbalanced issue which we can solve using smart and various other things which is out of syllabus for this particular program. If you want to get into this, you want to solve these problems of balancing out and various other things, you want to take this accuracy from 76 to 90, you need to learn a lot about feature engineering, hyperparameter optimization, etc., etc. And that basically comes under the career path of machine learning. Eventually as a data scientist or as an AI engineer if you are switching your career then you can go ahead with that you have to study a lot about these things. So building a classification model takes 5 to 10 minutes but the build the best model takes a lot of time right. So, but you can leverage chat GPT to get to the next part even without learning more about machine learning. But let's try to see what charge GP is telling or what Grock is telling indicating moderate performance in identifying positive cases. Below I'll explain. Okay, it has not quite understood what I wrote. Uh I basically wrote why do I have a low recall precision for class one? It has identified rec in general recall precision. Okay. Okay. What it is? Tele typically has a class. Okay. It is able to tell with fewer churn customers class one compared to churn class 074. Since you opted not to use mod or other imbalancing handling, the cannon model may prioritize the majority class leading to lower recall and precision. So, it's giving you some results here to perform some sort of uh okay, can we use mod to improve the accuracy? Let's try to see. And we should be able to take this piece of code uh class. Okay. Um so the code is ready. I will try to copy this. Take it here. paste it up and then uh let me go through the code as well. Where where did he Okay, here is when the smart is done. Okay, let's wait. It's taking a little bit more time as expected but okay it's because the grid search okay so that's what I told you right in order to work on the final model there are a lot of improvements that can be done improvements can be done on feature engineering side improvements can be done on on uh doing balancing out improvements can be done on hyperparameter optim optimization. There are a lot of things. You can see now the results are again not that great but it has improved. The recall has improved. Precision has gone little bit down. Let me try to check the accuracy. Accuracy has gone down. Okay. So that means smart is not working. Now in this case you'll have to try something else modn various other things are there but again as I told you that's beyond the scope of this uh predictive analytics part that is it in case you have any follow-up questions on this data set or any of the steps you're not able to understand you can use this chat to ask anything and let's say you go back and you want to go back to Grock on the same project. You can simply go to CH prediction and directly go to this conversation. That's it. And once you are in this conversation, you can ask back whatever questions you have. That's all about it. So using KN classifier, I'm trying to remove this piece of code. In case you want, you can generate it out. As I told you, it's not in scope. So I'm delete deleting it. So our KN&N classification model is done. 76.54 percentage of accuracy. Not bad. Uh 57 54 56 precision recall and F1 score for the minority class which is also not pretty bad. It's fine. Let's jump into the next classification model. and let's try to code it out. Similar to a KN&N algorithm, we also have a small use case or a small example uh talking about decisions. So this is the example where I have multiple columns like age, competition, type and profit and I have different attributes like for old age competition yes type software the profit is down that means there is a loss profit or loss it will be down means loss for age old competition no software it's loss for given certain conditions S we are having a class called as whether there is a profit or a loss whether the profit is up or down and that is what we need to predict. The problem statement here is to find for a given entity or for a given new transaction whether there will be profit or loss. As simple as that. So my class attribute is very simple. My Y variable is profit or loss column and my X variables are are always age, competition and type. I have only taken three variables just for the sake of simplicity because our ultimate goal is to identify the root node. how decision tree algorithms internally works to create your root node right so let's try to go ahead and understand this now there will be some mathematical uh application like there will be some mathematics I will be covering but if it is little bit difficult for you then it is completely okay to skip this part and directly jump into the Python code when it comes to the Python implementation the practical implementation of predictive analytics. It is super super simple. We can implement a decision tree algorithm Python code in just few minutes. So first of all I'll try to have a paintbrush in parallel. Let me move it to here and move this to here. Okay. Now the first as I told you decision trees there are two techniques. One is entropy and one is gen. We will be using the entropy technique. Based on entropy we will be trying to find which of them is the root node. That means which feature is most important. Okay. Now in this case how many profits we have? Five profits. How many losses? We have five losses. So the first formula is entropy is min - p by p + n log of p by p + n. - n by p + n log of n by p + n. So what will be my P? My positive case is by 10 log of 1 by 2 minus 5 by 10 log of 1 by 2. If I combine all of them it will be -1 log of 1 by 2 which is going to be 1. If you calculate this log of 0.5 it will be giving you minus one log of 0.5 is going to give you so this is a log base 10 oh log of base 2 my bad log of base 2 okay this is approximately minus one so log of base 2 this is base So -1 into -1 will be one. So my entropy is basically my one. There is a formula gain is equals to class entropy minus my normal entropy. This is my class entropy. Okay. Now I will try to first use my age column. In age, how many old do we have? We have three old. How many positive and how many negative? All are negative. So 0 positive, three negative. For mid, two positive, two negative. for new two three positive or negative. Okay. Now I will be calculating the entropy here. P I N I. Now information gain for this will be zero. If one of them is zero then the information gain will be zero. For this also will be zero. Okay. And for this it will be calculated using this same formula. So I will try to calculate in this small box. So -2x 4 log base 2 2x4 - 2x4 log base 2 2x4 and it's ultimately going to be 1. I will write down it as 1 0 and 0. Another formula is entropy of age which is my this part. Okay. Entropy of H formula is summation of P I + N I by P + N multiplied with I of P I N I which becomes in the first case my I part is 0 and 0 right so first case will be 0 + 3 by 5 + 5 into 0 + 2 + 2 by 5 + 5 into 1 + 3 + 0 by 5 + 5 into 0 cancel this becomes 4x 10 which becomes 0.4. So what will be my gain of age? My gain of age will be 1 - 0.4 which is 0.6. Now this is very important right? Similarly I will do it for competition. Yes and no. How many yeses? P I N I and I. How many yeses? Yes. Profit. No. Loss. Loss. Loss. Up. 1 3 4 2. What will be the I here? - 1x 4 log base 2 1x 4 - 3x 4 log base 2 3x4 which will be what is log base to 1x4 log base 2 0.25 is -2. So - 1.4 4 into - 2 - 3x 4 into log base 2 of 75 is 75 is -.41 -.41 41. So if I calculate it will be 2 minus uh sorry 1x 2.5 - 3x 4 3 into 0.41 divided by 4 is this +.5 is whatever it is.8 8 okay so this becomes my8 got it similarly I will calculate it for this - 4x 6 log base 2 4x 6 - 2x 6 log base 2 2x 6 log base 2 4x 6 which is - 2x3 log base 26 - 1 by 3 log base 2 1 by 3 which is.33. So log base 2 of 0.66 is -.6 and for 33 is - 1.6 - 1.6. So this will be 1x3 * with this minus sorry 2x 3 - 1x 3 * with this uh 2x 3 2x 3.2.4 - + 1x3 1.6 5 around.9 so this becomes.9. So once this becomes 0.9 what we need to do next we need to calculate this thing. So what will be my entropy of competition? Summation of pi + ni. So 1 + 3 by 10 *.8 8 + 4 + 2 by 10 *.9 which will be4 into8.32 plus 6 into.9.54 which is86 and what will be the gain of competition 1 -86 which is 0.14 so 04 0.13 whatever it is right now for age I got my gain as 6 and for competition it is this obviously age is having more gain so age is a important feature than competition I will be doing the same thing for the last attribute which is my type so for type software Down down down down down down up up. So down sorry not down software hardware P I N I. So this will be 33 22 right. So if it is 33 I will be 1 one right? It will use the same formula. What will be the entropy of type? Entropy of type will be 3 + 3 by 10 into 1 + 2 + 2 4 by 10 into 1 which is going to be 1. So what will my gain? My gain will be zero. Now out of age type and competition which of them is having the highest gain it is definitely age. So age becomes my top node. age becomes my root node. What comes next? Will it be competition? Will it be type? Now for competition the gain is.14. For type the gain is zero. But you cannot simply say that competition is better than type. Our next step will be for only for age attribute. Only for the age attribute. For old, mid and new, there will be three three rows. Old, mid and new. Only for old values, you will again be calculating the entropy for competition and type. For mid, you will again be calculating for competition and type. For new, you will be again be calculating. So the process goes on. So for mid, type could be an important one. For this, competition could be important. for this competition could be important and that's how you go ahead and build your decision tree. So even for a small example you can see how difficult it is to mathematically calculate who is the best lead who is the best node and how the decision trees are calculated. But when it comes to the practical applications part it is super simple. You just have to call your decision tree classifier and then done you will be able to get all the outputs. So that's all about decision trees classifier as an example. Uh as I told you again that if mathematics is little bit difficult for you either you practice or you probably skip it for the next part go ahead and implement it practically. So in the next video we shall be jumping into one of the use cases where we will be implementing decision tree classifier very similar to the one we did for KN. Hi welcome to this video on decision trees. We all studied about decision trees before that we studied about KN&N. While we were preparing the code for KN&N, our almost 90% of our work is done. We are just going to reuse the code to implement for the decision tree classifier. So I'll again run these all steps. So I'll try to restart my runtime because my kernel might have died. So I'm just restarting it. So we'll get started with the first thing which is mounting the Google Drive which was importing all the libraries and reading the file. So that's how my data set looks like. Data set.in information. If you closely look at all these things, I have been using this data set for quite a long time. So I know that there is a mistake here which is total charges which is a numerical attribute but it is showing as object. So I need to convert it into numericals. So I have converted into numericals using two numeric. And then when I am running the information you can see there are 11 null records. Now 11 null records can easily be treated but again you have to check which technique is best and all those things. So I have just ignored these 11 records because 11 records out of 7,043 is almost 0.15% which is very low. Okay. So I have dropped this and again I'm printing my head. My churn distribution is something like this. That means 73% of the customers are active. 27% churn rate. That means as a data scientist, if you are joining a company or as a data analyst, you have been told to create predictive model in order to predict for a new customer. In that case, your main aim is to bring this number down. How can we bring this number down? If we are able to predict a customer is going to be churned or not accordingly we can provide them some offers to retain them. Done. So next step was to divide the data into X and Y. Y basically means your churn which is your dependent variable and X are all my independent variables. And then I am converting the categorical features into numericals by doing feature encoding especially dummy encoding. run this and then run this and then I'm splitting the data into training and test. So if I draw if I draw my architecture once again let's say this is my data 73% of active customers 27% of churn customers what we did was we did some minute ADA and we kind of cleaned the data after that what we did was we divided the data into training and test training and test. After that, what we need to do is we need to perform some feature scaling technique. After feature scaling, there is a step called as feature engineering. But we have not studied about this. So I'm going to skip the feature engineering part. Once that is done, you have to create multiple models. Model one, model two and so on. model end. So your training data like after your feature scaling and feature engineering ultimately will also have your scaled training and testing data right so your training data will be used for training and testing data for testing and then once you get the predictions let's say this is giving 82 80 and 75 this will be treated as the best model and then further some other hyperparameter optimization or feature engineering steps will be performed to boost this accuracy to 90 or 95% and then our model is ready for deployment that is the end to end scenario. Now our model one is already done which is my KN&N. Now we are preparing the decision tree. So I will split my data standard scaler my scaled values. I will not run my cannon classifier but I will copy paste the code and I will go ahead and create my decision tree classifier and then I will again copy paste some other quotes which is my predict function and I think that's it and last but not the least to check the accuracy okay now I have copy pasted everything what we need to change here is the classifier name that's it now decision tree is basically present inside skarntree but if you don't remember simply do sklearn decision tree classifier and you will be able to check the documentation about decision tree classifier there are so many arguments in this decision tree classifier and all these are hyperparameters By default the criterion is genie. By default the splitter is best. By default max depth is none and all these features are there. You have to perform hyperparameter optimization in order to get the best version of your decision tree. But we will be using the basic one. So I'll be making it model 2 or model_dt model_dt. And done. Now my classifier is ready. I will be calling yred dt model_dt to predict on my testing data and once that is done I will pass my yred dt and I will print my accuracy my accuracy is 72% which is bad definitely this is bad as compared to k&m if I go back to the model you can see this accuracy was 75 now this is 72 2 again if you want to do the same steps for the out ofthe-box prediction you can do it but I will not repeat it as we have already done for KN&N the same thing you can do it for decisionary classifiers now I know some techniques to quickly enhance the classifiers uh accuracy by doing some minor changes but I will keep that to you so that you can learn hyperparameter optimization later on uh and then you can try it out by yourself. So that's all about this particular video on the decision tree classifier implementation and similarly any classifier you want to implement let's say be it a SVM classifier or a knife based classifier you just have to copy paste these codes let's say you want to implement SVM classifier simply type sklearn SVM you will be able to redirect it to their official documentation import them in initiate this classifier and everything is same. So that's all about decision tree classifier. In the next video we shall be talking about random forest and further we will be talking about the random forest implementation. And once random forest is done we will also compare all these models in order to decide what's our next step. Hi welcome back. In the previous VIP coding session we had created till the KN classification model. Now let's jump into implementing the decision tree model. Decision tree classification. Um okay let me also write down KN&L classification model here by coding okay then classification okay and let's say I will copy this part here and okay Done. And we had run this code last time and then the results were 76% for K9 classification model. Now let's try to jump into decision tree classification model. Now as we have already done some sort of smart related chat. Now this model I mean Grock already knows the context of the oversampling and the downsampling problem that we faced because of which the recall and precision was on the lower side. So in order to not take into the account what I want is I want to paste the entire code right away. Can you simply change the KN&N classification model to decision tree model? It's very simple. I could have done it uh just one line of change. Instead of KN&N classification model, you just simply have to let me try to copy paste it. decision classification and then that's it and then I will try to copy this piece of code and then I will copy this piece of code dt dt dt and then let's try to make it y dt D just change KN to DT that's it and change here DT classification classifier classification report uh Y bread wre DT and then here DT classifier classification report Y bread DT that's it if you run this piece of code you will be able to run your decision tree classifier. But let's try to check what it has done. You can see everything is same till here and it has directly worked on this part. Right? So that's it very simple. Now if you can also compare the results the decision tree classifier is again little bit worse than Ken classification model. So you can see the majority class results have also deteriorated. Minority class results have also deteriorated. Accuracy has also deteriorated from 76 to 72. Okay. But this is what it is. Again if you want to improvise I just don't want to repeat all those things again and again. There are thousand ways of improving it which is not in the scope of predictive analytics. In predictive analytics, we only focus on what are the capabilities of predictive analytics. How do we get started with predictions, predictive AI part, but eventually if you want to get into this machine learning field, there's a lot of things that you need to study, right? So that's that's that's all about it for decision classification. In the next W coding session, we will be jumping into random forest. In this video, we shall be focusing on random forest. Our next classification algorithm and one of the most important algorithm in the field of classification. Random forest. What do you mean by random forest? We all know what a forest is. A forest is nothing but a combination of trees. So the similar concept is also used here. Here forest is nothing but a combination of trees. And what are these trees? These trees are decision trees. What do you mean by random? Randomness is basically I will explain you what exactly and how exactly random forests are created and then I will explain you what is the random part here. So random forests or random decision forests are an ensemble learning method. Ensemble here means it's a hybrid learning model for classification, regression, and other tasks that operates by constructing a multitude of decision trees at training time. For classification tasks, the output of the random forest is the class selected by most trees. So I'll try to explain it using a small example. Imagine you have a data set. I'll talk about the same churn churn prediction list of 10,000 customers out of them 2,000 churners and 8,000 active. So usually what happens is you divide it into training and test data and on the training data you create your models and once the model is done you pass the testing data to finally get the predictions. That's how it works, right? Now, what happens in in random forest is same example 10,000 records, 2,000 churn, and 8,000 active. You do the same thing. You create your training data and testing data. Let's say your training data has 8,000 records. Let's say out of them 6400 are active and 1,600 are joined. And here again 2,000 records around 1,600 active and around 400 joined. Now random forest as we know is nothing but a combination of trees. So what happens is it creates multiple trees. Tree 1, tree 2, tree 3, tree 4, dot dot dot, tree n. So multiple trees are created. How do we decide how many trees to create? When you call the random forest classifier, there is an argument called as estimators. By default, this value is 100. Once we jump into the practicals, I will explain you this thing again. By default, the n estimators are 100. Which means by default, if you create a random forest, 100 decision trees will be internally created. That is understood. That is the forest part. Forest is combination of trees. What is random here? So what happens is whenever these decision trees are created, randomly data points are being passed for training. Which means if your training data is 8,000, all the 8,000 records will not go into all the trees. Let's say 2,000 record goes here, 1,500 goes here, 1 1200 goes here, 5,000 goes here, 2,000 goes here. So the data points that goes into multiple trees are random in nature. That is the first randomness behavior. Another randomness behavior is inside this 10,000 records. Let's say there are 20 features. That means 20 x variables that is also random in nature. Maybe here you pass five features, here you pass four features, here you pass 18 features, here you pass three features and so on. So the number of data points that goes to a tree and the number of features both are randomly distributed and each of these decision tree is nothing but a weak learner. They are a weak learner because they are fed up with less amount of data points and less features. So they are called as weak learners. And ultimately these weak learners are created and finally once they once they are created you pass the testing data for predictions. Just do some predictions, predictions, predictions, predictions and predictions and finally you take voting out of it. That means for an example one of the customers data is passed. This tree predicted as active active churned churned dot dot dot churned. So there are more voters for churned. So our out final output will be this customer is likely to be churned as per this uh random forest. And that is one of the reasons why random forests are very powerful at least as compared to decision trees. And in 90% of the cases, random forest outperforms as compared to decision rays. There are still certain cases where decision trees could be better. But most of the cases we see random forest performing better. So same thing has been depicted in this example also. You have data set, you divide the data set into multiple features. And when you divide it into multiple features, repetitions are allowed and you create multiple decision tree. So for this decision tree probably age could be an important factor. For this decision tree probably salary is the most important factor. So all these decision trees that are created that are unique and important and all these importance are accumulated and voting is taken and that's why random forest ends up being the best classifier. In the last video about decision tree classifier, we saw how we copied the code from the cannon classifier and implemented the decision tree classifier. So I'm going to do the same thing for random forest classifier as well. We all know about random forest random and forest what the random part is all about. What the forest part is all about. So let's try to implement the random forest classifier. So I'll quickly run all these codes again. I'm simply blindly going to follow and run all the codes. That's it. And once we are done with all the codes, then we will start implementing the random forest classifier. Okay, everything is done. Now opening back my paintbrush that we used for the decision tree classifier thing. I hope everybody is clear about the architecture. We have data. We know the data's distribution. We perform some EDS steps to clean the data. Let's say null value imputation or any kind of redundant information is there. We have to get rid of them. Then we divide the data into training and test. Then we perform some feature scaling technique so that our data are scaled so that the data is not biased to the model. Then we perform some feature engineering which we didn't perform in our existing piece of code because feature engineering is an advanced topic that we are not covering here. So let me ignore this. So after the scaled values the scale training and the scale testing data you are using it to create the models. So our model one which was KN was giving us 75% accuracy and decision tree was giving 72% accuracy. Now I'm going to create model 3 which is a random forest model and we'll see what is the accuracy of random forest. So I will quickly implement my random forest classifier. I'll copy paste the code. So that's the beauty of writing Python code. So you have you can simply create your first predictive model in just few lines of code. So instead of this I will use my random forest classifier. But my random forest classifier is present inside n symbol. Again as I told no need to remember all these things. Simply search random forest classifier and you will be able to get all the details. All these are different parameters in random forest classifier. And this is the most important parameter. By default there are 100 trees. But if you increase the number of trees obviously the accuracy will increase. So we'll go ahead initiate this. I'll mark it as model RF model RF. Everything else remains the same. My random forest classifier is ready. My predictions will be run on my model_rf and based on the predictions I will be printing the score and my accuracy is 79%. If I go back here I will note down my accuracy is 79%. So let me just rerun this code with some high amount of estimators. I'll rerun this code. Accuracy has decreased. That means the best number of trees are definitely between 100 and 500. 79. Okay, let it be. So I have got 75 using KN, 72 using random uh decision tree and 79 using random forest. Your next step is to keep trying multiple other algorithms. And once your hit and trial process is done using multiple algorithms, your next step is to choose one or two or three best performing algorithms and go ahead and perform this hyperparameter optimization step. So implementing your first classifier is very simple and that's how we implement our random forest classifier which is present inside your skarn.se symbol. So I'll quickly change this text to random forest classifier and same for this is a tree also dt classifier. So that's it. See you in the next video. Hi welcome back. So in the previous video we ran till decision tree classification model. Let's try to copy this as our next task is jumping into the random forest classification model. Classification model so I'll try to copy paste it right. You can always use AI. Let's say can you change it to random forest classifier? Just write the last few lines of code. No need to repeat everything. Okay, I just told not to repeat everything because unnecessarily a lot of tokens are being used. Uh so you can see I will just copy this. The last few lines are basically this right. One more thing is which is your import statement. Now I know it is basically inside n symbol but let me also ask how to import just give a one liner it's basically from the ensemble uh technique or so the ensemble method so skarnse symbol and here you just have to add random first that's it and not add random seed and all those things and everything else um should be okay and estimators is basically number of trees let's say I'm providing as 500 training data and doing the prediction Okay, for the sake of having different variable names, I'm just changing it, but it's not super mandatory. Everything is good. Let's run this. Okay, you can see the results are little bit better than KN. This is entry. I will try to copy and paste the classification report sorry the accuracy random forest classifier 72 is the accuracy but overall I would say that these are better results. your majority class results deterioration is completely fine but minority class precision has gone up which is a good thing and recall is has gone down but it's okay overall I think it's a good progress similarly you can also try out different other classification models there are like more than so many there are like 20 25 models right KN classification knife based classification, decision tree, bad classifier, um random forest and there are multiple ensemble techniques like add a boost, XG boost, GB boost, many many other things, right? So give it a try and see you in the next video. In this video we shall be talking about another concept called as boosting. Before getting into boosting let me quickly talk about ensemble learning. Ensemble learning is a technique where one of your algorithm or multiple of your algorithm starts failing. Now imagine you have a cannon classifier. It is giving you 65% accuracy. Your decision trees are giving you 70% accuracy and some other algorithm let's say SBM is giving you 71% accuracy. No matter what you try these algorithms are not giving you more than 80 or 85% accuracies. What if you combine each one of them to create a final classifier? Let's say you can name it as a superhero classifier. What internally does is whenever for a new data point all of these algorithms will be predicting let's say this algorithm predicts as class A this as class A and this as class B and you finally take a voting out of it and you predict it as A. So this is a hybrid classifier and somehow you must be remembering that we also covered random forest which also works on this principle. Inside random forest your small small classifiers are different decision trees right. So random forest is also one of the ensemble learning technique. Inside ensemble learning technique we have two concepts. One is called as bagging. This is an example of bagging. Random forest is an example of bagging. Another concept is boosting. Now, how does boosting works? Boosting is little bit different from bagging. Bagging you have a data point, you divide it into training and test samples. Then you create small small classifiers, you fed them with little bit of data, repetitions are allowed. And those classifiers are created. they predict on on your testing data and you take the voting out of it. That is all about bagging. Bagging is important. Boosting is also important. Let's try to cover boosting in this particular slide. In this example, you can see I have a data set. Let's say I have 10,000 records. I divide it into training and test data. Fine. Now in bagging what happens is from here you directly create multiple bags and you feed them with different types of data and then you take the voting. In boosting what happens is you create one bag just one bag. You randomly populate data here. Let's say from 8,000 records you populate 2,000 records. Repetitions allowed. You train them and you create a model. Then what happens is if you have passed 2,000 records then definitely there are 6,000 records that are not a part of this model. So you pass any of these data points here and test with the training data set. So let's say you pass 1,000 records or 2,000 or 3,000 records and you see whichever data points are giving wrong predictions. Let's say 2,000 or 3,000 records are giving wrong predictions. Now these data points becomes a part of your second bag along with some new data points and again it is trained model is trained again the unseen data will be tested and this process goes on. So it is nothing but an iterative process of learning. I hope everybody's clear. You have your original data set, training data set, testing data set. Just forget about testing. We are not using it here. The first time your bag is created, you pass randomly 2,000 records. And let's say you created a model. So how many records are not passed here? 6,000 records. Let's say out of this 6,000 records, you are testing your first model with 1,000 records. Simple. Out of these 1,000 records, maybe 400 records are wrong predictions. 600 records are correctly predicted. Then what happens is when you create your second bag, you randomly populate data points. Let's say 2,000 or 3,000. And this 400 also goes here. So all total 3,400 records goes here. Again, a model is created. All the unseen data that are not a part of this bag are tested and this process goes on. It goes on and goes on and goes on till the time you finally get your final model. So how many algorithms do we study under boosting? There are multiple algorithms starting from gradient boosting, gradient boost, extreme gradient boost, cat boost, ada boost and so on. Multiple boosting algorithms are there. XG boost is however one of the best boosting algorithms and it was one of those algorithms when it was launched. Almost every car competition winner was implementing this algorithm. It is that powerful. And so far till date also XG boost is one of the most important algorithms that we perform or that we study inside classification. XGB boost can also be used for regression. But again as we are only talking about classification right now I'll only be showing you the practical applications of XGB boost using the XG boost classifier using a certain data set. Let's try to implement one of the boosting algorithms one of the advanced ensemble learning algorithms. The use case and everything remains the same. This is the same churn prediction use case where we did some pre-processing steps training and test sample split and all those things implementing cannon classifier decision tree classifier and random forest classifier. If you want to look at the architecture this is how it is right our cann is giving us 75 decision entry 72 and random forest 79. Our next step is to perform a ensemble learning technique. Now there are multiple types of as we all know ensemble learning is of two types bagging and boosting and one of the bagging algorithms we have already covered which is random forest right let's try to implement one of the boosting algorithms there are multiple boosting algorithms which starts with gradient boosting we also have extreme gradient boosting which is also called as xg boost we we have add boost, we have cat boost and so on etc etc that means there are multiple other types of boosting algorithms also we'll try to implement one of them so I'll try to implement add a boost so I'll copy paste the quotes from random forest classifier everything is same only the classifier will change that's it so here I will implement add boost classifier. If you want to cross check, simply do something like this and you will be able to see it's the same skarn ensemble and add a boost classifier. Done. I will rename it to add a boost classifier. I will initiate it model adder. I will call this adder boost classifier and estimators. Let's make it 100 model adder. Everything remains the same and my adder boost classifier is ready with n estimators as 100. I will do the same predictions on my adab boost model and my predictions are ready. Now I will print my accuracy. So I will pass my y test and yred adder and I will run this. And you can see my accuracy is 80%. So what do we find? We find that the boosting algorithm is giving me 80%. So add a boost is giving me 80% accuracy. Now what are the next steps? The next steps are obviously to run multiple other models. I can at least name 20 to 30 classifiers. So going through each and every classifier theoretically and practically won't be possible. So if you want to take this exercise to the next step, go ahead and implement multiple other algorithms. We already implemented KN&N, decision tree, random forest and ATA. What you can perform is SVMs, knives based classifiers, logistic regression, extra trees classifier, bagged decision trees, all the different three types of boosting technique, XG boost, normal gradient boosting, cat boost and so on. just go ahead and implement all these algorithms and then you compare the accuracies of each one of them and then the next step is to choose one two or the three most performing models and go ahead and perform the hyperparameter optimization. So that's all about this particular video on ensemble learning. I hope you understood the end to end model building steps end toend architecture on how a predictive model is actually created. executed and run and theoretically you know what could be the next steps and how to deploy it. Deployment obviously needs the expertise of data engineers in realtime scenarios not required for data analysts and data scientists. But I hope everybody is clear with the entire flow. That's all about this particular video and this is where we wrap up classification. Hi, welcome back to the VIP coding session. We will jump into the next part which is ensemble techniques. So theoretically what is ensemble techniques? Ensemble techniques. So you can call it as ensemble, ensemble, whatever you want to. But eventually ensemble techniques are hybrid techniques, right? Where your individual classification model fails, ensemble learning comes into picture, right? There are two types, bagging and boosting. I did not have to cover them once again. Let's try to take help of croc to code the last few lines of code for multiple ensemble learning. Uh can we also implement some ensemble learning models such as ADA boost, cat boost, XG boost, etc. and just write down the last few lines of code. along with import statement. So let's try to implement add a boost, CAD boost and XG boost. You can see first of all it has already provided me all the why it's failing. CAD boost is failing. CAD post is also a part of skarn if I'm not wrong but let me try it out. cat boost classifier. Okay, no problem. Let me try to I thought CAD boost is also available inside skarn but it's okay. We will try to install it and then import it. Okay, it's running. Let's give couple of minutes. In the meanwhile, let me jump into croc and check the code. C code for ensemble methods for adder boost. It has yeah see it's pretty straightforward. You are just changing the model name. That's it. And in most of the cases nothing else, right? You are still using the X train Y train as fit then predicting the X test. That's it. We're just changing the model name. Now I'll rerun the command and then I will take my first piece of code which is add a classifier and try to see add a classifier has done like this. Maybe we can also print the accuracy score. Accuracy score is 80%. Very very good, right? So better far better than our traditional algorithms. Let's try to go for CAD post. I will also print the accuracy. I'm assuming that XG Boost will give us a little bit better results than all of these models, but that's just an assumption. Sometimes very basic models can also give you better results. And the more complex ones might not, which is fine. And let's try to run XG boost. And here we go. XGB classifier. Okay. So, add a boost has given us the best results 80%. Can we move from 80 to 90? Of course, the answer is yes. By spending some time with this use case, you can definitely reach from 80 to 90. 80 to 85 for sure. But to reach from 85 to 90 is when the challenge will start. More than accuracy, your challenge should be improvising the precision, recall and F1 score for the minority class. But once you balance it out, I think you will be able to solve it. Right? So that's all about wipe coding for classification. I think we have covered most of the things. In case you have a very specific use case where you want to get started with classification models, you can do the same techniques that I have explained um just after the KN&N videos on how to get started with project workspace and other things. Set it up and start chatting with AI and do white coding. See you in the next video. The next type of technique or next type of problem that we study under supervised learning is basically regression. What do you mean by regression? We are all clear with classification and we will be learning about regression. Regression analysis is a statistical method to model the relationship between a dependent target and independent variables with one or more independent variables. More specifically, regression analysis helps us to understand how the value of the dependent variable is changing corresponding to an independent variable when other independent variables are held fixed. It predicts continuous or real values such as temperature, age, salary, price etc. So one of the examples I will talk and explain is let's say I have multiple records for area and price. For 500 square ft let's say the price is 2 million. For,000 square ft let's say the price is 5 million. For 1200 square ft, let's say the price is 6 million. For 700 square ft, let's say the price is 3 million and so on. The question here is what will be the price at 1,500 square ft? That is the question. What we need to do here is we need to predict price. The price is my y variable based on my area x. So if I draw my graph based on my area and price, let's say 502 millions is somewhere here. 1,05 million is somewhere here. 1,26 million is somewhere here. 703 million is somewhere here. These are my multiple data points. How to find the relationship between price and area? I hope everybody remembers the formula of line is mx + c. y = to mx + c where m is nothing but your slope and c is nothing but your intercept or bias or constant whatever you can say. Right now based on these data points what we do is we come up with the best fit line that goes through these points. a line that is closest to these four data points. Let's say this line. There could be multiple lines. There could be millions of lines, right? But we need to find the line that is closest to the data points. Of course, there is a strategy to find. Let's say I talk about let's say I'll talk about these two three these three points. This is a line and another line is this. How do we find line one is better or line two is better? Of course, we have to see how far these data points are from each of these lines. This data point I'll calculate the distance. Similarly, I will also calculate the distance from these data points to this line. Let's say this is distance 1, distance 2 and distance 3. So d1 + d2 plus d3 and let's say this is distance 4, distance 5 and distance 6. Whether this is larger or this is larger. If this is larger, then L2 is discarded. L1 becomes my best fit line. Right? We will again be using the concept of ukidian distance to find the distance between two points. Right? using examples I will be explaining in the next particular video in depth. But coming back to this example, if you are able to find this line which is called as the best fit line. Now you just have to find where your 1500 point is. Let's say your 1500 is somewhere here. So you take this and try to see where you are dissecting this line. This dissection is basically your predicted price. Let's say the predicted price is around 10 million. So this is what you do in the regression part. Based on multiple features, you predict your target variable. So in the next video we shall be talking about different types of regression using examples and further we will also jump into the practicals part to show you how regression can be performed on a given data set. Let's go ahead and implement our regression code. We have done the same thing. I'm using Google Collab and I have already mounted my Google Drive. My data set is in my Google Drive and this is the data set that I'm going to use which is 50 startup.csv. I'll try to read the file and I'll show you how the file looks like. That's how the file looks like. I have R&D spend, administration spend, marketing spend, my state and the profit. So what we need to do here is let me explain the use case. We need to predict the profit given any kind of these information. So if I pass my R&D spend is X, my administration is something, my marketing spend is something and I stay in California, what will be the profit? That is the simple use case. And this is why we are going to implement regression code here, regression algorithms because we are predicting on a continuous numerical variable. Now, of course, I cannot really show you the graph for this particular example, but as we are doing multiple features, we are using multiple features. That's why we will call it as a multiple linear regression problem. Imagine I'm going to do a profit prediction only based on R&D spend. That means my R&D spend will be on my x-axis and my profit will be on y-axis. Let's say for first one 165 and 192 somewhere here 162 and 191 somewhere here 153 and 191 somewhere here 144 and 182 somewhere here 142 and 166 somewhere here. Now based on this I will be creating my line and if you ask me what will be the profit at 150k spend R&D spend probably here. So that will be my profit. Easy to visualize. Now this is a case of simple linear regression problem where you are only having one feature. R&D spend is your only feature and based on that you are doing a prediction on profit. The formula here is y = to mx + c. But in this example as we are having multiple features 1 2 3 4 there are four features but inside state also there are multiple other variables right. So let's say you want to do some label encoding or you want to do some one hot encoding. You can do oneote encoding. Once you do oneote encoding, let's say you will end up having three or four valid values or three or more uh dummy variables. So those many features will be treated. So we'll try to perform some feature encoding here. Sorry. Um yeah, we'll performing some feature encoding here. So let's say I will divide my X data into DF dot ilio dot values and y equals to df do iilock the last column minus one dot values. I'll run this. What is my x? My x is everything except my profit and my y is my profit. My y is my profit. Now I will implement one hot encoder skarn.compose import column transformer skarn. pre-processing one hot encoder in the classification part we used get dummies right so that's another way of implementing dummy encoding or one hot encoding now this is another way of encoding so you can use either of these techniques to perform encoding right CT equals to column transformer so here my transformers Transformers encoder or not encoder. How many? Three. And then we mind the request to pass through ct.fit transform. Done. Now if you want to print my X, that's how my X looks like. So three dummy variables are created. Right? Now if you ask me what will be the formula of my multiple linear regression. The formula is very clear. 1 2 3 4 5 6 features. So y = m1 x1 plus m_sub_2 x2 plus m3 x3 dot dot m6 x6 + c. That's how my multiple linear regression problem looks like. And as we have so many features, it is impossible to plot a graph. So I'll not be able to show you the a graph like this as this is a multiple linear regression problem. But we will try to go ahead and create our model. Our next step is to do a training and test split. So if you want to ask me about the architecture, it somehow remains the same. We have data. We are dividing it into training and test. Again we will be creating our multiple linear regression model. passing the training data. Using the testing data, we will be doing predictions. Simple, right? So, X train, X test, Y train, Y test. I will be calling my train test split from skarn.odel selection. Input train test split. I'll be calling this passing my x and y test size equals to 0.25. Done. Then I'll be performing my feature scaling technique from sklearn.processing pre-processing import standard scaler SC equals to this extra equals to SC.fit transform. So fit transform is for my training data and only transform is for my testing data. All looks good. fit transform for training and transform for test. We because we fit the training data for model building. Now I will call my model. Now exactly the similar way that we create classification model you import your libraries decision tree classifier initiate the classifier and fit the training data. Same here also. So from sklearn dot linear model import linear regression and then I will call my model equals to linear regression model.pit training data done. So my linear regression model is done. Our next step will be predicting the test results. So what we need to do same fashion y pred equals to model dot predict testing data and then your predictions are all captured here. Now in the classification part your prediction was nothing but your y variable. Let's say your y variable was churn column whether it is churning or not. So your output was binary but here your output will be a continuous numerical variable right it could be anything and your actual data is something like this. Now let's try to plot the actual data and the testing data and see how far each how far are they from each other. Plotting the testing versus prediction data. So I will be using plt.plot plot. First I will pass my testing data. I will mark it as blue color. I will make the label as test. Copy paste red predictions plt.show run. And that's how your prediction versus test data looks like. Now out ofthe-box prediction. Let's say you want to do a prediction of out of the out of the sample prediction. You want to do a prediction on a data set or a data point that is not a part of the data. Right? So simply let's say I will do let's say I want to have New York as my city. Let's say my R&D spend is 120K. My administration spend is again 120K and my marketing spend is around 400K. So what we can do is df.head head x. Okay. DF head one. Yeah. R&D spend. And what was my X? Yeah. Uh yeah. This is for R&D spend. This is for administration. This is for marketing. Correct. So given this conditions, I will try to create my data. 0.0 0 comma 1 0.0 comma 1.0 0 comma 120 K comma 120 K comma 400 K that's my data right now I will be passing this data to my data frame so I will create a pandas data frame so simply I will name it as newf equals to pd dot dataf frame I'll pass my data now obviously before passing the information my data has to be transformed based on the training data based on my training X train data I have to transform this right so I'll pass my new DF and then I'll pass single equals to model predict on new DF and then print single so what is my single so my my profit is 152K that is my profit and that is all about regression. Now you simply have to instead of all these features imagine you just take one of them. Let's say this one which is which is you can just simply drop it here itself. You drop all these columns and only keep one of them and then try to predict for profit. That becomes your simple linear regression problem. And in that you will easy you will be easily be able to plot the graph and show your best fit line. But as we are implementing a multiple linear regression problem multiple linear regression algorithm we will not be able to plot that graph between features and your class distribution or sorry features and your y variable distribution. So that's all about regression and that's all about out ofbox sample prediction as well using a regression code. In the next video we shall be jumping into clustering and further we will also be talking about clustering practicals and then various other topics. Hi welcome back. The way we did our classification model by coding we will be doing a regression model as well. In the previous video you have seen that we have implemented a regression model a linear regression model for one of the use cases and in this video we are going to take the same data set and we will be doing some bip coding. To get started with let's try to upload the file here. So the file was 50 startups. if I was not wrong. And that's the file. I will try to rename it or let it be. Doesn't really matter a lot. Rename it 50 startups. Right now, we are going to leverage whatever we have done in classification. We are going to replicate it. Not leverage, sorry. We are going to replicate that. And to replicate, I have gone to grog.com. I have gone to the previous chat window where I had asked something about creating a new project. Right? And this is what I had asked. Not this one, this one. Right? Let me give you some context. I'll be uploading da da da. Right? I will be asking another question. Forget what has been discussed so far. Start fresh. Let me give you some context. I'll be uploading a CSV file in the project and would need to eventually create a regression model. I I could have still written ML or maybe we'll make it ML like a regression model. on 50 startup data set and I will be quickly importing few of the basic libraries because I want to have a quick look at the data is equals to PDR read CSV 50 startups CSV TF head. So I'm going to give an initial context. Let's say these are my columns. I'm going to give that data set with columns da da da. So I need to interact with the word upload the CSV file and ask questions about it. You need to behave as an expert when creating a new project on grog. What should be okay? So same question I've asked. Now in the meanwhile I'm going to create a new project. I'm going to write it down as regression project. 50 startups and then I'm going to add this as my suggested start a conversation. I I'm going to change it because last time we added it and then we changed the prompt while we started interacting with the bot and the second one is project instructions. So these are my project instructions. I'll again go through it. Project title is this uh objective data set and then data set is plain instruction for group uh correlation analysis lot of things it has mentioned we might not want so many things model optimization and all. Okay let it be let it be. uh building a multiple linear regression model. Um building a linear regression model. Okay. To predict profit. That's it. Now everything looks good. Attachment. I will try to attach this file 50 startups. Is this attached? Not able to confirm that. 15 startups. Okay. Attached. And then start. Okay. I have received and confirmed the uploaded da da da. And then here we have everything is ready. Now, of course, I can give more detailed instructions on what we have done in the previous video to replicate the same in this current video uh uh in this current uh lecture. But let's try to take directly Grock Grock's output and try to run them. Okay. So, I I'll delete this piece of code so that we can do everything from scratch. Everything is in one uh code window. I would like to break it down and then let's break it down step by step. The second step is loading the file and displaying the basic information about the data set. We have a no such file error. That's because of the naming convention. Just change it out. You can see the basic data set information is here 50 records. The first five records looks like this. And the summary statistics looks like this. Right? Now even before moving into the next part, if you also want to get some insights about summary statistics, you can always go back and can you tell me the summary statistics inside three to four bullet points and let's see what it does. So R&D spend and marketing spend shows high variability with some startups spending nothing indicating potential outliers or diverse business models. Profit ranges from this to this suggesting significant variation in startup success. Skew distributions. the large standard deviations R&D spend marketing spend compared to their means da da da suggest write skew distribution. So insights can also be you can easily get insights. In case you want to make your notebook even more detailed, you can add these things here. And let's say I'm adding these things here with minute editing. That's it. Now let's move on to the next part. missing values. All good. No missing values. And then going to the next part. So it's visualizing the distribution of numerical features. And here you have different pictures. Right? Now you can see this is somehow following the normal distribution. somehow following the normal distribution but distribution of marketing spend and distribution of R&D spend is skewed right so this is something you might interpret moving on to the next part which is correlation analysis here you will be able to see that this is how my metrics looks like our major focus is profit right because profit is the y variable that we want to predict And you can see profit has a very high correlation with marketing spend. That's it. Uh marketing spend and R&D spend. With administration, it has a very low score, very close to zero, which basically means it is not correlated. So if you want to delete this column, it also should not make a lot of difference. Moving on. So next part is checking for multi-olinearity using VIF. It's not that mandatory. So I'll try to remove it. That's what I told you that some prior experience knowledge is also needed. You cannot blindly copy paste all the code. So here it's trying to visualize some of the things like profit by state and then multiple other things. So let's try to run this. So we have New York, California and Florida. Based on that we have all these box plots. Similarly, there are few other plots as well like scatter plot between R&D spend and profit and it clearly means that there is a increasing trend in all of these states. For Florida, I can see a steady increase for New York also steady increase for uh for California also steady increase. So the correlation is very high with respect to state uh for profit even though it didn't come up in the correlation matrix because correlation matrix is only plotted on numerical columns but in case you want to plot this for state you need to do encoding right. So you can pause here and in case you want to do that step of changing the state into encoded values you can prompt it back change it to encoded values right I'm copy pasting the code you can see the very immediate step is encoding right so df encoded pf dum state drop first right and then let's say we have this code and let's say I will try to copy paste the future code as well. But after this, after the dummies, I'll come back to this correlation analysis and try to do that again because this is what I was talking right now just uh just couple of seconds back. Right now, let's try to plot this on the encoded one. Wait. Uh encoded Oh uh one second. So we okay it's because state Florida and state New York is coming as boolean that's why it has not a part of my correlation matrix. Let me try to ask uh I know the answer but I will just try to use chat GPT. After this I am getting state Kelly sorry which one was that? State Florida and state New York as boolean values. How to convert them to numerical? because I want to plot the heat map. So let's try to see. Of course, we will be having to change the boolean from boolean to numeric. So you can see uh okay it has directly taken this piece of code which should also work. Yeah. Yeah. You can see now you can see that profit versus state Florida profit versus state Florida the correlation is low and profit with state new why it's low it's bit weird it should not be low profit with okay this was profit with respect to R&D spend and then as a hue we were using state. So that is the thing. Okay. No problem. No problem. This is what it is. There is no strong correlation. So for profit the strong correlation is with two things. Uh in fact three. No it's two. So R&D spend and marketing spend. Right now moving on and rest of the things I don't want to do it in separate. Oh, we can do it in separate uh uh code lines. We have defined the features and then encoded them and then standardized them using uh standard scaler and then I am passing the data into the model and running the regression model and doing the prediction. And once the predictions are done, I'm going to evaluate the model. So for evaluation I'm using some matrices like R2 score MSE which is mean squared error and mean absolute error and then I will try to plot it up in the next line. Now here for those who might not be able to understand these metrics whether these are good numbers or not. It's very difficult to say whether these are good numbers or not. You can only identify whether this is good or bad when you compare it with any other regression model. Right? And and here if you go ahead and see here you have the actual profit and the predicted profit and these are the things right. So really the predictions are in the blue dots and the y uh the red line is nothing but your um linear regression predicted line. Right? So that is all about regression. In case you have any follow-up questions you can always ask. Let's say I want to ask uh I don't have any other models in mind. So just interrating the results from linear regression. Can you tell me whether these metrics are good or bad? Let's try to ask this whether these metrics are good or bad. See R2 score is good.89 R2 score is uh not bad. Mean squared error and mean absolute error is something that is very difficult to say right because it depends on the scale of the numbers as well. So to interpret the linear regression model's performance R2 to score this this this in the context of data set high R2 score and low MSE I'll use the summary statistics you shared to provide a clear concise assessment as an ML expert tailored to your project R2 score is good as that's what I told you what it means the R2 score indicates 89.87% 87% of the variance in profit is explained by the model's predictor R&D spend administration marketing spend state Florida and state New York is it good in the context of startup profit prediction and R2 score of around 0.9 is strong as it suggest the model captures most of the variable in profit mean squared error as I told you it's context dependent see whatever I'm saying chat GP is also responding the same right it's it's very context specific again mean absolute error also I would not consider this as good it's a very context related uh metric but yeah this interpretation has told it is good so it's good no problem let's say you want to rerun this piece of code with just R&D spend and marketing spend. We can do that as well. I want to rewrite the code only with features R&D spend and marketing spend. Can you remove other X variables and rewrite the code? because these two are having the highest correlation, others are not. I'm just trying to remove them and see whether we have an improved model performance or not. Uh you can see it has given you the latest code. Let's try to add few lines model with just R&D spend and marketing spend. And then I'll run this piece of code. Everything looks good. Um, reduced this, this, this, this, this. Okay, I'll just copy paste it because it has not copy pasted it properly. And then run this code. And you can see results are R2 score has improved right now the metrics are can you tell me this model was better or the or the previous okay definitely okay I'll not tell my verdict let's do a model comparison. I'll try to expand it. The new model results are these. The old model results are these, right? Evaluation. The new model has a higher R2 indicating it explains more of the variance and profit. So, model 2 is better. Talking about mean squared error, the new model's lower MSE indicates smaller average squared prediction errors. So model two is better right? And then main absolute error the new models lower m ae indicates predictions are on average closer to actual profit value by almost 492 units. Again the new model is better. So according to all of these features, all of these metrics sorry you can see. So I will write it down as notes here so that you can follow. But I think why it's like this the new model is better. Higher art to score. Do your MSE. Lower this. Why it's better? Make it bold. And that's pretty much it. Right. So that brings us to an end to this beautiful regressionbased by coding session. Well, in case you have any other use case where you want to get started with, I think you can just replicate the steps that I have done and I'm pretty much sure you should be able to figure this out. That's it. See you in the next video. Hi, in this video we shall be focusing on clustering algorithm. Now we know that machine learning are of multiple types which is supervised learning, unsupervised and reinforcement learning and predictive modeling or predictive analytics is majorly the supervised part where we will be creating some predictive model to predict something from the future be it a classification problem or a regression problem. Clustering algorithms basically comes under unsupervised learning technique. and unsupervised learning techniques do not have labeled data. So they are not considered as predictive modeling or predictive analytics or predictive models. However, clustering is also important in the field of machine learning. So let's go ahead and understand what exactly this algorithm is all about. Clustering or cluster analysis is a machine learning technique which groups the unlabeled data set. That means you have a data set let's say 10,000 customers. You don't have labels. That means you don't know which of them customer is active, which of them is churned or which of them is a fraudulent, which of them is a not fraudulent. You just have features like age, salary, location, blah blah blah. You just have customer information. Now if your manager or somebody asks you to create segments out of it. Hey, you have 10,000 records. Can you create segments? That means can you find which customers are likely to be similar to each other? The first question that you ask in these kind of questions is how many groups do I make? Should we make four groups, five groups or six groups? There is no certain answer for this. So we go ahead and apply clustering algorithm to find clusters. It can be defined as a way of grouping the data points into different clusters consisting of similar data points. The objects with the possible similarities remain in a group that has less or no similarities with another group. So what are some of the clustering algorithms? The most important algorithm is K means. We also have main shift. We also have hierarchical and various other DB scan and various other clustering algorithms. We'll majorly be focusing on Kins when we jump into the practical part. What are the use cases of clustering? As I told you, customer segmentation, market segmentation, news segmentation, social network analysis, anomaly detection. All these are different examples of applying clustering algorithms. The classic example that we might have talked about which is fraud detection that can also be considered as a unsupervised learning problem. What if you get a data set from your manager, it has 1 million records and it does not have labels whether this transaction is a fraud, this is a not fraud and so on. That means you don't have this labels. If you have labels, then you can simply call your classification problem, divide it into training and test, create models, train the training data, test it on the testing data, do the predictions. So when a new transaction comes in you will be able to predict. Done. But in clustering we don't have labels. So what can we do? We have to apply clustering techniques and finalize clusters. Once we get clusters we probably come to a conclusion that all these data points are similar in nature. We don't know whether these are fraud, these are fraud or these are fraud. But we kind of get clusters where data points are similar in nature. Now once you finalize these clusters you might be asking fine we got the clusters what's next? What is the business utility of these kind of algorithms? Because ultimately if our goal is to identify them as frauds and not frauds, how can we solve this problem? Now this is a complex example but the way we solve this problem is using clustering algorithm. You finalize the clusters and then you take help of domain experts or some cyber security experts or your company uh domain related experts and experts try to analyze these particular clusters. Let's say one of them one of the clusters is having the lowest records. So people start analyzing from the lowest cluster. So smallest cluster and using the smallest cluster using their domain knowledge they will try to label the data. So once the labeling of data is done then they move on to the next cluster then next and then next it goes on but our clustering algorithm is limited to this. It helps you to identify clusters. So that's all about clustering. In the next video we shall be talking about one of these use cases and we will be solving them using K means clustering algorithm. Let's try to implement the K means clustering algorithm. So we have already imported the libraries. We'll try to read the file and this is the file that I'm using which is maul.csv. I'll show you the file. That's how the file looks like. I have customer ID, gender, age, annual income and spending score. Now for the ease of understanding as we will be plotting the graph again we all know that it is feasible to plot a two-dimensional graph that means variable one versus variable two beyond that it is very difficult to plot three-dimensional graphs four-dimensional graphs five-dimensional and so on are impossible to plot right so we will be taking only these two features and based on these two features we will be performing the clustering technique we'll be implementing the clustering algorithm so I'll quickly take choosing two dimensions for easy plotting so x equals to df do iilo which one I will only take my third and fourth which is 0 1 2 3 and four. That's it. Dot values. Done. In C means clustering. In K means clustering the most important thing is to identify the optimum K value. And K value is obtained by a technique called as elbow method. Elbow method where we draw a graph between the number of clusters and the WCSS value in my y-axis. What is WCSS? WCSS means within the cluster sum of squares. Within the cluster sum of squares within clusters, sum of squares. Now, what does this mean? This means if you have a data something like this, if you are drawing one cluster then the center point is here. The WCSS is the addition of all these distances which will be very high. Right? When you have one cluster the WSS value will be very high. What if instead of this you have two clusters centroidids are here now the distances are low right definitely the WCSS for two clusters will be low as compared to one cluster and it goes on imagine for each and every point you have a cluster the WCSS value will almost be zero right so WCSS is nothing but within cluster sum of squares where you have to plot with respect to the number of clusters and somehow the graph looks like this. Why it is called as a elbow graph? Because the point where it forms an elbow that becomes the optimum number of clusters. Now in this particular example I see the optimum value of K is three. But does that make the K value as three prominent? No. we should always do a plus minus activity. So if I am implementing this algorithm, I will do a clustering on k= to 3, on k= to 2, k= to 4, k= to 5, something like that. So I'll also do some boundary case cases. So moving ahead, I will be implementing the elbow method. The code is one time. You have to write down it for one time. No need to memorize. So from sklearn.clusters clusters importance. I will mark my WCS WCSS value as empty cluster and then I will start with within within cluster sum of squares for I in range of 1 11. Let's say I will do it for 10 clusters. K means equals to K means n clusters equals to I in it equals to K means ++. As I told no need to memorize all this code. You can simply copy paste these quotes whenever you are implementing uh K means clustering for a new data set dot fit on my X data and then WCSS.append K means dot inertia. And once that is done, plt.plot plt.title title elbow method x label. So my x label will be clusters. My y label will be wcss. I'll run this. And you can see that's how my elbow method is giving me. This is my wcss graph. Now where does this graph forms an elbow? You can see somewhere here this point it is forming an elbow. So my first guess will be here number of clusters must be five. This is my initial intake. So what I will do is I will apply K means algorithm to the data set. So what I will do K means equals to K means N clusters equals to 5. random state equals to none. In it equals to K means ++ and then YK means equals to K means dot bit predict_X. Done. Now last step is to visualize the clusters. plt.scatter Scatter y k means equals to = 0 comma 0 equals to red label equals equals to cluster one. So here basically C means color S means marker size plt.catter scatter x of yk means= to= to0 yk means equals to 0a 1 s= to 100 yeah I think uh should be okay so I'll copy paste this for five clusters and finally I will do it for my centroids also centroids that means for each cluster what are the centroid locations five I will change all these colors to some random colors. Red, blue, green, yellow. Anything else? Cyan, and black. Uh, and just change this to 1 2 3 4 1 2 3 4. That's it. For centrids, that has to be two things. A means that cluster centers colon 0 and colon 1. Done. And I will copy paste these things from title till show. cluster of clients. My x label is my annual income and my y label is pending score. Done. And that's how my graph looks like. And as we are visualizing, okay, X level and Y level. As we are visualizing As we are visualizing just two features which is pending score and annual income. That is how my graph looks like. And these are the five clusters. Now if you look at this cluster very closely. You can look at this cluster cluster very closely. This piece of group group of people are people with low income and low spending score. Low income and high spending score. High income and high spending score. So if you are one of the companies or one of the shops from the mall, you have to always focus on these type of customers whose annual income is high and whose spending score is also high. These type of people annual income is high but spending score is low. These are medium people. So you can prioritize them first then them then them then them them and then them them. And that is what your clustering is doing. It is segregating the different types of customers into different clusters. And all these customers are similar to each other within their clusters. Now the point is what if I want to plot it for five six clusters. So I will change it to six and instead of five clusters I will just have one more six and let's say purple make it five. Make it five. Simple done. And now I will have six clusters instead of five. And that is what we are getting using K means clustering. Now as we all discussed this thing in the theoretical class that in clustering problems the output is not certain. We don't know what we are trying to solve. And the end of a clustering algorithm is not an end of creation of a model. Once you have segregated your customers, you can further kind of assign them these labels as cluster one, cluster two, cluster three and that acts as a class. And for a new customer you can also predict the predict the cluster in which cluster the client is falling and so on. So lot of things are there also to discuss about clustering but that's all about clustering that we understood uh using Cayman's clustering how to solve a simple problem. So that's it. See you in the next video. Till then bye. The next topic of discussion is time series forecasting. Now time series forecasting is nothing but an extension of a regression problem. But it is not considered as a pure regression problem because of some factors. First of all, it is studied under supervised learning. And of course, the way we create our time series model, it is also considered as a predictive model because it predicts the future. It predicts the next instances. Now, one of the classic examples of time series forecasting could be temperature forecasting or any kind of sales forecasting. Imagine you have a data like this. You have date sales and multiple other features and the date is like 1 Jan the sales was like to 120 million 2nd Jan 3rd Jan and so on and let's say you have data till 30th of July and you want to predict for the next 5 days. This is a classic example of time series forecasting problem. Why? Because time plays a vital role here. Time is one of the X variables. So a time series is a series of data points indexed or listed or graphed in time order. And one more criteria of being a time series data is the data points has to be in equal intervals. If your data is like this first gen and then 3rd gen and then 11th gen and then 15th Feb. No this cannot be considered as a time series data. I mean there are ways to convert this into time series data. If you accumulate or take an average and consider Jan web and so on. So you cannot consider it it as a daily time series data but as a monthly time series data yes possible when you are aggregating data into months you can see the data points are in equal intervals but the raw data will not be a time series data and based on the type of data you have you can also predict your future let's say you have daily data and you want to predict for the next 5 days possible if you have monthly data and you want to predict for the next 15 days or 20 days or 30 days, possible. All these things are possible in time series. But time series is simply an extension of regression problem. But the time series algorithms are completely different. The way they work are completely different as compared to regression problems. Most commonly a time series is a sequence taken at successive equally spaced points in time. Thus it is a sequence of discrete time data. Some of the examples are heights of ocean tide counts of sunspots and the daily closing value of blah blah blah industrial average and all those things right I'll give you some classic examples one of them being temperature prediction one of them being u sales prediction in your company you are analyzing what is the CPU utilization of multiple servers you want to predict what could be the CPU utilization for the next 5 is that could be an example. Let's say in your server you are maintaining some file servers some of the files in your server. So your server is also eating up some file space. You are doing a file space utilization prediction. All those things comes under your time series forecasting. You are running a newspaper agency daily. If the number of newspapers that are sold from your agencies are like 250k, 300k, 320k and so on and you want to predict for the next 5 days, 10 days or 1 month. That can also be a time series forecasting problem. That's all about time series forecasting problem. In the next video, we shall be seeing a small Python code on on how time series forecasting problems work. Let's implement time series forecasting using one of the time series forecasting algorithms. We all know time series forecasting algorithms are very similar to regression problems because they are nothing but an extension of regression problem because they do a prediction on continuous variable. But one of the most important feature, one of the most important aspect of a time series data is date. Date feature is very important. So in this particular exercise, we will be using one of the data sets which is air passengers data which talks about in which month how many passengers were there. And let's say you have data from 1949 till 1960 which will be used in your model and you will be able to predict for 1961 or probably 1962 62 and so on. This is what the use case is all about. So there are multiple algorithms in time series forecasting which starts with ARMA. ARMA is auto reggressive integrated moving average model. Internally there are multiple flavors of ARMA which is AR M A R M A A R I M A seasonal ARMA seasonal AR max and so on multiple other sub flavors of this algorithm. Another algorithm is LSTM which is long short-term memory which is nothing but one of the extension of recurrent neural networks which is again an advanced machine learning topic which comes under deep learning. Another one is Facebook profit. Facebook profit is one of the algorithms obviously by the Facebook team where they made this algorithm opensourced probably in 2017 or 2018 and since then it has been one of the top performing algorithms. There are multiple other algorithms like hold spinter and various other algorithms like garch some advanced algorithms like n bits and so on. So in this particular exercise we will be using Facebook profit to read a data and then do a out ofthebox forecasting for the next 6 months, 10 months, 12 months or 5 years. Okay. So let's go ahead and implement the Facebook profit algorithm. So I have already imported all these libraries. My data set I have locally uploaded it. So I'm using directly from here. That's how my data set looks like. I have month from 1949 January. Let's say we have till 1960 December. So we have data from 1949 till 1960 almost 10 12 years of data and using this 12 years of data let's say we will be doing a forecasting for next 5 years. Okay. So I have renamed all these columns because that is how Facebook profit requires. Facebook profit requires you to rename your date column to DS and your Y variable to Y. So I'll do this and I'll do dot info which basically talks about this. You can see my DS is nothing but a date object but here it is mentioned as object. So I will convert it into date time. So I'll do this. Once this is done very simple, I'm just calling my profit model. M model equals to profit and I'm fitting my data frame. Here there is no concept of training and test. If you want you can also divide this data. Let's say till 1958 you keep it as training data. then call your Facebook profit and then do a prediction for the next 2 years and then on these predictions you compare and check your results. That is possible but what we are doing is we directly feeding the 12 years data to a model so that the model understands everything and directly we will ask the model to do a forecasting of five years. How do we validate whether this model is performing good or not? That is a questionable thing here. It is difficult to validate. If you are directly passing entire data set to your model and predicting, it is difficult to validate or you have to wait for one year to validate. Obviously, this is a better technique in in case you are trying to validate your model. But imagine your model is already validated. In that case you can directly pass the entire data to the model and predict for the future. So I've already run this code model.fit. Here I'm calling the model dot make future data frame. How many periods I'm passing? I'm passing 60 periods. Frequency equals to m basically means month. So there are multiple other frequencies like m d is day year wake and so on. So if you want to do a frequency of two sorry frequency of year and 60 that means it will be predicting for 60 years. So till 2020 it will predict 2021 and if you pass m and 60 it will do it for next 5 years which is from 1961 till 1965. So I'll run this and then I will pass my future to my model.predict and save it in the forecast and then I'm plotting my forecast and that is how my forecast looks like. So till here till 1960 is my original data 1960 December and this is my forecasting and obviously as this is a very predictable data you know I can easily predict that obviously the trend is high there is an increasing trend and there is a seasonality as well like every year at certain point of time there is a spike so it is quite predicted that it will be something like this and the model has also done a good job. Another method inside Facebook profit model is plot components which basically talks about the different components of time series. You can see an increasing trend. You can see some yearly seasonality also. And that brings to an end to our time series forecasting. As this was one of the simplest algorithms, I choose this in case you want to implement other algorithms you have to explore by yourself. That brings to an end to a predictive model module. So I hope you enjoyed the predictive model building especially clustering especially the classification regression and time series forecasting which are basically considered as a predictive model. Clustering algorithms however are not considered as predictive models but well and good in case you want to learn all these things you have already learned. That's all about this module on predictive modeling. See you in the next video. Hi, welcome back. In this video, we shall be implementing a time series forecasting algorithm using wipe coding using AI tools. So the data set that we are going to use is air passengers data set. Let me quickly read it on Google Collab and rename it. And here we are. Our data set is ready. Let's try to do a quick importing of the basic libraries. Import pandas as pd numpy as np import mattplot lib.pipplot as plt and then df is equals to pd sorry df is equals to pd.feed cssp air passengers dot csp and df.head head five. Okay. So this is how my data looks like. Let's go to copilot and start working on this particular project. So for this exercise I'm going to use one of the algorithms. We can go for Facebook profit. So let's go ahead and ask. So I'm not showing you the Okay, let's try to quickly upload. Sorry, not new upload air passengers file and then quickly tell that um you want to implement act as a time series expert and using Facebook profit. Can you prepare a time series model that is trained on 80% of this data and rest 20% can be used as testing purpose do this I think this is fine let's try to ask and see how are We getting the results? Absolutely. Let's walk through how to set up a time series forecasting model using Facebook profit with the 8020 training test sprint. Right. The very first thing is loading and preparing the data. I'm just deleting this uh I will be following exactly from this copilot. Right? So let's try to use this and let us write it down as step one. I will move it up. Step one, load and prepare the data. And here I'm loading it. That's it. And the second step is splitting the data, 80% training and 20% testing. Right? So split index and then I'm dividing the data into training and test. Right? Then I'm fitting the profit model. Fitting the profit model from profit import profit model equals to profit model.fit train. In case in in your case if it is not working you just have to install pyan and then install profit. If you encounter any kind of issues ask copilot directly. You will be able to get the results. The step number four is forecasting the future same length as test right because test is of 20% of the records. So we will be using the same forecasting uh technique for those many records so that you can compare those results right and then visualizing the forecast. Personally I am using Microsoft copilot on a very rare basis but this is how it is. You can see this part is your prediction part, right? So it has already done a good prediction. Go going back. Step six is evaluating your model where it will tell whether the model has done a good job or not using multiple metrics. Now here is the catch in regression like in classification. Even if you're running one model, you can still identify whether these models results are good or not by comparing the metrics, right? You have recall precision, F1 score, accuracy score. But in regression, you still have metrics like ME, MAE, MSE, RMSSE, but the values can vary a lot, right? So unless you run multiple models, you will not be able to judge whether this model has done well or not. Right? But even though we have evaluated the model, we have got MA as 33 and RMS as 41. Now here let's say I want to do I want to do a more five records prediction. and plot them with the data frame. So I'm I'm just extending like five additional future predictions. So I will try to extend it extend the forecast by five periods. Copy this and then appending the forecast to the original data and then copy this and here we are plotting it all together. Trust me when I started working on Facebook profit I created the very first model after a lot of hassle and there were installation issues there were so many issues I cannot really talk about that but right now with the help of GPT oh uh this is the time series graph and the red one is the forecasted so it has not done the plotting at the right part. Uh, okay. Okay. Combined. Let's try to check what combined is. 1958. Okay, let's try to see df. tail. Yeah, that is the problem. So, what it has done is it has taken five steps from the testing data, right? That is why testing data was still um how much it was till 1958 or something. So what we need to do is let's let's go back to the code and I'll try to be I'll I'll be able to identify the problem. So we have the training data here. Then we have model profit and then forecast into the future and then we have evaluation forecast. Future extended. What is this? Future extended. Ah, okay. This is the problem. Future extended is future extended is model. Okay. The model is trained on uh the model is trained on training data, right? That's why. So what we need to do is we need to change here directly, right? Um okay, let's go back. Let's go back. Let's say we want to extend this forecast by four periods. So what we need to do is first we need to do model.fit on the entire data right not only the training data um and then model.fit fit the entire DF then I can I can uh go ahead and do the other things. Profit object can only be fit once. Okay, no problem. Let's try to create a new model. Uh model two, model two fit on data frame. That's it. And then model two. Model two. That's it. Future extended. Yes. Now you have 1961 data. Now you are applying the forecast to the original data. Now you have the new combined. Now you are plotting it all together. And now you have the results. You can see the results are not bad. Right? Because eventually if you take this to the next level, it would eventually look like this. Right? If I have to plot it right it is something like this right it is following this pattern right here and then it let's say I'll do it in grain it would have been here and then it would have been something like this and then something like this and then it would have been so sorry it would have been something like this and Then something like this and then something like this. Right? So I would say it's not bad. Maybe what I can do is I can have more predictions. Let's say we'll try to do like a 15 predictions. Uh make future predictions. Plot 20. Let's say we'll do 20. Let's say we'll do one year. Okay. One year. Okay. Uh the reason it's not taking is the tail 20. Uh white's taking from eight. That's why it is doing a prediction for after a little bit of gap. Let's let's let's let's see uh frequency is month tf.tail 5 is 1960 December. Let's say I'm just uh creating the model. I'm extending. I'm doing a tail of 15. Uh, one, two, three. 1 2 3 4 5 6 7 8 9 10 11 12. Okay, I understand what's happening. But I think this is better. And now if we do this and do this and do this. No, it's still not coming properly. Okay. Uh let's do one thing. Uh as we are doing wipe coding, let's move back to copilot and give our prompt. I want to do a 12step future prediction and train the model with the entire DF. Can you rewrite the code? Okay. So I will try to delete it from here and evaluate the model was fine. from here uh I will try to make it 12 periods and from here I will start deleting it. Okay. And I will rewrite this code. We have everything. I'm just creating uh another model model profit and then forecasting for the next 12 months and then visualizing the next 12 months and view the final 12 predictions and you can see there are these are the 12 next predictions right and if you want to check the last 12 records here We have the last 12 records, right? So this is how we can do the predictions. If you want to do out of sample forecasting for next 10 years or 10 days or 10 weeks, I think you know that we need to change the frequency or directly go here and tell that I want to do a next 5 year predictions but keep periods equals to five and change the frequency and rewrite write the code and it will be able to give you the latest code. Right? So you can see everything is fine but here it has just changed the frequency to year start. Right? And then everything is same right? Just if you want to rerun this code, we can also rerun predictions for next 5 years. We will run this and then we will run this. Then we will run this and then we will not need it. And you can see yes uh as this is a huge prediction see the data is monthly data when you're doing a prediction on yearly data it is somehow taking the aggregated values it is able to identify the linear trend but the results are not up to the mark right so if you change it to instead of 5 years make it 60 months which is also 5 years Right? But make it MS and rerun this. We we will do it in the next piece of code. Let's say we change it here to MS and make it 60. And here you go. Better results, right? But the same thing, you're still doing a five 60 uh uh 60 months or 5 years is same, right? You're doing the same thing. You're doing a prediction for the next five years. But here you are doing on monthly level but here you are doing on yearly level that's why you can see aggregated values that is the difference right so it is always recommended to do the predictions in the same interval as the original data is but yeah there are options if you want to change it you can change it but the results will not be great it will be aggregated right so that brings us to an end to this wipe coding session on time series data and uh the way we have explored Microsoft copilot without sign ining you can see sign in is not mandatory if you sign in it's always good if you are working on private data for your company never ever post it online on public forums on public chat GPT or on public copilot Always make sure you are signed in and make sure uh do not upload any kind of very confidential data as well. So in in my case what I do is if the data is not confidential uh sign in is mandatory. I never upload it publicly. Even if I'm working on my YouTube projects or my Udemy projects, I never upload anything publicly. Now this was just a basic air passengers data which is publicly available. So I used it without signing in. I usually sign in and do everything whenever it is confidential data. I don't even use these tools. I have already created a tool which is very similar to chat gpt or gro which is on my local machine and it is also deployed on AWS uh EC2 instance. So whenever I have to use I can use that. Uh well I cannot show you that. Uh but to build such models of course you need to have some more knowledge but you can also take help of chatgity to build such kind of a tool. You just have to use open AI APIs and create something. Well that is something which is taught in the data science and AI program as well. Well so that's all about this particular session. I hope you enjoyed it. See you in the next video.
Predictive Analytics in 5 Hours | Complete Beginner to Advanced Guide | Python + Machine Learning Master Predictive Analytics from scratch in this comprehensive 5-hour masterclass! Whether you're a complete beginner or looking to level up your Data Science skills, this full course covers everything you need to build powerful predictive models. 𝐓𝐢𝐦𝐞𝐋𝐢𝐧𝐞: ✅ 00:00 Introduction ✅ 01:20 CHAPTER 1 ✅ 06:33 CHAPTER 2 ✅ 20:38 CHAPTER 3 ✅ 26:15 CHAPTER 4 ✅ 28:41 CHAPTER 5 ✅ 31:55 CHAPTER 6 ✅ 37:41 CHAPTER 7 ✅ 56:06 CHAPTER 8 ✅ 01:02:08 CHAPTER 8.1 ✅ 01:13:14 CHAPTER 8.2 ✅ 01:23:38 CHAPTER 8.3.1 ✅ 01:38:13 CHAPTER 8.3.2 ✅ 01:52:30 CHAPTER 8.3.3 ✅ 02:20:21 CHAPTER 8.5 ✅ 02:34:44 CHAPTER 8.6.1 ✅ 02:42:50 CHAPTER 8.6.2 ✅ 02:47:24 CHAPTER 8.7 ✅ 02:53:24 CHAPTER 8.8.1 ✅ 02:58:33 CHAPTER 8.8.2 ✅ 03:02:19 CHAPTER 8.9 ✅ 03:08:40 CHAPTER 8.10 ✅ 03:13:30 CHAPTER 8.11 ✅ 03:18:53 CHAPTER 9.1 ✅ 03:23:56 CHAPTER 9.2 ✅ 03:38:00 CHAPTER 9.3 ✅ 03:59:48 CHAPTER 10.1 ✅ 04:05:35 CHAPTER 10.2 ✅ 04:18:42 CHAPTER 11.1 ✅ 04:22:55 CHAPTER 11.2 ✅ 04:31:08 CHAPTER 11.3 🔴 Code: At 100 likes, 50 comments.. Subscribe our YouTube Channel and press the bell icon to get regular updates👇: https://bit.ly/3tt2eNY Join our Telegram Channel For Exclusive Data Science Resources👍 : https://bit.ly/3FzObsk Check Out Our Other Helpful Videos:😍 ➮ Data Analyst vs Data Scientist vs Data Engineer - Roles || Responsibilities & Skills https://bit.ly/3qsg2pL ➮Live Implementation of End To End Machine Learning Project With Deployment | Customer Churn https://bit.ly/3FqPQjF ➮ Build your own Alexa in 30 minutes using Python | NLP | Data Science https://bit.ly/3FFBWdP ✅✅✅ Follow us on: ✌️ 👥 FACEBOOK: https://bit.ly/3I89SB4 📸 INSTAGRAM: https://bit.ly/3GLSvFn 💬 LINKEDIN: https://bit.ly/3fmwmCa 🔴 TELEGRAM: https://bit.ly/3MjBODX 🔴 TELEGRAM Discussion Group: https://bit.ly/3mcusay Hello Friends , I am Satyajit Pattnaik, In my channel you will find every information about Data Science & Analytics which will help you become an expert Data Scientist or a Data Analyst along with which you would enjoy a loads of interesting and useful projects. More & more great stuffs coming soon, keep supporting & learning 🎓 THANKS FOR WATCHING 😊 #PredictiveAnalytics #MachineLearning #DataScience #Python #KNN #CustomerChurn #DataAnalytics #BeginnerToAdvanced #ArtificialIntelligence