In [2]:
import time
import sys
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
def Plotvec1(u,z,v):
#this function is used in code
ax = plt.axes()
ax.arrow(0, 0, *u, head_width=0.05,color ='r', head_length=0.1)
plt.text(*(u+0.1), 'u')
ax.arrow(0, 0, *v, head_width=0.05,color ='b', head_length=0.1)
plt.text(*(v+0.1), 'v')
ax.arrow(0, 0, *z, head_width=0.05, head_length=0.1)
plt.text(*(z+0.1), 'z')
plt.ylim(-2,2)
plt.xlim(-2,2)
def Plotvec2(a,b):
#this function is used in code
ax = plt.axes()
ax.arrow(0, 0, *a, head_width=0.05,color ='r', head_length=0.1)
plt.text(*(a+0.1), 'a')
ax.arrow(0, 0, *b, head_width=0.05,color ='b', head_length=0.1)
plt.text(*(b+0.1), 'b')
plt.ylim(-2,2)
plt.xlim(-2,2)
If you recall, a Python list is a container that allows you to store and access data. We can create a Python List as follows:
In [4]:
a=["0",1,"two","3",4]
We can access the data via an index:
We can access each element using a square bracket as follows:
In [5]:
print("a[0]:",a[0])
print("a[1]:",a[1])
print("a[2]:",a[2])
print("a[3]:",a[3])
print("a[4]:",a[4])
A numpy array is similar to a list, it's usually fixed in size and each element is of the same type. We can cast a list to a numpy array by first importing numpy:
In [6]:
import numpy as np
We then cast the list as follows:
In [7]:
a=np.array([0,1,2,3, 4])
a
Out[7]:
Each element is of the same type, in this case integers:
As with lists, we can access each element via a square bracket:
In [8]:
print("a[0]:",a[0])
print("a[1]:",a[1])
print("a[2]:",a[2])
print("a[3]:",a[3])
print("a[4]:",a[4])
The value of “a” is stored a follows:
In [9]:
a
Out[9]:
If we check the type of the array we get "numpy.ndarray":
In [10]:
type(a)
Out[10]:
As numpy arrays contain data of the same type, we can use the attribute "dtype" to obtain the Data-type of the array’s elements. In this case a 64-bit integer:
In [11]:
a.dtype
Out[11]:
We can create a numpy array with real numbers:
In [12]:
b=np.array([3.1,11.02,6.2, 213.2,5.2])
When we check the type of the array we get "numpy.ndarray":
In [13]:
type(b)
Out[13]:
If we examine the attribute "dtype" we see float 64, as the elements are not integers:
In [14]:
b.dtype
Out[14]:
We can change the value of the array, consider the array "c":
In [15]:
c=np.array([20,1,2,3,4])
c
Out[15]:
We can change the first element of the array to 100 as follows:
In [16]:
c[0]=100
c
Out[16]:
We can change the 5th element of the array as follows:
In [17]:
c[4]=0
c
Out[17]:
Like lists, we can slice the numpy array, and we can select the elements from 1 to 3 and assign it to a new numpy array 'd' as follows:
In [18]:
d=c[1:4]
d
Out[18]:
We can assign the corresponding indexes to new values as follows:
In [19]:
c[3:5]=300,400
c
Out[19]:
Similarly, we can use a list to select a specific index. The list ' select ' contains several values:
In [20]:
select=[0,2,3]
We can use the list as an argument in the brackets. The output is the elements corresponding to the particular index:
In [21]:
d=c[select]
d
Out[21]:
We can assign the specified elements to a new value. For example, we can assign the values to 100 000 as follows:
In [22]:
c[select]=100000
c
Out[22]:
Let's review some basic array attributes using the array ‘a’:
In [23]:
a=np.array([0,1,2,3, 4])
a
Out[23]:
The attribute size is the Number of elements in the array:
In [24]:
a.size
Out[24]:
The next two attributes will make more sense when we get to higher dimensions but let's review them. The attribute “ndim” represents the Number of array dimensions or the rank of the array, in this case, one:
In [25]:
a.ndim
Out[25]:
The attribute “shape” is a tuple of integers indicating the size of the array in each dimension:
In [26]:
a.shape
Out[26]:
In [27]:
a=np.array([1,-1,1,-1])
In [28]:
mean=a.mean()
mean
Out[28]:
In [29]:
standard_deviation=a.std()
standard_deviation
Out[29]:
In [30]:
b=np.array([1,2,3,4,5])
b
Out[30]:
In [31]:
max_b=b.max()
In [32]:
max_b=b.min()
Consider the numpy array 'u':
In [33]:
u=np.array([1,0])
u
Out[33]:
Consider the numpy array 'v':
In [34]:
v=np.array([0,1])
v
Out[34]:
We can add the two arrays and assign it to z:
In [35]:
z=u+v
z
Out[35]:
The operation is equivalent to vector addition:
In [36]:
Plotvec1(u,z,v)
In [37]:
u - v
Out[37]:
Consider the vector numpy array 'y':
In [38]:
y=np.array([1,2])
y
Out[38]:
We can multiply every element in the array by 2:
In [39]:
z=2*y
z
Out[39]:
This is equivalent to multiplying a vector by a scaler:
In [40]:
-2 * z
Out[40]:
Consider the following array 'u':
In [41]:
u=np.array([1,2])
u
Out[41]:
Consider the following array 'v':
In [42]:
v=np.array([3,2])
v
Out[42]:
The product of the two numpy arrays 'u' and 'v' is given by:
In [43]:
z=u*v
z
Out[43]:
In [44]:
np.array([1,2,3,4,5]) * np.array([1,0,1,0,1])
Out[44]:
The dot product of the two numpy arrays 'u' and 'v' is given by:
In [45]:
np.dot(u,v)
Out[45]:
In [47]:
a = np.array([-1, 1])
b = np.array([1, 1])
Plotvec2(a, b)
np.dot(a, b)
Out[47]:
In [48]:
a = np.array([1, 0])
b = np.array([0, 1])
Plotvec2(a, b)
np.dot(a, b)
Out[48]:
In [49]:
a = np.array([1, 1])
b = np.array([0, 1])
Plotvec2(a, b)
np.dot(a, b)
Out[49]:
perpendicular vectors vs. non-perpendicular vectors
Consider the following array:
In [51]:
u=np.array([1,2,3,-1])
u
Out[51]:
Adding the constant 1 to the array adds 1 to each element in the array:
In [52]:
u+1
Out[52]:
The process is summarised in the following animation:
This part of Broadcasting check out the link for more detail.
We can access the value of pie in numpy as follows :
In [53]:
np.pi
Out[53]:
We can create the following numpy array in Radians:
In [54]:
x=np.array([0,np.pi/2 , np.pi] )
We can apply the function "sine" to the array 'x' and assign the values to the array 'y'; this applies the sine function to each element in the array:
In [55]:
y=np.sin(x)
y
Out[55]:
A useful function for plotting mathematical functions is "linespace". Linespace returns evenly spaced numbers over a specified interval. We specify the starting point of the sequence and the ending point of the sequence. The parameter "num" indicates the Number of samples to generate, in this case 5:
In [56]:
np.linspace(-2,2,num=5)
Out[56]:
If we change the parameter num to 9, we get 9 evenly spaced numbers over the interval from -2 to 2:
In [57]:
np.linspace(-2,2,num=9)
Out[57]:
We can use the function line space to generate 100 evenly spaced samples from the interval 0 to 2 pi:
In [59]:
x=np.linspace(0,2*np.pi,num=100)
x
Out[59]:
We can apply the sine function to each element in the array 'x' and assign it to the array 'y':
In [60]:
y=np.sin(x)
In [61]:
plt.plot(x,y)
Out[61]:
If you're looking for an enterprise-ready environment to use Python on a big scale with two free Apache Spark executors, consider signing up for a free account on Data Science Experience
Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.
Copyright © 2017 cognitiveclass.ai. This notebook and its source code are released under the terms of the MIT License.
In [ ]: