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

Arrays

Uploaded by

dhirajkapila
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arrays

Uploaded by

dhirajkapila
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Numpy

• Numpy is the core library for scientific computing in Python. It


provides a high-performance multidimensional array object, and
tools for working with these arrays
• 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 positive integers. In NumPy dimensions are
called axes.
• EG:[[ 1., 0., 0.],
• [ 0., 1., 2.]]
• In the example pictured below, the array has 2 axes. The first axis
has a length of 2, the second axis has a length of 3.
How to use numpy for creating arrays

• NumPy’s array class is called ndarray. It is also known by the alias


array. 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.
How to use numpy for creating arrays

• 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.
How to use numpy for creating arrays

• 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.
An example¶
• >>> import numpy as np
• >>> a = np.arange(15).reshape(3, 5)
• >>> a
• array([[ 0, 1, 2, 3, 4],
• [ 5, 6, 7, 8, 9],
• [10, 11, 12, 13, 14]])
• >>> a.shape
• (3, 5)
• >>> a.ndim
• 2
• >>> a.dtype.name
• 'int64'
• >>> a.itemsize
• 8
An example continued

• >>> a.size
• 15
• >>> type(a)
• <type 'numpy.ndarray'>
• >>> b = np.array([6, 7, 8])
• >>> b
• array([6, 7, 8])
• >>> type(b)
• <type 'numpy.ndarray'>
Array Creation

• >>> import numpy as np


• >>> a = np.array([2,3,4])
• >>> a
• array([2, 3, 4])
• >>> a.dtype
• dtype('int64')
• >>> b = np.array([1.2, 3.5, 5.1])
• >>> b.dtype
• dtype('float64')
Array Creation

• >>> a = np.array(1,2,3,4) # WRONG


• >>> a = np.array([1,2,3,4]) # RIGHT
• >>> b = np.array([(1.5,2,3), (4,5,6)])
• >>> b
• Output-array([[ 1.5, 2. , 3. ],
• [ 4. , 5. , 6. ]])
• >>> c = np.array( [ [1,2], [3,4] ], dtype=complex )
• >>> c
• Output-array([[ 1.+0.j, 2.+0.j],
• [ 3.+0.j, 4.+0.j]])
function zeros
• 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.
• >>> np.zeros( (3,4) )

• Output-array([[ 0., 0., 0., 0.],


• [ 0., 0., 0., 0.],
• [ 0., 0., 0., 0.]])
Array Creation

• >>> np.ones( (2,3,4), dtype=np.int16 ) # dtype can also be


specified
• Output-array([[[ 1, 1, 1, 1],
• [ 1, 1, 1, 1],
• [ 1, 1, 1, 1]],
• [[ 1, 1, 1, 1],
• [ 1, 1, 1, 1],
• [ 1, 1, 1, 1]]], dtype=int16)
Array Creation

• >>> np.empty( (2,3) ) # uninitialized, output may vary


• array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260],
• [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
• >>> np.arange( 10, 30, 5 )
• array([10, 15, 20, 25])
• >>> np.arange( 0, 2, 0.3 ) # it accepts float arguments
• array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
Printing Arrays

• >>> np.linspace( 0, 2, 9 ) # 9 numbers from 0 to 2


• array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])
• >>> a = np.arange(6) # 1d array
• >>> print(a)
• [0 1 2 3 4 5]
• >>> b = np.arange(12).reshape(4,3) # 2d array
• >>> print(b)
• [[ 0 1 2]
• [ 3 4 5]
• [ 6 7 8]
• [ 9 10 11]]
Printing Arrays

• >>> c = np.arange(24).reshape(2,3,4) # 3d array


• >>> print(c)
• [[[ 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.
• >>> a = np.array( [20,30,40,50] )
• >>> b = np.arange( 4 )
• >>> b
• array([0, 1, 2, 3])
• >>> c = a-b
• >>> c
• Output-array([20, 29, 38, 47])
Basic Operations

• >>> b**2
• Output-array([0, 1, 4, 9])
• >>> 10*np.sin(a)
• Output-array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854])
• >>> a<35
• Output-array([ True, True, False, False])
Matrix operations
• >>> A = np.array( [[1,1],
• ... [0,1]] )
• >>> B = np.array( [[2,0],
• ... [3,4]] )
• >>> A * B # elementwise product
• OUTPUT-array([[2, 0],
• [0, 4]])
Matrix operations

• >>> A @ B # matrix product


• array([[5, 4],
• [3, 4]])
• >>> A.dot(B) # another matrix product
• array([[5, 4],
• [3, 4]])
Matrix operations
• >>> a = np.random.random((2,3))
• >>> a
• OUTPUT-array([[ 0.18626021, 0.34556073, 0.39676747],
• [ 0.53881673, 0.41919451, 0.6852195 ]])
• >>> a.sum()
• 2.5718191614547998
• >>> a.min()
• 0.1862602113776709
• >>> a.max()
• 0.6852195003967595
Other Operations

• >>> b = np.arange(12).reshape(3,4)
• >>> b
• Output-array([[ 0, 1, 2, 3],
• [ 4, 5, 6, 7],
• [ 8, 9, 10, 11]])
>>> b.sum(axis=0) # sum of each column
Output-array([12, 15, 18, 21])
>>> b.min(axis=1) # min of each row
array([0, 4, 8])
Other Operations
• >>> b.cumsum(axis=1) # cumulative sum along each row
• Output- array([[ 0, 1, 3, 6],
• [ 4, 9, 15, 22],
• [ 8, 17, 27, 38]])
Universal Functions

• >>> B = np.arange(3)
• >>> B
• Output-array([0, 1, 2])
• >>> np.exp(B)
• Output-array([ 1. , 2.71828183, 7.3890561 ])
• >>> np.sqrt(B)
• array([ 0. , 1. , 1.41421356])
• >>> C = np.array([2., -1., 4.])
• >>> np.add(B, C)
• array([ 2., 0., 6.])
Indexing, Slicing and Iterating
• One-dimensional arrays can be indexed, sliced and iterated over, much
like lists and other Python sequences.
• >>> a = np.arange(10)**3
• >>> a
• array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
• >>> a[2]
• 8
• >>> a[2:5]
• array([ 8, 27, 64])
Indexing, Slicing and Iterating

• >>> a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to


position 6, exclusive, set every 2nd element to -1000
• >>> a
• array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729])
• >>> a[ : :-1] # reversed a
• array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000])
Indexing, Slicing and Iterating in
multidimensional
• array([[ 0, 1, 2, 3],
• [10, 11, 12, 13],
• [20, 21, 22, 23],
• [30, 31, 32, 33],
• [40, 41, 42, 43]])
• >>> b[2,3]
• 23
Indexing, Slicing and Iterating in
multidimensional
• >>> b[0:5, 1] # each row in the second column of barray
• ([ 1, 11, 21, 31, 41])
• >>> b[ : ,1] # equivalent to the previous example
• array([ 1, 11, 21, 31, 41])
• >>> b[1:3, : ] # each column in the second and third row of
b
• array([[10, 11, 12, 13],
• [20, 21, 22, 23]])
Indexing, Slicing and Iterating in
multidimensional
• >>> c = np.array( [[[ 0, 1, 2], # a 3D array (two stacked 2D
arrays)
• ... [ 10, 12, 13]],
• ... [[100,101,102],
• ... [110,112,113]]])
• >>> c.shape
• (2, 2, 3)
Indexing, Slicing and Iterating in
multidimensional
• >>> c[1,...] # same as c[1,:,:] or c[1]
• array([[100, 101, 102],
• [110, 112, 113]])
• >>> c[...,2] # same as c[:,:,2]
• array([[ 2, 13],
• [102, 113]])

You might also like