top of page
Search
  • Writer's pictureHox Framework

Prediction with Linear Regression in Python | LBOET | HOXFRAMEWORK



Hello and welcome, today we are doing some predictions with python.


Here is the python code:

----------------------------------------------

import numpy as np

from statistics import mean

import matplotlib.pyplot as plt

import csv

import sys


def new_entry():

mylist = []


print("ML system isnt made to take into consideration things like other months or years,so we only work with days.\n")

user_day = str(input("Enter the day (numeric):"))

user_price = str(input("Enter the price(numeric, not float-point value):"))


print("\nSaving old data...")

file2 = open("btc2019.csv", "r")

for k in file2:

mylist.append(k)

file2.close()


print("\nAdding new data...")

file = open("btc2019.csv", "w+")


#lets form our new entry

newentry = "\n"+user_day+","+"1"+","+"2019"+","+user_price


for r in mylist:

operationManager = str(r)

file.write(operationManager)

file.write(newentry)

file.close()

print("Done.")


def predict_btc():

day = []

price = []


with open('btc2019.csv', newline='') as csvfile:

reader = csv.DictReader(csvfile)

for row in reader:

day.append(row['day'])

price.append(row['price'])


lastestentry = day[-1]

print("\nLatest entry (day):",lastestentry,"\n")

print("What number would you like to predict(date-day)\nEnter a day (numeric):")

keys = int(input(">"))

xs = np.array(day, dtype=np.float64)

ys = np.array(price, dtype=np.float64)



def best_fit_slope_and_intercept(xs,ys):

m = ( ((mean(xs) * mean(ys)) - mean(xs*ys)) /

((mean(xs) * mean(xs)) - mean(xs*xs)))

b = mean(ys) - m*mean(xs)

return m,b



m,b = best_fit_slope_and_intercept(xs,ys)

print("m and b : ",m,b)

regression_line = [(m*x)+b for x in xs]



predict_x = keys #sami zadamo

predict_y = (m*predict_x)+b

print("Predicting x (",predict_x,") to be :",predict_y)


plt.scatter(xs,ys,color='b')

plt.scatter(predict_x, predict_y,color='r')

plt.plot(xs, regression_line,color='r')

plt.title("Prediction of the Bitcoin price - January 2019.")

plt.show()


usage= """

USAGE:


>myprogram.py <argument>

-arguments are:

-->predict

-->new


-Predict allows you to predict based on your csv

-new allows you to add new data into your csv, sometimes if you do it wrong

it messes shit up.


example:

> titaniumsky.py predict


"""

def Main():

try:

if sys.argv[1] == "predict":

print("Loading the prediction tool.")

predict_btc()


elif sys.argv[1] == "new":

print("Making a new entry")

new_entry()


else:

print("Invalid argument passed.Printing usage...\n")

print("Usage:\n",usage)

print("\n\n")

Main()

except IndexError:

print("No parameters given.")

print("Printing out help.\n",usage)



Main()



---------------------------------

And here is my CSV file, you can always make yours or use a different one:


---------

day,month,year,price

1,1,2019,3485

2,1,2019,3443

3,1,2019,3468

4,1,2019,3584

5,1,2019,3604

6,1,2019,3599

7,1,2019,3607

8,1,2019,3584

9,1,2019,3605

10,1,2019,3575

11,1,2019,3600

12,1,2019,3725

13,1,2019,3652

14,1,2019,3677

15,1,2019,3651

16,1,2019,3631

17,1,2019,3704

18,1,2019,3557

19,1,2019,3658

20,1,2019,3686

21,1,2019,3674

22,1,2019,4034

23,1,2019,4034

24,1,2019,4028

25,1,2019,4078

26,1,2019,3836

27,1,2019,3851

28,1,2019,3832

29,1,2019,3931

30,1,2019,3849

31,1,2019,3950


-----------------------------------------


Feel free to edit this dataset or the code itself, the code does not need the "New entry" function, it does not need to be ran with a sys module,

this can be done way less complicated.


Thank you so much for watching and have a nice day. :)



7 views0 comments

Recent Posts

See All
bottom of page