Advertisement
Guest User

LinearRegressionYerba

a guest
Nov 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. import pandas as pd
  2. import quandl, math, datetime
  3. import numpy as np
  4. from sklearn import preprocessing, model_selection, svm
  5. from sklearn.linear_model import LinearRegression
  6. import matplotlib.pyplot as plt
  7. from matplotlib import style
  8.  
  9. style.use('ggplot')
  10.  
  11. quandl.ApiConfig.api_key = 'Not gonna show the API key'
  12.  
  13. #Get data from quandl
  14. df = quandl.get('WIKI/GOOGL')
  15.  
  16. #Deleting not important data
  17. df = df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume',]]
  18.  
  19. #Calc HL_PCT
  20. df['HL_PCT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. Low'] * 100.0
  21.  
  22. #Calc PCT_change
  23. df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0
  24.  
  25. #Getting rid of useless data
  26. df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]
  27.  
  28. forecast_col = 'Adj. Close'
  29.  
  30. #Fill empty data with -99999
  31. df.fillna(-99999, inplace=True)
  32.  
  33. #Calculate the value we want to see in the future
  34. forecast_out = math.ceil(0.01*len(df))
  35.  
  36. #Shifting the table up
  37. df['label'] = df[forecast_col].shift(-forecast_out)
  38.  
  39. #Define X
  40. X = np.array(df.drop(['label'], 1))
  41. X = preprocessing.scale(X)
  42. X = X[:-forecast_out]
  43. X_lately = X[-forecast_out:]
  44.  
  45. df.dropna(inplace=True)
  46.  
  47. #Define y
  48. y = np.array(df['label'])
  49. y = np.array(df['label'])
  50.  
  51.  
  52. X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2)
  53.  
  54. #Choosing algorythm
  55. clf = svm.SVR()
  56.  
  57. #Fitting the data
  58. clf.fit(X_train, y_train)
  59.  
  60. #Calculating accuracy
  61. accuracy = clf.score(X_test, y_test)
  62.  
  63. print('Accuracy: ', (accuracy*100.00), '%')
  64.  
  65. #Predicting values
  66. forecast_predict = clf.predict(X_lately)
  67.  
  68. #Printing predicted values
  69. print(forecast_predict, forecast_out)
  70.  
  71. df['Forecast'] = np.nan
  72.  
  73.  
  74. #Date calculation for visualisation
  75. last_date = df.iloc[-1].name
  76. last_unix = last_date.timestamp()
  77. one_day = 86400
  78. next_unix = last_unix + one_day
  79.  
  80. for i in forecast_predict:
  81.     next_date = datetime.datetime.fromtimestamp(next_unix)
  82.     next_unix += one_day
  83.     df.loc[next_date] = [np.nan for _ in range(len(df.columns)-1)] + [i]
  84.  
  85. #Displaying the graph
  86. df['Adj. Close'].plot()
  87. df['Forecast'].plot()
  88. plt.legend(loc=4)
  89. plt.xlabel('Date')
  90. plt.ylabel('Price')
  91. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement