灰色关联分析

4 篇文章 0 订阅
2 篇文章 0 订阅

一、灰色关联分析

有关灰色关联的数学解释 向博客大佬 进行学习

值得一说的是 灰色关联矩阵 并没有像协方差矩阵和皮尔逊系数矩阵具有对称性

1.导入依赖包

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import MinMaxScaler

2.数据存放格式如下

# 表格形如以下形式,数据写入Excel即可调用RA函数
pd.DataFrame(np.ones(shape=(5, 5)),
             columns=['指标{}'.format(i) for i in range(5)],
             index=['对象{}'.format(i) for i in range(5)]) 

数据存放样式

3.归一化数据
4.编写程序
ζ i ( k ) = min ⁡ s   min ⁡ t   ∣ x 0 ( t ) − x s ( t ) ∣ + ρ max ⁡ s   max ⁡ t   ∣ x 0 ( t ) − x s ( t ) ∣ ∣ x 0 ( k ) − x i ( k ) ∣ + ρ max ⁡ s   max ⁡ t   ∣ x 0 ( t ) − x s ( t ) ∣ {{\zeta }_{i}}(k)=\frac{\underset{s}{\mathop{\min }}\,\underset{t}{\mathop{\min }}\,\left| {{x}_{0}}(t)-{{x}_{s}}(t) \right|+\rho \underset{s}{\mathop{\max }}\,\underset{t}{\mathop{\max }}\,\left| {{x}_{0}}(t)-{{x}_{s}}(t) \right|}{\left| {{x}_{0}}(k)-{{x}_{i}}(k) \right|+\rho \underset{s}{\mathop{\max }}\,\underset{t}{\mathop{\max }}\,\left| {{x}_{0}}(t)-{{x}_{s}}(t) \right|} ζi(k)=x0(k)xi(k)+ρsmaxtmaxx0(t)xs(t)smintminx0(t)xs(t)+ρsmaxtmaxx0(t)xs(t)

def minmin(x0, x):  #x0为参考数列;x为对象矩阵
    a = np.abs(x - x0)
    b = np.min(a, axis=1)
    return b.min()


def maxmax(x0, x):
    a = np.abs(x - x0)
    b = np.max(a, axis=1)
    return b.max()


def kesi(x0, x, amin, bmax, k, ro=0.5):
    c = np.abs(x - x0)
    kesi_k = (amin + ro * bmax) / (c + ro * bmax)
    return kesi_k.mean(axis=1).reshape(-1)


# 关联矩阵
def RA(x1, x):  #x,x均为矩阵
    amin = minmin(x1[0], x)
    bmax = maxmax(x1[0], x)
    res = kesi(x1[0], x, amin, bmax, 1, ro=0.5)
    for row in range(1, x1.shape[0]):
        x0 = x1[row]
        amin = minmin(x0, x)
        bmax = maxmax(x0, x)
        res1 = kesi(x0, x, amin, bmax, 1, ro=0.5)
        res = np.vstack((res, res1))
    return res

5.数据导入验证模型

x0 = np.array([[8, 9, 8, 7, 5, 2, 9], [7, 8, 7, 5, 7, 3, 8]])

x = np.array([[7, 8, 7, 5, 7, 3, 8], [9, 7, 9, 6, 6, 4, 7],
              [6, 8, 8, 8, 4, 3, 6], [8, 6, 6, 9, 8, 3, 8],
              [8, 9, 5, 7, 6, 4, 8], [8, 9, 8, 7, 5, 2, 9],
              [7, 8, 7, 5, 7, 3, 8], [9, 7, 9, 6, 6, 4, 7],
              [6, 8, 8, 8, 4, 3, 6]])
x = np.random.normal(0, 1, (6, 9))

6.计算矩阵,并打印热力图

G = RA(x, x)
import seaborn as sns
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  #显示中文
plt.rcParams['axes.unicode_minus'] = False  #用来正常显示负号
fig, ax = plt.subplots(1, 1)  #必须这一句,不然无法show
sns.heatmap(G, cmap='Greys', annot=True)
plt.xticks(
    np.arange(x.shape[0]) + 0.5,
    ['因子{}'.format(i) for i in range(1, x.shape[0] + 1)])
plt.yticks(
    np.arange(x.shape[0]) + 0.5,
    ['因子{}'.format(i) for i in range(1, x.shape[0] + 1)])
plt.savefig(r'./灰色关联热力图.png', dpi=300)
plt.show()

关联系数矩阵7、完整代码

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from sklearn.preprocessing import MinMaxScaler

plt.rcParams['font.sans-serif'] = ['SimHei']  #显示中文
plt.rcParams['axes.unicode_minus'] = False  #用来正常显示负号


def minmin(x0, x):  #x0为参考数列;x为对象矩阵
    a = np.abs(x - x0)
    b = np.min(a, axis=1)
    return b.min()


def maxmax(x0, x):
    a = np.abs(x - x0)
    b = np.max(a, axis=1)
    return b.max()


def kesi(x0, x, amin, bmax, k, ro=0.5):
    c = np.abs(x - x0)
    kesi_k = (amin + ro * bmax) / (c + ro * bmax)
    return kesi_k.mean(axis=1).reshape(-1)


# 关联矩阵
def RA(x1, x):  #x,x均为矩阵
    amin = minmin(x1[0], x)
    bmax = maxmax(x1[0], x)
    res = kesi(x1[0], x, amin, bmax, 1, ro=0.5)
    for row in range(1, x1.shape[0]):
        x0 = x1[row]
        amin = minmin(x0, x)
        bmax = maxmax(x0, x)
        res1 = kesi(x0, x, amin, bmax, 1, ro=0.5)
        res = np.vstack((res, res1))
    return res

看完了,不管好不好点个赞呗

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值