如何在两个矩阵之间进行优化并返回矩阵参数?

2024-09-28 03:13:30 发布

您现在位置:Python中文网/ 问答频道 /正文

通过公式得出的矩阵如下所示:

公式矩阵由参数A、B和alpha组成,它们是xx2和yy2的系数,它们的A、B和alpha值未知。 只有参数的极限是已知的(“A”,“B”在两个数字1和5之间,除了偶数也包含数字1和5,“alpha”在两个数字1.01和1.5之间,可以包括数字1.01和1.5

例如: uu=alpha*(xx2)+A*(yy2)+B*xx2

yy2和xx2的值如下所示:

yy2= array([0.        , 0.03846154, 0.07692308, 0.11538462, 0.15384615,
       0.19230769, 0.23076923, 0.26923077, 0.30769231, 0.34615385,
       0.38461538, 0.42307692, 0.46153846, 0.5       , 0.53846154,
       0.57692308, 0.61538462, 0.65384615, 0.69230769, 0.73076923,
       0.76923077, 0.80769231, 0.84615385, 0.88461538, 0.92307692,
       0.96153846, 1.        ])

xx2 = array([0.        , 0.01470588, 0.02941176, 0.04411765, 0.05882353,
       0.07352941, 0.08823529, 0.10294118, 0.11764706, 0.13235294,
       0.14705882, 0.16176471, 0.17647059, 0.19117647, 0.20588235,
       0.22058824, 0.23529412, 0.25      , 0.26470588, 0.27941176,
       0.29411765, 0.30882353, 0.32352941, 0.33823529, 0.35294118,
       0.36764706, 0.38235294, 0.39705882, 0.41176471, 0.42647059,
       0.44117647, 0.45588235, 0.47058824, 0.48529412, 0.5       ,
       0.51470588, 0.52941176, 0.54411765, 0.55882353, 0.57352941,
       0.58823529, 0.60294118, 0.61764706, 0.63235294, 0.64705882,
       0.66176471, 0.67647059, 0.69117647, 0.70588235, 0.72058824,
       0.73529412, 0.75      , 0.76470588, 0.77941176, 0.79411765,
       0.80882353, 0.82352941, 0.83823529, 0.85294118, 0.86764706,
       0.88235294, 0.89705882, 0.91176471, 0.92647059, 0.94117647,
       0.95588235, 0.97058824, 0.98529412, 1.        ])

从Excel获得的矩阵如下,其中主矩阵为“df_nump”:

path = '100802_1.xlsx' 

df_main = pd.read_excel(path)


df_summarry = df_main.iloc[1:2, 3:15]
df_summarry.columns = df_main.iloc[0,3:15]

df = df_main.iloc[8:,0:].reset_index()


del df['index']



df.columns = ['x_' + str(x) for x in range(np.size(df,1))]


df_y = normalize(df.iloc[1:,0])


'''x coordinate #experiment data'''
df_y1 = (df.iloc[1:,0]) 


df_x = normalize(df.iloc[0, 1:])


''' y coordinate #experiment data ''' 
df_x1 = (df.iloc[0, 1:]) 


''' the main matrix ''' 
df_nump = df.iloc[1:,1:].fillna(0)

和代码,用于查找两个矩阵中最近的元素(“uu”、“df_nump”),无需重复[duplicated],并返回两个矩阵的索引:

list1=np.array(df_y1)
list2=xx2

index_dfy1=[]
index_xx2=[]
for i in range(len(list1)):

  if (len(list2)) > 1: #When there are elements in list2

    temp_result = abs(list1[i] - list2) #Matrix subtraction

    min_val = np.amin(temp_result) #Getting the minimum value to get closest element
    min_val_index = np.where(temp_result == min_val) #To find index of minimum value

    closest_element = list2[min_val_index] #Actual value of closest element in list2

    list2 = list2[list2 != closest_element] #Remove closest element after found

    print(i, list1[i], min_val_index[0][0], closest_element[0]) #List1 Index, Element to find, List2 Index, Closest Element
    index_dfy1.append(i)
    index_xx2.append(min_val_index[0][0])

  else: #All elements are already found

    print(i, list1[i], 'No further closest unique closest elements found in list2')

idx_dfy1 = index_dfy1
idx_xx2 = index_xx2

print(idx_dfy1)
print(idx_xx2)

###################################################

list1=np.array(df_x1)
list2=yy2

index_dfx1=[]
index_yy2=[]
for i in range(len(list1)):

  if (len(list2)) > 1: #When there are elements in list2

    temp_result = abs(list1[i] - list2) #Matrix subtraction

    min_val = np.amin(temp_result) #Getting the minimum value to get closest element
    min_val_index = np.where(temp_result == min_val) #To find index of minimum value

    closest_element = list2[min_val_index] #Actual value of closest element in list2

    list2 = list2[list2 != closest_element] #Remove closest element after found

    print(i, list1[i], min_val_index[0][0], closest_element[0]) #List1 Index, Element to find, List2 Index, Closest Element
    index_dfx1.append(i)
    index_yy2.append(min_val_index[0][0])

  else: #All elements are already found

    print(i, list1[i], 'No further closest unique closest elements found in list2')

idx_dfx1 = index_dfx1
idx_yy2 = index_yy2

print(idx_dfx1)
print(idx_yy2)

现在我想在两个“uu”、“df_nump”矩阵之间进行优化,以确定“a”、“b”、“alpha”参数的最佳值,并基于平方根(R-square)方法确定误差量


Tags: indfindex矩阵valelementminprint

热门问题