Python NumPy

Introduction

NumPy is a fundamental library in Python used for scientific computing and data analysis. It stands for “Numerical Python” and provides powerful tools for working with multi-dimensional arrays and matrices, along with a vast collection of mathematical functions to operate on these arrays efficiently.

Slicing and indexing are fundamental operations in NumPy that allow you to access and manipulate specific elements or subsets of an array.

  1. Single Element: You can access a single element of an array by specifying its index using square brackets.
  1. Multiple Elements: You can access multiple elements of an array by passing a list or an array of indices inside the square brackets.
  1. Basic Slicing: You can use slicing to extract a portion of an array by specifying the start and end indices, separated by a colon inside the square brackets.
  1. Step Slicing: You can specify a step value to slice every nth element from the array.
  1. Negative Indices: Negative indices allow you to slice from the end of the array.
  1. Slicing Multi-dimensional Arrays: You can slice multi-dimensional arrays using multiple indexing and slicing operations.

Python Numpy functions

  1. np.array(): Create a NumPy array from a Python list or tuple.
    Syntax: np.array(object, dtype=None, copy=True, order=’K’, subok=False, ndmin=0)
  1. np.arange(): Create an array with evenly spaced values.
    Syntax: np.arange([start,] stop[, step,], dtype=None)
  1. np.zeros(): Create an array filled with zeros.
    Syntax: np.zeros(shape, dtype=float, order=’C’)
  1. np.ones(): Create an array filled with ones.
    Syntax: np.ones(shape, dtype=None, order=’C’)
  1. np.linspace(): Create an array with a specified number of evenly spaced values.
    Syntax: np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
  1. np.eye(): Create an identity matrix.
    Syntax: np.eye(N, M=None, k=0, dtype=float, order=’C’)
  1. np.random.rand(): Generate random values from a uniform distribution.
    Syntax: np.random.rand(d0, d1, …, dn)
  1. np.random.randn(): Generate random values from a standard normal distribution.
    Syntax: np.random.randn(d0, d1, …, dn)
  1. np.random.randint(): Generate random integers within a specified range.
    Syntax: np.random.randint(low, high=None, size=None, dtype=int)

       10. np.shape():  Get the dimensions of an array. Syntax: np.shape(array)

  1. np.reshape(): Reshape an array to a specified shape.
    Syntax: np.reshape(array, newshape, order=’C’)
  1. np.concatenate(): Join arrays along a specified axis.
    Syntax: np.concatenate((array1, array2, …), axis=0)
  1. np.split(): Split an array into multiple sub-arrays.
    Syntax: np.split(array, indices_or_sections, axis=0)
  1. np.max(): Find the maximum value in an array.
    Syntax: np.max(array, axis=None, out=None, keepdims=False, initial=None)
  1. np.min(): Find the minimum value in an array.
    Syntax: np.min(array, axis=None, out=None, keepdims=False, initial=None)
  1. np.mean(): Compute the arithmetic mean of an array.
    Syntax: np.mean(array, axis=None, dtype=None, out=None, keepdims=False)
  1. np.median(): Compute the median of an array.
    Syntax: np.median(array, axis=None, out=None, overwrite_input=False)
  1. np.std(): Compute the standard deviation of an array.
    Syntax: np.std(array, axis=None, dtype=None, out=None, ddof=0, keepdims=False)
  1. np.sum(): Compute the sum of array elements.
    Syntax: np.sum(array, axis=None, dtype=None, out=None, keepdims=False, initial=0)
  1. np.abs():Compute the absolute values of array elements.
    Syntax: np.abs(array)
  1. np.exp(): Compute the exponential of array elements.
    Syntax: np.exp(array)
  1. np.log(): Compute the natural logarithm of array elements.
    Syntax: np.log(array)
  1. np.sin(): Compute the sine of array elements.
    Syntax: np.sin(array)
  1. np.cos(): Compute the cosine of array elements.
    Syntax: np.cos(array)
  1. np.tan(): Compute the tangent of array elements.
    Syntax: np.tan(array)
  1. np.dot(): Compute the dot product of two arrays.
    Syntax: np.dot(a, b, out=None)
  1. np.transpose(): Transpose the dimensions of an array.
    Syntax: np.transpose(array, axes=None)
  1. np.sort(): Sort an array.
    Syntax: np.sort(array, axis=-1, kind=None, order=None)
  1. np.unique(): Find the unique elements of an array.
    Syntax: np.unique(array, return_index=False, return_inverse=False, return_counts=False, axis=None)
  1. np.argmax(): Find the indices of the maximum values in an array.
    Syntax: np.argmax(array, axis=None, out=None)
  1. np.argmin(): Find the indices of the minimum values in an array.
    Syntax: np.argmin(array, axis=None, out=None)
  1. np.where(): Return the indices of array elements that satisfy a condition.
    Syntax: np.where(condition, x, y)
  1. np.any():Check if any element in an array satisfies a condition.
    Syntax: np.any(array, axis=None, out=None, keepdims=False)
  1. np.all(): Check if all elements in an array satisfy a condition.
    Syntax: np.all(array, axis=None, out=None, keepdims=False)
  1. np.isnan(): Check for NaN (Not a Number) values in an array.
    Syntax: np.isnan(array)
  1. np.logical_and(): Perform element-wise logical AND operation on arrays.
    Syntax: np.logical_and(array1, array2)
  1. np.logical_or(): Perform element-wise logical OR operation on arrays.
    Syntax: np.logical_or(array1, array2)
  1. np.logical_not(): Perform element-wise logical NOT operation on an array.
    Syntax: np.logical_not(array)
  1. np.sinh(): Compute the hyperbolic sine of array elements.
    Syntax: np.sinh(array)
  1. np.cosh(): Compute the hyperbolic cosine of array elements.
    Syntax: np.cosh(array)
  1. np.tanh(): Compute the hyperbolic tangent of array elements.
    Syntax: np.tanh(array)
  1. np.arcsin(): Compute the inverse sine of array elements.
    Syntax: np.arcsin(array)
  1. np.arccos(): Compute the inverse cosine of array elements.
    Syntax: np.arccos(array)
  1. np.arctan(): Compute the inverse tangent of array elements.
    Syntax: np.arctan(array)
  1. np.pi: A constant representing the value of pi (π). A constant representing the value of pi (π)

        46. np.e: A constant representing the value of Euler’s number (e). A constant representing the value of Euler’s number (e)

  1. np.log10(): Compute the base-10 logarithm of array elements.
    Syntax: np.log10(array)
  1. np.floor(): Round down array elements to the nearest integer.
    Syntax: np.floor(array)
  1. np.ceil(): Round up array elements to the nearest integer.
    Syntax: np.ceil(array)
  1. np.isclose(): Check if two arrays are element-wise approximately equal.
    Syntax: np.isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)

      51. np.correlate(): Compute the cross-correlation of two arrays.
Syntax: np.correlate(a, v, mode=’valid’)

  1. np.cov(): Compute the covariance matrix of an array.
    Syntax: np.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None)

Leave a Comment