0% found this document useful (0 votes)
18 views

Day 3-Numpy Basics - Jupyter Notebook

Uploaded by

Jesica D'cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Day 3-Numpy Basics - Jupyter Notebook

Uploaded by

Jesica D'cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

NumPy’s main object is the homogeneous multidimensional array.

It is a table of elements (usually


numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are
called axes.

NumPy’s array class is called ndarray.

The more important attributes of an ndarray object are:

ndarray.ndim

the number of axes (dimensions) of the array.

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]:

array([[1.+0.j, 2.+0.j, 3.+0.j],


[4.+0.j, 5.+0.j, 6.+0.j]])

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]:

array([[1., 1., 1., 1.],


[1., 1., 1., 1.]])
In [26]:

c=np.empty((3,3))
c

Out[26]:

array([[0.00000000e+000, 0.00000000e+000, 0.00000000e+000],


[0.00000000e+000, 0.00000000e+000, 3.65608578e-321],
[1.78019625e-306, 1.69121231e-306, 2.56765117e-312]])

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]:

array([0. , 0.42857143, 0.85714286, 1.28571429, 1.71428571,


2.14285714, 2.57142857, 3. ])

Changing the shape of an array

-An array has a shape given by the number of elements along each axis

Shape can be changed by reshape() function and T (for transpose) attribute


In [27]:

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]:

a = np.arange(6) # one dimensional


a

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]:

c = np.arange(24).reshape(2, 3, 4) # 3d array list of Matrices


c

Out[7]:

array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],

[[12, 13, 14, 15],


[16, 17, 18, 19],
[20, 21, 22, 23]]])
Basic Operations

Arithmetic operators on arrays apply elementwise. A new array is created and filled with the
result.

In [15]:

a = np.array([20, 30, 40, 50])


b = np.arange(4)
b

Out[15]:

array([0, 1, 2, 3])

In [16]:

c=a+b
c

Out[16]:

array([20, 31, 42, 53])

In [17]:

c=a*b
c

Out[17]:

array([ 0, 30, 80, 150])

In [18]:
b**2

Out[18]:

array([0, 1, 4, 9], dtype=int32)

In [20]:

b<3

Out[20]:

array([ True, True, True, False])

In [28]:

A = np.array([[1, 1],[0, 1]])


B=np.array([[2,0],[3,4]])
C=A@B # dot product of Matrix
C

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]:

array([2.20264658e+04, 5.98741417e+04, 1.62754791e+05, 4.42413392e+05,


1.20260428e+06, 3.26901737e+06, 8.88611052e+06, 2.41549528e+07,
6.56599691e+07, 1.78482301e+08])

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 [ ]:

You might also like