• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / NumPy / 10 Basic Arithmetic Operations with NumPy array

10 Basic Arithmetic Operations with NumPy array

December 9, 2018 by cmdlinetips

NumPy is the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object sophisticated (broadcasting) functions tools for integrating C/C++ and Fortran code useful linear algebra, Fourier transform, and random number capabilities
Basic Aritmetic Operations with NumPy

NumPy is one of most fundamental Python packages for doing any scientific computing in Python. NumPy’s N-dimenisonal array structure offers fantastic tools to numerical computing with Python.

Let us see 10 most basic arithmetic operations with NumPy that will help greatly with Data Science skills in Python.

Let us first load the NumPy library

# import NumPy
import numpy as np

Let us create two NumPy arrays using NumPy’s random module. We will use random.seed to reproduce the same random numbers in the two arrays.

# set seed for random numbers
np.random.seed(42)
# create arrays using NumPy's random module
a = np.random.randint(1,3,5)
b = np.random.randint(0,10,5)

We have two numpy arrays a and b and we will use them in our examples below.

>print(a) 
[1 2 1 1 1]
>print(b)
[7 4 6 9 2]

1. How to subtract two arrays?

np.subtract(b,a)
array([-2,  2,  2, -2,  3])

2. How to add two arrays?

np.add(b,a)
array([16,  6, 14, 12, 11])

3. How to divide two arrays?

np.divide(a,b)
array([0.14285714, 0.5, 0.16666667, 0.11111111, 0.5])

4. How to multiply two arrays?

np.multiply(a,b)
array([7, 8, 6, 9, 2])

5. How To Compute Exponent of an array?

np.exp(a)
array([2.71828183, 7.3890561 , 2.71828183, 2.71828183, 2.71828183])

6. How to Compute Square Root of an array?

np.sqrt(a)
array([2., 4., 2., 2., 2.])

7. How to Compute Sine/Cosine?

np.sin(a)
array([0.84147098, 0.90929743, 0.84147098, 0.84147098, 0.84147098])

8. How to Take Logarithm?

np.log(a)  
array([0., 0.69314718, 0., 0., 0.])
np.log2(a)  
array([0., 1., 0., 0., 0.])

9. How to Take Dot Product?

a.dot(b)
32

10. How to Round an Array?

np.random.seed(42)
a = np.random.rand(5)
print(a)
[0.37454012 0.95071431 0.73199394 0.59865848 0.15601864]
np.around(a)
array([0., 1., 1., 1., 0.])

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default Thumbnail12 Basic Commands with NumPy Array Default ThumbnailHow To Concatenate Arrays in NumPy? Default Thumbnail9 Basic Linear Algebra Operations with NumPy Default Thumbnail3 Basic Commands to Manipulate NumPy 2d-arrays

Filed Under: NumPy, NumPy Basics, Python Tips Tagged With: Basic NumPy, NumPy Arithmetics, NumPy Tutorial

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version