/    /  Numpy – Array using arange

Numpy – Array using arange:

The arange function which almost like a Range function in Python. The arange function will return an array as a result.

let us see an example.

>>> a=np.arange(5)

>>> a
array([0, 1, 2, 3, 4])

>>> a[0]
0

>>> a[1]
1

>>> a[4]
4

In the below example, first argument is start number ,second is ending number, third is nth position number. It means that it has to display the numbers for every 5th step starting from one to 20.

Example:

>>> b=np.arange(1,20,5)

>>> b
array([ 1, 6, 11, 16])

If you want to divide it by number of points, linspace function can be used. This will reach the end number by the number of points you give as the last argument.

Example:

First argument - 0
Second argument - 1
Third Argument - 5

>>> b=np.linspace(0,1,5)

>>> b
array([ 0. , 0.25, 0.5 , 0.75, 1. ])

>>>

>>> b=np.linspace(0,1,6)

>>> b
array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. ])

>>> b=np.linspace(0,1,7)

>>> b
array([ 0. , 0.16666667, 0.33333333, 0.5 , 0.66666667, 0.83333333, 1. ])

Creating Arrays using other functions like ones, zeros, eye.
Below example is using the ones function.

>>> a=np.ones([3,3])

>>> a
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])

Below example is using the zeros function.

>>> a=np.zeros([2,3])

>>> a
array([[ 0., 0., 0.],
[ 0., 0., 0.]])

# Accessing the value of a[1,1]

>>> a[1,1]
0.0

>>> np.ones(5)
array([ 1., 1., 1., 1., 1.])

>>> np.zeros(5)
array([ 0., 0., 0., 0., 0.])

Below example is using the eye function.

>>> a=np.eye(3)
>>> a
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])