已有物质物质的颜色与物质浓度数据,已求出各物质的'Multiple R', 'R Square', 'Adjusted R Square', '标准误差', 'SS', 'MS', 'F显著性统计量'七个指标,拟采用灰色关联度分析法对各个指标数值进行分析,具体步骤如下: 把 5 种物质作为评 价对象,相关系数、拟合效果、决定系数、标准误差、误差平方和、均方差和 F 显著性统计量为评价指标,计算灰色关联系数:计算灰色加权关系度,得到各个物质的灰色关联度,评价物质的优劣,求代码实现上述过程
时间: 2023-12-22 17:02:25 浏览: 64
(完整word)Excel数据分析工具进行多元回归分析.doc
以下是使用Python进行灰色关联度分析的代码实现,其中使用了pandas、numpy和scipy库:
```python
import pandas as pd
import numpy as np
from scipy.stats import pearsonr
# 读取数据
data = pd.read_csv('data.csv')
# 提取各个指标的数据
X = data.iloc[:, 1:].values
# 计算各指标之间的相关系数
corr = np.zeros((X.shape[1], X.shape[1]))
for i in range(X.shape[1]):
for j in range(X.shape[1]):
corr[i][j] = pearsonr(X[:, i], X[:, j])[0]
# 计算各指标的拟合效果、决定系数、标准误差、误差平方和、均方差和 F 显著性统计量
Y = X[:, 0] # 第一列为评价对象
Multiple_R = np.zeros(X.shape[1] - 1)
R_Square = np.zeros(X.shape[1] - 1)
Adjusted_R_Square = np.zeros(X.shape[1] - 1)
SE = np.zeros(X.shape[1] - 1)
SS = np.zeros(X.shape[1] - 1)
MS = np.zeros(X.shape[1] - 1)
F = np.zeros(X.shape[1] - 1)
for i in range(1, X.shape[1]):
x = X[:, i]
slope, intercept, r_value, p_value, std_err = linregress(x, Y)
Multiple_R[i - 1] = r_value
R_Square[i - 1] = r_value ** 2
Adjusted_R_Square[i - 1] = 1 - (1 - r_value ** 2) * (X.shape[0] - 1) / (X.shape[0] - 2)
SE[i - 1] = std_err
SS[i - 1] = sum((Y - slope * x - intercept) ** 2)
MS[i - 1] = SS[i - 1] / (X.shape[0] - 2)
F[i - 1] = MS[i - 1] / np.mean(MS)
# 计算关联度
def grey_relation(x, y):
return np.min((np.abs(x - y) + 0.5) / (np.max(x) - np.min(x) + 0.5))
grey_relation_matrix = np.zeros((X.shape[1] - 1, X.shape[1] - 1))
for i in range(X.shape[1] - 1):
for j in range(X.shape[1] - 1):
grey_relation_matrix[i][j] = grey_relation(X[:, i + 1], X[:, j + 1])
# 计算灰色加权关系度
weight = np.zeros(X.shape[1] - 1)
for i in range(X.shape[1] - 1):
weight[i] = np.sum(grey_relation_matrix[i, :]) / np.sum(grey_relation_matrix)
# 计算各个物质的灰色关联度
grey_relation_degree = np.zeros(X.shape[0] - 1)
for i in range(X.shape[0] - 1):
grey_relation_degree[i] = np.sum(weight * grey_relation_matrix[:, i])
# 输出各个物质的灰色关联度
print(grey_relation_degree)
```
需要注意的是,这里的数据格式为csv格式,第一列为评价对象,后面的列为各个指标的数据。在实际使用时,需要根据数据格式进行相应的修改。
阅读全文