Stock Market Prediction Using Machine Learning Model(SVM)

Rupesh Sharma
3 min readJun 19, 2020

--

In the previous post we saw how to predict stock price using Linear Regression, In this blog we will see how SVM (Support Vector Machine) can predict the price and compare the result with LR.

# Load the data directly from NSE (National Stock Exchange)

Functions from pandas_datareader.data and pandas_datareader.wb extract data from various Internet sources into a pandas DataFrame

Take a look at the data where xx is the name of the stock, get the stock name from https://in.finance.yahoo.com/quote/%5ENSEI/

# Import Python Librariesimport numpy as np
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
import pandas_datareader.data as web
import datetime as dt
stock='^NSEI'
df1=web.DataReader('xx.NS','yahoo',start='1996-01-01',end='2020-06-19')

xx.NS is the name of the stock, yahoo is the method name, start/end is the start & end date of data extraction.

df1.tail(
Note : Adjusted closing price amends a stock’s closing price to accurately reflect that stock’s value after accounting for any corporate actions.
df = df1[[‘Adj Close’]]
df[[‘Adj Close’]].tail()

Above data frame is having Date and Stock Prices. lets create one more column which is having stock values 15 days in advance, by shifting 15 rows up.

let create Machine learning LR model based on the current and 15 days later values and predict the values 15 days in advance.

#Create one more column Prediction shifted 15 days up. 
df[‘Prediction’] = df[[‘Adj Close’]].shift(-15)
#print data set
print(df)
#Create a data set X and convert it into numpy array , which will be having actual values
X = np.array(df.drop([‘Prediction’],1))
#Remove the last 15 rows
X = X[:-15]
print(X)
# Create a dataset y which will be having Predicted values and convert into numpy array
y = np.array(df[‘Prediction’])
# Remove Last 15 rows
y = y[:-15]
print(y)
# Split the data into train and test with 90 & 10 % respectively
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1)

Apply SVM Model on Train Data

# https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html# SVM Model
svr = SVR(kernel=’rbf’, C=1e3, gamma=0.1)
# Train the model
svr.fit(x_train, y_train)

The radial basis function kernel, or RBF kernel, is a popular kernel function used in various kernelized learning algorithms. In particular, it is commonly used in support vector machine classification

C : Regularization parameter. The strength of the regularization is inversely proportional to C. Must be strictly positive. The penalty is a squared l2 penalty.

gamma : Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’.

Testing Model: Returns the coefficient of determination R² of the prediction.

# The best possible score is 1.0svm_confidence = svr.score(x_test, y_test)
print(“svm confidence: “, svm_confidence)
confidence value as much close to 1 will have high accurate prediction

get the last 15 rows of the original data set from Adj. Close column and convert into numpy array

forecast = np.array(df.drop([‘Prediction’],1))[-15:]
print(forecast)
This is the latest 15 days stock prices, we will use these values to predict next 15 days prices
# support vector model predictions for the next ‘15’ days
svm_prediction = svr.predict(forecast)
print(svm_prediction)
Predicted Values for next 15 days stock prices

Thanks for reading..

--

--