• Reshaping And Flattening Multidimensional Arrays?

    Reshaping And Flattening Multidimensional Arrays?

    Reshaping And Flattening Multidimensional Arrays? Table Of Contents: arr.flatten() Examples Of arr.flatten() arr.ravel() Examples Of arr.ravel() (1) arr.flatten() Return a copy of the array collapsed into one dimension. Syntax: ndarray.flatten(order=’C’) Parameters: order: {‘C’, ‘F’, ‘A’, ‘K’}, optional – ‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran- style) order. ‘A’ means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. ‘K’ means to flatten a in the order the elements occur in memory. The default is ‘C’. Returns: y: ndarray – A copy of the input array, flattened to one dimension. (2) Examples Of

    Read More

  • How To Reverse An Numpy Array ?

    How To Reverse An Numpy Array ?

    How To Reverse An Numpy Array ? Table Of Contents: np.flip( ) Examples Of ‘np.flip( )’ (1) np.flip( ) Reverse the order of elements in an array along the given axis. The shape of the array is preserved, but the elements are reordered. Syntax: numpy.flip(m, axis=None) Parameters: m: array_like – Input array. axis: None or int or tuple of ints, optional – Axis or axes along which to flip over. The default, axis=None, will flip over all of the axes of the input array. If axis is negative it counts from the last to the first axis.If axis is a

    Read More

  • Transposing And Reshaping A Matrix

    Transposing And Reshaping A Matrix

    Transposing And Reshaping A Matrix Table Of Contents: arr.reshape(), arr.transpose(), arr.T (1) arr.reshape() Syntax: numpy.reshape(a, newshape, order=’C’) Parameters: a:array_like – Array to be reshaped. newshape: int or tuple of ints – The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions. order{‘C’, ‘F’, ‘A’}, optional –  Read the elements of a using this index order, and place the elements into the reshaped array using this index

    Read More

  • How To Get Unique Items And Counts In Numpy ?

    How To Get Unique Items And Counts In Numpy ?

    How To Get Unique Items And Counts In Numpy ? Table Of Contents: np.unique() Examples Of ‘np.unique( )’ (1) np.unique() Returns the sorted unique elements of an array. You can also get the unique element counts. Syntax: numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None, *, equal_nan=True) Parameters: ar: array_like – Input array. Unless axis is specified, this will be flattened if it is not already 1-D. return_index: bool, optional – If True, also return the indices of ar (along the specified axis, if provided, or in the flattened array) that result in the unique array. return_inverse: bool, optional – If True, also return the indices of

    Read More

  • How To Generate Random Numbers Using NumPy ?

    How To Generate Random Numbers Using NumPy ?

    How To Generate Random Numbers Using NumPy ? Table Of Contents: What Are Random Numbers? Examples Of Random Number Generators. (1) What Are Random Numbers? As the name suggests, a random number is a number chosen by chance – i.e., randomly, from a set of numbers. All the numbers in a specified distribution have an equal probability of being chosen randomly. (2) Random Numbers? Syntax: Default Random Number Generators np.random.default_rng() Example: Initializing The Generator rng = np.random.default_rng() Using ‘random()’ Function To Generate Numbers Syntax: numpy.random.random(size=None) Parameters: size : int or tuple of ints, optional – Output shape. If the given shape is,

    Read More

  • More Useful Array Operations.

    More Useful Array Operations.

    More Useful Array Operations. Table Of Contents: Maximum, Minimum, Sum, Mean, Product, Standard Deviation (1) data.max() data.max( ) will give you the ‘max’ value present in the array. Example: import numpy as np data = np.array([3,1,5,9,2,-3,9,10]) data.max() Output: 10 (2) data.min() data.min( ) will give you the ‘minimum’ value present in the array. Example: import numpy as np data = np.array([3,1,5,9,2,-3,9,10]) data.min() Output: -3 (3) data.sum() data.sum( ) will give you the ‘sum’ of values present in the array. Example: import numpy as np data = np.array([3,1,5,9,2,-3,9,10]) data.sum() Output: 36 (4) np.prod() np.prod( ) will give you the ‘product’ of values

    Read More

  • What Is Numpy Broadcasting ?

    What Is Numpy Broadcasting ?

    What Is Numpy Broadcasting ? Table Of Contents: What Is Broadcasting ? Examples Of Broadcasting. (1) What Is Broadcasting ? The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations.  Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes. (2) Examples Of Broadcasting ? Example-1: import numpy as np data = np.array([1.0, 2.0]) data * 1.6 Output: array([1.6, 3.2]) Example-2: a = np.array([[ 0.0, 0.0, 0.0], [10.0, 10.0, 10.0], [20.0, 20.0, 20.0], [30.0, 30.0, 30.0]]) b = np.array([1.0, 2.0, 3.0]) a + b Output: array([[ 1.,

    Read More

  • Basic Numpy Array Operations.

    Basic Numpy Array Operations.

    Basic Numpy Array Operations. Table Of Contents: addition, subtraction, multiplication, division sum( ) (1) Numpy Addition You can use ‘+’ operator for addition operation. Example: data = np.array([1, 2]) data array([1, 2]) ones = np.ones(2, dtype=int) ones array([1, 1]) data + ones Output: array([2, 3]) (2) Numpy Substraction You can use ‘-‘ operator for subtraction operation. Example: data = np.array([1, 2]) data array([1, 2]) ones = np.ones(2, dtype=int) ones array([1, 1]) data – ones Output: array([0, 1]) (3) Numpy Multiplication You can use ‘*’ operator for multiplication operation. Example: data = np.array([1, 2]) data array([1, 2]) ones = np.ones(2, dtype=int) ones

    Read More

  • How To Create An Array From Existing Data ?

    How To Create An Array From Existing Data ?

    How To Create An Array From Existing Data ? Table Of Contents: Slicing and Indexing np.vstack( ) np.hstack( ) np.hsplit( ) .view( ) copy( ) (1) Slicing and Indexing You can easily create a new array from a section of an existing array. You need to mention the start and end index position of the array. Example: import numpy as np a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) arr1 = a[3:8] arr1 Output: array([4, 5, 6, 7, 8]) Note: Here I have created a new array from an existing array by mentioning the start and

    Read More

  • Indexing And Slicing In Numpy Array.

    Indexing And Slicing In Numpy Array.

    Indexing And Slicing In Numpy Array. Table Of Contents: Numpy Indexing Numpy Slicing (1) Numpy Indexing Syntax: array[start:end:steps] Parameters: Start: Starting position of the element End: Ending Position Of The Element Steps: Number Of Steps To Jump While Travelling. Example-1: data = np.array([1, 2, 3]) data array([1, 2, 3]) data[0] 1 Note: It will select the value at index position ‘0’ which is ‘1’. Example-2: data[0:2] array([1, 2]) Note: It will select the value from ‘0’ to (end_index -1)  = (2 – 1)  = 1. Example-3: data[1:] array([2, 3]) Note: Here start index is ‘1’ and we did not mention

    Read More