From 138583399739caac72e78d7fbe09a7511d437cf3 Mon Sep 17 00:00:00 2001 From: Prathamesh Shakewar Date: Sat, 3 Jan 2026 10:12:44 +0530 Subject: [PATCH] Addgraph.py Addgraph for predicted stock prices this method is easy to understand --- Addgraph.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Addgraph.py diff --git a/Addgraph.py b/Addgraph.py new file mode 100644 index 0000000..10e5313 --- /dev/null +++ b/Addgraph.py @@ -0,0 +1,86 @@ + +# This program predicts stock prices by using machine learning models + +#Install the dependencies +import matplotlib.pyplot as plt +import quandl +import numpy as np +from sklearn.linear_model import LinearRegression +from sklearn.svm import SVR +from sklearn.model_selection import train_test_split + +#Get the stock data +df = quandl.get("WIKI/AMZN") +# Take a look at the data +print(df.head()) + +# Get the Adjusted Close Price +df = df[['Adj. Close']] +#Take a look at the new data +print(df.head()) + +# A variable for predicting 'n' days out into the future +forecast_out = 30 #forecast_out = 'n=30' days +#Create another column (the target or dependent variable) shifted 'n' units up +df['Prediction'] = df[['Adj. Close']].shift(-forecast_out) +#print the new data set +print(df.tail()) + +### Create the indeoendent data set (X) ####### +# Convert the dataframe to a numpy array +X = np.array(df.drop(['Prediction'],1)) + +#Remove the last 'n' rows +X = X[:-forecast_out] +print(X) + +### Create the dependent data set (y) ##### +# Convert the dataframe to a numpy array (All of the values including the NaN's) +y = np.array(df['Prediction']) +# Get all of the y values except the last 'n' rows +y = y[:-forecast_out] +print(y) + +# Split the data into 80% training and 20% testing +x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2) + +# Create and train the Support Vector Machine (Regressor) +svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1) +svr_rbf.fit(x_train, y_train) + +# Testing Model: Score returns the coefficient of determination R^2 of the prediction. +# The best possible score is 1.0 +svm_confidence = svr_rbf.score(x_test, y_test) +print("svm confidence: ", svm_confidence) + +# Create and train the Linear Regression Model +lr = LinearRegression() +# Train the model +lr.fit(x_train, y_train) + +# Testing Model: Score returns the coefficient of determination R^2 of the prediction. +# The best possible score is 1.0 +lr_confidence = lr.score(x_test, y_test) +print("lr confidence: ", lr_confidence) + +# Set x_forecast equal to the last 30 rows of the original data set from Adj. Close column +x_forecast = np.array(df.drop(['Prediction'],1))[-forecast_out:] +print(x_forecast) + +# Print linear regression model predictions for the next 'n' days +lr_prediction = lr.predict(x_forecast) +print(lr_prediction) + +# Print support vector regressor model predictions for the next 'n' days +svm_prediction = svr_rbf.predict(x_forecast) +print(svm_prediction) + + +plt.figure(figsize=(10,5)) +plt.plot(y_test, color='blue', label='Actual Price') +plt.plot(predictions, color='red', linestyle='--', label='Predicted Price') +plt.title('Stock Price Prediction') +plt.xlabel('Time') +plt.ylabel('Price') +plt.legend() +plt.show()