Found 47 Articles for Scientific Computing

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

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

125 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

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

340 Views

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

What is scipy.cluster.vq.kmeans2()method?

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

133 Views

scipy.cluster.vq.kmeans2(data, k, iter=10, thresh=1e-05, minit='random', missing='warn', check_finite=True)− The kmeans2() method classify a set of observations vectors into k clusters by performing k-means algorithm. To check for convergence, the kmeans2() method does not use threshold values. It has additional parameters to decide the method of initialization of centroids, to handle empty clusters, and to validate if the input metrices contain only finite numbers or not.Below is given the detailed explanation of its parameters −Parametersdata− ndarrayIt is an ‘M’ by ‘N’ array of M observations in N dimension.k− int or ndarrayThis parameter represents the number of clusters to form and the centroids ... Read More

What is scipy.cluster.vq.kmeans()method?

Gaurav Kumar
Updated on 24-Nov-2021 08:07:49

100 Views

The scipy.cluster.vq.kmeans(obs, k_or_guess, iter=20, thresh=1e- 05, check_finite=True)method forms k clusters by performing a 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.Below is given the detailed explanation of its parameters−Parametersobs− ndarrayIt is an ‘M’ by ‘N’ array where each row is an observation, and the columns are the features seen during each observation. Before using, these features ... Read More

Which function of scipy.cluster.vq module is used to assign codes from a code book to observations?

Gaurav Kumar
Updated on 24-Nov-2021 08:02:10

119 Views

Before implementing k-means algorithms, the scipy.cluster.vq.vq(obs, code_book, check_finite = True) used to assign codes to each observation from a code book. It first compares each observation vector in the ‘M’ by ‘N’ obs array with the centroids in the code book. Once compared, it assigns the code to the closest centroid. It requires unit variance features in the obs array, which we can achieve by passing them through the scipy.cluster.vq.whiten(obs, check_finite = True)function.ParametersBelow are given the parameters of the function scipy.cluster.vq.vq(obs, code_book, check_finite = True) −obs− ndarrayIt is an ‘M’ by ‘N’ array where each row is an observation, and ... Read More

Which function of scipy.cluster.vq module is used to normalize observations on each feature dimension?

Gaurav Kumar
Updated on 23-Nov-2021 13:23:51

76 Views

Before implementing k-means algorithms, it is always beneficial to rescale each feature dimension of the observation set. The function scipy.cluster.vq.whiten(obs, check_finite = True)is used for this purpose. To give it unit variance, it divides each feature dimension of the observation by its standard deviation (SD).ParametersBelow are given the parameters of the function scipy.cluster.vq.whiten(obs, check_finite = True) −obs− ndarrayIt is an array, to be rescaled, where each row is an observation, and the columns are the features seen during each observation. The example is given below −obs = [[ 1., 1., 1.], [ 2., 2., 2.], ... Read More

How can we call the documentation for NumPy and SciPy?

Gaurav Kumar
Updated on 23-Nov-2021 13:15:28

112 Views

If you are unsure of how to use a particular function or variable in NumPy and SciPy, you can call for the documentation with the help of ‘?’. In Jupyter notebook and IPython shell we can call up the documentation as follows −ExampleIf you want to know NumPy sin () function, you can use the below code −import numpy as np np.sin?OutputWe will get the details about sin() function something like as follows −We can also view the source with the help of double question mark (??) as follows −import numpy as np np.sin??Similarly, if you want to see the ... Read More

Implementing K-means clustering of Diabetes dataset with SciPy library

Gaurav Kumar
Updated on 14-Dec-2021 08:59:17

609 Views

The Pima Indian Diabetes dataset, which we will be using here, is originally from the National Institute of Diabetes and Digestive and Kidney Diseases. Based on the following diagnostic factors, this dataset can be used to place a patient in ether diabetic cluster or non-diabetic cluster −PregnanciesGlucoseBlood PressureSkin ThicknessInsulinBMIDiabetes Pedigree FunctionAgeYou can get this dataset in .CSV format from Kaggle website.ExampleThe example below will use SciPy library to create two clusters namely diabetic and non-diabetic from the Pima Indian diabetes dataset.#importing the required Python libraries: import matplotlib.pyplot as plt import numpy as np from scipy.cluster.vq import whiten, kmeans, vq ... Read More

Implementing K-means clustering with SciPy by splitting random data in 3 clusters?

Gaurav Kumar
Updated on 14-Dec-2021 08:48:44

117 Views

Yes, we can also implement a K-means clustering algorithm by splitting the random data in 3 clusters. Let us understand with the example below −Example#importing the required Python libraries: import numpy as np from numpy import vstack, array from numpy.random import rand from scipy.cluster.vq import whiten, kmeans, vq from pylab import plot, show #Random data generation: data = vstack((rand(200, 2) + array([.5, .5]), rand(150, 2))) #Normalizing the data: data = whiten(data) # computing K-Means with K = 3 (3 clusters) centroids, mean_value = kmeans(data, 3) print("Code book :", centroids, "") print("Mean of Euclidean distances :", mean_value.round(4)) ... Read More

Advertisements