scipy 中的whiten函式

2021-10-12 10:25:09 字數 3363 閱讀 2650

呼叫kmeans函式,kmeans中呼叫了whited函式。查後,發現whiten是對輸入資料按標準差做歸一化處理。

e​經過whiten

x i′

=xis

tand

_dev

atio

nx_^ =\frac}

xi′​=s

tand

_dev

atio

nxi​

與標準化不同的是,白化處理沒有減去均值。

下面是按步驟實現和呼叫函式實現,結果是一樣的。

import numpy as np

from numpy import array

from scipy.cluster.vq import vq, kmeans, whiten

import matplotlib.pyplot as plt

features = array([[

1.9,

2.3],[

1.5,

2.5],[

0.8,

0.6],[

0.4,

1.8],[

0.1,

0.1],[

0.2,

1.8],[

2.0,

0.5],[

0.3,

1.5],[

1.0,

1.0]])

mean = np.mean(features,axis=0)

# 求每列的均值[0.91111111 1.34444444]

np_square = np.power(features-mean,2)

# # print('mean:\n',mean)

print

('features - mean:\n'

,features-mean)

# print('square:\n',np_square)

mean_sqare = np.mean(np_square,axis =0)

# 方差

# print('mean_square:\n',mean_sqare)

stand_devation = np.sqrt(mean_sqare)

# 標準差

# print('stand_devation:\n',stand_devation)

np_whit = features/stand_devation# scales by devation

print

('******* np_white ******'

)print

(np_whit)

whitened = whiten(features)

# 呼叫scipy中的函式

print

('******* whitened ******'

)print

(whitened)

whiten後kmeans

from numpy import random

import numpy as np

from numpy import array

from scipy.cluster.vq import vq, kmeans, whiten

import matplotlib.pyplot as plt

random.seed(

(1000

,2000))

codes =

3kmeans(whitened,codes)

# (array([[ 2.3110306 , 2.86287398], # random

# [ 1.32544402, 0.65607529],

# [ 0.40782893, 2.02786907]]), 0.5196582527686241)

# create 50 datapoints in two clusters a and b

pts =

50a = np.random.multivariate_normal([0

,0],

[[4,

1],[

1,4]

], size=pts)

# 中心點、協方差、資料量

b = np.random.multivariate_normal([30

,10],

[[10,

2],[

2,1]

],size=pts)

features = np.concatenate(

(a, b)

)# whiten data

whitened = whiten(features)

# find 2 clusters in the data

codebook, distortion = kmeans(whitened,2)

# 返回聚類中心和誤差

scipy庫中的odeint函式

scipy.integrate.odeint func,y0,t,args dfun none,col deriv 0,full output 0,ml none,mu none,rtol none,atol none,tcrit none,h0 0.0,hmax 0.0,hmin 0.0,ixpr...

scipy庫中的leastsq函式

只需要輸入一系列樣本點,給出待求函式的基本形狀 如二元二次函式就是一種形狀 f x,y w0x 2 w1y 2 w2xy w3x w4y w5 在形狀給定後,我們只需要求解相應的係數w0 w5 即可得到相應的引數。至於中間到底是怎麼求的,這一部分內容就像乙個黑箱一樣。則使用leastsq函式求解其擬...

scipy中squareform函式詳解

scipy.spatial.distance.squareform x,force no checks true converts a vector form distance vector to a square form distance matrix,and vice versa.解釋用來把乙...