top

Search

Machine Learning Tutorial

Numpy is a module in Python that refers to Numerical Python. It is an essential package when working with data that is fed as input to machine learning algorithms. It is considered to be a general-purpose package that is used for quick array computations. They contain array data structure, which has advantages in comparison to plain Python lists. How to install Numpy package? The numpy package can be installed using the ‘pip’ command. pip insall numpy How to import numpy package? The numpy package can be imported and given an alias name so that it is easy to reference the package. import numpy as np Numpy stores multidimensional array object that helps in efficient storage and computation of arrays and matrix data types. There are a wide number of mathematical functions which can be used to manipulate these arrays in a short span of time. Creating a numpy array import numpy as np my_list = [1,2,6,7,8,0] my_np_array = np.array(my_list) print(my_np_array) Output: [1 2 6 7 8 0] Creating a numpy array with ones The row and column size have to be passed as parameters to the ones function present in the numpy package. import numpy as np  my_np_array = np.ones((3,3))  print(my_np_array) Output: [[1. 1. 1.]  [1. 1. 1.]  [1. 1. 1.]] Creating an empty numpy array The empty function in numpy package can be used to create an empty numpy array. import numpy as np  my_np_array = np.empty((3,2))  print(my_np_array) Output: [[0.e+000 0.e+000]  [0.e+000 5.e-324]  [5.e-324 5.e-324]] Creating an array of zeroes, by specifying the type of the values The zeroes function can be used to create multi-dimensional array, as well as providing the data type of the values. import numpy as np  my_np_array = np.zeros((2,4,3),dtype=np.int16)  print(my_np_array) Output: [[[0 0 0]  [0 0 0]  [0 0 0]  [0 0 0]]  [[0 0 0]  [0 0 0]  [0 0 0]  [0 0 0]]] Inspecting numpy array The attributes of the numpy array needs to be known to the user so that it is easy for them to manipulate such an array. Different functions like shape, data, dtype, can be used to know more about the numpy array. The ‘ndim’ attribute gives the number of dimensions that the numpy array has. The ‘size; attribute gives the total number of elements in the numpy array, which is the product of the row and the column. The ‘flags’ attribute describes the memory layout of the numpy array. The ‘itemsize’ attribute gives the length of single array element in terms of bytes. The ‘nbytes’ gives the total number of bytes that is occupied by the numpy array’s elements Note: The np.eye function is used to create an identity matrix or a matrix that has horizontal row values of 1. import numpy as np  my_np_array = np.eye(3,3)  print(my_np_array)  print(my_np_array.ndim)  print(my_np_array.size)  print(my_np_array.flags)  print(my_np_array.itemsize)  print(my_np_array.nbytes) Output: [[1. 0. 0.]  [0. 1. 0.]  [0. 0. 1.]]  2  9  C_CONTIGUOUS : True  F_CONTIGUOUS : False  OWNDATA : True  WRITEABLE : True  ALIGNED : True  WRITEBACKIFCOPY : False  UPDATEIFCOPY : False  8 Conclusion In this post, we understood the significance of numpy package and we saw a few functions associated with the Numpy package. 
logo

Machine Learning Tutorial

Numpy

Numpy is a module in Python that refers to Numerical Python. It is an essential package when working with data that is fed as input to machine learning algorithms. It is considered to be a general-purpose package that is used for quick array computations. They contain array data structure, which has advantages in comparison to plain Python lists. 

How to install Numpy package? 

The numpy package can be installed using the ‘pip’ command. 

pip insall numpy 

How to import numpy package? 

The numpy package can be imported and given an alias name so that it is easy to reference the package. 

import numpy as np 

Numpy stores multidimensional array object that helps in efficient storage and computation of arrays and matrix data types. There are a wide number of mathematical functions which can be used to manipulate these arrays in a short span of time. 

Creating a numpy array 

import numpy as np 
my_list = [1,2,6,7,8,0] 
my_np_array = np.array(my_list) 
print(my_np_array) 

Output: 

[1 2 6 7 8 0] 

Creating a numpy array with ones 

The row and column size have to be passed as parameters to the ones function present in the numpy package. 

import numpy as np 
my_np_array = np.ones((3,3)) 
print(my_np_array) 

Output: 

[[1. 1. 1.] 
[1. 1. 1.] 
[1. 1. 1.]] 

Creating an empty numpy array 

The empty function in numpy package can be used to create an empty numpy array. 

import numpy as np 
my_np_array = np.empty((3,2)) 
print(my_np_array) 

Output: 

[[0.e+000 0.e+000] 
[0.e+000 5.e-324] 
[5.e-324 5.e-324]] 

Creating an array of zeroes, by specifying the type of the values 

The zeroes function can be used to create multi-dimensional array, as well as providing the data type of the values. 

import numpy as np 
my_np_array = np.zeros((2,4,3),dtype=np.int16) 
print(my_np_array) 

Output: 

[[[0 0 0] 
[0 0 0] 
[0 0 0] 
[0 0 0]] 
[[0 0 0] 
[0 0 0] 
[0 0 0] 
[0 0 0]]] 

Inspecting numpy array 

The attributes of the numpy array needs to be known to the user so that it is easy for them to manipulate such an array. Different functions like shape, data, dtype, can be used to know more about the numpy array. 

  • The ‘ndim’ attribute gives the number of dimensions that the numpy array has. 
  • The ‘size; attribute gives the total number of elements in the numpy array, which is the product of the row and the column. 
  • The ‘flags’ attribute describes the memory layout of the numpy array. 
  • The ‘itemsize’ attribute gives the length of single array element in terms of bytes. 
  • The ‘nbytes’ gives the total number of bytes that is occupied by the numpy array’s elements 

Note: The np.eye function is used to create an identity matrix or a matrix that has horizontal row values of 1. 

import numpy as np 
my_np_array = np.eye(3,3) 
print(my_np_array) 
print(my_np_array.ndim) 
print(my_np_array.size) 
print(my_np_array.flags) 
print(my_np_array.itemsize) 
print(my_np_array.nbytes) 

Output: 

[[1. 0. 0.] 
[0. 1. 0.] 
[0. 0. 1.]] 
2 
9 
C_CONTIGUOUS : True 
F_CONTIGUOUS : False 
OWNDATA : True 
WRITEABLE : True 
ALIGNED : True 
WRITEBACKIFCOPY : False 
UPDATEIFCOPY : False 
8 

Conclusion 

In this post, we understood the significance of numpy package and we saw a few functions associated with the Numpy package. 

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Vinu

After reading your article, I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article. Thanks for sharing.

Johnson M

Good and informative article.

Vinu

I enjoyed reading your articles. This is truly a great read for me. Keep up the good work!

Vinu

Awesome blog. I enjoyed reading this article. This is truly a great read for me. Keep up the good work!

best data science courses in India

Thanks for sharing this article!! Machine learning is a branch of artificial intelligence (AI) and computer science that focus on the uses of data and algorithms. I came to know a lot of information from this article.