Home » »

# NumPy Array Tutorial

|| 0

NumPy Array Tutorial
Stechies

## NumPy Array Basic Operations

This tutorial explains step by step how to perform basic array operations in NumPy Python.

Please follow the steps below in order to perform basic array operations in NumPy Python:

1.Start by importing NumPy module on Python IDLE. Then create a one-dimensional array.

Python 3.5.2  (v3.5.2 : 4def2a2901a5
>>> import numpy as np
>>> a=np .array ([6,5,7])
>>> a[0]
5
>>> a[1]
6
>>>

2. Use 'n.dim' property to print the dimensions of the arrays.

>>> import numpy as np
>>> a=np .array ([6,5,7])
>>> a[0]
5
>>> a[1]
6
>>> a=np .array ([[1,2] , [3,4] , [5,6]])
>>> a.ndim
2
>>> a=np.array ([6,5,7])
>>> a.ndim
1

3. Use 'a.itemsize' to print the size and a.dtype to print the datatype of each element in the array.

1
>>> a=np.array ([[1,2] , [3,4] , [5,6]])
>>> a.ndim
2
>>> a.itemsize
4
>>> a.dtype
dtype ('int32')
>>> a=np.array ([[1,2] , [3,4] , [5,6]]) ,dtype=np.float64)
>>> a.itemsize
8
>>> a
array    ([[1., 2.],
( 3., 4.],
( 5., 6.]]),

4. Use 'a.size' to print the total number of elements and 'a.shape' to display the number of rows and columns present in the array.

>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> a.size
6
>>>a
array ([[1., 2.],
( 3., 4.],
( 5., 6.]]),
>>> a.shape
(3,2)
>>> T

5. You can initialise your arrays with some placeholder numbers according to the given shape of the array.

>>> import numpy as np
>>> n.p zeroes ( (3,4) )
array ([[ O., O., O.,] ,
[ O., O., O.,] ,
[ O., O., O.,] ] )
>>> np.ones ( (3,4) )
array ( [ [ 1., 1., 1., 1.,],
[ 1., 1., 1., 1.,],
[ 1., 1., 1., 1.,] ])
>>>

6. Use the range function to create a NumPy array or a stack of arrays.

>>> 1
range  (0, 5)
>>> 1[0]
0
>>> 1[1]
1
>>> np.arange (1,5)
array ( [1, 2, 3, 4] )
>>> np.arange (1,5,2)
array ( [1, 3] )
>>>

7. You can use 'linspace' to create an array of equally spaced values. On declaring a start value, stop value, and the number of points in between those points, an array will be generated.

array ([1, 3])
>>> np.linspace(1,5,10)
array([ 1.     ,  1.444444444,   1.88888888, 2.33333333, 2.77777778,
3.222222, 3.666666667,   4.11111111, 4.55555556, 5.
>>> np.linspce(1,5,5)
array([1., 2., 3., 4., 5.,])
>>> np.linspace(1,5,20)
array([ 1.       ,1.210052632,  1.42105263, 1.63157895, 1.84210526,
2.05263  ,2.26315789,   2.47368421, 2.68421053, 2.89473684,
3.10526  ,3.31578947,   3.52631579, 3.73684211, 3.94742,
4.157894 ,4.36842105,   4.57894737, 4.78947368, 5.  ])

8. The 'a.reshape' function can be used to change the shape of the array.

>>> import numpy as np
>>> a=np.array ([[1,2] , [3,4] , [5,6]])
>>> a
array ([[ 1., 2.],
( [ 3., 4.],
( [ 5., 6.]]),
>>> a.shape
(3,2)
>>> a.reshape (2,3)
array ([[1, 2, 3],
[4, 5, 6]])
>>>

9. You can use the 'a.ravel' function to create another one-dimensional array of same elements. It does not make any changes in the original existing array.

>>> a.reshape (2,3)
array ([1, 2, 3],
([4, 5, 6]])
>>> a.reshape (6,1)
array([ [1] ,
[2] ,
[3] ,
[4] ,
[5] ,
[6]])
>>> a.ravel ()
array ([1, 2, 3, 4, 5, 6,])
>>> a
array ([[ 1., 2.],
([[ 3., 4.],
([[ 5., 6.]])

10. The 'a.min', 'a.max', 'a.sum' are functions used to print the minimum, maximum, and summation of the elements in the array.

>>>
>>>
>>> a
array ([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
>>> a.min ()
1
>>> a.max ()
6
>>> a.sum ()
21

11. On using the 'a.sum' function with axis specifications, the summation of all elements present along that axis is printed.

.>>
.>>
.>> a
array ([[ 1., 2.],
([[ 3., 4.],
([[ 5., 6.]])
.>> a.min ()
.
.>> a.max ();
;
.>> a.sum ()
21
>>> a.sum (axis=0)
array([ 9, 12])
.>> a.sum (axis=1)
array([ 3, 7, 11])

12. To perform square root operation, use the sqrt NumPy function on the array.

>>> a.sum (axis=1)
array([7,6  , 4 .])
>>>
>>>
>>>
>>> a.sqrt ()
Traceback (most recent call last) :
file "<pyshe11#28>, line 1,  in <modu
a.sqrt ()
AttributiveError : 'numpy.ndarray' object
>>> np.sqrt () (a)
array([[ 1.        , 1.414421256] ,
[ 1.73205081, 2.         ] ,
[ 2.23606789, 2.4494894  ]])
>>> a
array ([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])

13. To print value of standard deviation of all elements in the array use the NumPy function std.

>>> a.sqrt ()
Traceback (most recent call last) :
file "<pyshe11#28>, line 1,  in <module>
a.sqrt ()
AttributiveError : 'numpy.ndarray' object
>>> np.sqrt (a)
array([[ 1.        , 1.414421256] ,
[ 1.73205081, 2.         ] ,
[ 2.23606789, 2.4494894]])
>>> a
array ([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
>>> np.std (a)
1.707825127659933

14. Simple mathematical operations can be performed on arrays by writing simple and shortcodes on NumPy module.

>>> a
array ([[ 1, 2],
[ 3, 4]]),
>>> b
array ([[ 5, 6],
[ 7, 8]]),
>>> a+b
array ([[ 6,   8],
[ 10, 12]]),
>>> a*b
array ([[ 5, 12],
[ 21, 32]]),
>>> a/b
array ([[ 0.2,    0.333333],
([[ 0.4285, 0.5      ]])
>>>
>>>
>>>
>>>a.dot (b)
array ([[19, 22]
[43, 50]])

Related Articles