Training a Neural Profit Model for Stock Price Prediction Using Historical Data
Learn how to train a Neural Profit model to predict stock prices using historical data. Follow step-by-step instructions from data import to model evaluation.
File
Learn How to PREDICT TRENDS with Python and Machine Learning
Added on 09/29/2024
Speakers
add Add new speaker

Speaker 1: This is our actual data set. This is how the trend looks and we have to now provide this to the model. Now it's time for our model to train. Now let's just allow for the model to make predictions. When you run this, we get the predictions and this is how our model performs. Let's get into it. So we're going to start off with installing and importing all of the required modules and we will need two things. One for the historical stock value data set and the library used for this is WideFinance. Make sure you install that. You run this code so for me it says that the requirement is already satisfied for you. It might be different. It might be installing the package. Just make sure to install it and the next thing that we will need is neural profit. That is the model that we were using this video. Let's just import that. Again it says requirement already satisfied for me. Just make sure that you do it on your end. Now after you've done that, you will have to import all of the required modules. So first we're going to import neural profit and WideFinance itself. We just have to abbreviate this a little bit and this is the class that we imported for neural profit. This is the model that we will be building on. After which I will also import pandas and matplotlib just to do a bit of analysis and visualization. Let's just run this code. So yeah and that is done and now it's time for us to get the data set itself. And in order to do this, first we will need the stock symbol from where we're going to get the data from and I'm going to use Samsung for the first case. So stock symbol which is what I'm going to call will be just use the code that I'm using currently 005935.ks. This is the one that is used to indicate Samsung. After which we will have to mention the start date and the end date of our entire data set. So the start date will be from 2015 for my case. You can go to as back as 2010 as well. And the end date for our data set will be 2023 very first today. Now in order to get the data set, you have to pass all of these parameters to the function called yf.download. I'm going to call it stock data download. So this give all three of the parameters that you just created. The stock symbol, the start will be start date. Similarly for end as well. Now that that is done, let's just print the stock data and see how it is. And since this is going to be a pandas identifiable object, you can just use the .head function and then see the first five records alone. We get all of the values, we get the date, the open, I, low, close, and adjacent, close, and volume. Now we want to write this to a CSV file. What I'm going to do is use the stock data.to CSV function. And that's it. Now we have created the CSV file and added our data onto that. Let's just import that CSV file itself and make a bit of data preprocessing. Over here what I'm going to do is the stocks will be pd.readcsv function. Pass in the stock data. Now there's only two things that we need. The stocks date and then the adjacent close value. We will use this value to predict and then we will also use the date value. But before using the date value, I believe that this is going to be in the string format. Let me just, let me see that date is an object. We want it to be in the date format. So what we have to do is stocks of date will be, use the pd.doDateTime function for the already existing date. So what this will do is when you actually now print the D types, you will see that the date is converted to date times, which is actually what we need, which is actually the model is going to understand. The next one I'm going to do is extract only the date column and the close column. I'm just going to rewrite the stocks data set itself. Stocks will be stocks of, since this is two-dimensional, we will have to use two square brackets and inside this we have to mention the date and the close because those are the two columns that we want to use. Like that. Now when you print out stocks, we have date and close. Now there's just one more thing that we have to do before providing this to the model. Our model, the neural profit model, only understands data in the terms of DS and Y. DS standing for the X label and Y standing for the output label. So what we have to do is just rename the columns to DS, which means the input label, and then Y, that is our close value. Now when you print the stocks data frame, we see that the date is our input label and then Y, the close, is our output label, which is what we want. In fact, let's just plot the initial data set and see how it looks. I'm using the plt.plot function because we've imported that already and then we're using the date as the x-axis and then the corresponding stock prices, the close, as the y-axis. Let me just run this and show you the actual data set. This is our actual data set. This is how the trend looks and we have to now provide this to the model. This is by no means a financial advice. This is just showing you how a machine learning model is performing on a given data set and this seems to be satisfying. Now you can try this on many different data sets, which is related to time, maybe a sales data or maybe a weather data, but you can do all sorts of things with just Neural Profit. In order to train the model, as said earlier, the model that we were using is known as Neural Profit, the class that we imported. So this is the class that we imported from above and we're using it over here. This is the class that we imported and we're just using the instance over here. So that is our model and now we want to provide the data to the model so that it can read and identify how the trend works. So model.fit will do that and then you have to provide the stocks data like that. Now it's time for our model to train and you run this. It's going to take a while to train. So the model is done training and now it's time for us to evaluate the model by asking it to make predictions. So we need to generate a data set, a future data set let's say, by using a function known as makeFutureDataFrame like that and then inside this you have to mention the original data set that you provided to the model, stocks, and then say how much days do you want to predict in the future. So let's say that I want around about 300 days and see how the model is performing on that and this will be counted as forecast. So forecast will be, we use the model.predict function on the data set that we created to pass in the future data set like that. Now let's just allow for the model to make predictions when you run this. Predictions have been made. Now it's time for us to look at that and you just run forecast. We see that we have the dates and then the y hat value. This is the predictions made by the model. Let me just actually show you that specifically. ForecastDS and then you have to use forecast y hat 1 and you see the date and the corresponding predictions over here. So what we will be doing is plotting these two and see how it's performing using the function plt.plot and pass in these two values. Mention the label to be future prediction and I'm going to set the color to be blue. I also want to predict the original data set itself just to see how the model is performing for the already existing data. So actual again use model.predict function, pass in the original data like that. I want to plot this as well. Just the same line of code changing the values to just actual prediction and actual prediction over here. I've also set the color to red and finally I want to plot the original data set itself. So I'm going to call these two. Copy and then paste. Let's just actually type in plt.legend to segregate properly that and now when you run this code we get the predictions and this is how our model performs. So initially it identifies the trends and then it makes predictions for the future. Very nice. Now when you also use the model.plot components function like that and then pass in the forecast. This will show you all of the trends which it identified by looking at the data set. So it says that the trend is actually going down and then we also have the yearly seasonality how it increases how it decreases. This is the weekly seasonality. It doesn't change that much. Now this is the model predicted trend for Apple. Here is the seasonality of how it works and here is the model prediction for Google plus the corresponding trend as well.

ai AI Insights
Summary

Generate a brief summary highlighting the main points of the transcript.

Generate
Title

Generate a concise and relevant title for the transcript based on the main themes and content discussed.

Generate
Keywords

Identify and highlight the key words or phrases most relevant to the content of the transcript.

Generate
Enter your query
Sentiments

Analyze the emotional tone of the transcript to determine whether the sentiment is positive, negative, or neutral.

Generate
Quizzes

Create interactive quizzes based on the content of the transcript to test comprehension or engage users.

Generate
{{ secondsToHumanTime(time) }}
Back
Forward
{{ Math.round(speed * 100) / 100 }}x
{{ secondsToHumanTime(duration) }}
close
New speaker
Add speaker
close
Edit speaker
Save changes
close
Share Transcript