<jupyter_start><jupyter_text><jupyter_code>pip install quandl<jupyter_output><empty_output><jupyter_text>**We will be using Machine Learning Models to predict the price of a stock**<jupyter_code>#import all dependencies 
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 price data
df= quandl.get('WIKI/GOOGL')

# Take a look at the data
print(df.head())<jupyter_output>Open    High     Low  ...   Adj. Low  Adj. Close  Adj. Volume
Date                                ...                                    
2004-08-19  100.01  104.06   95.96  ...  48.128568   50.322842   44659000.0
2004-08-20  101.01  109.08  100.50  ...  50.405597   54.322689   22834300.0
2004-08-23  110.76  113.48  109.05  ...  54.693835   54.869377   18256100.0
2004-08-24  111.24  111.60  103.57  ...  51.945350   52.597363   15247300.0
2004-08-25  104.76  108.00  103.88  ...  52.100830   53.164113    9188600.0

[5 rows x 12 columns]<jupyter_text>**We need only Adjusted close price**<jupyter_code># Get the adjusted close price 
df=df[['Adj. Close']]

#Take a lot at the data 
print(df.head())
# A variable for predicting 'n' days out in the future
forecast_out=30

# Create another column (target or dependent variable) shifted 'n' units up
df['Prediction']=df[['Adj. Close']].shift(-forecast_out)

# print the new dataset after shifting
print(df.tail())<jupyter_output>Adj. Close  Prediction
Date                              
2018-03-21     1094.00         NaN
2018-03-22     1053.15         NaN
2018-03-23     1026.55         NaN
2018-03-26     1054.09         NaN
2018-03-27     1006.94         NaN<jupyter_text>Create independent dataset (X)<jupyter_code>#convert 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 dataset(y) ###
#Convert the dataset into a numpy array ("All the values including the NaN's")
y=np.array(df['Prediction'])

# Get all the 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 of R^2 of the prediction
# Best score possible 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()
lr.fit(x_train,y_train)
# Testing Model: Score returns the coefficient of determination of R^2 of the prediction
# Best score possible is 1.0
lr_confidence=lr.score(x_test,y_test)
print('lr confidence:', lr_confidence)
#Set x_forecast as 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 for the next 'n' days 
lr_prediction=lr.predict(x_forecast)
print(lr_prediction)


#Print SVR  for the next 'n' days
svm_prediction=svr_rbf.predict(x_forecast)
print(svm_prediction) 

#After getting the values of the next 'n' days from the Linear Regression Model and SVM Model we compare which model is better based on the confidence score and use those values<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10173913043478261, 'fraction_numerical': 0.09652173913043478, 'mean_word_length': 3.7338820301783264, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '140803', 'license': 'Apache-2.0', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1198, 'n_words': 358, 'path': "Stock_Prediction_of_next_'n'_days_.ipynb", 'repo_name': 'varun226/Stock-Prediction-of-next-n-days', 'size': '3450'}