python

Python Simple Linear Regression Code

The code for our youtube tutorial Predict Stock Market Using Simple Linear Regression and Python

RahulVk April 2020 read
Share this

Python Code for our youtube tutorial is given below

Don't forget to Subscribe to Our Channel and Let us know your thoughts in the comments section of our youtube video

   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
  31
  32
  33
  import pandas as pd

  import numpy as np
  from sklearn.model_selection import train_test_split
  from sklearn.linear_model import LinearRegression
  from sklearn import metrics
  import matplotlib.pyplot as plt
  from matplotlib import style

  style.use('ggplot')

  df = pd.read_csv('datafile.csv')
  #replace datafile.csv with your csv file

  print(df.head(25))

  x = df[['High Price','Open Price','Low Price','Total Traded Quantity']].values
  y = df['Close Price'].values

  x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=0)

  regressor = LinearRegression()
  regressor.fit(x_train,y_train)

  print(regressor.coef_)

  y_pred = regressor.predict(x_test)
  result = pd.DataFrame({'Actual':y_test.flatten(),'Predicted':y_pred.flatten()})
  result.head(25)

  print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
  print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
  print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
  

If you have any doubts or run into any problem feel free to ask them at our youtube video comments here

Featured Image by Mudassar Iqbal from Pixabay
Comments

It works! I also watched your youtube video and subscribed to your channel

by Kannan

Add your Comment

Read next

Netlify vs Github Pages Speedtest

Github pages and Netlify are among the most popular choices for hosting static sites. Here we compare them on page load times.

RahulVk April 2 2020 · 10 min read

Mi Home Security Camera 360 review

by RahulVk
April 10 · 10 min read

Why you should use a static site generator ?

by RahulVk
March 27 2020 · 15 min read