NumPy Arrays
Tags: python, beginners, programming, datascience
Published: 2022-11-19 20:26:32

NumPy is a Python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices. NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely. This module is mainly used for Data Science and Machine Learning.

## Installation
We can install NumPy using pip [pip is a python package manager]. Go to your terminal and type:

```
pip install numpy
```

## Using NumPy
To use the built in function from NumPy we have to import it first.

```
import numpy
```
But after that if we want to use any function we have to write numpy every time to use that. That's why it is convention to import numpy as np.

```
import numpy as np
```

## NumPy Arrays
NumPy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-d arrays and matrices are 2-d (but you should note a matrix can still have only one row or one column).

## Creating Numpy Arrays
We can create an array by directly converting a list or list of lists:

![NumPy Array](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oj720u93pwm0udg8sfma.png)


![NumPy Matrix](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/luwlcgj6uic53e4t49k0.png)

![NumPy Array Data Type](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpcb8e7tih23153fy9mf.png)

There are also some built in functions to generate arrays. 

## arange
`numpy.arange([start, ]stop, [step, ])`
arange is like the range function of python. It returns evenly spaced values within a given interval. Here start and step is optional if we need it but stop is mandatory.

arange can be called with a varying number of positional arguments:

arange(stop): Values are generated within the half-open interval [0, stop) (in other words, the interval including start but excluding stop).

arange(start, stop): Values are generated within the half-open interval [start, stop).

arange(start, stop, step) Values are generated within the half-open interval [start, stop), with spacing between values given by step.

![NumPy arange](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxh3gz7qfzfzm56ib0sa.png)

## zeros
`numpy.zeros(shape, dtype=float)`
It generates a array of given shape and type, filled with zeroes. Here shape can be a int which will generate 1D array or a tuple of ints which will generate matrices. Instead of tuple we can use list of ints also. And dtype is by default float but we can change it into int.

![NumPy zeros](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/roen466q6w6kcx0bxhrf.png)

## ones
`numpy.ones(shape, dtype=float)`
It is same as the zeros function but instead of zeroes all of the elements are one. 

![NumPy ones](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ojo9nkffsy9j9dzrre4.png)
## linspace
`numpy.linspace(start, stop, num=50)`
Returns num evenly spaced samples, calculated over the interval [start, stop]. And num is by default 50.

![NumPy linspace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hebgepdsy3ziuu5lymx.png)

## eye
`numpy.eye(n)`
Return a 2-D array with ones on the diagonal and zeros elsewhere. Simply it generates a nxn identity matrix.

![NumPy eye](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xbzygyjseashas6e5i8.png)

## Random
NumPy also has lots of ways to create arrays with random numbers. 

## rand
`numpy.random.rand(d0, d1, ..., dn)`
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).

![NumPy Random rand](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cc8mvjdlu3ai9cu03sfo.png)

## randn
`numpy.random.randn(d0, d1, ..., dn)`
Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform. Also rand cannot generate negative values but randn can.

![NumPy Random randn](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6om4bwfqrwrow04c32e9.png)

## randint
`random.randint(low, high=None, size=None)`
Return random integers from low (inclusive) to high (exclusive). If we don't mention size then it will return one value. For size we can pass an int or a tuple of ints or a list of ints. 

![NumPy Random randint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/novbj1g8qc2svt5ysedy.png)

## Array Attributes and Methods


## reshape
Returns an array containing the same data with a new shape. But we have to declare the shape such that we have the proper amount of elements. Suppose we have a array of 25 elements. We can transform it into a 5x5 matrix but we can not reshape into a 3x4 matrix as it can not contain 25 element.

![NumPy reshape](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9cw4g981prhbpuxvr5n.png)

## max
It will return the max value of the array.

![NumPy max](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu4xnygjx6ecgybh1svj.png)

## argmax
It will return the index of the max value of the array.

![NumPy argmax](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ycfkmwcvi3vgr0zcfcm7.png)

## min
It will return the min value of the array.

![NumPy min](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4k2ud1x7pqf5l7swg0r.png)

## argmin
It will return the index of the min value of the array.

![NumPy argmin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfteg6bnbppo193ygkuo.png)

## Shape
Shape is an attribute that arrays have which returns the shape of the array.

![NumPy shape](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uw21ttzgyejii8lustn0.png)

## dtype
You can also grab the data type of the object in the array.

![NumPy dtype](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yezl8toh6r101g1qwlks.png)

That's it for today. Feel free to ask any question in the comment.