python-pour-finance/02-NumPy/.ipynb_checkpoints/1-tableaux-NumPy-checkpoint...

974 lines
20 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"___\n",
"\n",
"<a href='http://www.pieriandata.com'> <img src='../Pierian_Data_Logo.png' /></a>\n",
"___\n",
"<center>*Copyright Pierian Data 2017*</center>\n",
"<center>*For more information, visit us at www.pieriandata.com*</center>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NumPy \n",
"\n",
"NumPy (or Numpy) is a Linear Algebra Library for Python, the reason it is so important for Finance with Python is that almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks. Plus we will use it to generate data for our analysis examples later on!\n",
"\n",
"Numpy is also incredibly fast, as it has bindings to C libraries. For more info on why you would want to use Arrays instead of lists, check out this great [StackOverflow post](http://stackoverflow.com/questions/993984/why-numpy-instead-of-python-lists).\n",
"\n",
"We will only learn the basics of NumPy, to get started we need to install it!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installation Instructions\n",
"\n",
"### NumPy is already included in your environment! You are good to go if you are using pyfinance env!\n",
"\n",
"_____\n",
"##### For those not using the provided environment:\n",
"\n",
"**It is highly recommended you install Python using the Anaconda distribution to make sure all underlying dependencies (such as Linear Algebra libraries) all sync up with the use of a conda install. If you have Anaconda, install NumPy by going to your terminal or command prompt and typing:**\n",
" \n",
" conda install numpy\n",
" \n",
"**If you do not have Anaconda and can not install it, please refer to [Numpy's official documentation on various installation instructions.](http://docs.scipy.org/doc/numpy-1.10.1/user/install.html)**\n",
"\n",
"_____"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using NumPy\n",
"\n",
"Once you've installed NumPy you can import it as a library:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy has many built-in functions and capabilities. We won't cover them all but instead we will focus on some of the most important aspects of Numpy: vectors,arrays,matrices, and number generation. Let's start by discussing arrays.\n",
"\n",
"# Numpy Arrays\n",
"\n",
"NumPy arrays are the main way we will use Numpy throughout the course. Numpy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-d arrays and matrices are 2-d (but you should note a matrix can still have only one row or one column).\n",
"\n",
"Let's begin our introduction by exploring how to create NumPy arrays.\n",
"\n",
"## Creating NumPy Arrays\n",
"\n",
"### From a Python List\n",
"\n",
"We can create an array by directly converting a list or list of lists:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list = [1,2,3]\n",
"my_list"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array(my_list)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_matrix = [[1,2,3],[4,5,6],[7,8,9]]\n",
"my_matrix"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9]])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array(my_matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Built-in Methods\n",
"\n",
"There are lots of built-in ways to generate Arrays"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### arange\n",
"\n",
"Return evenly spaced values within a given interval."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(0,10)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 2, 4, 6, 8, 10])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(0,11,2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### zeros and ones\n",
"\n",
"Generate arrays of zeros or ones"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 0., 0.])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.zeros(3)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0.]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.zeros((5,5))"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 1., 1.])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones(3)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 1., 1.],\n",
" [ 1., 1., 1.],\n",
" [ 1., 1., 1.]])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones((3,3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### linspace\n",
"Return evenly spaced numbers over a specified interval."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 5., 10.])"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linspace(0,10,3)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0. , 0.20408163, 0.40816327, 0.6122449 ,\n",
" 0.81632653, 1.02040816, 1.2244898 , 1.42857143,\n",
" 1.63265306, 1.83673469, 2.04081633, 2.24489796,\n",
" 2.44897959, 2.65306122, 2.85714286, 3.06122449,\n",
" 3.26530612, 3.46938776, 3.67346939, 3.87755102,\n",
" 4.08163265, 4.28571429, 4.48979592, 4.69387755,\n",
" 4.89795918, 5.10204082, 5.30612245, 5.51020408,\n",
" 5.71428571, 5.91836735, 6.12244898, 6.32653061,\n",
" 6.53061224, 6.73469388, 6.93877551, 7.14285714,\n",
" 7.34693878, 7.55102041, 7.75510204, 7.95918367,\n",
" 8.16326531, 8.36734694, 8.57142857, 8.7755102 ,\n",
" 8.97959184, 9.18367347, 9.3877551 , 9.59183673,\n",
" 9.79591837, 10. ])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linspace(0,10,50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## eye\n",
"\n",
"Creates an identity matrix"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 0., 0., 0.],\n",
" [ 0., 1., 0., 0.],\n",
" [ 0., 0., 1., 0.],\n",
" [ 0., 0., 0., 1.]])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.eye(4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random \n",
"\n",
"Numpy also has lots of ways to create random number arrays:\n",
"\n",
"### rand\n",
"Create an array of the given shape and populate it with\n",
"random samples from a uniform distribution\n",
"over ``[0, 1)``."
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.11570539, 0.35279769])"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.rand(2)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.66660768, 0.87589888, 0.12421056, 0.65074126, 0.60260888],\n",
" [ 0.70027668, 0.85572434, 0.8464595 , 0.2735416 , 0.10955384],\n",
" [ 0.0670566 , 0.83267738, 0.9082729 , 0.58249129, 0.12305748],\n",
" [ 0.27948423, 0.66422017, 0.95639833, 0.34238788, 0.9578872 ],\n",
" [ 0.72155386, 0.3035422 , 0.85249683, 0.30414307, 0.79718816]])"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.rand(5,5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### randn\n",
"\n",
"Return a sample (or samples) from the \"standard normal\" distribution. Unlike rand which is uniform:"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.27954018, 0.90078368])"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randn(2)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.70154515, 0.22441999, 1.33563186, 0.82872577, -0.28247509],\n",
" [ 0.64489788, 0.61815094, -0.81693168, -0.30102424, -0.29030574],\n",
" [ 0.8695976 , 0.413755 , 2.20047208, 0.17955692, -0.82159344],\n",
" [ 0.59264235, 1.29869894, -1.18870241, 0.11590888, -0.09181687],\n",
" [-0.96924265, -1.62888685, -2.05787102, -0.29705576, 0.68915542]])"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randn(5,5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### randint\n",
"Return random integers from `low` (inclusive) to `high` (exclusive)."
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"44"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randint(1,100)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([13, 64, 27, 63, 46, 68, 92, 10, 58, 24])"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.randint(1,100,10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Array Attributes and Methods\n",
"\n",
"Let's discuss some useful attributes and methods or an array:"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"arr = np.arange(25)\n",
"ranarr = np.random.randint(0,50,10)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19, 20, 21, 22, 23, 24])"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10, 12, 41, 17, 49, 2, 46, 3, 19, 39])"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reshape\n",
"Returns an array containing the same data with a new shape."
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14],\n",
" [15, 16, 17, 18, 19],\n",
" [20, 21, 22, 23, 24]])"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.reshape(5,5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### max,min,argmax,argmin\n",
"\n",
"These are useful methods for finding max or min values. Or to find their index locations using argmin or argmax"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10, 12, 41, 17, 49, 2, 46, 3, 19, 39])"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"49"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr.max()"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr.argmax()"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr.min()"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranarr.argmin()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Shape\n",
"\n",
"Shape is an attribute that arrays have (not a method):"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(25,)"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Vector\n",
"arr.shape"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19, 20, 21, 22, 23, 24]])"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Notice the two sets of brackets\n",
"arr.reshape(1,25)"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 25)"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.reshape(1,25).shape"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0],\n",
" [ 1],\n",
" [ 2],\n",
" [ 3],\n",
" [ 4],\n",
" [ 5],\n",
" [ 6],\n",
" [ 7],\n",
" [ 8],\n",
" [ 9],\n",
" [10],\n",
" [11],\n",
" [12],\n",
" [13],\n",
" [14],\n",
" [15],\n",
" [16],\n",
" [17],\n",
" [18],\n",
" [19],\n",
" [20],\n",
" [21],\n",
" [22],\n",
" [23],\n",
" [24]])"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.reshape(25,1)"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(25, 1)"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.reshape(25,1).shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### dtype\n",
"\n",
"You can also grab the data type of the object in the array:"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int64')"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.dtype"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Great Job!"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}