Skip to content

iamkirankumaryadav/NumPy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NumPy

  • Provides support for large, multidimensional arrays and matrices.
  • Large collection of mathematical functions to operate on arrays
  • NumPy is foundation of other libraries like Pandas, SciPy and Scikit Learn
  • DataFrames are build on NumPy arrays and can leverage NumPy functions.
  • NumPy's core data structure is the ndarray (n dimensional array)
  • Arrays are homogeneous collection of elements of same data types.
  • Array: Fixed size containers of items that are more efficient than Python Lists or Tuples for data processing.
  • Only store a single data type (Mixed data type is stored as object | string)
  • They can be one dimensional or multi dimensional.
  • Array elements can be modified, but the array size cannot change.

Why to use NumPy ?

  • Arithmetic, statistcal, mathematical, bitwise operations and linear algebra.
  • Stacking, searching, sorting, counting, broadcasting and matrix operations.
  • Elementwise arithmetic operations, scientific and financial calculations.
  • N dimensional array processing, fast vector and matrix operations.
  • Broadcasting : Arithmetic operations between array of different shapes and size.
  • NumPy is used as a base library for Pandas, SciPy, Statsmodel, Matplotlib, Scikit Learn.
  • Essential for OpenCV computer vision applications ( Images pixels are converted into array )
  • The datasets which are accepted by models for fit ( training | learning ) are also in the form of array.
  • NumPy has a very rich set of number generator, attributes and methods
import numpy as np

marks = [50, 55, 90, 99, 100]

marks_array = np.array(marks)

Key Properties

Properties Description
ndim Number of dimensions (axes) in an array.
shape Size of an array for each dimension.
size Total number of elements in the array.
dtype Data type of elements in the array.

Dimensions ( Array )

Dimensions
0 Dimension 1 Dimension 2 Dimensions 3 Dimensions
Point Line Square Cube
Scalar Vector ( Row or Column ) Matrix ( Table or Data Frame ) Tensor
Rank 0 Tensor Rank 1 Tensor Rank 2 Tensor Rank 3 Tensor
Array Dimensions
np.array([1, 2, 3]) 1D Array
np.array([(1, 2, 3), (4, 5, 6)]) 2D Array
np.arange(start, stop, step) Range Array
Attribute Description
ndarray.shape Tuple of array dimensions
ndarray.size Number of elements present in the array
ndarray.ndim Number of array dimensions ( 1, 2, 3 )
ndarray.dtype Data type of Numpy array
ndarray.itemsize Number of bytes required by each array element
ndarray.nbytes Total number of bytes occupied by array
ndarray.T NumPy.Transpose ( Transpose of a matrix )
Method Description
np.array() Creates an array
array.arange() Creates an array in a range with a specified increment
array.unique() Number of unique elements in an array
array.repeat() Creates an array by repeating the number of elements
array.argmax() Indices of maximum values along an axis
array.argmin() Indices of minimum values along an axis
array.reshape() Changes the shape of an array
array.count_nonzero() Count of non-zero elements in an array
array.hsplit() Split arrays horizontally ( Row wise )
array.vsplit() Split arrays vertically ( Column wise )
array.hstack() Stack arrays horizontally ( Row wise )
array.vstack() Stack arrays vertically ( Column wise )
array.flatten() Transform a multi-dimensional Numpy array to 1D Numpy array
array.ravel() View a multi-dimensional Numpy array as 1D Numpy array
array.transpose() Transposes an array
array.absolute() Absolute values of elements in an array
array.round() Round up the floats to a specified number of decimal points

...

Placeholders

Operator Description
np.linspace(0,2,9) Add evenly spaced values between interval to array of length.
np.zeros((1,2)) Creates an array filled with zeros
np.ones((1,2)) Creates an array filled with ones
np.random.random((5,5)) Creates an array with random numbers.
np.empty((2,2)) Creates an empty array.

Similarities between Python Lists and NumPy Arrays

  • Both lists and arrays have similar syntax.
  • Elements in both are ordered, mutable and accept duplicates
  • Both allows indexing, slicing and iterating
# Python List
marks = [50, 45, 34]  

# NumPy Array
marks = np.array([50, 45, 34])

Differences between Python Lists Vs NumPy Arrays

Python List NumPy Array
No support for vectorized operations Support vectorized operations ( addition, multiplication )
Hold Heterogeneous / mixed data types Hold Homogeneous data type
For loops are not efficient For loops are fast and efficient
List is a default structure Array needs import of NumPy library
Lists are flexible Once the dimensions are defined cannot be expanded
Consumes more memory Consumes 6x less memory
Slow ( Data type of each element is checked ) Faster ( All the elements are of same data type )

Python List ( Ordered, Subscriptable, Mutable, Heterogeneous, Duplicate, Indexing, Slicing, Iterable )

List = [1, 2, 3]

NumPy Array

import numpy as np

Array = np.array([1, 2, 3])

Why NumPy array is faster than Python's list ?

  • NumPy array elements only need memory to store element values.
  • List stores much more information as compared to NumPy array.
  • List stores object type, reference count, object value and size of value.
  • Mathematical operations performed by Arrays are faster than Python's list.
# Python List: Uses a for loop for just multiplying the elements with 5.
[i * 5 for i in list]

# NumPy Array: Simply multiplies each elements of an array with 5.
array * 5

Back to Questions

About

Numerical Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published