欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)灰色關(guān)聯(lián)分析與結(jié)果可視化的詳細(xì)代碼

 更新時間:2022年03月25日 16:40:02   作者:FontTian的專欄  
今天小編通過代碼以灰色色系為例給大家介紹Python灰色關(guān)聯(lián)分析實(shí)現(xiàn)方法,灰色關(guān)聯(lián)度分析對于一個系統(tǒng)發(fā)展變化態(tài)勢提供了量化的度量,非常適合動態(tài)歷程分析,感興趣的朋友一起看看吧

之前在比賽的時候需要用Python實(shí)現(xiàn)灰色關(guān)聯(lián)分析,從網(wǎng)上搜了下只有實(shí)現(xiàn)兩個列之間的,于是我把它改寫成了直接想Pandas中的計(jì)算工具直接計(jì)算person系數(shù)那樣的形式,可以對整個矩陣進(jìn)行運(yùn)算,并給出了可視化效果,效果請見實(shí)現(xiàn)

灰色關(guān)聯(lián)分析法

對于兩個系統(tǒng)之間的因素,其隨時間或不同對象而變化的關(guān)聯(lián)性大小的量度,稱為關(guān)聯(lián)度。在系統(tǒng)發(fā)展過程中,若兩個因素變化的趨勢具有一致性,即同步變化程度較高,即可謂二者關(guān)聯(lián)程度較高;反之,則較低。因此,灰色關(guān)聯(lián)分析方法,是根據(jù)因素之間發(fā)展趨勢的相似或相異程度,亦即“灰色關(guān)聯(lián)度”,作為衡量因素間關(guān)聯(lián)程度的一種方法。

簡介

灰色系統(tǒng)理論提出了對各子系統(tǒng)進(jìn)行灰色關(guān)聯(lián)度分析的概念,意圖透過一定的方法,去尋求系統(tǒng)中各子系統(tǒng)(或因素)之間的數(shù)值關(guān)系。因此,灰色關(guān)聯(lián)度分析對于一個系統(tǒng)發(fā)展變化態(tài)勢提供了量化的度量,非常適合動態(tài)歷程分析。

計(jì)算步驟

  • 確實(shí)參考數(shù)列與比較數(shù)列
  • 對參考數(shù)列與比較數(shù)列進(jìn)行無量綱化處理
  • 計(jì)算關(guān)聯(lián)系數(shù),求關(guān)聯(lián)度

此處我給出的是第三步的實(shí)現(xiàn)方式,無量綱化請自己處理.數(shù)據(jù)使用UCI的紅酒質(zhì)量數(shù)據(jù)集.

代碼實(shí)現(xiàn)

下載數(shù)據(jù)

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

# 定義下載數(shù)據(jù)的函數(shù)
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
# 從硬盤讀取數(shù)據(jù)進(jìn)入內(nèi)存
wine = pd.read_csv("/home/font/Data/UCI/WINE/wine.csv")
wine.head()

實(shí)現(xiàn)灰色關(guān)聯(lián)分析

import pandas as pd
from numpy import *
def GRA_ONE(DataFrame,m=0):
    gray= DataFrame
    #讀取為df格式
    gray=(gray - gray.min()) / (gray.max() - gray.min())
    #標(biāo)準(zhǔn)化
    std=gray.iloc[:,m]#為標(biāo)準(zhǔn)要素
    ce=gray.iloc[:,0:]#為比較要素
    n=ce.shape[0]
    m=ce.shape[1]#計(jì)算行列

    #與標(biāo)準(zhǔn)要素比較,相減
    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)
    #計(jì)算值
    result=zeros([m,n])
            result[i,j]=(d+0.5*c)/(a[i,j]+0.5*c)
    #求均值,得到灰色關(guān)聯(lián)值
    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") 存儲結(jié)果到硬盤
data_wine_gra
Empty DataFrame
Columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Index: []

結(jié)果可視化

# 灰色關(guān)聯(lián)結(jié)果矩陣可視化
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)

參考文章

到此這篇關(guān)于Python實(shí)現(xiàn)灰色關(guān)聯(lián)分析與結(jié)果可視化的文章就介紹到這了,更多相關(guān)Python灰色關(guān)聯(lián)分析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論