<jupyter_start><jupyter_text>Pandas and numpy Load the data from: http://opendata.dc.gov/datasets that I have include in this github   into a dataframe.  ( The file has been is available in directory ./data/ccp_current_csv.csv )<jupyter_code>import pandas as pd
import numpy as np

ccpdata = pd.read_csv("./data/ccp_current_csv.csv")
ccpdata.head(10) #first 10 records, to give a preview of the data<jupyter_output><empty_output><jupyter_text>What is its shape and what does that mean?<jupyter_code>print(ccpdata.shape)
#we could aternatively use the numpy.shape() function
print(np.shape(ccpdata))

print
print("No.of Rows: ", len(ccpdata)) #prints the number of rows)
print("No. of Columns: ", len(ccpdata.columns)) #prints the number of columns)<jupyter_output>(465, 27)
(465, 27)

('No.of Rows: ', 465)
('No. of Columns: ', 27)<jupyter_text>Shape of a dataset means the dimensionality of the dataset. The dimensions of a dataset refers to the number of rows and the number of columns, which gives us an idea of the size of the dataset and how the data are arranged.The shape of ccpdata given by the statment ccpdata.shape gives a tuple (No. of rows, No. of Columns).From the above ouput, we can understand that the dataset has 465 rows and 27 columns. Or, there are 465 records in the data and each record has 27 columns.We can also describe a shape of a dataset using statistical values such as standard deviation, mean, median, and mode.  What are the number of rows in each 'QUADRANT' ?<jupyter_code>#We can use the value_counts() method to calculate the frequency of values in a column
print ("QUAD. No. of Rows")
print (ccpdata['QUADRANT'].value_counts())


print
#We could also get the counts and store into a dictionary as follows:
rowcount = ccpdata['QUADRANT'].value_counts().to_dict()
print (rowcount)<jupyter_output>QUAD. No. of Rows
NW    195
NE    163
SE     84
BN     12
SW     11
Name: QUADRANT, dtype: int64

{'BN': 12L, 'SW': 11L, 'NE': 163L, 'SE': 84L, 'NW': 195L}<jupyter_text>Array math demonstration For two arrarys  a= [1,2,3,4]  type=floatb= [5,6,7,8]  type=floatPeform the following array operations using numpy   ( show both operational use of numpy and functional (example addition operation => + vs addition function => numbpy.add() ) addition a+b<jupyter_code>import numpy as np
a = np.array([1.,2.,3.,4.])
b = np.array([5.0,6.0,7.0,8.0])
print(type(a))
print(type(b))


#adding a and b using numpy.add() function
print(np.add(a,b))

#adding a and b using the operator +
print(a+b)

#We can also use the astype() function to change the datatype of an array. 
#For instance, we can declare a as an integer type array and convert it to a float type array
a = np.array([1,2,3,4])
print("Datatype of 1st element of a is ", type(a[0]))
print(a)
a = a.astype(float)
print("Datatype of 1st element of a is ", type(a[0]))
print(a)<jupyter_output><type 'numpy.ndarray'>
<type 'numpy.ndarray'>
[ 6.  8. 10. 12.]
[ 6.  8. 10. 12.]
('Datatype of 1st element of a is ', <type 'numpy.int32'>)
[1 2 3 4]
('Datatype of 1st element of a is ', <type 'numpy.float64'>)
[1. 2. 3. 4.]<jupyter_text>subtraction a-b<jupyter_code>import numpy as np
a = np.array([1.,2.,3.,4.])
b = np.array([5.0,6.0,7.0,8.0])

#adding a and b using numpy.add() function
print(np.subtract(a,b))
print(np.subtract(b,a))

#adding a and b using the operator +
print(a - b)
print(b - a)<jupyter_output>[-4. -4. -4. -4.]
[4. 4. 4. 4.]
[-4. -4. -4. -4.]
[4. 4. 4. 4.]<jupyter_text>multiplication a*b<jupyter_code>import numpy as np
a = np.array([1.,2.,3.,4.])
b = np.array([5.0,6.0,7.0,8.0])

#multiplying elements of a by elements of b using numpy.multiply() function
print(np.multiply(a,b))

#diving elements of a by elements of b using the operator *
print(a * b)<jupyter_output>[ 5. 12. 21. 32.]
[ 5. 12. 21. 32.]<jupyter_text>divsion a/b<jupyter_code>import numpy as np
a = np.array([1.,2.,3.,4.])
b = np.array([5.0,6.0,7.0,8.0])

#diving elements of a by elements of b using numpy.divide() function
print(np.divide(a,b))

#diving elements of a by elements of b using the operator /
print(a / b)<jupyter_output>[0.2        0.33333333 0.42857143 0.5       ]
[0.2        0.33333333 0.42857143 0.5       ]<jupyter_text>modulo  a%b<jupyter_code>import numpy as np
a = np.array([1.,2.,3.,4.])
b = np.array([5.0,6.0,7.0,8.0])

#adding a and b using numpy.add() function
print(np.mod(a,b))

#adding a and b using the operator +
print(a % b)<jupyter_output>[1. 2. 3. 4.]
[1. 2. 3. 4.]<jupyter_text>power  a^b<jupyter_code>import numpy as np
a = np.array([1.,2.,3.,4.])
b = np.array([5.0,6.0,7.0,8.0])

#adding a and b using numpy.add() function
print(np.power(a,b))

#adding a and b using the operator +
print(a ** b)
print(a ^ b) #the caret symbol is not used for power in pythong. It represents xor operation<jupyter_output>[1.0000e+00 6.4000e+01 2.1870e+03 6.5536e+04]
[1.0000e+00 6.4000e+01 2.1870e+03 6.5536e+04]<jupyter_text>Provide an interesting analysis of the data columns ( frequency or averages )<jupyter_code>np.shape(mkl_data)<jupyter_output><empty_output><jupyter_text>The imported dataset has 3201 records, each of which has 7 columns. This implies that the stock price of Markel Corp, USA  for 3201 days are present in the data.Let's get the list of the columns.<jupyter_code>columns = mkl_data.columns.values #returns a numpy.ndarray object with the column names of the dataset
print (columns)
print(type(columns))
print
print (mkl_data)<jupyter_output>['Date' 'Open' 'High' 'Low' 'Close' 'Volume' 'OpenInt']
<type 'numpy.ndarray'>

            Date     Open     High      Low    Close  Volume  OpenInt
0      2/25/2005   365.70   365.70   359.50   360.10   14800        0
1      2/28/2005   361.00   361.00   356.25   357.50   11200        0
2       3/1/2005   358.00   362.00   358.00   359.26   20600        0
3       3/2/2005   360.25   361.50   358.00   358.00   22600        0
4       3/3/2005   357.00   364.00   356.50   357.51   17300        0
5       3/4/2005   358.50   359.50   358.00   358.35    8700        0
6       3/7/2005   359.35   360.50   358.17   359.00   15000        0
7       3/8/2005   360.00   360.99   358.50   360.00   25500        0
8       3/9/2005   360.00   360.00   357.05   357.05    8800        0
9      3/10/2005   357.20   359.00   357.00   358.99    5400        0
10     3/11/2005   359.50   362.55   357.50   357.75   43300        0
11     3/14/2005   358.75   368.00   358.60   367.91   40900        0
12     3/1[...]<jupyter_text>The dataframe lists the opening price, closing price, volume of units sold and highest and lowest prices per day.<jupyter_code>print "The mean of opening prices of Markel Corp - ", np.mean(mkl_data['Open'])
print "The mean of closing prices of Markel Corp - ", np.mean(mkl_data['Close']) 
print "The mean volume of units sold per day - ", (np.mean(mkl_data['Volume']))
print
mkl_data.mean() #we can simly get the mean of all columns with numerical values<jupyter_output>The mean of opening prices of Markel Corp -  529.084975008
The mean of closing prices of Markel Corp -  529.306919713
The mean volume of units sold per day -  32944.4667291<jupyter_text>The mean price of the stock is probably not a good parameter to judge a stock. However the MEAN volume of units sold in a single day, which here is almost 33000 is a good number to look at of how many people are interested in the stock.<jupyter_code>print "The highest price which the stock of Markel Corp was bought/sold at is: ", mkl_data['High'].max()
print "The lowest price which the stock of Markel Corp was bought/sold at is: ", mkl_data['Low'].min()
print
print "The highest number of units sold in a single day :", mkl_data['Volume'].max()
print "The lowest number of units sold in a single day :", mkl_data['Volume'].min()
print mkl_data['Close'].std() #Standard Deviation of Closing Price
print mkl_data['Close'].var() #Variance of Closing Price<jupyter_output>221.99735196
49282.8242771<jupyter_text>High STANDARD DEVIATION refers to volatile stock prices. This is great for aggressive investors while conservative investors would opt for a company with less volatile stocks. Variance describes the same thing basically.<jupyter_code>import matplotlib.pyplot as plt
mkl_data.iloc[2800:3201].plot(x='Date',y='Close')
plt.show()<jupyter_output><empty_output><jupyter_text>Plotting the last 1200 closing prices of the Markel Corp show a generally increasing trend. The price has also dropped considerably at the times. Seems like a volatile stock! If you are an aggressive investor, you should go for it!<jupyter_code>mkl_data.corr()<jupyter_output><empty_output><jupyter_text>This shows the correlation between the columns of the dataframe.From this we can see a strong correlation between the the prices. For example, we can see how the opening prices affects the high price, low price or the closing price. We can also see that there is a very low correlation between the price of the stock and the daily volume of units.<jupyter_code>mkl_data.describe()
#We can simply summarize the various statistical values using the describe() method<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11320961897441528, 'fraction_numerical': 0.08740529263204129, 'mean_word_length': 3.7167270844122218, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '477769', 'license': 'MIT', 'n_tokens_mistral': 3642, 'n_tokens_neox': 3175, 'n_words': 1158, 'path': 'Homeworks/Homework3/Maharjan_Homework3_Pandas_Numpy_Sept2018.ipynb', 'repo_name': 'jenishmaharjan/big-data-python-class', 'size': '9107'}