Python Programming — Part 3: Numpy with Python
Welcome back to my article chain on “Python Programming”. Today I am going to discuss about every essential things about Numpy lib what you need to know before go to learn Machine Learning. Hope you can get better knowledge about Numpy at the end of this article.Let’s start 😊 !!
What is the Numpy(Numerical Python)? Numpy is an open source library in Python that used mathematical, statistical operations scientific, engineering and data science programming. Different operations can be performed by using Numpy. Any operations related to leaner algebra or any number generation or random numbers generation etc that we can perform by using Numpy. Lets go to python. By default Numpy is not available in the Python. so we need to install manually.for That we have to go to command prompt and have to enter path of other python libraries are available. Next we have to follow the syntax it is called “pip install numpy”
OK 😀!..Now we are going to work with Anaconda. Anaconda is having some of the integrated development environments. Integrated development environment means these are the graphical user interface for the Python programming. Open the Anaconda navigator and go to Environments and search numpy. If you installed numpy successfully you can see as below.
Firstly we should import numpy to work with numpy. To check version of numpy that you installed follow as below.
import numpy as np
np.__version__
ctrl+enter to execute
Lets see how to create a numpy array. This numpy arrays are little bit similar to the list. using this “np.array()” function we can create or convert data into numpy array.
you can add, subtraction or stultification any other mathematical operations by using numpy array. (It is a main difference between list and numpy array)
b=np.array(a)
print(type(b))
b+100
<class 'numpy.ndarray'>Out[10]:
array([102, 104, 106, 108, 110, 112])lets talk about attributes of the numpy arrays. Shape is one of the attribute of the numpy array.
b=np.array(a)
print(b)
b.shape
[ 2 4 6 8 10 12]Out[11]:
(6,)output is (6,). The number which is in before the comma is no of rows. other number is no of columns that b numpy array have.
import numpy as np
x = np.array([
[67, 63, 87],
[77, 69, 59],
[85, 87, 99],
[79, 72, 71],
[63, 89, 93],
[68, 92, 78]
])
Get Nayomi Rashmika’s stories in your inbox
Join Medium for free to get updates from this writer.
np.shape(x)
(6, 3)no of rows=6 and no of columns=3. I think that can you understand now. If you want to check the internal data type of this numpy array you can use the “dtype”. dtype is one of the attribute that numpy arrays have. It use to check internal data type of the numpy arrays. numpy has different data types like int16, int32, int64, float, float32 etc. Those data type belongs to numpy not the Python.
import numpy as np
x = np.array([ [67, 63, 87],[77, 69, 59],[85, 87, 99],[79, 72, 71],[63, 89, 93],[68, 92, 78]])
x.shape
x.dtype
dtype('int32')How to get this ‘int32’. Its depend on size of value and it gets by default. If you take float values like below,
import numpy as np
x = np.array([
[67.4, 63, 87],
[77.2, 69, 59],
[85, 87, 99.3],
[79, 72, 71.5],
[63.2, 89, 93],
[68, 92, 78]
])
x.dtype
dtype('float64')There are different type of arrays single dimensional two dimensional and multi dimensional etc. dimension of an array can be defined by using ndim argument.
import numpy as np
x = np.array([67, 63, 87],ndmin=2)
x
array([[67, 63, 87]])we can check the dimension of an array by using ndim.
import numpy as np
x = np.array([67, 63, 87],ndmin=2)
x.ndim
2and also we can define data type of the internal values of an array.
import numpy as np
x = np.array([67, 63, 87],dtype=complex)
x
array([67.+0.j, 63.+0.j, 87.+0.j])If we take data in the one list its single dimensional If we take data in two list its two dimensional.
import numpy as np
x = np.array([67, 63, 87])
print(‘dimension of x= ‘,x.ndim)
y = np.array([[67, 63, 87]])
print(‘dimension of y= ‘,y.ndim)
dimension of x= 1
dimension of y= 2You can also change the shape of an array if you want.
import numpy as np
x = np.array([67, 63, 87,54])
print(‘before change ‘,x.shape)
x.shape=(2,2)
print(‘after change ‘,x.shape)
before change (4,)
after change (2, 2)You can check and practice above codes by using jupyter notebook. If you have any problems with Numpy contact me here. I hope you got clear idea about Numpy with Python Programming. So this is the end of this article. See you soon with next article . Happy Coding😊!!!









