gpt4 book ai didi

python - 在 Python 中从每组行索引中的多个列中查找最大值,将这些值按对角线排列在矩阵中,并找到行列式

转载 作者:太空宇宙 更新时间:2023-11-04 04:10:59 26 4
gpt4 key购买 nike

我是 Python 新手。我想从重复行索引的所有列中找到最大值(即 5 到 130),并在输出中显示其行和列索引标签。最大值应该是绝对值。 (不考虑 + 或 - 符号)。不同组中的行索引不应重复。从每组中找到最大的值后,我想将这些值对角排列在方阵中。然后用主数据框中每个组的相应索引值填充剩余的数组,并找到它的行列式。

df=pd.DataFrame(
{'0_deg': [43, 50, 45, -17, 5, 19, 11, 32, 36, 41, 19, 11, 32, 36, 1, 19, 7, 1, 36, 10],
'10_deg': [47, 41, 46, -18, 4, 16, 12, 34, -52, 31, 16, 12, 34, -71, 2, 9, 52, 34, -6, 9],
'20_deg': [46, 43, -56, 29, 6, 14, 13, 33, 43, 6, 14, 13, 37, 43, 3, 14, 13, 25, 40, 8],
'30_deg': [-46, 16, -40, -11, 9, 15, 33, -39, -22, 21, 15, 63, -39, -22, 4, 6, 25, -39, -22, 7]
}, index=[5, 10, 12, 101, 130, 5, 10, 12, 101, 130, 5, 10, 12, 101, 130, 5, 10, 12, 101, 130]
)

数据集:

data

预期输出:

op1

我的代码只显示到输出 1。

op2

op3

实际输出:

actop

代码:

df = pd.read_csv ('Matrixfile.csv')
df = df.set_index('Number')

def f(x):
x1 = x.abs().stack()
x2 = x.stack()
x = x2.iloc[np.argsort(-x1)].head(1)
return x

groups = (df.index == 5).cumsum()
df1 = df.groupby(groups).apply(f).reset_index(level=[1,2])
df1.columns = ['Number','Angle','Value']

print (df1)
df1.to_csv('Matrix_OP.csv', encoding='utf-8', index=True)

最佳答案

根据我从您的问题中理解的内容,我不确定@piRSquared 的输出。那里可能有一些错误,例如在第2组中,max(abs(values)) = 52(图中红色下划线)但是41显示在左侧...

这是一种不太优雅的方式,但对您来说可能更容易理解:

import numpy as np

# INPUT
data_dict ={'0_deg': [43, 50, 45, -17, 5, 19, 11, 32, 36, 41, 19, 11, 32, 36, 1, 19, 7, 1, 36, 10],
'10_deg': [47, 41, 46, -18, 4, 16, 12, 34, -52, 31, 16, 12, 34, -71, 2, 9, 52, 34, -6, 9],
'20_deg': [46, 43, -56, 29, 6, 14, 13, 33, 43, 6, 14, 13, 37, 43, 3, 14, 13, 25, 40, 8],
'30_deg': [-46, 16, -40, -11, 9, 15, 33, -39, -22, 21, 15, 63, -39, -22, 4, 6, 25, -39, -22, 7],
}

# Row idx of a group in this list
idx = [5, 10, 12, 101, 130]


# Getting some dimensions and sorting the data
row_idx_length = len(idx)
group_length = len(data_dict['0_deg'])
number_of_groups = len(data_dict.keys())
idx = idx*number_of_groups
data_arr = np.zeros((group_length,number_of_groups),dtype=np.int32)
#
col = 0
keys = []
for key in sorted(data_dict):
data_arr[:,col] = data_dict[key]
keys.append(key)
col+=1
def get_extrema_value_group(arr):
# function to find absolute extrema value of a 2d array
extrema = 0
for i in range(0, len(arr)):
max_value = max(arr[i])
min_value = min(arr[i])
if (abs(min_value) > max_value) and (abs(extrema) < abs(min_value)):
extrema = min_value
elif (abs(min_value) < max_value) and (abs(extrema) < max_value):
extrema = max_value
return extrema

# For output 1
max_values = []
for i in range(0,row_idx_length*number_of_groups,row_idx_length):
# get the max value for the current group
value = get_extrema_value_group(data_arr[i:i+row_idx_length])
# get the row and column idx associated with the max value
idx_angle_number = np.nonzero(abs(data_arr[i:i+row_idx_length,:])==value)
print('Group number : ' + str(i//row_idx_length+1))
print('Number : '+ str(idx[idx_angle_number[0][0]]))
print('Angle : '+ keys[idx_angle_number[1][0]])
print('Absolute extrema value : ' + str(value))
print('------')
max_values.append(value)

# Arrange those values diagonally in square matrix for output 2
A = np.diag(max_values)
print('A = ' + str(A))

# Fill A with desired values
for i in range(0,number_of_groups,1):
A[i,0] = data_arr[i*row_idx_length+2,2] # 20 deg 12
A[i,1:3] = data_arr[i*row_idx_length+3,1] # x2 : 10 deg 101
A[i,3] = data_arr[i*row_idx_length+1,1] # 10 deg 10

# Final output
# replace the diagonal of A with max values
# get the idx of diag
A_di = np.diag_indices(number_of_groups)
# replace with max values
A[A_di] = max_values
print ('A = ' + str(A))

# Compute determinant of A
det_A = np.linalg.det(A)
print ('det(A) = '+str(det_A))

输出 1:

Group number : 1
Number : 12
Angle : 20_deg
Absolute extrema value : -56
------
Group number : 2
Number : 101
Angle : 10_deg
Absolute extrema value : -52
------
Group number : 3
Number : 101
Angle : 10_deg
Absolute extrema value : -71
------
Group number : 4
Number : 10
Angle : 10_deg
Absolute extrema value : 52
------

输出 2:

A = [[-56  0  0  0]
[ 0 -52 0 0]
[ 0 0 -71 0]
[ 0 0 0 52]]

输出 3:

A = [[-56 -18 -18  41]
[ 33 -52 -52 12]
[ 37 -71 -71 12]
[ 25 -6 -6 52]]

det(A) = -5.4731330578761246e-11

关于python - 在 Python 中从每组行索引中的多个列中查找最大值,将这些值按对角线排列在矩阵中,并找到行列式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com