基本思想
确定参考数据列和若干个比较数据列的几何形状相似程度来判断其联系是否紧密,它反映了曲线之间的关联程度
写在前面pd.read_csv不需要去头
pd.DataFrame用于定义如字面意思的数据框架,数据结构,也就是有几列,列的index是什么,列名是什么,其中的数据是什么等等
pd.DataFrame赋予变量的iloc方法是按数字索引来取值,loc是按索引名来取值
输入:四个以上样本容量的数据
输出:这几个变量之间的关联程度
实例代码# 佛了。不总结了,把它当黑箱处理吧
# 反正我能得到结果就行┑( ̄Д  ̄)┍
import pandas as pd
import numpy as np
def gra_one(data_frame, m=0):
gray = data_frame
gray = (gray - gray.min()) / (gray.max() - gray.min())
# 标准要素,iloc取列
std = gray.iloc[:, m]
# 比较要素
ce = gray.iloc[:, 0:]
n = ce.shape[0]
m = ce.shape[1]
# 与标准要素比较,相减
a = np.zeros([m, n])
for i in range(m):
for j in range(n):
a[i, j] = abs(ce.iloc[j, i] - std[j])
# 取出矩阵中最大值和最小值
c = np.amax(a)
d = np.amin(a)
# 计算值
result = np.zeros([m, n])
for i in range(m):
for j in range(n):
result[i, j] = (d+0.5*c)/(a[i, j]+0.5*c)
# 求均值,得到灰色关联值
result2 = np.zeros(m)
for i in range(m):
result2[i] = np.mean(result[i, :])
RT = pd.DataFrame(result2)
return RT
def gra(data_frame):
list_columns = [str(s) for s in range(len(data_frame.columns)) if s not in [None]]
df_local = pd.DataFrame(columns=list_columns)
for i in range(len(data_frame.columns)):
# 按列进行的操作
df_local.iloc[:, i] = gra_one(data_frame, i)[0]
return df_local
if __name__ == '__main__':
wine = pd.read_csv("1.csv")
data_wine_gra = gra(wine)
data_wine_gra.to_csv("C:/Users/Herrrb/Desktop/result1.csv")
效果图
处理前:
样本容量为63
处理后这七个量的关联程度
参考文章