<jupyter_start><jupyter_text>Polynomial Regression on Stock data (<NAME>)In this project, a time series data is taken as input and a polynomial regression is executed on the dataset to predict the future stock prices.<jupyter_code>%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import numpy as np
import pandas as pd
import matplotlib.pyplot as mp<jupyter_output><empty_output><jupyter_text>Load and prepare the dataThis project works with a small dataset saved into a `.csv` file. Let us load that up and perform required pre-processing if any.<jupyter_code>data_path = 'tri.csv'
data = pd.read_csv(data_path)

data.head()
data = data.sort_index(ascending=False)
data.plot(x='Date', y='Close')
data = data.sort_index()
updated_data = data.drop(['Open', 'High', 'Low', 'Volume', 'Date'], axis=1)
updated_data['Serial'] = range(len(updated_data) - 1, -1, -1)
updated_data.index = updated_data['Serial']
updated_data = updated_data.drop(['Serial'], axis=1)<jupyter_output><empty_output><jupyter_text>Polynomial Regression with up to 16 degreesGenerally, the more coefficients you take, in other words, the higher the degrees, the more accurate the line fits. With 16 degrees predicted values are rather optimistic as it ends with a peak.<jupyter_code>coeffs = np.polyfit(updated_data.index, updated_data['Close'], 16)
x_p = np.linspace(0, 260, 500)
y_p = np.polyval(coeffs, x_p)
mp.plot(updated_data.index, updated_data['Close'])
mp.plot(x_p, y_p)
mp.legend(['Actual', 'Regression'])
mp.ylim(38, 48)<jupyter_output><empty_output><jupyter_text>Polynomial Regression with above 16 degreesInterestingly, the moment we increase coefficients beyond 16, for this particular dataset, it ends with a sharp decline. Therefore, it is not a predictable strategy.<jupyter_code>coeffs = np.polyfit(updated_data.index, updated_data['Close'], 17)
x_p = np.linspace(0, 260, 500)
y_p = np.polyval(coeffs, x_p)
mp.plot(updated_data.index, updated_data['Close'])
mp.plot(x_p, y_p)
mp.legend(['Actual', 'Regression'])
mp.ylim(38, 48)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11887433284813198, 'fraction_numerical': 0.018922852983988356, 'mean_word_length': 6.870229007633588, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '424767', 'license': 'MIT', 'n_tokens_mistral': 721, 'n_tokens_neox': 680, 'n_words': 203, 'path': 'thomson-reuters-poly-reg/thomson-reuters-poly-reg.ipynb', 'repo_name': 'tsaqib/ml-playground', 'size': '2070'}