Python NumPy

Python NumPy Tutorial

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.

NumPy is the foundation for many popular libraries in the Python ecosystem, including pandas, which is a high-performance data manipulation and analysis library. Pandas builds upon the capabilities of NumPy, offering additional data structures and functionalities specifically designed for data handling and manipulation tasks.

With NumPy, you can create and manipulate arrays of homogeneous data types, such as integers or floating-point numbers. These arrays, called NumPy arrays or ndarrays (n-dimensional arrays), are highly efficient in terms of memory consumption and execution speed. They provide a convenient way to store and manipulate large amounts of data, making it ideal for numerical computations, data analysis, and scientific simulations.

NumPy offers a wide range of mathematical functions and operations that can be applied element-wise to arrays, allowing for fast and vectorized computations. These operations include arithmetic operations (addition, subtraction, multiplication, division, etc.), trigonometric functions, statistical operations, linear algebra routines, and more. NumPy’s ability to perform these operations efficiently on arrays makes it a powerful tool for data manipulation and analysis.

Python Numpy features

  1. Multi-dimensional array objects: NumPy provides the `ndarray` object, which allows you to store and manipulate multi-dimensional arrays efficiently. These arrays can have any number of dimensions and contain elements of the same data type, such as integers or floating-point numbers.
  2. Fast mathematical operations: NumPy provides a comprehensive collection of mathematical functions that operate element-wise on arrays. These functions are implemented in highly optimized C code, resulting in faster execution compared to traditional Python loops.
  3. Broadcasting: NumPy’s broadcasting feature enables arithmetic operations between arrays of different shapes and sizes. It automatically applies operations on arrays with compatible shapes, eliminating the need for explicit looping or resizing of arrays.
  4. Array indexing and slicing: NumPy offers powerful indexing and slicing capabilities for accessing and modifying specific elements or sub-arrays within an array. This allows for efficient extraction of data and manipulation of array elements based on specific conditions or criteria.
  5. Linear algebra operations: NumPy provides a comprehensive set of linear algebra functions, including matrix multiplication, matrix decomposition, solving linear equations, computing determinants, eigenvalues, and more. These operations are crucial for tasks involving linear algebra, such as solving systems of equations, performing matrix operations, and analyzing networks.
  6. Random number generation: NumPy includes a robust random number generator that allows you to generate random values from various distributions. This is particularly useful for simulations, statistical analysis, and generating random samples for testing and experimentation.
  7. Integration with other libraries: NumPy seamlessly integrates with other popular libraries in the scientific Python ecosystem, such as pandas, SciPy, Matplotlib, and scikit-learn. This interoperability enables a comprehensive toolset for data analysis, scientific computing, machine learning, and visualization.
  8. Memory efficiency: NumPy arrays are more memory-efficient compared to Python lists. They store data in a contiguous block of memory, allowing for faster access and reducing memory overhead.
  9. Performance optimizations: NumPy is implemented in highly optimized C code, making it significantly faster than equivalent Python code. It leverages vectorized operations and efficient memory management techniques to achieve high-performance computations.
  10. Open-source and community-driven: NumPy is an open-source project with an active and supportive community. This ensures continuous development, bug fixes, and the availability of extensive documentation, tutorials, and resources for learning and troubleshooting.

Advantages of Python Numpy library

  1. Efficient numerical computations: NumPy is highly optimized for numerical computations and offers efficient data structures like arrays and matrices. Its underlying C implementation allows for fast execution of mathematical operations, making it suitable for handling large datasets and performing complex calculations.
  2. Vectorized operations: NumPy enables vectorized operations, which means you can perform operations on entire arrays or matrices at once, without the need for explicit loops. This leads to concise and efficient code, reducing the execution time and enhancing performance.
  3. Memory efficiency: NumPy arrays are more memory-efficient compared to Python lists. They provide a compact way to store large amounts of data, resulting in reduced memory consumption. Additionally, NumPy’s memory management techniques allow for efficient handling of data, optimizing the performance of computations.
  4. Broadcasting: NumPy’s broadcasting feature allows arrays with different shapes to interact seamlessly in arithmetic operations. This eliminates the need for explicit array reshaping or looping, simplifying code and enhancing readability.
  5. Interoperability with other libraries: NumPy seamlessly integrates with other popular Python libraries used in scientific computing and data analysis, such as pandas, SciPy, Matplotlib, and scikit-learn. This interoperability enables a comprehensive toolset for data manipulation, analysis, visualization, and machine learning.
  6. Extensive mathematical functions: NumPy provides a vast collection of mathematical functions and operations for array manipulation, linear algebra, statistics, Fourier analysis, and more. These functions are implemented in optimized C code, ensuring fast and accurate computations.
  7. Random number generation: NumPy includes a robust random number generator that offers various probability distributions. This is useful for simulations, statistical analysis, and generating random data for testing and experimentation.
  8. Open-source and active community: NumPy is an open-source library with an active community of developers and users. This ensures continuous development, bug fixes, and the availability of extensive documentation, tutorials, and resources. The community support makes it easier to learn, troubleshoot, and stay updated with new features and improvements.
  9. Widely adopted in scientific and data analysis communities: NumPy is widely adopted by scientists, researchers, and data analysts for its reliability, performance, and extensive functionalities. Its popularity ensures a rich ecosystem of libraries and tools built on top of NumPy, further expanding its capabilities.

Disadvantages of Python Numpy library

  1. Learning curve: NumPy has a steep learning curve, especially for users who are new to scientific computing or data analysis. Understanding concepts like arrays, broadcasting, and vectorized operations may require some initial effort and familiarity with numerical computing principles.
  2. Fixed data types: NumPy arrays have a fixed data type for all elements. This can be restrictive when dealing with heterogeneous data or datasets that require different data types for different elements. In such cases, using a more flexible data structure like pandas may be more suitable.
  3. Memory consumption: While NumPy arrays are generally more memory-efficient than Python lists, they can still consume significant memory for large datasets. Storing multiple large arrays in memory simultaneously can pose memory limitations, particularly for systems with limited resources.
  4. Lack of built-in data manipulation capabilities: While NumPy provides efficient array manipulation and mathematical operations, it lacks some higher-level data manipulation functionalities available in libraries like pandas. Tasks such as data cleaning, merging, and handling missing values may require additional steps or the integration of other libraries.
  5. Limited support for structured data: NumPy is primarily focused on numerical computations and works best with homogeneous numerical data. It doesn’t offer built-in support for handling structured data, such as data with different data types or named columns. For structured data analysis, pandas is generally a more appropriate choice.
  6. Slower execution for certain operations: While NumPy’s vectorized operations are generally faster than equivalent Python loops, there may be cases where certain operations or algorithms are more efficiently implemented using specialized libraries or frameworks. Depending on the specific task and requirements, alternative libraries might offer better performance.
  7. Inflexible array resizing: Modifying the size of a NumPy array after it’s created requires creating a new array with the desired dimensions and copying the data. This can be inefficient and time-consuming for large arrays or frequent resizing operations. In such cases, other data structures like dynamic arrays or linked lists may be more efficient.
  8. Limited support for non-numeric data: NumPy is primarily designed for numerical computations and lacks built-in support for non-numeric data types like strings or categorical variables. While it’s possible to represent non-numeric data using NumPy arrays, specialized libraries like pandas offer more convenient and efficient options for handling such data.
  9. Lack of advanced statistical functionalities: While NumPy provides basic statistical functions, it doesn’t offer the full range of advanced statistical techniques available in dedicated statistical libraries like SciPy or statsmodels. For complex statistical analysis, you may need to combine NumPy with these specialized libraries.
  10. Maintenance and updates: NumPy is an open-source project that relies on community contributions for maintenance and updates. While the community is active, the pace of updates and bug fixes may vary, and certain issues may take longer to resolve compared to commercially supported software.

Slicing and Indexing using Python NumPy library

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

Indexing:

  1. Single Element: You can access a single element of an array by specifying its index using square brackets.
				
					   import numpy as np   
   arr = np.array([1, 2, 3, 4, 5])
   print(arr[0])  # Output: 1

				
			
  1. Multiple Elements: You can access multiple elements of an array by passing a list or an array of indices inside the square brackets.
				
					   import numpy as np
   arr = np.array([1, 2, 3, 4, 5])
   indices = [0, 2, 4]
   print(arr[indices])  # Output: [1 3 5]

				
			

Slicing:

  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.
				
					   import numpy as np   
   arr = np.array([1, 2, 3, 4, 5])
   sliced_arr = arr[1:4]  # Elements from index 1 to 3 (exclusive)
   print(sliced_arr)  # Output: [2 3 4]

				
			
  1. Step Slicing: You can specify a step value to slice every nth element from the array.
				
					   import numpy as np   
   arr = np.array([1, 2, 3, 4, 5])
   sliced_arr = arr[::2]  # Elements with a step of 2
   print(sliced_arr)  # Output: [1 3 5]

				
			
  1. Negative Indices: Negative indices allow you to slice from the end of the array.
				
					   import numpy as np   
   arr = np.array([1, 2, 3, 4, 5])
   sliced_arr = arr[-3:-1]  # Elements from the third-last to the second-last
   print(sliced_arr)  # Output: [3 4]

				
			
  1. Slicing Multi-dimensional Arrays: You can slice multi-dimensional arrays using multiple indexing and slicing operations.
				
					   import numpy as np   
   arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
   sliced_arr = arr[1:, :2]  # Rows from index 1 onwards, columns up to index 1 (exclusive)
   print(sliced_arr)
   # Output:
   # [[4 5]
   #  [7 8]]

				
			

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)
				
					   # Example::
   import numpy as np   
   # Create a NumPy array from a Python list
   my_list = [1, 2, 3, 4, 5]
   my_array = np.array(my_list)
   print(my_array)  # Output: [1 2 3 4 5]

				
			
  1. np.arange():Create an array with evenly spaced values. Syntax: np.arange([start,] stop[, step,], dtype=None)
				
					   # Example::
   import numpy as np   
   # Create an array with values from 0 to 9
   my_array = np.arange(10)
   print(my_array)  # Output: [0 1 2 3 4 5 6 7 8 9]

				
			
  1. np.zeros(): Create an array filled with zeros. Syntax: np.zeros(shape, dtype=float, order=’C’)
				
					   # Example::
   import numpy as np   
   # Create a 1D array filled with zeros
   my_array = np.zeros(5)
   print(my_array)  # Output: [0. 0. 0. 0. 0.]

				
			
  1. np.ones(): Create an array filled with ones. Syntax: np.ones(shape, dtype=None, order=’C’)
				
					   # Example::
   import numpy as np   
   # Create a 2D array filled with ones
   my_array = np.ones((3, 2))
   print(my_array)
   # Output:
   # [[1. 1.]
   #  [1. 1.]
   #  [1. 1.]]

				
			
  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)
				
					   # Example::
   import numpy as np   
   # Create an array with 5 evenly spaced values between 0 and 1
   my_array = np.linspace(0, 1, 5)
   print(my_array)  # Output: [0.   0.25 0.5  0.75 1.  ]

				
			
  1. np.eye(): Create an identity matrix. Syntax: np.eye(N, M=None, k=0, dtype=float, order=’C’)
				
					   # Example:
   import numpy as np   
   # Create a 3x3 identity matrix
   my_array = np.eye(3)
   print(my_array)
   # Output:
   # [[1. 0. 0.]
   #  [0. 1. 0.]
   #  [0. 0. 1.]]

				
			
  1. np.random.rand():Generate random values from a uniform distribution. Syntax: np.random.rand(d0, d1, …, dn)
				
					   # Example::
   import numpy as np   
   # Generate a 2x2 array of random values from a uniform distribution
   my_array = np.random.rand(2, 2)
   print(my_array)
   # Output:
   # [[0.64822645 0.03382209]
   #  [0.45753694 0.67940323]]

				
			
  1. np.random.randn(): Generate random values from a standard normal distribution. Syntax: np.random.randn(d0, d1, …, dn)
				
					   # Example::
   import numpy as np   
   # Generate a 3x3 array of random values from a standard normal distribution
   my_array = np.random.randn(3, 3)
   print(my_array)
   # Output:
   # [[ 0.21372011 -0.76571721  0.54756781]
   #  [ 0.95714164 -0.12939294 -0.57725997]
   #  [-0.28264262 -0.45355784 -0.33564826]]

				
			
  1. np.random.randint(): Generate random integers within a specified range. Syntax: np.random.randint(low, high=None, size=None, dtype=int)
				
					   # Example::
   import numpy as np   
   # Generate a 1D array of 5 random integers between 0 and 9
   my_array = np.random.randint(0, 10, 5)
   print(my_array)  # Output: [4 7 1 2 9]

				
			

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

				
					    # Example::
    import numpy as np    
    # Get the dimensions of an array
    my_array = np.array([[1, 2, 3], [4, 5, 6]])
    shape = np.shape(my_array)
    print(shape)  # Output: (2, 3)

				
			
  1. np.reshape():Reshape an array to a specified shape. Syntax: np.reshape(array, newshape, order=’C’)
				
					    # Example::
    import numpy as np    
    # Reshape a 1D array into a 2D array
    my_array = np.array([1, 2, 3, 4, 5, 6])
    reshaped_array = np.reshape(my_array, (2, 3))
    print(reshaped_array)
    # Output:
    # [[1 2 3]
    #  [4 5 6]]

				
			
  1. np.concatenate():Join arrays along a specified axis. Syntax: np.concatenate((array1, array2, …), axis=0)
				
					    # Example::
    import numpy as np    
    # Concatenate two arrays along the first axis
    array1 = np.array([[1, 2], [3, 4]])
    array2 = np.array([[5, 6]])
    concatenated_array = np.concatenate((array1, array2), axis=0)
    print(concatenated_array)
    # Output:
    # [[1 2]
    #  [3 4]
    #  [5 6]]

				
			
  1. np.split():Split an array into multiple sub-arrays. Syntax: np.split(array, indices_or_sections, axis=0)
				
					    # Example::
    import numpy as np   
    # Split an array into three sub-arrays
    my_array = np.array([1, 2, 3, 4, 5, 6])
    split_array = np.split(my_array, 3)
    print(split_array)
    # Output: [array([1, 2]), array([3, 4]), array([5, 6])]

				
			
  1. np.max():Find the maximum value in an array. Syntax: np.max(array, axis=None, out=None, keepdims=False, initial=None)
				
					    # Example::
    import numpy as np    
    # Find the maximum value in an array
    my_array = np.array([1, 5, 3, 9, 2])
    max_value = np.max(my_array)
    print(max_value)  # Output: 9

				
			
  1. np.min():Find the minimum value in an array. Syntax: np.min(array, axis=None, out=None, keepdims=False, initial=None)
				
					    # Example::
    import numpy as np    
    # Find the minimum value in an array
    my_array = np.array([1, 5, 3, 9, 2])
    min_value = np.min(my_array)
    print(min_value)  # Output: 1

				
			
  1. np.mean():Compute the arithmetic mean of an array. Syntax: np.mean(array, axis=None, dtype=None, out=None, keepdims=False)
				
					    # Example::
    import numpy as np    
    # Compute the mean of an array
    my_array = np.array([1, 2, 3, 4, 5])
    mean_value = np.mean(my_array)
    print(mean_value)  # Output: 3.0

				
			
  1. np.median():Compute the median of an array. Syntax: np.median(array, axis=None, out=None, overwrite_input=False)
				
					    # Example::
    import numpy as np    
    # Compute the median of an array
    my_array = np.array([1, 3, 2, 4, 5])
    median_value = np.median(my_array)
    print(median_value)  # Output: 3.0

				
			
  1. np.std():Compute the standard deviation of an array. Syntax: np.std(array, axis=None, dtype=None, out=None, ddof=0, keepdims=False)
				
					    # Example::
    import numpy as np    
    # Compute the standard deviation of an array
    my_array = np.array([1, 2, 3, 4, 5])
    std_value = np.std(my_array)
    print(std_value)  # Output: 1.4142135623730951

				
			
  1. np.sum():Compute the sum of array elements. Syntax: np.sum(array, axis=None, dtype=None, out=None, keepdims=False, initial=0)
				
					    # Example::
    import numpy as np    
    # Compute the sum of array elements
    my_array = np.array([1, 2, 3, 4, 5])
    sum_value = np.sum(my_array)
    print(sum_value)  # Output: 15

				
			
  1. np.abs():Compute the absolute values of array elements. Syntax: np.abs(array)
				
					    # Example::
    import numpy as np    
    # Compute the absolute values of array elements
    my_array = np.array([-1, -2, 3, -4, 5])
    abs_values = np.abs(my_array)
    print(abs_values)  # Output: [1 2 3 4 5]

				
			
  1. np.exp():Compute the exponential of array elements. Syntax: np.exp(array)
				
					    # Example::
    import numpy as np    
    # Compute the exponential of array elements
    my_array = np.array([1, 2, 3])
    exp_values = np.exp(my_array)
    print(exp_values)  # Output: [ 2.71828183  7.3890561  20.08553692]

				
			
  1. np.log():Compute the natural logarithm of array elements. Syntax: np.log(array)
				
					    # Example::
    import numpy as np    
    # Compute the natural logarithm of array elements
    my_array = np.array([1, np.e, np.e**2])
    log_values = np.log(my_array)
    print(log_values)  # Output: [0. 1. 2.]

				
			
  1. np.sin():Compute the sine of array elements. Syntax: np.sin(array)
				
					    # Example::
    import numpy as np   
    # Compute the sine of array elements
    my_array = np.array([0, np.pi/2, np.pi])
    sin_values = np.sin(my_array)
    print(sin_values)  # Output: [0.0000000e+00 1.0000000e+00 1.2246468e-16]

				
			
  1. np.cos():Compute the cosine of array elements. Syntax: np.cos(array)
				
					    # Example::
    import numpy as np    
    # Compute the cosine of array elements
    my_array = np.array([0, np.pi/2, np.pi])
    cos_values = np.cos(my_array)
    print(cos_values)  # Output: [ 1.000000e+00  6.123234e-17 -1.000000e+00]

				
			
  1. np.tan():Compute the tangent of array elements. Syntax: np.tan(array)
				
					    # Example::
    import numpy as np    
    # Compute the tangent of array elements
    my_array = np.array([0, np.pi/4, np.pi/2])
    tan_values = np.tan(my_array)
    print(tan_values)  # Output: [0.00000000e+00 1.00000000e+00 1.63312394e+16]

				
			
  1. np.dot(): Compute the dot product of two arrays. Syntax: np.dot(a, b, out=None)
				
					    # Example::
    import numpy as np   
    # Compute the dot product of two arrays
    array1 = np.array([1, 2])
    array2 = np.array([3, 4])
    dot_product = np.dot(array1, array2)
    print(dot_product)  # Output: 11

				
			
  1. np.transpose():Transpose the dimensions of an array. Syntax: np.transpose(array, axes=None)
				
					    # Example::
    import numpy as np    
    # Transpose the dimensions of an array
    my_array = np.array([[1, 2], [3, 4]])
    transposed_array = np.transpose(my_array)
    print(transposed_array)
    # Output:
    # [[1 3]
    #  [2 4]]

				
			
  1. np.sort():Sort an array. Syntax: np.sort(array, axis=-1, kind=None, order=None)
				
					    # Example::
    import numpy as np    
    # Sort an array
    my_array = np.array([3, 1, 4, 2, 5])
    sorted_array = np.sort(my_array)
    print(sorted_array)  # Output: [1 2 3 4 5]

				
			
  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)
				
					    # Example::
    import numpy as np    
    # Find the unique elements of an array
    my_array = np.array([1, 2, 1, 3, 2, 4])
    unique_values = np.unique(my_array)
    print(unique_values)  # Output: [1 2 3 4]

				
			
  1. np.argmax():Find the indices of the maximum values in an array. Syntax: np.argmax(array, axis=None, out=None)
				
					    # Example::
    import numpy as np    
    # Find the indices of the maximum values in an array
    my_array = np.array([3, 1, 4, 2, 5])
    max_index = np.argmax(my_array)
    print(max_index)  # Output: 4

				
			
  1. np.argmin():Find the indices of the minimum values in an array. Syntax: np.argmin(array, axis=None, out=None)
				
					    # Example::
    import numpy as np    
    # Find the indices of the minimum values in an array
    my_array = np.array([3, 1, 4, 2, 5])
    min_index = np.argmin(my_array)
    print(min_index)  # Output: 1

				
			
  1. np.where():Return the indices of array elements that satisfy a condition. Syntax: np.where(condition, x, y)
				
					    # Example::
    import numpy as np    
    # Return the indices of array elements that satisfy a condition
    my_array = np.array([1, 2, 3, 4, 5])
    indices = np.where(my_array > 2)
    print(indices)  # Output: (array([2, 3, 4]),)

				
			
  1. np.any():Check if any element in an array satisfies a condition. Syntax: np.any(array, axis=None, out=None, keepdims=False)
				
					    # Example::
    import numpy as np    
    # Check if any element in an array satisfies a condition
    my_array = np.array([1, 2, 3, 4, 5])
    has_positive = np.any(my_array > 0)
    print(has_positive)  # Output: True

				
			
  1. np.all():Check if all elements in an array satisfy a condition. Syntax: np.all(array, axis=None, out=None, keepdims=False)
				
					    # Example::    
    import numpy as np    
    # Check if all elements in an array satisfy a condition
    my_array = np.array([1, 2, 3, 4, 5])
    all_positive = np.all(my_array > 0)
    print(all_positive)  # Output: True

				
			
  1. np.isnan():Check for NaN (Not a Number) values in an array. Syntax: np.isnan(array)
				
					    # Example::
    import numpy as np    
    # Check for NaN (Not a Number) values in an array
    my_array = np.array([1, np.nan, 3, np.nan])
    has_nan = np.isnan(my_array)
    print(has_nan)  # Output: [False  True False  True]

				
			
  1. np.logical_and():Perform element-wise logical AND operation on arrays. Syntax: np.logical_and(array1, array2)
				
					    # Example::    
    import numpy as np    
    # Perform element-wise logical AND operation on arrays
    array1 = np.array([True, False, True, False])
    array2 = np.array([True, True, False, False])
    result = np.logical_and(array1, array2)
    print(result)  # Output: [ True False False False]

				
			
  1. np.logical_or():Perform element-wise logical OR operation on arrays. Syntax: np.logical_or(array1, array2)
				
					    # Example::    
    import numpy as np    
    # Perform element-wise logical OR operation on arrays
    array1 = np.array([True, False, True, False])
    array2 = np.array([True, True, False, False])
    result = np.logical_or(array1, array2)
    print(result)  # Output: [ True  True  True False]

				
			
  1. np.logical_not():Perform element-wise logical NOT operation on an array. Syntax: np.logical_not(array)
				
					    # Example::    
    import numpy as np    
    # Perform element-wise logical NOT operation on an array
    my_array = np.array([True, False, True, False])
    result = np.logical_not(my_array)
    print(result)  # Output: [False  True False  True]

				
			
  1. np.sinh():Compute the hyperbolic sine of array elements. Syntax: np.sinh(array)
				
					    # Example::    
    import numpy as np
    # Compute the hyperbolic sine of array elements
    my_array = np.array([0, 1, 2])
    sinh_values = np.sinh(my_array)
    print(sinh_values)  # Output: [ 0.   1.17520119  3.62686041]

				
			
  1. np.cosh():Compute the hyperbolic cosine of array elements. Syntax: np.cosh(array)
				
					    # Example::    
    import numpy as np   
    # Compute the hyperbolic cosine of array elements
    my_array = np.array([0, 1, 2])
    cosh_values = np.cosh(my_array)
    print(cosh_values)  # Output: [ 1.   1.54308063  3.76219569]

				
			
  1. np.tanh():Compute the hyperbolic tangent of array elements. Syntax: np.tanh(array)
				
					    # Example::    
    import numpy as np    
    # Compute the hyperbolic tangent of array elements
    my_array = np.array([0, 1, 2])
    tanh_values = np.tanh(my_array)
    print(tanh_values)  # Output: [0. 0.76159416 0.96402758]

				
			
  1. np.arcsin():Compute the inverse sine of array elements. Syntax: np.arcsin(array)
				
					    # Example::    
    import numpy as np    
    # Compute the inverse sine of array elements
    my_array = np.array([0, 0.5, 1])
    arcsin_values = np.arcsin(my_array)
    print(arcsin_values)  # Output: [0.    0.52359878 1.57079633]

				
			
  1. np.arccos():Compute the inverse cosine of array elements. Syntax: np.arccos(array)
				
					    # Example::    
    import numpy as np    
    # Compute the inverse cosine of array elements
    my_array = np.array([0, 0.5, 1])
    arccos_values = np.arccos(my_array)
    print(arccos_values)  # Output: [1.57079633 1.04719755 0.    ]

				
			
  1. np.arctan(): Compute the inverse tangent of array elements. Syntax: np.arctan(array)
				
					    # Example::    
    import numpy as np
    # Compute the inverse tangent of array elements
    my_array = np.array([0, 1, np.inf])
    arctan_values = np.arctan(my_array)
    print(arctan_values)  # Output: [0.    0.78539816 1.57079633]

				
			
  1. np.pi: A constant representing the value of pi (π). A constant representing the value of pi (π)
				
					    # Example::    
    import numpy as np
       # Use the constant pi
    radius = 1.0
    circumference = 2 * np.pi * radius
    print(circumference)  # Output: 6.283185307179586

				
			

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

				
					    # Example::   
    import numpy as np    
    # Use the constant e
    exponent = 1.0
    result = np.exp(exponent)
    print(result)  # Output: 2.718281828459045

				
			
  1. np.log10():Compute the base-10 logarithm of array elements. Syntax: np.log10(array)
				
					    # Example::    
    import numpy as np    
    # Compute the base-10 logarithm of array elements
    my_array = np.array([1, 10, 100])
    log10_values = np.log10(my_array)
    print(log10_values)  # Output: [0. 1. 2.]

				
			
  1. np.floor():Round down array elements to the nearest integer. Syntax: np.floor(array)
				
					    # Example::    
    import numpy as np    
    # Round down array elements to the nearest integer
    my_array = np.array([1.2, 2.7, 3.5])
    floor_values = np.floor(my_array)
    print(floor_values)  # Output: [1. 2. 3.]

				
			
  1. np.ceil():Round up array elements to the nearest integer. Syntax: np.ceil(array)
				
					    # Example::    
    import numpy as np   
    # Round up array elements to the nearest integer
    my_array = np.array([1.2, 2.7, 3.5])
    ceil_values = np.ceil(my_array)
    print(ceil_values)  # Output: [2. 3. 4.]

				
			
  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)
				
					    # Example::    
    import numpy as np    
    # Check if two arrays are element-wise approximately equal
    array1 = np.array([1.0, 2.0, 3.0])
    array2 = np.array([1.1, 2.2, 3.3])
    is_close = np.isclose(array1, array2, rtol=0.1, atol=0.1)
    print(is_close)  # Output: [ True  True  True]

				
			

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

				
					    # Example::
    import numpy as np    
    # Compute the cross-correlation of two arrays
    a = np.array([1, 2, 3, 4, 5])
    v = np.array([0, 1, 0.5])
    correlation = np.correlate(a, v, mode='valid')
    print(correlation)  # Output: [4.5 6.  8.5]

				
			
  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)
				
					    # Example::
    import numpy as np    
    # Compute the covariance matrix of an array
    data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    cov_matrix = np.cov(data, rowvar=False)
    print(cov_matrix)
    # Output:
    # [[6. 6. 6.]
    #  [6. 6. 6.]
    #  [6. 6. 6.]]

				
			

Conclusion

NumPy is a powerful Python library for scientific computing and data manipulation. It provides a wide range of functions and capabilities for working with arrays and matrices efficiently. Some of the key functions covered include array creation (np.array, np.arange, np.zeros, np.ones, np.linspace, np.eye), random number generation (np.random.rand, np.random.randn, np.random.randint), array manipulation (np.shape, np.reshape, np.concatenate, np.split), basic mathematical operations (np.max, np.min, np.mean, np.median, np.std, np.sum, np.abs, np.exp, np.log, np.sin, np.cos, np.tan), array operations (np.dot, np.transpose, np.sort, np.unique), logical operations (np.logical_and, np.logical_or, np.logical_not), trigonometric and hyperbolic functions (np.sinh, np.cosh, np.tanh, np.arcsin, np.arccos, np.arctan), constants (np.pi, np.e), and other useful functions (np.log10, np.floor, np.ceil, np.isclose, np.histogram, np.gradient, np.polyfit, np.polyval, np.correlate, np.cov, np.fft.fft, np.fft.ifft, np.loadtxt, np.savetxt).

These functions can be used to perform a wide range of tasks, including creating arrays, manipulating their shape and content, computing statistics and mathematical operations, handling missing values, performing data analysis and visualization, and working with Fourier transforms and linear algebra operations.

NumPy offers a comprehensive and efficient toolkit for numerical computing and is widely used in various fields such as data science, machine learning, scientific research, and engineering. It provides a foundation for many other libraries and frameworks in the Python ecosystem.

Leave a Comment