import matplotlib.
pyplot as plt
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.linear_model import LinearRegression
# Load the dataset
data = pd.read_csv("C:/Users/x/Desktop/Desktop/ML/AAPL.csv")
# Dependent variable Y
close_prices = data['Close(t)']
#Open + Volume = independent variables X
data2 = np.genfromtxt("C:/Users/x/Desktop/Desktop/ML/AAPL.csv", dtype='float', delimiter=',',
skip_header=1, usecols=(1,2,3,6))
X = np.array(data2)
print(X)
# Split the data into training/testing sets
X_train = np.array(X[:450]).reshape((-1, 4)) #we take the first 450 elements
X_test = np.array(X[450:]).reshape((-1, 4)) #we take elements after 450 index
# Split the targets into training/testing sets
Y_train = close_prices[:450] #we take the first 450 elements
Y_test = close_prices[450:] #we take elements after 450 index
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit(X_train, Y_train)
# Make predictions using the testing set
Y_pred = regr.predict(X_test)
# The coefficients
print("Coefficients: \n", regr.coef_)
# The mean squared error
print("Mean squared error: %.2f" % mean_squared_error(Y_test, Y_pred))
# The coefficient of determination: 1 is perfect prediction
print("Coefficient of determination: %.2f" % r2_score(Y_test, Y_pred))
# x axis values
x = data['Close(t)']
# corresponding y axis values
y = data['Year']
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('Price')
# naming the y axis
plt.ylabel('Date')
# giving a title to my graph
plt.title('Closing price')
# function to show the plot
plt.show()
# x axis values
x = Y_pred
# corresponding y axis values
y = data['Year']
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('pred.Price')
# naming the y axis
plt.ylabel('Date')
# giving a title to my graph
plt.title('Closing price pr')
# function to show the plot
plt.show()