# Machine Learning: Future Sales Prediction

Predicting the future sales of a product helps a business manage the manufacturing and advertising costs of the product. There are many more benefits of predicting the future sales of a product. So if you want to learn to predict the future sales of a product with machine learning, let's code step by step.

First, we need to understand the dataset. In the dataset, you can see that there are

1. **TV:** Advertising cost spent in dollars for advertising on TV;
    
2. **Radio:** Advertising cost spent in dollars for advertising on Radio;
    
3. **Newspaper:** Advertising cost spent in dollars for advertising on Newspaper;
    
4. **Sales:** Number of units sold.
    

First, we need to import the libraries.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213607066/d486f0f2-a1c1-4132-8a61-b06d41192224.png align="center")

Then, we need to load the dataset using the pandas which is from the read\_csv.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213622708/0817c98f-bd61-4b74-893c-359b50ac10da.png align="center")

Using the head command which will give you the five top rows and columns

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213641103/37c7badf-7349-4a1f-baa4-d0bd0542d421.png align="center")

getting the bottom five through the tail() command

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213661782/a8048322-2266-4cc9-9e3b-4ef7df2d0bd5.png align="center")

getting the more information which gives the in-depth information about the dataset

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213677870/d5afd9a4-8b08-4f0a-9a9c-98ea32249a90.png align="center")

The describe method is used to generate descriptive statistics of a data frame which includes numeric columns, including count, mean, standard deviation, minimum and maximum.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213692941/fd59f30e-6b0a-48e9-b707-b1e3077acc8e.png align="center")

Now, the isnull() method will give the details about each row and column which is to check for missing or null values in a DataFrame.

d-types will provide you the information about the data type of each column, helping you understand how your data is stored.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213722739/506699e4-5c06-48bc-a56d-5edc98544c9a.png align="center")

Now, plot the graph using the plotly. express.

first plot the graph for the sales and tv using the px scatter plot.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213741966/539d3b88-ed5c-4c17-b520-3cc18c7fc0bc.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213772236/eb8613a1-758d-4ee7-ad18-dfcce41b0c51.png align="center")

scatter plot for the dataset between the sales and Newspaper

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213803564/877e5103-427a-4909-ab39-421e55ba28c4.png align="center")

scatter plot the graph between the sales and radio

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213823408/55b7f3a4-fc25-4963-b48e-f200ffa7c9e2.png align="center")

now, finding the correlation for the dataset which gives correlation of the dataset.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213844820/86c4d661-24f8-457f-92ed-3c63ea177bf0.png align="center")

Now, spliting the dataset into the X and y in which the X includes all the columns except the sales column and y column includes only the sales.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213886396/911d5e22-e604-4e26-b36e-4ceacfb3bd64.png align="center")

now, importing the necessary libraries for the further step which is train\_test\_split and the Linear regression from the sklearn model.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213925507/b47b23c7-475c-4770-9875-f684738f7bf2.png align="center")

Now, with the help of the train\_test\_split we are doing the x\_train, x\_test, y\_train, y\_test. taking the values of X, y, the test\_size and the random\_state.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213961653/a1f0060e-b565-479f-9e3d-a9906b5c642b.png align="center")

Finally, the most important step of all building the model using the Linear regression. Then fitting the module using the X\_train and the y\_train.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213974208/e297ea1d-5165-47bd-9d0f-9ccaa92d2edb.png align="center")

getting the score of the model in that we are taking the values of the x\_test and the y\_test.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696213988524/3ad15933-922e-4051-abed-d8d4dfd23504.png align="center")

Finally we take the new values for the prediction we then take the features and then make the model to predict the features using the machine learning algorithm.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696214002861/d4c192f9-3333-4900-8e9a-573d3b23a97d.png align="center")

Thanks for reading,

Mohammed Muqafamuddin.
