Found 33676 Articles for Programming

How to implement ‘cubic’ 1-D interpolation using SciPy library?

Gaurav Kumar
Updated on 24-Nov-2021 11:05:04

223 Views

To implement ‘cubic’ 1-D interpolation using SciPy, we need to specify the kind of interpolation as ‘cubic’ in the ‘kind’ parameter of scipy.interpolate.interp1d class. Let’s see the example below to understand it−ExampleFirst let’s generate some data to implement interpolation on that −import numpy as np from scipy.interpolate import interp1d import matplotlib.pyplot as plt A = np.linspace(0, 10, num=11, endpoint=True) B = np.cos(-A**2/9.0) print (A, B)OutputThe above script will generate the following points between 0 and 4 − [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.] [ 1. 0.99383351 0.90284967 0.54030231 -0.20550672 -0.93454613 -0.65364362 0.6683999 0.67640492 -0.91113026 ... Read More

What is interpolation and how can we implement it in the SciPy Python library?

Gaurav Kumar
Updated on 24-Nov-2021 10:58:54

235 Views

Interpolation is a method of generating a value between two given points on a line or a curve. In machine learning, interpolation is used to substitute the missing values in a dataset. This method of filling the missing values is called imputation. Another important use of interpolation is to smooth the discrete points in a dataset.SciPy provides us a module named scipy.interpolate having many functions with the help of which we can implement interpolation.ExampleIn the below example we will implement Interpolation by using the scipy.interpolate() package −First let’s generate some data to implement interpolation on that −import numpy as np ... Read More

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

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

309 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

308 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

480 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

710 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

978 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

621 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

1K+ 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

992 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

Advertisements