hotelling t2 matlab,Hotelling的T ^ 2在python中得分

这段代码展示了如何利用matplotlib的PCA类进行主成分分析,并实现Hotelling T^2统计量的计算。通过一个示例数据集,验证了计算结果与Matlab的PCA函数相一致。主要涉及PCA的系数、潜变量和T^2统计量的输出。
摘要由CSDN通过智能技术生成

matplotlib的

PCA类不包括Hotelling T2计算,但只需几行代码即可完成.以下代码包括计算每个点的T2值的函数. __main__脚本将PCA应用于

Matlab’s pca documentation中使用的相同示例,因此您可以验证函数是否生成与Matlab相同的值.

from __future__ import print_function, division

import numpy as np

from matplotlib.mlab import PCA

def hotelling_tsquared(pc):

"""`pc` should be the object returned by matplotlib.mlab.PCA()."""

x = pc.a.T

cov = pc.Wt.T.dot(np.diag(pc.s)).dot(pc.Wt) / (x.shape[1] - 1)

w = np.linalg.solve(cov, x)

t2 = (x * w).sum(axis=0)

return t2

if __name__ == "__main__":

hald_text = """Y X1 X2 X3 X4

78.5 7 26 6 60

74.3 1 29 15 52

104.3 11 56 8 20

87.6 11 31 8 47

95.9 7 52 6 33

109.2 11 55 9 22

102.7 3 71 17 6

72.5 1 31 22 44

93.1 2 54 18 22

115.9 21 47 4 26

83.8 1 40 23 34

113.3 11 66 9 12

109.4 10 68 8 12

"""

hald = np.loadtxt(hald_text.splitlines(), skiprows=1)

ingredients = hald[:, 1:]

pc = PCA(ingredients, standardize=False)

coeff = pc.Wt

np.set_printoptions(precision=4)

# For coeff and latent, compare to

# http://www.mathworks.com/help/stats/pca.html#btjpztu-1

print("coeff:")

print(coeff)

print()

latent = pc.s / (ingredients.shape[0] - 1)

print("latent:" + (" %9.4f"*len(latent)) % tuple(latent))

print()

# For tsquared, compare to

# http://www.mathworks.com/help/stats/pca.html#bti6r0c-1

tsquared = hotelling_tsquared(pc)

print("tsquared:")

print(tsquared)

输出:

coeff:

[[ 0.0678 0.6785 -0.029 -0.7309]

[ 0.646 0.02 -0.7553 0.1085]

[-0.5673 0.544 -0.4036 0.4684]

[ 0.5062 0.4933 0.5156 0.4844]]

latent: 517.7969 67.4964 12.4054 0.2372

tsquared:

[ 5.6803 3.0758 6.0002 2.6198 3.3681 0.5668 3.4818 3.9794 2.6086

7.4818 4.183 2.2327 2.7216]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值