Found 46 Articles for Scipy

Finding determinant of a square matrix using SciPy library

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

282 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

455 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

530 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

452 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

651 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

545 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

469 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

What are various inbuilt methods used to access constants database in scipy.constants() module?

Gaurav Kumar
Updated on 24-Nov-2021 08:27:48

128 Views

It is difficult to remember the values, units, and precisions of all physical constants. That’s the reason scipy.constants() have four methods with the help of which we can access physical constants. Let’s understand these methods along with examples −scipy.constants.value(key)− This method will give us the value in physical constants indexed by key.Parameterskey- It represents the key in dictionary physical_constants. Its value is a Python string or Unicode.Returnsvalue- It represents the value in physical_constants corresponding to the key parameter. Its value is of float type.Examplefrom scipy import constants constants.value(u'proton mass')Output1.67262192369e-27scipy.constants.unit(key)− This method will give us the unit in physical constants indexed ... Read More

How can we use various mathematical and physical constants in scipy library?

Gaurav Kumar
Updated on 24-Nov-2021 08:17:26

81 Views

To implement Scientific or Mathematical calculation, we need various universal constants. For example, the formula to calculate area of a circle is pi*r*r where Pi is a constant having value = 3.141592653. There are various other scenarios like this where we need constants. It would really be helpful if we can incorporate these constants into our calculation with ease. The scipy.constants(), a sub-module inside the Scipy library, does the job for us and provide us a reference material to look up exhaustive list of Physical Constants, universal mathematical constants, and various units such as SI prefixes, Binary prefixes, Mass, Angle, ... Read More

Advertisements