Found 44 Questions for Scientific Computing

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

Gaurav Kumar
Updated on 24-Nov-2021 10:33:55
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
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
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
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
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
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
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
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
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

What is the difference between scipy.cluster.vq.kmeans() and scipy.cluster.vq.kmeans2() methods?

Gaurav Kumar
Updated on 24-Nov-2021 08:11:58
The scipy.cluster.vq()has two methods to implement k-means clustering namely kmeans() and kmeans2(). There is a significant difference in the working of both these methods. Let us understand it −scipy.cluster.vq.kmeans(obs, k_or_guess, iter=20, thresh=1e-05, check_finite=True)− The kmeans() method forms k clusters by performing k-means algorithm on a set of observation vectors. To determine the stability of the centroids, this method uses a threshold value to compare the change in average Euclidean distance between the observations and their corresponding centroids. The output of this method is a code book mapping centroid to codes and vice versa.scipy.cluster.vq.kmeans2(data, k, iter=10, thresh=1e-05, minit='random', missing='warn', check_finite=True)− The ... Read More
Advertisements