# Getting Started with NumPy.

# What is NumPy?

NumPy \[Numerical Python\] is a fundamental package for scientific computing with python. It was created in 2005 by Travis Oliphant. It has functions for working in the domain of arrays, linear algebra, Fourier transform, and matrices. NumPy is an open-source library and free to use.

# Why use NumPy?

*   NumPy provides efficient storage of data.
    
*   NumPy provides better ways for handling data for processing.
    
*   It is fast.
    
*   Numpy is easy to use.
    
*   Numpy uses relatively less memory to store data.
    
*   Numpy is optimized enough to work with latest CPU architecture.
    

# Installation of Numpy.

Install NumPy by running the following command on your terminal. It may take some time to install.

```plaintext
pip install numpy
```

# Jupyter Notebook

*   We use Jupyter Notebook inorder to test, run and execute our NumPy scripts.
    
*   The Jupyter Notebook is an open source web application that allows us to create and share documents that contains line code, equations, visualizations and narrative text.
    
*   The notebook has support for over 40 programming languages, including Python, R, Julia and Scala.
    
*   Notebooks can be shared with others using email, drop-box, Github and Jupyter Notebook Viewer.
    
*   It was spun off from IPython in 2014 by Fernando Pérez and Brian Granger.
    

# Installation of Jupyter Notebook

Install Jupyter Notebook by running the following command on your terminal. It may take some time to install.

```plaintext
pip install jupyter
```

## How to open Jupyter Notebook

Open the Jupyter Notebook by running the following command on your terminal.

```plaintext
jupyter notebook
```

A Jupyter Notebook server will get launched on your default browser. Select your preferred language (in this case, Python) and get started on writing python scripts.

# Import NumPy

Once NumPy is installed, import it in your applications by adding the `import` keyword:

```python
import numpy as np
```

NumPy is usually imported under the `np` alias. Now NumPy is imported and ready to use.

# Creating an array in NumPy

In NumPy, an array can be created by using the folllowing format

```python
import numpy as np
myarr = np.array([3, 4, 5, 6])
```

To print the array run `myarr`, the following output will get printed

```plaintext
array([3, 4, 5, 6])
```

# Creating a 2D array in NumPy

In NumPy, a 2D array can be created using the following format

```python
import numpy as np
myarr2 = np.array([[1, 2, 3],[4, 5, 6]])
```

To print the array run `myarr2`, the following output will get printed

```plaintext
array([[1, 2, 3],
       [4, 5, 6]])
```

# Array Methods in NumPy

`shape` : This method will give the number of rows and number of columns present in n array.

```python
import numpy as np
myarr2 = np.array([[1, 2, 3],[4, 5, 6]])
myarr2.shape
```

Output:

```plaintext
(2, 3)
```

`myarr2[rowNo, colNo]` : This method will give the value present at a specific row number and column number

```python
import numpy as np
myarr2 = np.array([[1, 2, 3],[4, 5, 6]])
myarr2[1, 2]
```

Output:

```plaintext
6
```

**While traversing an array, the row number and column number starts from 0.**

`myarr2[rowNo, colNo] = Value` : This method is used to insert a value at a specific position, replacing the old value.

```python
import numpy as np
myarr2 = np.array([[1, 2, 3],[4, 5, 6]])
myarr2[0, 2] = 33
myarr2
```

Output:

```plaintext
array([[ 1,  2, 33],
       [ 4,  5,  6]])
```

`argmax()` : This function is used to return the index of maximum value from an array.

```python
import numpy as np
myarr = np.array([3, 4, 5, 6])
a.argmax()
```

Output:

```plaintext
3
```

`argmin()` : This function is used to return the index of minimum value from an array.

```python
import numpy as np
myarr = np.array([3, 4, 5, 6])
a.argmin()
```

Output:

```plaintext
0
```

`argsort()` : This functions sorts the array in ascending order

```python
import numpy as np
myarr = np.array([3, 4, 5, 6])
a.argsort()
```

Output:

```plaintext
array([0, 1, 2, 3], dtype=int64)
```

Here, `dtype=int64` tells us about the datatype of the array.

`size` : This function is use to calculate and print the total number of elements present in the array.

```python
import numpy as np
myarr = np.array([3, 4, 5, 6])
myarr.size
```

Output:

```plaintext
4
```

`ndim` : This function is used to print the dimension of the array.

```python
import numpy as np
myarr2 = np.array([[1, 2, 3],[4, 5, 6]])
myarr2.ndim
```

Output:

```plaintext
2
```

`nbytes` : This function is used to print the total number of bytes consumed by the array.

```python
import numpy as np
myarr2 = np.array([[1, 2, 3],[4, 5, 6]])
arr2.nbytes
```

Output:

```plaintext
24
```

`count_nonzero()` : It returns the count of non-zero elements in an array

```python
import numpy as np
a = np.array([1, 4, 3, 6, 2])
np.count_nonzero(a)
```

Output:

```plaintext
5
```

`sqrt()` : It returns a new array containg the square root of each element of an array.

```python
import numpy as np
a = np.array([1, 4, 3, 6, 2])
np.sqrt(a)
```

Output:

```plaintext
array([1.        , 2.        , 1.73205081, 2.44948974, 1.41421356])
```

## Initializing an array of zeros

Using `np.zeros` we initialize an array of zeros.

```python
import numpy as np
zeros = np.zeros((2, 5))
zeros
```

Output:

```plaintext
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
```

## Initializing an empty array

Here `np.empty()` will give us an array full of garbage values, which we can re-assign them according to our use.

```python
import numpy as np
emp = np.empty((4, 6))
emp
```

Output:

```plaintext
array([[6.23042070e-307, 4.67296746e-307, 1.69121096e-306,
        6.23058707e-307, 2.22526399e-307, 6.23053614e-307],
       [7.56592338e-307, 1.60216183e-306, 7.56602523e-307,
        3.56043054e-307, 1.37961641e-306, 2.22518251e-306],
       [1.33511969e-306, 6.23036978e-307, 6.23053954e-307,
        9.34609790e-307, 8.45593934e-307, 9.34600963e-307],
       [1.86921143e-306, 6.23061763e-307, 6.89804132e-307,
        1.11261162e-306, 8.34443015e-308, 2.12203497e-312]])
```

## Axes in NumPy

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1671183261578/Fn-392_sG.webp align="center")

A simple 2-dimensional Cartesian coordinate system has two axes, the x axis and the y axis.

These axes are essentially just directions in a Cartesian space (orthogonal directions).

Moreover, we can identify the position of a point in Cartesian space by it’s position along each of the axes.

So if we have a point at position `(2, 3)`, we’re basically saying that it lies 2 units along the x axis and 3 units along the y axis.

**In NumPy:**

Axes 0 -&gt; points towards rows

Axes 1 -&gt; Points towards column

In 1D arrays, we only have Axes 0, while in 2D arrays, we have Axes 0, as well as Axes 1.

### Operations on Axes

`sum(axis = 0)` : Prints sum column-wise

```python
import numpy as np
x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr = np.array(x)
arr.sum(axis = 0)
```

Output:

```plaintext
array([12, 15, 18])
```

`sum(axis = 1)` : Prints sum column-wise

```python
import numpy as np
x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr = np.array(x)
arr.sum(axis = 1)
```

Output:

```plaintext
array([ 6, 15, 24])
```

### Conversion of 2D array to 1D array

Using `ravel()` , we convert 2D arrays to 1D arrays.

```python
import numpy as np
myarr2 = np.array([[1, 2, 3],[4, 5, 6]])
myarr2.ravel()
```

Output:

```plaintext
array([1, 2, 3, 4, 5, 6])
```

### Iterating through an array

Using `.flat` , an iterator is generated which points to the startimg of an array.

```python
using numpy as np
x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr = np.array(x)
arr.flat
for item in arr.flat:
    print(item)
```

Output:

```plaintext
1
2
3
4
5
6
7
8
9
```

### Creating a NumPy array from Python lists

```python
import numpy as np
listarray = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
listarray
```

Output:

```plaintext
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
```

# Learning Resources:

This is just for start, NumPy is a very vast resource, some of the resouces through which NumPy can be learned in a professional way are:

*   [Resource 1](https://numpy.org/)
    
*   [Resource 2](https://jupyter.org/)
    
*   [Resource 3](https://www.w3schools.com/python/numpy/numpy_intro.asp)
    
*   [Resource 4](https://youtu.be/Rbh1rieb3zc)
    
*   [Resource 5](https://youtu.be/QUT1VHiLmmI)
    

Do like, comment and share if you like this artical.

Follow [me](https://akshato2.hashnode.dev/) for reading more of such technical blogs, contents and articals.
