Here we see the Amazon stock price last five year and we also calculate the last five year mean value and variance
Step:1
First, we import necessary librates
import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline import seaborn as sns from sklearn.metrics import mean_squared_error from statsmodels.graphics.tsaplots import plot_acf from statsmodels.tsa.ar_model import AR from statsmodels.tsa.arima_model import ARIMA from statsmodels.tsa.arima_model import ARMA
Step:2
Then we will upload the dataset of Amazon_Dataset
Amazon_Data = pd.read_csv('Amazon_Dataset.csv') Amazon_Data
Step:3
Now we will do basic data exploration
head(): This helps to see a few sample rows of the data
Amazon_Data.head()
Amazon_Data.describe()
Step:4
Now we look at Data and Close column together
t=Amazon_Data['Date'] t
t.head()
c=Amazon_Data['Close'] c.head()
new=pd.concat([t,c],axis=1) new.head()
Y = Amazon.values size = int(len(Y) * 0.70) # 70 % # Training set train = Y[:size] # testing set test = Y[size:len(Y)] print("Total Samples : %d" % len(Y)) print("Training Samples : %d" % len(train)) print("Testing Samples : %d" % len(test))
Step:5
Plotting Scatter v/s Density
def plotScatterMatrix(Amazon_Data, plotSize, textSize): Amazon_Data = Amazon_Data.select_dtypes(include =[np.number]) Amazon_Data = Amazon_Data.dropna('columns') Amazon_Data = Amazon_Data[[col for col in Amazon_Data if Amazon_Data[col].nunique() > 1]] columnNames = list(Amazon_Data) if len(columnNames) > 10: columnNames = columnNames[:10] Amazon_Data = Amazon_Data[columnNames] ax = pd.plotting.scatter_matrix(Amazon_Data, alpha=0.2, figsize=[plotSize, plotSize], diagonal='density') corrs = Amazon_Data.corr().values for p, q in zip(*plt.np.triu_indices_from(ax, k = 1)): ax[p, q].annotate('Corr. coef = %.2f' % corrs[p, q], (0.8, 0.2), xycoords='axes fraction', ha='center', va='bottom', size=textSize) plt.plot(Amazon.Date, marker= '+', markersize =2, markerfacecolor= 'green', markeredgecolor= 'black') plt.show()
plotScatterMatrix(Amazon_Data, 18, 10)
Step:6
Now we keep only the close column and see the last five-year closing stock price
# Keep only 'Close' column Amazon = Amazon_Data.drop(['Open', 'High', 'Low', 'Adj Close', 'Volume'], axis=1)
Amazon = Amazon[Amazon['Date'] >='2015-01-01']
plt.figure(figsize=(10, 5)) plt.title('Amazon stock closing prices for last 5 years', fontsize=14) plt.plot(Amazon.Close, marker= '+', markersize =2, markerfacecolor= 'green', markeredgecolor= 'red')
So we calculate the last five year mean and variance price
Q1_2019_mean = Amazon[(Amazon['Date'] >= '2019-01-01') & (Amazon['Date'] < '2019-03-31')].mean() Q1_2019_var = Amazon[(Amazon['Date'] >= '2019-01-01') & (Amazon['Date'] < '2019-03-31')].var() Q2_2017_mean = Amazon[(Amazon['Date'] >= '2017-01-01') & (Amazon['Date'] < '2017-03-31')].mean() Q2_2017_var = Amazon[(Amazon['Date'] >= '2017-01-01') & (Amazon['Date'] < '2017-03-31')].var() Q3_2015_mean = Amazon[(Amazon['Date'] >= '2015-10-01') & (Amazon['Date'] < '2015-12-31')].mean() Q3_2015_var = Amazon[(Amazon['Date'] >= '2015-10-01') & (Amazon['Date']< '2015-12-31')].var() print('2019 Quarter 1 closing price mean : %.2f ' % (Q1_2019_mean)) print('2019 Quarter 1 closing price variance : %.2f ' % (Q1_2019_var)) print("---------------------------------------------- ") print('2017 Quarter 2 closing price mean : %.2f ' % (Q2_2017_mean)) print('2017 Quarter 2 closing price variance : %.2f ' % (Q2_2017_var)) print("---------------------------------------------- ") print('2015 Quarter 3 closing price mean : %.2f ' % (Q3_2015_mean)) print('2015 Quarter 3 closing price variance : %.2f ' % (Q3_2015_var))
Here we calculate the closing mean price and variance and see the stock price increases in the last five years
Submitted by Subhojit Jalal (Subhojit1234)
Download packets of source code on Coders Packet
Comments