Your go to Numpy checklist

Udbhav Pangotra
Geek Culture
Published in
4 min readJan 22, 2022

A quick glance at all the important numpy commands!

Photo by Bench Accounting on Unsplash

Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays.

Arrays

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.

import numpy as np

a = np.array([1242, 213, 312321])
# Create a rank 1 array
print(type(a))
# Prints "<class 'numpy.ndarray'>"
print(a.shape)
# Prints "(3,)"
print(a[0], a[1], a[2])
# Prints "1242 213 312321"
a[0] = 5
# Change an element of the array
print(a)
# Prints "[5, 213, 312321]"

b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array
print(b.shape) # Prints "(2, 3)"
print(b[0, 0], b[0, 1], b[1, 0]) # Prints "1 2 4"

Numpy also provides many functions to create arrays:

import numpy as np

a = np.zeros((2,2)) # Create an array of all zeros
print(a) # Prints "[[ 0. 0.]
[ 0. 0.]]"

b = np.ones((1,2)) # Create an array of all ones
print(b) # Prints "[[ 1. 1.]]"

c = np.full((2,2), 7) # Create a constant array
print(c) # Prints "[[ 7. 7.]
[ 7. 7.]]"

Array indexing

Numpy offers several ways to index into arrays.

Slicing: Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional, you must specify a slice for each dimension of the array:

# slice single item 
import numpy as np

a = np.arange(10)
b = a[5]
print b

NumPy package contains an iterator object numpy.nditer. It is an efficient multidimensional iterator object using which it is possible to iterate over an array. Each element of an array is visited using Python’s standard Iterator interface.

print 'Modified array is:'
for x in np.nditer(a):
print x
Output:
Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55

NumPy has quite a few useful statistical functions for finding minimum, maximum, percentile standard deviation and variance, etc. from the given elements in the array. The functions are explained as follows −

ptp(a[, axis, out, keepdims])
Range of values (maximum — minimum) along an axis.

percentile(a, q[, axis, out, ...])
Compute the q-th percentile of the data along the specified axis.

nanpercentile(a, q[, axis, out, ...])
Compute the qth percentile of the data along the specified axis, while ignoring nan values.

quantile(a, q[, axis, out, overwrite_input, ...])
Compute the q-th quantile of the data along the specified axis.

nanquantile(a, q[, axis, out, ...])
Compute the qth quantile of the data along the specified axis, while ignoring nan values

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.

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 | Mathematical Function

NumPy contains a large number of various mathematical operations. NumPy provides standard trigonometric functions, functions for arithmetic operations, handling complex numbers, etc.

numpy.sin(x[, out]) = ufunc ‘sin’) : This mathematical function helps user to calculate trignmetric sine for all x(being the array elements).

numpy.cos(x[, out]) = ufunc ‘cos’) : This mathematical function helps user to calculate trignmetric cosine for all x(being the array elements).

Bonus! np.clip

The function clipis used to keep the values of an array within an interval between a defined upper and lower range.

# Example-41: 1D array 
x = np.array([1,0,3,2,4,9,8])

print(x.clip(3, 5))
[3 3 3 3 4 5 5]

Do reach out and comment if you get stuck!

Other articles that might be interested in:

Cheers and do follow for more such content! :)

You can now buy me a coffee too if you liked the content!
samunderscore12 is creating data science content! (buymeacoffee.com)

--

--