6

Add a value to each element of an array in Python

 2 years ago
source link: https://thispointer.com/add-a-value-to-each-element-of-an-array-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Add a value to each element of an array in Python

In this article, we will learn how to add a number to each element of a NumPy Array in Python.

Given a NumPy Array, we need to Add number to each element of NumPy Array.

Example:
Given array = [1, 2, 3, 4, 5]
After adding 10 to each value of array: [11 12 13 14 15]
Example:             

Given array = [1, 2, 3, 4, 5]
After adding 10 to each value of array: [11 12 13 14 15]

There are multiple ways to add number to each element of NumPy Array. Let’s discuss all the methods one by one with a proper approach and a working code example.

Adding the number directly to the array using + operator

If we directly add a number to the NumPy Array object, then it will be added to each element of the array. Detailed steps are as follows,

  • Import numpy library and create a numpy array
  • Now add the number to array using the plus operator.
  • This will return a new array contains the elements from original numpy array and given number added to each of them.
  • Print the array

Source code

Advertisements

vid5e6258f9da92c874459691.jpg?cbuster=1600267117
00:00/11:46
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY1MTU5NDU2NlZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDM1MxQmMDMmNUYmMTM5N0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR2Vwn28yMwxyMwBDnHJioWUyMxY3Nl4jLwM4NwUhMTIjJTIjU2FzYXJcJTJGNTM3LwM2JzNmqXVcZD02MwpkNTU0Nwt3MDEjJzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NTE1OTQ1Nwt3NwEzqWyxPVNyn2yhZG9TUGkurWVlNwI3MTU1NDp3N2FyYSZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZuZGQgYS12YWk1ZS10ol1yYWNbLWVfZW1yoaQgo2YgYW4gYXJlYXxgnW4gpHy0nG9hJTJGJzZfo2F0U3RuqHVmPWZuoHNyJzVcZHNjPXBlZWJcZA==
import numpy as np
# creating numpy array
arr = np.array([1, 2, 3, 4, 5])
# printing the original array
print(" The original array := " , arr)
# add 10 to every element of NumPy array
arr=arr + 10
# printing the array after adding given number
print(" The array after adding given number := " , arr)
import numpy as np

# creating  numpy array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The original array := " , arr)

# add 10 to every element of NumPy array 
arr=arr + 10

# printing the array after adding given number
print(" The array after adding given number := " , arr)

OUTPUT:

The original array := [1 2 3 4 5]
The array after adding given number := [11 12 13 14 15]
The original array :=  [1 2 3 4 5]
The array after adding given number :=  [11 12 13 14 15]

Add a value to each element of an array using vectorized function

The numpy module has a vectorize class. The vectorize class takes a python function as argument and returns a vectorized function. This vectorized function accepts a numpy array as argument and applies that initial function on each element of the array. Then returns a numpy array containing the values returned by the applied function.

Syntax of vectorize

numpy.vectorize(pyfunc)
numpy.vectorize(pyfunc)

Parameters:

pyfunc = Python function or method.
pyfunc          = Python function or method.

Return:

Returns a vectorized function.
Returns a vectorized function.    

Approach

  • Import NumPy Library and create a NumPy Array.
  • Create a function that takes a number as parameter and returns it after adding 10 in it.
  • Pass this function to the vectorize class, It returns a vectorized function
  • Pass the NumPy Array to the vectorized function, It will return an array with each elements added with the given number.
  • Print the array

Source code

import numpy as np
def add(num):
return num + 10
# Creating a NumPy Array
arr = np.array([1, 2, 3, 4, 5])
# Printing the original array
print(" The original array := " , arr)
# add 10 to every element of NumPy array
addTen = np.vectorize(add)
arr = addTen(arr)
# printing the array after adding given number
print(" The array after adding given number := " , arr)
import numpy as np

def add(num):
    return num + 10

# Creating a NumPy Array
arr = np.array([1, 2, 3, 4, 5])

# Printing the original array
print(" The original array := " , arr)

# add 10 to every element of NumPy array 
addTen = np.vectorize(add)
arr = addTen(arr)

# printing the array after adding given number
print(" The array after adding given number := " , arr)

OUTPUT:

The original array := [1 2 3 4 5]
The array after adding given number := [11 12 13 14 15]
 The original array :=  [1 2 3 4 5]
 The array after adding given number :=  [11 12 13 14 15]

Add a value to each element of an array using map()

The python map() function will take an iterator and a function as input and returns the iterator by applying the function to the each element of the iterator;

Syntax of map()

map(function, iterator)
map(function, iterator)

Parameters:

function = Python function or method.
iterator = List, set, tuple.
function          = Python function or method.
iterator          = List, set, tuple.

Returns:

Returns an iterator.
Returns an iterator.    

Approach

  • Import numpy library and create a numpy array
  • Create a function to add a number to the functional parameter.
  • Pass this function and the array to the map, It will return an list by applying function to each element of iterator
  • Convert it into array and print it.

Source code

import numpy as np
def add(num):
return num+10
# creating numpy array
arr = np.array([1, 2, 3, 4, 5])
# printing the original array
print(" The original array : " , arr)
# add 10 to every element of NumPy array
arr = np.array(list(map(add, arr)))
# printing the array after adding given number
print(" The array after adding given number : " , arr)
import numpy as np

def add(num):
    return num+10

# creating  numpy array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The original array : " , arr)

# add 10 to every element of NumPy array 
arr = np.array(list(map(add, arr)))

# printing the array after adding given number
print(" The array after adding given number : " , arr)

OUTPUT:

The original array : [1 2 3 4 5]
The array after adding given number : [11 12 13 14 15]
The original array :  [1 2 3 4 5]
The array after adding given number :  [11 12 13 14 15]

Add a value to each element of an array using for Loop

Use the for loop to iterate over the array and add the given number to each element of the numpy array.

Approach

  • Import numpy library and create a numpy array
  • Using a for loop and range() method iterate the array.
  • Add the given number to the each element
  • Print the array

Source code

import numpy as np
def add(num):
return num+10
# creating numpy array
arr = np.array([1, 2, 3, 4, 5])
# printing the original array
print(" The original array := " , arr)
# add 10 to every element of NumPy array
for i in range(0,len(arr)):
arr[i]=arr[i]+10
# printing the array after adding given number
print(" The array after adding given number := " , arr)
import numpy as np

def add(num):
    return num+10
# creating  numpy array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The original array := " , arr)

# add 10 to every element of NumPy array 
for i in range(0,len(arr)):
    arr[i]=arr[i]+10

# printing the array after adding given number
print(" The array after adding given number := " , arr)

OUTPUT:

The original array := [1 2 3 4 5]
The array after adding given number := [11 12 13 14 15]
 The original array :=  [1 2 3 4 5]
 The array after adding given number :=  [11 12 13 14 15]

Add a value to each element of an array using List Comprehension

Use the List Comprehension to iterate over the array and apply the add a value to each element of the numpy array.

Approach

  • Import numpy library and create a numpy array
  • Use List Comprehension to iterate over the array and apply the add function
  • It will return a list, Convert that list into a numpy array and print it.

Source code

import numpy as np
def add(num):
return num+10
# creating numpy array
arr = np.array([1, 2, 3, 4, 5])
# printing the original array
print(" The original array := " , arr)
# add 10 to every element of NumPy array
arr = np.array([add(num) for num in arr])
# printing the array after adding given number
print(" The array after adding given number := " , arr)
import numpy as np

def add(num):
    return num+10

# creating  numpy array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The original array := " , arr)

# add 10 to every element of NumPy array 
arr = np.array([add(num) for num in arr])

# printing the array after adding given number
print(" The array after adding given number := " , arr)

OUTPUT:

The original array := [1 2 3 4 5]
The array after adding given number := [11 12 13 14 15]
 The original array :=  [1 2 3 4 5]
 The array after adding given number :=  [11 12 13 14 15]

Summary

Great! you made it, We have discussed all possible methods to add a number to each element of NumPy Array in Python. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK