脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现灰色关联分析与结果可视化的详细代码

Python实现灰色关联分析与结果可视化的详细代码

2022-11-14 09:51FontTian的专栏 Python

今天小编通过代码以灰色色系为例给大家介绍Python灰色关联分析实现方法,灰色关联度分析对于一个系统发展变化态势提供了量化的度量,非常适合动态历程分析,感兴趣的朋友一起看看吧

之前在比赛的时候需要用Python实现灰色关联分析,从网上搜了下只有实现两个列之间的,于是我把它改写成了直接想Pandas中的计算工具直接计算person系数那样的形式,可以对整个矩阵进行运算,并给出了可视化效果,效果请见实现

灰色关联分析法

对于两个系统之间的因素,其随时间或不同对象而变化的关联性大小的量度,称为关联度。在系统发展过程中,若两个因素变化的趋势具有一致性,即同步变化程度较高,即可谓二者关联程度较高;反之,则较低。因此,灰色关联分析方法,是根据因素之间发展趋势的相似或相异程度,亦即“灰色关联度”,作为衡量因素间关联程度的一种方法。

简介

灰色系统理论提出了对各子系统进行灰色关联度分析的概念,意图透过一定的方法,去寻求系统中各子系统(或因素)之间的数值关系。因此,灰色关联度分析对于一个系统发展变化态势提供了量化的度量,非常适合动态历程分析。

计算步骤

  • 确实参考数列与比较数列
  • 对参考数列与比较数列进行无量纲化处理
  • 计算关联系数,求关联度

此处我给出的是第三步的实现方式,无量纲化请自己处理.数据使用UCI的红酒质量数据集.

代码实现

下载数据

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

# 定义下载数据的函数
def ReadAndSaveDataByPandas(target_url = None,file_save_path = None ,save=False):
  if target_url !=None:
      target_url = ("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv")   
  if file_save_path != None:
      file_save_path = "/home/fonttian/Data/UCI/Glass/glass.csv"
  wine = pd.read_csv(target_url, header=0, sep=";")
  if save == True:
      wine.to_csv(file_save_path, index=False)
  return wine
# 从硬盘读取数据进入内存
wine = pd.read_csv("/home/font/Data/UCI/WINE/wine.csv")
wine.head()

Python实现灰色关联分析与结果可视化的详细代码

实现灰色关联分析

import pandas as pd
from numpy import *
def GRA_ONE(DataFrame,m=0):
  gray= DataFrame
  #读取为df格式
  gray=(gray - gray.min()) / (gray.max() - gray.min())
  #标准化
  std=gray.iloc[:,m]#为标准要素
  ce=gray.iloc[:,0:]#为比较要素
  n=ce.shape[0]
  m=ce.shape[1]#计算行列

  #与标准要素比较,相减
  a=zeros([m,n])
  for i in range(m):
      for j in range(n):
          a[i,j]=abs(ce.iloc[j,i]-std[j])
  #取出矩阵中最大值与最小值
  c=amax(a)
  d=amin(a)
  #计算值
  result=zeros([m,n])
          result[i,j]=(d+0.5*c)/(a[i,j]+0.5*c)
  #求均值,得到灰色关联值
  result2=zeros(m)
          result2[i]=mean(result[i,:])
  RT=pd.DataFrame(result2)
  return RT
def GRA(DataFrame):
  list_columns = [str(s) for s in range(len(DataFrame.columns)) if s not in [None]]
  df_local = pd.DataFrame(columns=list_columns)
  for i in range(len(DataFrame.columns)):
      df_local.iloc[:,i] = GRA_ONE(DataFrame,m=i)[0]
  return df_local
data_wine_gra = GRA(wine)
# data_wine_gra.to_csv(path+"GRA.csv") 存储结果到硬盘
data_wine_gra
Empty DataFrame
Columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Index: []

Python实现灰色关联分析与结果可视化的详细代码

结果可视化

# 灰色关联结果矩阵可视化
import seaborn as sns
%matplotlib inline
def ShowGRAHeatMap(DataFrame):
  import matplotlib.pyplot as plt
  import seaborn as sns
  %matplotlib inline
  colormap = plt.cm.RdBu
  plt.figure(figsize=(14,12))
  plt.title('Pearson Correlation of Features', y=1.05, size=15)
  sns.heatmap(DataFrame.astype(float),linewidths=0.1,vmax=1.0, square=True, cmap=colormap, linecolor='white', annot=True)
  plt.show()
ShowGRAHeatMap(data_wine_gra)

Python实现灰色关联分析与结果可视化的详细代码

 

参考文章

到此这篇关于Python实现灰色关联分析与结果可视化的文章就介绍到这了,更多相关Python灰色关联分析内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/fonttian/p/9162716.html

延伸 · 阅读

精彩推荐