• 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 / Basic Numpy / 12 Basic Commands with NumPy Array

12 Basic Commands with NumPy Array

January 14, 2018 by cmdlinetips



NumPy (pronounced as Num-pee or Num-pai) is one of the important python packages (other being SciPy) for scientific computing. NumPy offers fast and flexible data structures for multi-dimensional arrays and matrices with numerous mathematical functions/operations associated with it. Core data structure in NumPy is “ndarray”, short for n-dimesional array for storing numeric values. Let us get started with some basic commands with NumPy 1d-array (one-dimensional array).

How to import NumPy package?

# import numpy package with nickname "np"
>import numpy as np

How to create a one-dimensional array?: Numpy array()

We can create a NumPy array using the function array with a list of numbers as argument.

# create a numpy array
>my_first_array = np.array([1, 2, 3,4,5])
>my_first_array
array([1, 2, 3, 4, 5])

How to find the length (or number of elements) of a 1d-array?

We can find the number of elements in a 1d-array or the length of the array using the function len.

# length of the array
>len(my_first_array)
5

How to sum all the elements in 1d-array?: Numpy sum()

If we want to sum all the elements in a 1d numpy array using the function sum. This is way faster than a manually using a for loop going through all elements in a 1d-array.

# sum of all elements in the array
>np.sum(my_first_array)
>my_first_array.sum()
15

How to find the maximum value in NumPy 1d-array?: Numpy max()

We can find the maximum value stored in 1d-array using NumPy’s max function.

# maximum value the elements in the array
>np.max(my_first_array)
>my_first_array.max()
5

How to find the minimum value in NumPy 1d-array?: Numpy min()

Similarly, we can find the minimum value stored in 1d-array using NumPy’s min function.

# minimum value the elements in the array
>np.min(my_first_array)
>my_first_array.min()
1

How to create NumPy 1d-array with 1s?: Numpy ones()

Sometimes you may want to create a numpy array with 1s in all elements. NumPy’s ones function can create 1d-array with 1s. We need to specify the length of NumPy array as argument.

# create a numpy array of 1s (of length 5)
>np.ones(5)
array([ 1.,  1.,  1.,  1.,  1.])

How to create NumPy 1d-array with 0s?: Numpy zeros()

Similarly we can create 1d NumPy array with 0s in it using zeros function.

# create a numpy array of length 5 with 5 zeros
>np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])

How to create empty NumPy 1d-array of specified length?: Numpy empty()

Sometime you may want to create an empty array with no values in it. We can use NumPy’s empty function to create empty NumPy array.

# create an empty numpy array of length 5
>np.empty(5)
array([  1.49166815e-154,   1.49166815e-154,   3.03574399e-152,
         2.64522460e+185,   1.45913207e-152])

How to create NumPy 1d-array with a range of numbers?: Numpy arange()

# create a numby array with a range of numbers 0 to 9
>np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# create a numpy array with a range of numbers and step size
>np.arange(0, 9, 2)
array([0, 2, 4, 6, 8])

How to create NumPy 1d-array with random numbers?

# create an array with random numbers
>np.random.random(5)
array([ 0.4648749 ,  0.9236576 ,  0.93804724,  0.86871356,  0.49829188])

How to create NumPy 1d-array with equally spaced numbers in an interval?

# Return evenly spaced numbers over a specified interval. 
>np.linspace(0, 1, 10)
array([ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
        0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ])

Share this:

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

Related posts:

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 capabilities10 Basic Arithmetic Operations with NumPy array Default ThumbnailHow to Randomly permute Numpy Array Default Thumbnail3 Basic Commands to Manipulate NumPy 2d-arrays Default ThumbnailHow to generate random numbers from Poisson distribution with Numpy

Filed Under: Basic Numpy, NumPy, NumPy Basics, Python, Python Tips Tagged With: Basic NumPy, NumPy, Python Tips

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