我有8个自变量X,优化这段代码的归一化处理部分为8个X。import numpy as np def gray_relation_analysis(X, Y): # 将X、Y序列进行归一化处理 X0 = np.array([min(X), max(X)]) X1 = (X - X0[0]) / (X0[1] - X0[0]) Y1 = (Y - min(Y)) / (max(Y) - min(Y)) # 求出X1、Y1的均值 x_mean = np.mean(X1) y_mean = np.mean(Y1) # 计算灰色关联度 k = len(X1) delta_x = np.abs(X1 - x_mean) delta_y = np.abs(Y1 - y_mean) # 求出最大值和最小值 delta_x_max = np.max(delta_x) delta_x_min = np.min(delta_x) delta_y_max = np.max(delta_y) delta_y_min = np.min(delta_y) # 计算关联度 r = 0.5 for i in range(k): a = r * (delta_x_max - delta_x[i]) / (delta_x_max - delta_x_min) + (1 - r) * (delta_y_max - delta_y[i]) / (delta_y_max - delta_y_min) print("第%d个元素的关联度为%f" % (i+1, a)) # 测试X = np.array([1, 2, 3, 4, 5])Y = np.array([2, 3, 5, 7, 9]) gray_relation_analysis(X, Y)
时间: 2024-03-23 10:41:24 浏览: 64
一种基于改进灰色关联分析的变量选择算法
_total);
printf("排序成功!\n");
}
int main() {
int choice;
while (1) {
show_menu优化后的代码如下:
import numpy as np
def gray_relation_analysis(X, Y):
# 将X、Y序列进行归一化处理
X0 = np.array([np.min(X, axis=0), np.max(X, axis=0)])
();
scanf("%d", &choice);
switch (choice) {
case 0:
printf("谢谢使用!\ X1 = (X - X0[0]) / (X0[1] - X0[0])
Y1 = (Y - np.min(Y, axis=0)) / (np.max(Y, axis=0) - np.min(Y, axis=0n");
return 0;
case 1:
input_worker();
break;
case 2:
add_worker();
))
# 求出X1、Y1的均值
x_mean, y_mean = np.mean(X1, axis= break;
case 3:
delete_worker();
break;
case 4:
modify_worker();
break;
0), np.mean(Y1, axis=0)
# 计算灰色关联度
k = len(X1)
case 5:
browse_workers();
break;
case 6:
query_worker_by_id();
break;
case delta_x, delta_y = np.abs(X1 - x_mean), np.abs(Y1 - y_mean)
# 求出最大值和最小值
delta_x_max, delta_x_min = np.max(delta_x, axis=0), np.min(delta_x7:
sort_workers_by_total();
break;
default:
printf("无效选项!\n");
break;
, axis=0)
delta_y_max, delta_y_min = np.max(delta_y, axis=0), np.min(delta_y, axis=0)
# 计算关联度
r = 0.5
a = r * (delta_x_max }
}
return 0;
}
```
在这个程序中,我们使用结构体 `worker` 来存储 - delta_x) / (delta_x_max - delta_x_min) + (1 - r) * (delta_y_max - delta_y)每个职工的信息,使用数组 `workers` 来存储所有职工的信息。程序通过菜单选项来实 / (delta_y_max - delta_y_min)
print("元素的关联度为:")
for i in range(k):
print("第%d个元素的关联度为:" % (i+1))
print(a[i])
# 测试
X现各种职工工资管理的功能,包括录入职工信息、添加新的记录、删除指定编号的 = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 记录、修改指定编号的记录、浏览职工信息、按编号查询职工信息和按工资总额排序。
阅读全文