Scipy优化机器学习模型

发布于 2025-02-13 12:38:49 字数 921 浏览 1 评论 0原文

最近,我一直面临一个问题,我认为Scipy可能是一个很好的解决方案。但是,我无法正确应用它。不确定我是否缺少某些东西,或者我正在寻找的东西实际上根本不可能。

这是一个虚构的示例,我为使事情变得更加清晰,更易于可视化。我的情况更加复杂。但是,我想发现的是,会有多少狗和猫给出特定数量的老鼠?

from sklearn.svm import SVR
from scipy.optimize import minimize
from sklearn.ensemble import RandomForestRegressor
import numpy as np
import pandas as pd

n_dogs = [10, 5, 5, 2, 19, 12, 1,2]
n_cats = [5, 100, 5, 3, 1000, 0, 1,2]
n_rats = [100, 0, 50, 30, 0, 1000, 10, 5]


X = np.array([n_dogs, n_cats]).T
y = np.array([n_rats]).T 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)    
model = RandomForestRegressor()
model.fit(X,y)

#Boundaries of condition
bnds = [(100,500), (100,150)]
#Initial guesses
x0 = [300,125]
#Predict from condition
residual_plant = minimize(lambda x: model.predict([[12,8]]), x0, method='SLSQP',bounds=bnds,options = {'eps': np.radians(5.0)})
print(residual_plant)```

Recently I have been facing a problem which I think that SciPy might be a good candidate to solve. However, I have not been able to properly apply it. Not sure if I am missing something or if what I am looking for is actually not possible at all.

This is a fictitious example which I made to makes things more clear and easier to visualize. My case is way more complicated. However, what I want to find out is how many dogs and cats would there be given a specific number of rats?

from sklearn.svm import SVR
from scipy.optimize import minimize
from sklearn.ensemble import RandomForestRegressor
import numpy as np
import pandas as pd

n_dogs = [10, 5, 5, 2, 19, 12, 1,2]
n_cats = [5, 100, 5, 3, 1000, 0, 1,2]
n_rats = [100, 0, 50, 30, 0, 1000, 10, 5]


X = np.array([n_dogs, n_cats]).T
y = np.array([n_rats]).T 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)    
model = RandomForestRegressor()
model.fit(X,y)

#Boundaries of condition
bnds = [(100,500), (100,150)]
#Initial guesses
x0 = [300,125]
#Predict from condition
residual_plant = minimize(lambda x: model.predict([[12,8]]), x0, method='SLSQP',bounds=bnds,options = {'eps': np.radians(5.0)})
print(residual_plant)```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文