<jupyter_start><jupyter_text>Internet Resources:[Python Programming.net - machine learning episodes 1-11](https://pythonprogramming.net/machine-learning-tutorial-python-introduction/)Linear regression finds the best possible linear equation that fits to the training data.<jupyter_code>%matplotlib inline
import os
import numpy as np
import pandas as pd
import quandl, math
from sklearn import preprocessing, svm, model_selection
from sklearn.linear_model import LinearRegression 
import matplotlib.pyplot as plt
from matplotlib import style
import pickle

# getting data
if not os.path.isfile(os.path.abspath("data/wiki_googl.csv")):
    df = quandl.get("WIKI/GOOGL")
else:
    df = pd.read_csv("data/wiki_googl.csv")

# use date as index
df["Date"] = pd.to_datetime(df["Date"])
df.set_index("Date", inplace=True)

# only keep adjusted data 
df = df[['Adj. Open',  'Adj. High',  'Adj. Low',  'Adj. Close', 'Adj. Volume']]
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. Close'] * 100.0 # HL_PCT High low percentage
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0
df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]

df.fillna(value=-99999, inplace=True)

predict_lenght = int(math.ceil(0.1 * len(df))) # we are using 90% of the data to "train" our model and 10% to make predictions
predict_col = 'Adj. Close' # we want to predict the adj close price
df['label'] = df[predict_col].shift(-predict_lenght)
df
# split and prepare data

# x = features
# y = labels

x = np.array(df.drop(['label'], 1)) # 1 denotes which axis
# mean value along the axis is subtracted from x and the resulting value 
# is divided by standard deviation along the axis.
# see https://scikit-learn.org/stable/modules/preprocessing.html
x = preprocessing.scale(x) 

x_predict = x[-predict_lenght:] # these are the last 10% of the data. We will use these to make predictions
# the predict data frame will hold our predicted values
predict_df = df.iloc[-predict_lenght:] # iloc: Purely integer-location based indexing for selection by position
predict_df = predict_df.rename(columns={"label":"Prediction"})

# getting our "training" data
x = x[:-predict_lenght]
df.dropna(inplace=True)
y = np.array(df["label"])

x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=0.2)

predict_df
# do linear regression

if os.path.exists('linearregression.pickle'):
    clf = pickle.load(open('linearregression.pickle','rb'))
else:
    clf = LinearRegression(n_jobs=-1) # do as many jobs as possible
    clf.fit(x_train, y_train) # fit/"train" to data
    with open('linearregression.pickle','wb') as f: # save classifier
        pickle.dump(clf, f)
    
accuracy = clf.score(x_test, y_test)
forecast = clf.predict(x_predict)

print(forecast,accuracy, predict_lenght)
print(f"accuracy: {accuracy}")
#plotting
style.use('ggplot')

predict_df["Predict"] = forecast

df['Adj. Close'].plot()
predict_df['Predict'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13977792292619204, 'fraction_numerical': 0.009797517962116264, 'mean_word_length': 5.0773809523809526, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '405376', 'license': 'MIT', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1059, 'n_words': 294, 'path': 'ML 1.1 - Linear Regression - example.ipynb', 'repo_name': 'cow51/Machine-Learning-Models', 'size': '3062'}