# All about NumPy

Introduction to NumPy

what is NumPy?

NumPy stands for Numerical Python. It is a standard library containing multidimensional array objects and various tools for working with arrays. The core of NumPy is code optimized in C language.

Why NumPy arrays?

.Here are some features that make NumPy a preference:

.Numpy offers comprehensive computing tools like mathematical functions, random no generators, etc

. It's an Open source

.Easy to use — high-level syntax, makes it easy for anyone to understand.

.Interoperable-supports a wide range of hardware and computing platforms.

NumPy arra

ys are better than Python lists in the following ways:

**. Size** — Numpy data structures take up less space

**.Performance** — they have a need for speed and are faster than lists

**.Functionality** — SciPy and NumPy have optimized functions such as linear algebra operations built-in.

Limitations of NumPy

**Require a contiguous allocation of memory:**Insertion and deletion operations become costly as data is stored in contiguous memory locations as shifting it requires shifting.**Using “nan” in Numpy:**“Nan” stands for “not a number”. It was designed to address the problem of missing values. NumPy itself supports “nan” but lack of cross-platform support within Python makes it difficult for the user.

Installation requirements

The only prerequisite for installing NumPy is Python itself. The two main tools that install Python packages are `pip`

and `conda.`

If you use conda,

`# Best practice, use an environment rather than install in the base env`

conda create -n my-env

conda activate my-env

# If you want to install from conda-forge

conda config --env --add channels conda-forge

# The actual install command

conda install numpy

If you use pip,

`pip install numpy`

NumPy ndArray Objects

NumPy’s array class is called **ndarray**. It is also known by the name **array**. In NumPy dimension of the array is called axes and all elements can be indexed by a tuple of positive integers.

Array indexing and slicing — Numpy offers may ways to do indexing that are :

**Slicing:**Just like lists in python, NumPy arrays can be sliced. As arrays can be multidimensional, you need to specify a slice for each dimension of the array.**Integer array indexing:**In this method, lists are passed for indexing for each dimension. One-to-one mapping of corresponding elements is done to construct a new arbitrary array.**Boolean array indexing:**This method is used when we want to pick elements from array which satisfy some condition.

Memory layout of ndArray

An instance of class **ndarray** consists of a contiguous one-dimensional segment of computer **memory** (owned by the **array**, or by some other object), combined with an indexing scheme that maps N integers into the location of an item in the block.

Views and Copies

Copy is essentially a feature which creates a copy of the original array and any changes made to the copy will not affect the original values. View on the other hand allows you to make the changes in the original array.

import numpy as np

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

x = arr.view()

x[0] = 31

print(arr)

print(x)

output

[31,2,3]

[31,2,3]import numpy as np

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

x = arr.copy()

y = arr.view()

print(x.base)

print(y.base)

Every NumPy array has the attribute `base`

that returns `None`

if the array owns the data.

Creating an array

We can create a NumPy `ndarray`

object by using the `array()`

function.

`import numpy as np`

arr = np.array([1, 2, 3, 4, 5])

print(arr)

output

[1,2,3,4,5]

To create an `ndarray`

, we can pass a list, tuple or any array-like object into the `array()`

method, and it will be converted into an `ndarray.`

Here’s how

`import numpy as np`

arr = np.array((1, 2, 3, 4, 5)) #tuple being passed

print(arr)

output

[1,2,3,4,5]

Data types of an array in NumPy

NumPy has some extra data types, and refer to data types with one character, like `i`

for integers, `u`

for unsigned integers etc.

Below is a list of all data types in NumPy and the characters used to represent them.

`i`

- integer`b`

- boolean`u`

- unsigned integer`f`

- float`c`

- complex float`m`

- timedelta`M`

- datetime`O`

- object`S`

- string`U`

- unicode string`V`

- fixed chunk of memory for other type ( void )

Thank you for delving into the absolute basics of NumPy with me!