Day 3-Numpy Basics - Jupyter Notebook
Day 3-Numpy Basics - Jupyter Notebook
ndarray.ndim
ndarray.shape
- the dimensions of the array. This is a tuple of integers indicating the size of the array in each
dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple
is therefore the number of axes, ndim.
ndarray.size
- the total number of elements of the array. This is equal to the product of the elements of shape.
ndarray.dtype
- an object describing the type of the elements in the array. One can create or specify dtype’s using
standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and
numpy.float64 are some examples.
ndarray.itemsize
-the size in bytes of each element of the array. For example, an array of elements of type float64 has
itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to
ndarray.dtype.itemsize.
ndarray.data
-the buffer containing the actual elements of the array. Normally, we won’t need to use this attribute
because we will access the elements in an array using indexing facilities.
In [23]:
import numpy as np
a = np.arange(1,16).reshape(15, 1)
In [24]:
Out[24]:
array([[ 1],
[ 2], [ 3],
[ 4], [ 5],
[ 6], [ 7],
[ 8], [ 9],
[10], [11],
[12], [13],
[14],
[15]])
In [5]:
a.ndim
Out[5]:
In [6]:
a.shape
Out[6]:
(3, 5)
In [7]:
a.size
Out[7]:
15
In [8]:
a.dtype
Out[8]:
dtype('int32')
In [9]:
a.itemsize
Out[9]:
In [ ]:
###
In [10]:
type(a)
Out[10]:
numpy.ndarray
We can create an array from a regular Python list or tuple using the array function. The type of the
resulting array is deduced from the type of the elements in the sequences.
In [8]:
a=np.array([1,2,3,4])
In [6]:
type(a)
Out[6]:
numpy.ndarray
In [13]:
a.dtype
Out[13]:
dtype('int32')
In [14]:
b=np.array([1.2,2.3,3.4,5.6])
b.dtype
Out[14]:
dtype('float64')
In [9]:
a=np.array([(1,2,3),(4,5,6)])
a
Out[9]:
array([[1, 2, 3],
[4, 5, 6]])
In [11]:
a=np.array([(1,2,3),(4,5,6)],dtype=complex)
a
Out[11]:
The function zeros creates an array full of zeros, the function ones creates an array full of ones, and the
function empty creates an array whose initial content is random and depends on the state of the
memory. By default, the dtype of the created array is float64, but it can be specified via the key word
argument dtype.
In [14]:
a=np.zeros((3,5,3),dtype=int)
a
Out[14]:
array([[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]])
In [25]:
b=np.ones((2,4))
b
Out[25]:
c=np.empty((3,3))
c
Out[26]:
To create sequences of numbers, NumPy provides the arange function which is analogous to the
Python built-in range, but returns an array.
In [16]:
a=np.arange(10,20,5)
a
np.arange(0, 2, 0.3)
Out[16]:
array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
The function linspace is similar to arange but it receives as an argument the number of elements that
we want, instead of the step
In [30]:
a=np.linspace(0,3,8)
a
Out[30]:
-An array has a shape given by the number of elements along each axis
a=np.arange(10,20)
a
b=a.reshape(2,5)
b
b.T
Out[27]:
array([[10, 15],
[11, 16],
[12, 17],
[13, 18],
[14, 19]])
Printing Arrays
When you print an array, NumPy displays it in a similar way to nested lists,
One-dimensional arrays are then printed as rows, bidimensionals as matrices #### and tridimensionals
as lists of matrices.
In [4]:
Out[4]:
array([0, 1, 2, 3, 4, 5])
In [6]:
b = np.arange(12).reshape(4, 3) # two dimensional
b
Out[6]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
In [7]:
Out[7]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
Arithmetic operators on arrays apply elementwise. A new array is created and filled with the
result.
In [15]:
Out[15]:
array([0, 1, 2, 3])
In [16]:
c=a+b
c
Out[16]:
In [17]:
c=a*b
c
Out[17]:
In [18]:
b**2
Out[18]:
In [20]:
b<3
Out[20]:
In [28]:
Out[28]:
array([[5, 4],
[3, 4]])
sum(),min() and max() functions of an array
In [34]:
a=np.arange(10,20)
a.sum()
x=a.reshape(2,5)
x
#x.sum()
x.sum(axis=1) # axis=0 for each column and axis =1 for each row
Out[34]:
array([60, 85])
Universal Functions
NumPy provides familiar mathematical functions such as sin, cos, and exp. In NumPy, these are called
“universal functions” (ufunc). Within NumPy, these functions operate elementwise on an array,
producing an array as output.
In [31]:
a=np.arange(10,20)
b=np.exp(a) #The exponential function is e**x where e is a mathematical constant called Eul
b
Out[31]:
In [35]:
a=np.arange(10,20)
b=a.reshape(2,5)
for row in b:
print(row)
[10 11 12 13 14]
[15 16 17 18 19]
However, if one wants to perform an operation on each element in the array, one can use the flat attribute which
is an iterator over all the elements of the array:
In [34]:
for i in b.flat:
print(i)
10
11
12
13
14
15
16
17
18
19
In [ ]: