python - 如何使用 scipy.minimize 函数拟合具有损失函数的多线性回归模型?
问题描述
我目前正在学习多元线性回归,我被困在这个问题上,希望我定义一个函数,该函数将“最小化使用不同的 theta 向量计算的平均损失,并估计模型的最佳 theta”。
from scipy.optimize import minimize
def l1(y, y_hat):
return np.abs(y - y_hat)
def l2(y, y_hat):
return (y - y_hat)**2
def minimize_average_loss(loss_function, model, X, y):
"""
Minimize the average loss calculated from using different theta vectors, and
estimate the optimal theta for the model.
Parameters
-----------
loss_function: either the squared or absolute loss functions defined above
model: the model (as defined in Question 1b)
X: a 2D dataframe of numeric features (one-hot encoded)
y: a 1D vector of tip amounts
Returns
-----------
The estimate for the optimal theta vector that minimizes our loss
"""
## Notes on the following function call which you need to finish:
#
# 0. The first '...' should be replaced with the average loss evaluated on
# the data X, y using the model and appropriate loss function.
# 1. x0 are the initial values for THETA. Yes, this is confusing
# but optimization people like x to be the thing they are
# optimizing. Replace the second '...' with an initial value for theta,
# and remember that theta is now a vector. DO NOT hard-code the length of x0;
# it should depend on the number of features in X.
# 2. Your answer will be very similar to your answer to question 2 from lab 7.
...
return minimize(lambda theta: ..., x0=...)['x']
# Notice above that we extract the 'x' entry in the dictionary returned by `minimize`.
# This entry corresponds to the optimal theta estimated by the function.
minimize_average_loss(l2, linear_model, one_hot_X, tips)
对于上下文,我的线性模型定义如下:
def linear_model(thetas, X):
"""
Return the linear combination of thetas and features as defined above.
Parameters
-----------
thetas: a 1D vector representing the parameters of our model ([theta1, theta2, ...])
X: a 2D dataframe of numeric features
Returns
-----------
A 1D vector representing the linear combination of thetas and features as defined above.
"""
return np.dot(X, thetas)
目前,我有:
def minimize_average_loss(loss_function, model, X, y):
return minimize(lambda theta: loss_function(y, linear_model(theta, X)), x0= [0.0, 0.0])['x']
有谁知道我应该怎么做?谢谢!
解决方案
推荐阅读
- java - 使用可以更改密钥的 RestTemplate 反序列化 JSON
- python - 为什么我最初为 BST 将 root 设置为 None 时会出错
- r - 如何定位列并将其转换为百分比
- vue.js - 使用
在Vue中标记为第一个孩子 tag in Vue 2 In Vue,js 2, I would like to use this pattern:
<template> <component :is="tagType"> ... </component> </template>
Where tagT
- javascript - 在 Web 应用程序中实现端到端 PKI
- python - 有没有更简单的方法可以从三组数字中找到等差数列?
- c++ - 为什么在函数参数中分配新指针不好?
- python - ByBit API:有没有办法在开仓后下单止盈止损单?
- java - Java - 在方法中允许一个线程而无需等待
- sql-server - 如何选择所有参数来运行报告,但让它们独立运行