## DEV Community

Tanveer Shahriar Arnob

Posted on

# NumPy Arrays

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:

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.

## 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.

## ones

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

## linspace

`numpy.linspace(start, stop, num=50)`
Returns num evenly spaced samples, calculated over the interval [start, stop]. And num is by default 50.

## eye

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

## 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).

## 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.

## 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.

## 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.

## max

It will return the max value of the array.

## argmax

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

## min

It will return the min value of the array.

## argmin

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

## Shape

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

## dtype

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

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