Numpy is library that provide all advanced array related functionalities, In this tutorial, you will learn how to create multi dimensional array and work with different type of functions and element, how to apply linear algebra operations to multi-dimensional arrays etc.
Before you can start working with numpy library, make sure you have installed compatible version for your development environment.
The following lines will help in most of the cases.
pip install numpy import numpy numpy.__version__
To start with numpy, we first learn how to create different type of array.
Make sure you have added numpy reference in your code import numpy as np
Create array with array function, remember this array function is from numpy library, and is different from python array function, using this numpy array function we can create multi dimensional array, which was not possible with python array function.
Remember, while using numpy array function to create an array, we need to provide all elements within a box bracket like [1,2,3]
import numpy as np a1 = np.array([2,3,4]) print(a1) # array with decimal b1 = np.array([1.4, 2.9, 4.6])
Above is the simple example of creating one-dimensional array , after creating any array we check array following properties by using following methods and property.
To create an array using arange function, we simply need to specify the first parameter, an integer number,
it will create an array that many number of elements like arange(6)
.
import numpy as np a3 = np.arange(6) print(a3)
the above numpy arange function will create following array.
[0 1 2 3 4 5]
Instead of providing number as first parameter, you can also provide a decimal value like arange(6.2)
.
that will create a array with decimal number like [0. 1. 2. 3. 4. 5. 6.]
In next section we also will learn how to use numpy arange function with reshape function for creating multi-dimensional array.
linspace is a built-in function that allow us to create an array with specified number of elements within a specified range, think of situation where you want to create an array of 10 elements within 0 to 4 number range.
linspace function takes three arguments, first two are range, and the third one is the number of elements we want to create.
import numpy as np a11= np.linspace( 0, 4, 10 ) print(a11)
the above code will create following array of 10 elements
[0. 0.44444444 0.88888889 1.33333333 1.77777778 2.22222222 2.66666667 3.11111111 3.55555556 4. ]
We also can create array with r in complex situation, here is quick example.
import numpy as np a10= np.r_[1:4,0,4] print(a10) print(a10.ndim)
Complex array means one element of the array will have at least one imaginary number.
Here are two examples of how we can define complex Array, complex 1D array and 2D Array.
complexAr1d = np.array([1+3j, 3+6j, 5+8*1j]) print(complexAr1d) complexAr2d = np.array([[1+3j, 3+6j, 5+8*1j],[3+6j, 5+8j, 1+3*9j]]) print(complexAr2d)
In next numpy tutorial [part-2] we learn more about complex number and array with examples.
Below are some common array property and functions we often need to work with.
Ndim property will tell the dimension of the array.
a1 = np.array([2,3,4]) print(a1.ndim) #1
dtype will tell what type of array, for example if we print
print(a1.dtype)
, that will return int32
In real-time program development we often come across situation when we need to split an array into multiple piece.
Numpy library has built in function called hsplit hsplit(array, number of split)
,
hsplit function will split an array horizontally column-wise
the first argument is the array we want to split, second argument is the number of elements we want to make.
the function will return list of sub-arrays.
import numpy as np a1 = np.array([2,3,4,9,7,6,1]) print(np.hsplit(a1,(2,2)))
Here is an example of how we can define two-dimensional array using numpy python class.
import numpy as np a2 = np.array([[2,3.1 , 9], [4. , 5. , 6. ]]) b2 = np.arange(12).reshape(4,3)
The above code will create following 2d array
[[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]]
Notice, how reshape function works in two-dimension array, reshape(rows, columns)
,
you can try with different number to reshape the array, but make sure arrange number shold be equal to
rows * columns
Here we try to create a 3d array using arrange and reshape function, notice the value of arrange is equal to multiplication of all reshape numbers.
In below example arange(24)
is equal to reshape(2 * 3 * 4)
import numpy as np a5 = np.arange(24).reshape(2,3,4)
The above code will create following 3d array.
[[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]]
Notice in three-dimension array, how reshape function works, reshape(number of sets, rows, columns)
remember, last digit is for number of columns, and second last number is for number of rows we want in subsets.
Like any other array structure we also can deep copy and clone any numpy array and access their elements.
Deep copy means copying array structure to new array with all data inside that.
ar1 = np.array([40,30,70,90]) ar2= ar1.copy() #changing one element value in original array ar1[0] = 15 #now print both array to see the difference. print(ar1) print(ar2)
Here is the output of above code. Notice, I have made changes in one element value of first array after copying that to a new array ar2, changes are not reflected to second array.
[15 30 70 90] [40 30 70 90]
Now instead of using copy function (like above), we use view function and see the difference
ar1 = np.array([40,30,70,90]) ar2= ar1.copy() ar3= ar1.view() #changing a value in original array ar1[0] = 15 print(ar1) print(ar2) print(ar3)
Now see the result of printing all three arrays.
[15 30 70 90] #ar1 [40 30 70 90] #ar2 [15 30 70 90] #ar3
We have made changes in array one, but the same value got reflected in array three, The value of array one and three is same, that’s the difference between view and copy function in numpy array.
Clone indicates copying the structure only without data.
You should also read following post