from SciPy.cluster.vq import kmeans,vq,whiten
from numpy import vstack,array
from numpy.random import rand
# data generation with three features
data = vstack((rand(100,3) + array([.5,.5,.5]),rand(100,3)))
array([[ 1.48598868e+00, 8.17445796e-01, 1.00834051e+00],
[ 8.45299768e-01, 1.35450732e+00, 8.66323621e-01],
[ 1.27725864e+00, 1.00622682e+00, 8.43735610e-01],
............
scipy.constant
package is available with a wide range of constants, which is used extensively in the scientific field. There are various physical, mathematical constants and units that we can import the required constants and use them as per needed. scipy.constant
provides the following list of mathematical constants.pi
golden
pi
' value by importing different modules.#Import pi constant from the scipy
from scipy.constants import pi
#Import pi from math package
from math import pi
#Comparing these two pi value
print("sciPy - pi Value = %.18f"%scipy.constants.pi)
print("math - pi Value = %.18f"%math.pi)
sciPy - pi Value = 3.141592653589793116
math - pi Value = 3.141592653589793116
x[n]
is calculated by fft()
function and the inverse transform is calculated using ifft()
.#Importing the fft and inverse fft functions from fftpackage
from scipy.fftpack import fft
#Importing numpy
import numpy as np
#create an array with random n numbers
x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
#Applying the fft function
y = fft(x)
print (y)
[ 4.5 +0.j , 2.08155948-1.65109876j,
-1.83155948+1.60822041j, -1.83155948-1.60822041j,
2.08155948+1.65109876j]
[1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0]
scipy.sparse
that provides functions to deal with sparse data. There are two types of sparse matrices that we use :scipy.sparse.csr_matrix()
.import numpy as np
from scipy.sparse import csr_matrix
arr = np.array([0, 0, 0, 0, 0, 1, 1, 0, 2])
print(csr_matrix(arr))
Output :
(0, 5) 1
(0, 6) 1
(0, 8) 2
scipy.integrate
sub-package provides several integration techniques including an ordinary differential equation integrator. scipy.interpolation
package.import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
x = np.linspace(0, 4, 12)
y = np.cos(x**2/3+4)
print (x,y)​
Output :
(
array([0., 0.36363636, 0.72727273, 1.09090909, 1.45454545, 1.81818182,
2.18181818, 2.54545455, 2.90909091, 3.27272727, 3.63636364, 4.]),
array([-0.65364362, -0.61966189, -0.51077021, -0.31047698, -0.00715476,
0.37976236, 0.76715099, 0.99239518, 0.85886263, 0.27994201,
-0.52586509, -0.99582185])
)