• How To Plot Numpy Arrays?

    How To Plot Numpy Arrays?

    How To Plot Numpy Arrays? Table Of Contents: Using ‘matplotlib’ Library To Plot The Numpy. Examples Of Plotting. Example-1 import matplotlib.pyplot as plt %matplotlib inline a = np.array([2, 1, 5, 7, 4, 6, 8, 14, 10, 9, 18, 20, 22]) plt.plot(a) Example-2 import matplotlib.pyplot as plt %matplotlib inline x = np.linspace(0, 5, 20) y = np.linspace(0, 10, 20) plt.plot(x, y, ‘purple’) # line plt.plot(x, y, ‘o’) # dots Example-3 import matplotlib.pyplot as plt %matplotlib inline fig = plt.figure() ax = fig.add_subplot(projection=’3d’) X = np.arange(-5, 5, 0.15) Y = np.arange(-5, 5, 0.15) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 +

    Read More

  • How To Save And Load NumPy Objects?

    How To Save And Load NumPy Objects?

    How To Save And Load NumPy Objects? Table Of Contents: np.save np.savez np.savetxt np.load np.loadtxt (1) np.save(): Save an array to a binary file in NumPy .npy format. Syntax: numpy.save(file, arr, allow_pickle=True, fix_imports=True) Parameters: file: file, str, or pathlib.Path – File or filename to which the data is saved. If the file is a file object, then the filename is unchanged. If the file is a string or Path, a .npy the extension will be appended to the filename if it does not already have one. arr: array_like – Array data to be saved. allow_pickle: bool, optional – Allow saving object arrays

    Read More

  • How To Access The Docstring For More Information?

    How To Access The Docstring For More Information?

    How To Access The Docstring For More Information? Table Of Contents: help( ) ? ?? (1) help () ‘help( )’ method is used to get information about an object. It will provide you with a quick and concise summary of the object and how to use it Syntax: help([object]) Note: You need to pass the object name to get the information about it. Example-1: help(np.ravel) Help on function ravel in module numpy: ravel(a, order=’C’) Return a contiguous flattened array. A 1-D array, containing the elements of the input, is returned. A copy is made only if needed. As of NumPy

    Read More

  • 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