例子1:
import numpy as np # np.argmax(a) 返回的是a中元素最大值所对应的索引值 correct = np.array([[1, 3, 5, 7],[5, 7, 2, 2],[4, 6, 8, 1]]) print(correct, '\n') # 行 最大值所对应的索引值 b = np.argmax(correct, axis=1) print(b) # 列 最大值所对应的索引值 c = np.argmax(correct, axis=0) print(c)
[[1 3 5 7] [5 7 2 2] [4 6 8 1]] [3 1 2] [1 1 2 0]
例子2
import numpy as np correct = np.array([0, 1, 2, 3, 4, 5, 6]) test = np.array([0, 1, 2, 5, 4, 3, 6]) n_data = len(correct) # -- 将 周一至周日 转换为独热编码格式 -- correct_data = np.zeros((n_data, 7)) test_data = np.zeros((n_data, 7)) #print(correct_data) print("\n") for i in range(n_data): correct_data[i, correct[i]] = 1 test_data[i, test[i]] = 1 print(correct_data, '\n') print(test_data, '\n') b = np.argmax(correct_data, axis=1) print(b) c = np.argmax(test_data, axis=1) print(c) # 统计两个数组相等的情况 print(b == c) # python中 true为1 false为0 count_test = np.sum(b == c) print(count_test)