Python NumPy module – 4 Important Type of Functions to Know


Hey, folks! Hope you all are doing well. In this article, we will be focusing on the important functions of the Python NumPy module.

So, let us get started!

A Brief about the Python NumPy Module

Python NumPy module ensembles a variety of functions to perform different scientific and mathematical operations at an ease.

Thus, the NumPy module can be considered as a module that all the programmers can have at handy to perform all the mathematical and complex calculation tasks.

Now, let us understand and implement some of the important functions of NumPy module.

1. NumPy Array Manipulation functions

The Array manipulation functions of NumPy module helps us to perform changes in the array elements.

Have a look at the below functions–

  • numpy.reshape(): This function allows us to change the dimensions of the array without hampering the array values.
  • numpy.concatenate(): Joins two arrays of the same shapes either in a row-wise or a column-wise manner.

Let us now focus on the implementation of the above functions.


import numpy

arr1 = numpy.arange(4)
print('Elements of an array1:\n',arr1)

arr2 = numpy.arange(4,8)
print('Elements of an array2:\n',arr2)

res1 = arr1.reshape(2,2)
print('Reshaped array with 2x2 dimensions:\n',res1)

res2 = arr2.reshape(2,2)
print('Reshaped array with 2x2 dimensions:\n',res2)

print("Concatenation two arrays:\n")
concat = numpy.concatenate((arr1,arr2),axis=1)

It is important to understand the shape i.e. the dimensions of the arrays need to be same to execute the above functions.


Elements of an array1:
 [0 1 2 3]
Elements of an array2:
 [4 5 6 7]
Reshaped array with 2x2 dimensions:
 [[0 1]
 [2 3]]
Reshaped array with 2x2 dimensions:
 [[4 5]
 [6 7]]
Concatenation two arrays:

[0 1 2 3 4 5 6 7]

2. NumPy String functions

With NumPy String functions, we can manipulate the string values contained in an array. Some of the most frequently used String functions are mentioned below:

  • numpy.char.add() function: Concatenates data values of two arrays, merges them and represents a new array as a result.
  • numpy.char.capitalize() function: It capitalizes the first character of the entire word/string.
  • numpy.char.lower() function: Converts the case of the string characters to lower string.
  • numpy.char.upper() function: Converts the case of the string characters to upper string.
  • numpy.char.replace() function: Replaces a string or a portion of string with another string value.


import numpy
res =  numpy.char.add(['Python'],[' JournalDev'])

print("Concatenating two strings:\n",res)

print("Capitalizing the string: ",numpy.char.capitalize('python data'))

print("Converting to lower case: ",numpy.char.lower('PYTHON'))

print("Converting to UPPER case: ",numpy.char.upper('python'))

print("Replacing string within a string: ",numpy.char.replace ('Python Tutorials with AA', 'AA', 'JournalDev'))


Concatenating two strings:
 ['Python JournalDev']
Capitalizing the string:  Python data
Converting to lower case:  python
Converting to UPPER case:  PYTHON
Replacing string within a string:  Python Tutorials with JournalDev

3. NumPy Arithmetic functions

The below mentioned NumPy functions are used to perform the basic arithmetic operations on the data values of an array–

  • numpy.add() function : It adds two arrays and returns the result.
  • numpy.subtract() function : Subtracts elements of array2 from array1 and returns the result.
  • numpy.multiply() function : Multiplies the elements of both the arrays and returns the product.
  • numpy.divide() function : Divides the array1 by array2 and returns the quotient of array values.
  • numpy.mod() function: Performs modulus operation and returns the remainder array.
  • numpy.power() function: Returns the exponential value of array1 ^ array2.


import numpy as np 
x = np.arange(4) 
print("Elements of array 'x':\n",x)

y = np.arange(4,8) 
print("Elements of array 'y':\n",y)

add = np.add(x,y)
print("Addition of x and y:\n",add)

subtract = np.subtract(x,y)
print("Subtraction of x and y:\n",subtract)

mul = np.multiply(x,y)
print("Multiplication of x and y:\n",mul)

div = np.divide(x,y)
print("Division of x and y:\n",div)

mod = np.mod(x,y)
print("Remainder array of x and y:\n",mod)

pwr = np.power(x,y)
print("Power value of x^y:\n",pwr)


Elements of array 'x':
 [0 1 2 3]
Elements of array 'y':
 [4 5 6 7]
Addition of x and y:
 [ 4  6  8 10]
Subtraction of x and y:
 [-4 -4 -4 -4]
Multiplication of x and y:
 [ 0  5 12 21]
Division of x and y:
 [ 0.          0.2         0.33333333  0.42857143]
Remainder array of x and y:
 [0 1 2 3]
Power value of x^y:
 [   0    1   64 2187]

4. NumPy Statistical functions

NumPy Statistical functions are very helpful in the domain of data mining and analysis of the huge amount of traits in the data.

Let us have a look at some of the popularly used functions

  • numpy.median() : Calculates the median value of the passed array.
  • numpy.mean() : Returns the mean of the data values of the array.
  • numpy.average() : It returns the average of all the data values of the passed array.
  • numpy.std() : Calculates and returns the standard deviation of the data values of the array.


import numpy as np 
x = np.array([10,20,30,4,50,60]) 

med = np.median(x)
print("Median value of array: \n",med)

mean = np.mean(x)
print("Mean value of array: \n",mean)

avg = np.average(x)
print("Average value of array: \n",avg)

std = np.std(x)
print("Standard deviation value of array: \n",std)


Median value of array: 
Mean value of array: 
Average value of array: 
Standard deviation value of array: 


By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

Till then, Happy Learning!!


  • Python NumPy Module — JournalDev