10X Sale
kh logo
All Courses
  1. Tutorials
  2. Data Science

Numpy

Updated on Aug 22, 2025
 
12,509 Views

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 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.

+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses