点击(此处)折叠或打开
- #coding:utf-8
- import sys
- import pandas as pd
- import numpy as np
- from numpy import *
- import matplotlib.pyplot as plt
- def GRA_ONE(gray, m=0):
- # 读取为df格式
- gray = (gray - gray.min()) / (gray.max() - gray.min())
- # 标准化
- std = gray.iloc[:, m] # 为标准要素
- ce = gray.iloc[:, 0:] # 为比较要素
- n, m = ce.shape[0], 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, d = amax(a), amin(a)
- # 计算值
- result = zeros([m, n])
- for i in range(m):
- for j in range(n):
- ss = a[i, j] + 0.5 * c
- if ss ==0:
- result[i, j] =1
- else:
- result[i, j] = (d + 0.5 * c) / (a[i, j] + 0.5 * c)
- # 求均值,得到灰色关联值,并返回
- return pd.DataFrame([mean(result[i, :]) for i in range(m)])
- 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
- wine = pd.read_csv("h2.csv")
- wine.head()
- print "----------------------------"
- data_wine_gra = GRA(wine)
- print data_wine_gra
附件
h.csv
h2.csv
该输出为矩阵 [i,j] 表示第i列和第j列关联度
h.csv
点击(此处)折叠或打开
- a,b,c ,d
- 1,2,1,1
- 2,2,2,1
- 3,3,3,2
- 4,2,4,2
- 5,4,5,3
- 6,1,6,3
h2.csv
点击(此处)折叠或打开
- a,b
- 1,100
- 2,200
- 3,300
- 4,400
- 5,500
- 6,491