NumPy

What is NumPy ?

NumPy is an array-processing package. It provides a multidimensional array as an object and tools to work with it.

NumPy Arrays

It’s a grid that contains values of the same type.

They come in 2 forms :

  1. 1D – also known has Vectors (have only a single row/column)
  2. Multi-Dimensional arrays – also known as Matrices (can have multiple rows and columns). In some cases, Matrices can have only one row or column.

Storage of 2D Arrays in Memory

2D Arrays are linearized for storage purposes in one of these two alternatives :

  1. Row-major or row wise.
  2. Column-major or column wise.

Anatomy of NumPy Arrays

There are certain terms associated by NumPy arrays which must be known to the user :

  1. Axes – NumPy refers to the dimensions of arrays as axes.
  2. Rank – The occurrence of axes in an ndarray is called its rank.
  3. Shape – It tells us about the number of elements along each axis.
  4. Datatype – It tells us about the type of data stored in ndarray.
  5. ItemSize – It gives us the size of each element of an ndarray in bytes.
  • Indexing in NumPy arrays is similar to Python lists indexing.

Properties of a NumPy array :

  1. A size of a NumPy array can’t be changed after its creation.
  2. An equivalent NumPy array occupies less space.
  3. Each element of a NumPy array has the same Datatype.
  4. It supports vectorized operations  i-e if a function is applied , it’s performed on every item in the array.
  • In order of specify NumPy’s datatypes, either numpy or np name is used before the datatype.

Creation Of NumPy arrays :

  1. Creating ndarrays using array()

ndarray is created via the following syntax :

Numpy.array(<arrayconvertibe1 object>, [<dtype>])

2D ndarrays can also be created using the same syntax. The list or tuple passing to it should be a 2D list or tuple.

  • Creating ndarrays using fromiter()

Syntax to use fromiter() function is :

Numpy.fromiter(<iterable sequence name>,<target data type>,[<count>])

  • Creating ndarrays with a numerical range using arrange()

Syntax for using this is :

<arrayname> = numpy.arange([start,] stop [,step] [, dtype])

2D arrays can also be created using arange() function by these steps :

  1. Creating an ndarray using arange()
  2. Reshaping the ndarray created in step a using reshap() function as per the following syntax :

<ndarray>.reshape(<shape tuple>)

  • Creating arrays with a numerical range using linspace()

Syntax for using linspace() function is :

<arrayname> = numpy.linspace(<start>,<stop>,<number of values to be generated>)

  • Just like Python lists, NumPy arrays can be sliced.

Joining Or Concatenating NumPy Arrays : there are 2 functions which deal with this i-e

  1. hstack() and vstack()

Syntax for this function is :

Combining existing arrays horizontally or vertically – –

numpy.hstack(<tuple containing names of arrays to be stacked>)

numpy.vstack(<tuple containing names of arrays to be stacked>)

  • concatenate()

Syntax for this function is :

numpy.concatenate((<tuple of arrays to be joined>), [axis = <n>])

Obtaining Subsets of Arrays :

  1. Using the hsplit() and vsplit() functions

Syntax for this functions are as follows :

numpy.hsplit(<array>,<n>)

numpy.vsplit(<array>,<n>)

  • Using the split() function

Syntax for this function are as follows :

numpy.split(<array>,<n>|<1D array>,[axis = 0])

Extracting Condition based Non-contiguous Subsets :

Syntax for extracting is as follows :

numpy.extract(<condition>,<array>)

Science & Technology , , ,

Discover more from Logic Searcher

Subscribe now to keep reading and get access to the full archive.

Continue reading