Python 求解器函数类似于 excel sheet 求解器函数基于数据集预测 a 和 b 值
Python solver function like excel sheet solver function to predict a and b value based on dataset
Excel sheet 求解器函数根据以下数据帧预测 a=-12.7705719809672
和 b=4.65590041575483
值。
Maturity (C-Days) Strength, (Mpa) x y y^2 (y-x)^2
10.8 23.8 23.8495018161717 -0.6 36 0.002450429804294
28.2 28.4 28.3164712450952 -1.4 1.96000000000001 0.006977052895941
70.7 32.6 32.5941766134432 2.8 7.84 3.3911830989322E-05
105.0 34.4 34.4398346638965 4.6 21.16 0.001586800447746
通过以下公式计算数据帧行值。
x[i] = -a + b*ln(M[i])
y[i] = S[i] - avg_strength
y^2[i] = y[i]^2
(y-x)^2[i] = S[i] - x[i]
sum(y^2) = 208.0944
sum((y-x)^2) = 0.011048194978971
哪里
avg_strength = 23.86
# 平均。强度
i - Row number
S - Strength
M - Maturity
计算 R^2
第一次假设a
和b
值来计算x
和(y-x)^2
。
R^2 = 1 - sum((y-x)^2)/sum(y^2)
其中必须是 R^2 >= 0.9
并预测 a
和 b
值。
I'm looking solution same as excel solver function in python to
predict a and b value.
我的Python代码:
import pandas as pd
import numpy as np
from pulp import LpVariable, LpMinimize, LpProblem,lpSum,LpStatus,lpSum,LpMaximize,LpInteger
import math
m_s_data = {'maturity':[0.1,10.8,28.2,70.7,105.0],'strength':[0.1,23.8,28.4,32.6,34.4]}
df = pd.DataFrame(m_s_data)
strength_avg = round(df['strength'].mean(),2)
df['y'] = df['strength'] - strength_avg
df['y2'] = df['y']**2
y2_sum = sum([df['y2'][idx] for idx in df.index])
x = LpVariable.dicts("x", df.index,lowBound=-100,upBound=100)
y3 = LpVariable.dicts("y3", df.index,lowBound=-100,upBound=100)
mod = LpProblem("calAB", LpMinimize)
a=1
b=1
for idx in df.index:
x_row_data = -a + b * np.log(df['maturity'][idx])
mod += x[idx] == x_row_data
strength = df['strength'][idx]
mod += y3[idx] == math.pow(strength,2) + x_row_data * x_row_data -2* strength * x_row_data
#R^2 must be greater than or equal to 0.9
mod += 1- (lpSum(y3[idx] for idx in df.index)/y2_sum) >= 0.9
mod.solve()
print(df)
# Each of the variables is printed with it's resolved optimum value
for idx in df.index:
print(y3[idx].name, "=", y3[idx].value)
输入数据帧:
异常输出数据帧:
您可以简单地使用任何一种使用最小二乘算法的线性求解器。这里我使用np.linalg.lstsq()
.
import numpy as np
#we have a system of linear equation: Ax = b, according to your equation :-x[0] + x[1]*ln(M) = b
M = np.log([10.80000,28.20000,70.70000,105.00000])
A = np.vstack((-np.ones(M.size),M))
b = np.array([23.84950,28.31647,32.59418,34.43983])
x = np.linalg.lstsq(A.transpose(),b)[0]
结果:
x = array([-12.77023019, 4.65571618])
Excel sheet 求解器函数根据以下数据帧预测 a=-12.7705719809672
和 b=4.65590041575483
值。
Maturity (C-Days) Strength, (Mpa) x y y^2 (y-x)^2
10.8 23.8 23.8495018161717 -0.6 36 0.002450429804294
28.2 28.4 28.3164712450952 -1.4 1.96000000000001 0.006977052895941
70.7 32.6 32.5941766134432 2.8 7.84 3.3911830989322E-05
105.0 34.4 34.4398346638965 4.6 21.16 0.001586800447746
通过以下公式计算数据帧行值。
x[i] = -a + b*ln(M[i])
y[i] = S[i] - avg_strength
y^2[i] = y[i]^2
(y-x)^2[i] = S[i] - x[i]
sum(y^2) = 208.0944
sum((y-x)^2) = 0.011048194978971
哪里
avg_strength = 23.86
# 平均。强度i - Row number
S - Strength
M - Maturity
计算 R^2
第一次假设a
和b
值来计算x
和(y-x)^2
。
R^2 = 1 - sum((y-x)^2)/sum(y^2)
其中必须是 R^2 >= 0.9
并预测 a
和 b
值。
I'm looking solution same as excel solver function in python to predict a and b value.
我的Python代码:
import pandas as pd
import numpy as np
from pulp import LpVariable, LpMinimize, LpProblem,lpSum,LpStatus,lpSum,LpMaximize,LpInteger
import math
m_s_data = {'maturity':[0.1,10.8,28.2,70.7,105.0],'strength':[0.1,23.8,28.4,32.6,34.4]}
df = pd.DataFrame(m_s_data)
strength_avg = round(df['strength'].mean(),2)
df['y'] = df['strength'] - strength_avg
df['y2'] = df['y']**2
y2_sum = sum([df['y2'][idx] for idx in df.index])
x = LpVariable.dicts("x", df.index,lowBound=-100,upBound=100)
y3 = LpVariable.dicts("y3", df.index,lowBound=-100,upBound=100)
mod = LpProblem("calAB", LpMinimize)
a=1
b=1
for idx in df.index:
x_row_data = -a + b * np.log(df['maturity'][idx])
mod += x[idx] == x_row_data
strength = df['strength'][idx]
mod += y3[idx] == math.pow(strength,2) + x_row_data * x_row_data -2* strength * x_row_data
#R^2 must be greater than or equal to 0.9
mod += 1- (lpSum(y3[idx] for idx in df.index)/y2_sum) >= 0.9
mod.solve()
print(df)
# Each of the variables is printed with it's resolved optimum value
for idx in df.index:
print(y3[idx].name, "=", y3[idx].value)
输入数据帧:
异常输出数据帧:
您可以简单地使用任何一种使用最小二乘算法的线性求解器。这里我使用np.linalg.lstsq()
.
import numpy as np
#we have a system of linear equation: Ax = b, according to your equation :-x[0] + x[1]*ln(M) = b
M = np.log([10.80000,28.20000,70.70000,105.00000])
A = np.vstack((-np.ones(M.size),M))
b = np.array([23.84950,28.31647,32.59418,34.43983])
x = np.linalg.lstsq(A.transpose(),b)[0]
结果:
x = array([-12.77023019, 4.65571618])