Found 47 Articles for Scientific Computing

What is the use of scipy.interpolate.interp1d class of SciPy python library?

Gaurav Kumar
Updated on 14-Dec-2021 11:48:18

177 Views

The scipy.interpolate.interp1d(x, y, kind, axis, copy, bounds_error, fill_value, assumesorted) class of SciPy library, as name implies, is used to interpolate a 1-Dimensional function. Here, x and y are the arrays of values which are used to approximate some function, say f; y=f(x). The output of this class is a function whose call method uses interpolation to find the value of new points.Below is given the detailed explanation of its parameters −Parametersx − (N, ) array_likeIt is a 1-dimensional array of real values.y − (…, N, …) array_likeIt is a N-dimensional array of real values. The condition is that the length ... Read More

Finding inverse of a square matrix using SciPy library

Gaurav Kumar
Updated on 24-Nov-2021 13:42:44

193 Views

SciPy library has scipy.linalg.inv() function for finding the inverse of a square matrix. Let’s understand how we can use this function to calculate the inverse of a matrix −ExampleInverse of a 2 by 2 matrix#Importing the scipy package import scipy.linalg #Importing the numpy package import numpy as np #Declaring the numpy array (Square Matrix) A = np.array([[3, 3.5], [3.2, 3.6]]) #Passing the values to scipy.linalg.inv() function M = scipy.linalg.inv(A) #Printing the result print('Inverse of {} is {}'.format(A, M))OutputInverse of [[3. 3.5] [3.2 3.6]] is [[-9. 8.75] [ 8. -7.5 ]]ExampleInverse of a 3 by 3 ... Read More

Finding determinant of a square matrix using SciPy library

Gaurav Kumar
Updated on 24-Nov-2021 10:35:59

273 Views

The determinant of a matrix, denoted by |A|, is a scalar value that can be calculated from a square matrix. With the help of the determinant of a matrix, we can find the inverse of a matrix and other things that are useful in the systems of linear equations, calculus, etc. The function named scipy.linalg.det() calculates the determinant of a square matrix.Let’s understand it with the below given examples −ExampleCalculating determinant of 2 by 2 matrix#Importing the scipy package import scipy #Importing the numpy package import numpy as np #Declaring the numpy array (Square Matrix) X = np.array([[5, ... Read More

How can we use the SciPy library to solve a Linear equation?

Gaurav Kumar
Updated on 24-Nov-2021 10:33:55

437 Views

SciPy has a function called scipy.linalg.solve() to solve linear equations. All we need to know is how we can represent our linear equation in terms of vectors. It will solve the linear equation set a * x = b for the unknown x. Let’s understand it with the help of below example −ExampleIn this example, we will be trying to solve a linear algebra system which can be given as follows −   3x + 2y = 2   x - y = 4   5y + z = -1The function scipy.linalg.solve() will find the values of x, y, and z for which ... Read More

Calculating the Hamming distance using SciPy

Gaurav Kumar
Updated on 24-Nov-2021 10:31:26

508 Views

Hamming distance calculates the distance between two binary vectors. Mostly we find the binary strings when we use one-hot encoding on categorical columns of data. In one-hot encoding the integer variable is removed and a new binary variable will be added for each unique integer value. For example, if a column had the categories say ‘Length’, ‘Width’, and ‘Breadth’. We might one-hot encode each example as a bitstring with one bit for each column as follows −Length = [1, 0, 0]Width = [0, 1, 0]Breadth = [0, 0, 1]The Hamming distance between any of the two categories mentioned above, can ... Read More

Calculating the Minkowski distance using SciPy

Gaurav Kumar
Updated on 14-Dec-2021 10:38:44

447 Views

The Minkowski distance, a generalized form of Euclidean and Manhattan distance, is the distance between two points. It is mostly used for distance similarity of vectors. Below is the generalized formula to calculate Minkowski distance in n-dimensional space −$$\mathrm{D= \big[\sum_{i=1}^{n}|r_i-s_i|^p\big]^{1/p}}$$Here, si and ri are data points.n denotes the n-space.p represents the order of the normSciPy provides us with a function named minkowski that returns the Minkowski Distance between two points. Let’s see how we can calculate the Minkowski distance between two points using SciPy library −Example# Importing the SciPy library from scipy.spatial import distance # Defining the points A = ... Read More

Calculating the Manhattan distance using SciPy

Gaurav Kumar
Updated on 14-Dec-2021 10:24:49

635 Views

The Manhattan distance, also known as the City Block distance, is calculated as the sum of absolute differences between the two vectors. It is mostly used for the vectors that describe objects on a uniform grid such as a city block or chessboard. Below is the generalized formula to calculate Manhattan distance in n-dimensional space −$$\mathrm{D =\sum_{i=1}^{n}|r_i-s_i|}$$Here, si and ri are data points.n denotes the n-space.SciPy provides us with a function named cityblock that returns the Manhattan Distance between two points. Let’s see how we can calculate the Manhattan distance between two points using SciPy library−Example# Importing the SciPy library ... Read More

Calculating Euclidean distance using SciPy

Gaurav Kumar
Updated on 14-Dec-2021 10:24:02

517 Views

Euclidean distance is the distance between two real-valued vectors. Mostly we use it to calculate the distance between two rows of data having numerical values (floating or integer values). Below is the formula to calculate Euclidean distance −$$\mathrm{d(r, s) =\sqrt{\sum_{i=1}^{n}(s_i-r_i)^2} }$$Here, r and s are the two points in Euclidean n-space.si and ri are Euclidean vectors.n denotes the n-space.Let’s see how we can calculate Euclidean distance between two points using SciPy library −Example# Importing the SciPy library from scipy.spatial import distance # Defining the points A = (1, 2, 3, 4, 5, 6) B = (7, 8, 9, 10, 11, ... Read More

What is scipy.cluster.hierarchy.fcluster()method?

Gaurav Kumar
Updated on 25-Nov-2021 06:43:02

1K+ Views

scipy.cluster.hierarchy.fcluster(Z, t, criterion=’inconsistent’depth=2, R=None, monocrat=None)− The fcluster() method forms flat clusters from the hierarchical clustering. This hierarchical clustering is defined by the given linkage matrix, identifying a link between clustered classes.Below is given the detailed explanation of its parameters −ParametersZ− ndarrayIt represents the hierarchical clustering which is encoded with the linkage matrix.t− scalarThe value of t depends on the type of criteria. For ‘inconsistent’, ‘distance’, and ‘monocrit’ criteria, the value of t represents the threshold to apply when forming flat clusters. On the other hand, for ‘maxclust’, and ‘maxclust_monocrit’ criteria, the value of t represents the maximum number of clusters ... Read More

What is scipy cluster hierarchy? How to cut hierarchical clustering into flat clustering?

Gaurav Kumar
Updated on 25-Nov-2021 07:05:23

464 Views

The scipy.cluster.hierarchy module provides functions for hierarchical clustering and its types such as agglomerative clustering. It has various routines which we can use to −Cut hierarchical clustering into the flat clustering.Implement agglomerative clustering.Compute statistics on hierarchiesVisualize flat clustering.To check isomorphism of two flat cluster assignments.Plot the clusters.The routine scipy.cluster.hierarchy.fcluster is used to cut hierarchical clustering into flat clustering, which they obtain as a result an assignment of the original data point to single clusters. Let’s understand the concept with the help of below given example −Example#Importing the packages from scipy.cluster.hierarchy import ward, fcluster from scipy.spatial.distance import pdist #The cluster ... Read More

Advertisements