Stock price prediction using machine learning is a difficult task, as the future course of a stock price is often influenced by a number of factors that are difficult to predict. In recent years, machine learning has become a popular tool for stock price prediction. Machine learning is a form of artificial intelligence that allows computers to learn from data without being explicitly programmed. It is based on the idea that a computer can learn to identify patterns in data without being explicitly told how to do so.

In this article, we will discuss the top 12 different ways that we can use stock price predictions using machine learning concepts and algorithms.

Before going to the stock price prediction using machine learning algorithms, we assume that you have a basic concept of Machine learning, artificial neural networks, and its various algorithms.

## Introduction to Stock Price Predictions Using Machine Learning

The stock market is a giant Wall Street casino where you can gamble your money away. There are a lot of scams out there, and it can be a very risky business. You could make a fortune, or lose everything you’ve invested. There is no guaranteed way to make money in the stock market, but there are a few things you can do to increase your chances of success. In recent years, Machine learning has played a crucial role in predicting the stock price and helps investors a lot. So, if you can learn how to use Machine learning algorithms to predict the stock market price, then you can get a lot of profit.

### How does Machine Learning Help to Predict the Stock Market Price?

If you are familiar with the working of machine learning models, which are trained on historical data and find the trends and hidden patterns in the data. In a similar way, the goal of Machine learning models in stock market price is to assess the relative significance of more recent and older data and identify the factors that have the greatest impact on prices on the “**current**” or “**next**” day. Each market feature is given a weight by the machine learning model, which also decides how much historical data should be considered in order to forecast future stock values.

Without wasting time, let us jump to the top 12 different ways for stock price predictions using machine learning.

## Top 12 Ways for Stock Market Price Predictions Using Machine Learning

Now we will explore 12 popular algorithms that we can use for stock market price predictions using Machine Learning. **NOTE** that in this article, we will only focus on different types of algorithms that you can use for stock market price predictions using Machine learning, we will not focus on their accuracy. In the other articles, we will go deep into each of these models to have better predictions by using various hyperparameter tuning methods.

As a sample, we will use the stock price dataset from Kaggle. Let us first import the dataset and explore it.

```
# importing the pandas module
import pandas as pd
# reading the dataset
data = pd.read_csv("Stock_price.csv")
# data head
data.head()
```

Output:

You can visualize the stock price using various plots in Python.

Let us now remove the index column and date column for simplicity.

```
# dropping the column
data.drop("Index",axis=1, inplace=True)
data.drop("Date",axis=1, inplace=True)
```

Now we will divide the dataset into input and output values.

```
# splitting the dataset into input and output values
x = data[["Open", "High", "High", "High", "Adj Close", "Volume"]]
y = data["CloseUSD"]
# converting to numpy array
x = x.to_numpy()
y = y.to_numpy()
# fixing the shape of output
y = y.reshape(-1, 1)
```

Now our data is fully ready and we can use various models for stock price predictions using machine learning.

### 1. Long Short Term Memory Networks for Stock Price Predictions

Long Short-Term Memory Network is a Neural network that is capable of learning order dependence in sequence prediction problems. It can be used for stock price predictions. We will use these simple important steps to build neural networks to build LSTM.

Let us initialize the model and build neural networks.

```
# importing the modules
from keras.models import Sequential
from keras.layers import Dense, LSTM
# initializing the model
model = Sequential()
# building the neural network with 128 nodes and specifing the input shape
model.add(LSTM(128, return_sequences=True, input_shape= (x.shape[1], 1)))
# adding hidden layers
model.add(LSTM(64, return_sequences=False))
# adding dense layer
model.add(Dense(25))
# adding output layer
model.add(Dense(1))
```

Once, we create the neural network, the next step is to train the model on the training dataset.

```
# compliting the neural network model
model.compile(optimizer='adam', loss='mean_squared_error')
# fiting the model on 10 epochs
model.fit(x, y, batch_size=1, epochs=2)
```

Because the dataset is very large, so we are using only 2 epochs for simplicity. You can change the value of epochs to get optimum results. Or you may also use hyperparameter tuning of neural networks to get optimum results. But here our main focus is not to optimize the model but to see how we can use LSTM for stock price predictions.

Once the model is trained, then we can use the input values to predict the closing price as shown below:

```
# import numpy
import numpy as np
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

As you can see, our model has predicted the closing price of the stock market for the given input values.

### 2. Gaussian Process Regressor for Stock Price Predictions

In the field of machine learning, Gaussian process regression (GPR) is a nonparametric and Bayesian approach to regression that is making waves in the area of machine learning. GPR has a number of advantages, including the ability to evaluate prediction uncertainty and do well on tiny datasets.

Let us import the Gaussian process regressor and train the model on the dataset.

```
# importing the module
from sklearn.gaussian_process import GaussianProcessRegressor
# iniliazing the gaussian process regressor
model = GaussianProcessRegressor()
# training the model
model.fit(x, y)
```

Once the training is complete, you can then move to the predictions part. **NOTE** If you get an error while training the model, it is because of having a large number of data. Try to train the data by reducing the training dataset.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

As you can see, we get 0 as a predicted value which means the model was not trained well on the training dataset. You can increase the performance of the model using various techniques.

### 3. RANSAC Algorithm to Predict the Stock Market Price

RANSAC stands for Random Sample Consensus. It is similar to simple linear regression but with some advanced features that handle the outliers and train the model on normal data points.

An iterative technique called random sample consensus, or RANSAC is used to estimate a mathematical model from a set of data that includes outliers. In order to estimate the target, the RANSAC algorithm first identifies the outliers in a data set then the model uses data that do not contain outliers to train the model.

Let us initialize the model and train using the training dataset.

```
# importing the module
from sklearn.linear_model import RANSACRegressor
# Initializing the model
model = RANSACRegressor()
# training the model
model.fit(x, y)
```

Once the training is complete, we can use the input values to predict the stock price.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

As you can see, the model has predicted the stock price to be nearly 153.

### 4. CatBoost Algorithm to Predict the Stock Market Price

The most recent boosting algorithm that works with classification, regression, and time series datasets is called Catboost. The module was created by Yandex, a firm that offers online taxi services, and it was made available to the general public in 2017. Catboost is widely utilized for many purposes, including weather forecasting, personal assistants, recommendation systems, and self-driving cars.

In this section, we will use the CatBoost algorithm for the stock market price predictions. Let us initialize the model and train it on the dataset.

```
# importing the required module
from catboost import CatBoostRegressor
# initializing the model
model = CatBoostRegressor()
# training model on stock price data
model.fit(x, y)
```

Once the training process is complete, we can then use the testing data points to predict the closing price.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

As you can see, we get a predicted value. You can use the hyperparameter tuning method to find the optimum values for the parameter to get the optimum results.

### 5. LightGBM for Predict the Stock Market Price

LightGBM is also a boosting algorithm, that works similarly to other boosting algorithms with some advanced features. It was developed by Microsoft company and was made publically available in 2017. It can handle null and categorical values.

LightGBM can be used for classification, regression, and time series datasets. In this section, we will use LightGBM for the stock market price predictions. Let us first import the module and train it on the training dataset.

```
# import lightgbm
import lightgbm as lgb
# initialzing the model
model = lgb.LGBMRegressor()
# train the model
model.fit(x,y)
```

Once the training is complete, we can then use the testing dataset to make the prediction for the closing price.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

As you can see the model has predicted the closing price of the stock. You can get the optimum result by using the hyperparameter tuning of LightGBM.

### 6. Extra Tree Regressor to Predict the Stock Market Price

Extra tree regressor is short for the Extreme Randomized tree. It is an **ensemble** machine learning algorithm. It uses decision trees to create an ensemble learning model.

Let us initialize the model and then train it on the training dataset to predict the price of the stock.

```
# importing the module
from sklearn.ensemble import ExtraTreesRegressor
# initializing the model
model = ExtraTreesRegressor()
# Training the model
model.fit(x, y)
```

Once the training is complete, we can then use the input data to predict the closing price of the stock.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

As you can see, the model has predicted the closing price.

### 7. XGBoost to Predict the Stock Market Price

XGBoost is a well-known boosting algorithm that is widely popular in many areas. It can be used to predict classification, regression, and time series datasets. In this section, we will use the XGBoost algorithm to predict the stock price.

```
# importing the xgboost model
import xgboost as xgb
# xgboost regressor
model = xgb.XGBRegressor()
# training the model
model.fit(x,y)
```

Let us now use the input data to predict the price of the stock.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

You can increase the performance of the model by using the hyperparameter tuning method if the model fails to predict correctly.

### 8. Gradient Boosting Algorithm to Predict the Stock Market Price

Gradient boosting is one of the famous boosting algorithms, that can be used to predict classification and regression datasets. It combines many weak learners to create a strong predictive model.

Let us now import the Gradient boosting model and train on the training dataset.

```
# importing the regressor
from sklearn.ensemble import GradientBoostingRegressor
# training the model with
model=GradientBoostingRegressor()
# training the model
model.fit(x, y)
```

Let us now use the testing data to make predictions.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

You can use the hyperparameter tuning method to get an optimum result.

### 9. AdaBoosting Algorithm to Predict the Stock Market Price

AdaBoost is short for the Adaptive boosting algorithm. It is considered to be the first boosting algorithm. If you understand the working of AdaBoost, you can then easily understand the working of other boosting algorithms.

Let us now import the model and train it on the training dataset.

```
# importing ada boost regressor
from sklearn.ensemble import AdaBoostRegressor
# Create adaboost regressor
model = AdaBoostRegressor()
# training the ada boost regressor
model.fit(x, y)
```

Once, the training is complete, we can then use the testing dataset to make predictions.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

You can increase the performance of the model by using hyperparameter tuning methods.

### 10. Random Forest Regressor to Predict the Stock Market Price

The Random Forest Algorithm is a type of Supervised Machine Learning algorithm that builds decision trees on different samples and takes their majority vote for classification and average in case of regression. We can also use the Random Forest Regressor for the stock price predictions as well.

Let us import the model and train it on the training dataset.

```
# importing the model
from sklearn.ensemble import RandomForestRegressor
# instantiate random forest method
model = RandomForestRegressor()
# fit the model
model.fit(x, y)
```

Next, we will use the testing data to make the prediction.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

As you can see, the model has predicted the price of the stock market.

### 11. Decision Tree for Regression to Predict the Stock Market Price

The decision tree is the most commonly used and famous algorithm. Many boosting algorithms use decision trees as weak learners. Let us initialize the decision tree regressor and train the model.

```
# importing the decision tree
from sklearn.tree import DecisionTreeRegressor
# initializing the model
model = DecisionTreeRegressor()
# providing the training dataset
model.fit(x,y)
```

The next step is to use the input dataset to make predictions.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

As you can see, the algorithm was able to predict the price.

### 12. Linear Regression to Predict the Stock Market Price

Linear regression is one of the simple algorithms in Machine learning that is commonly used for regression problems. Let us import the model and train it on the training dataset.

```
# importing the model
from sklearn.linear_model import LinearRegression
# linear regression
model = LinearRegression()
# training the model
model.fit(x,y)
```

Let us now use the testing data to make predictions.

```
# input values
Open = 2568.300049
High = 2540.100098
low = 2552.399902
close = 2583.899902
ad_price = 2607.100098
volume = 0
# concerting the input to array
features = np.array([[Open, High, low, close, ad_price, volume]])
# prediting the closing price
model.predict(features)
```

Output:

You can use various hyperparameter tuning methods to get an optimum result.

NOTE: You can get access to all the source code and the dataset from my GitHub account. Please don’t forget to follow and give me a star.

## Summary

Nowadays predicting stock prices using machine learning methods is becoming popular and popular and many competitors around the world are using it. In this article, we discussed the top 12 different algorithms that we can use for stock price predictions using machine learning.

Pingback: Two simple ways to analyze stock market using Machine Learning - TechFor-Today

Pingback: Hyperparameter tuning of Linear regression algorithm in machine learning - TechFor-Today