task_ensemble_01

求解Rosenbrock函数最小值

  • 定义a=1,b=100
    图像如:
    在这里插入图片描述
    采用梯度下降:
    x n + 1 = x n − α ∇ f ( x n ) , n ≥ 0 x_{n+1}=x_n-\alpha \nabla f(x_n),\quad n\geq0 xn+1=xnαf(xn),n0

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker


def f(x, y):
    return (1 - x) ** 2 + 100 * (y - x * x) ** 2


def H(x, y):
    return np.matrix([[1200 * x * x - 400 * y + 2, -400 * x],
                      [-400 * x, 200]])


def grad(x, y):
    return np.matrix([[2 * x - 2 + 400 * x * (x * x - y)],
                      [200 * (y - x * x)]])


def delta_grad(x, y):
    g = grad(x, y)

    alpha = 0.002
    delta = alpha * g
    return delta


# ----- 绘制等高线 -----
# 数据数目
n = 256
# 定义x, y
x = np.linspace(-1, 1.1, n)
y = np.linspace(-0.1, 1.1, n)

# 生成网格数据
X, Y = np.meshgrid(x, y)

plt.figure()
# 填充等高线的颜色, 8是等高线分为几部分
plt.contourf(X, Y, f(X, Y), 5, alpha=0, cmap=plt.cm.hot)
# 绘制等高线
C = plt.contour(X, Y, f(X, Y), 8, locator=ticker.LogLocator(), colors='black', linewidth=0.01)
# 绘制等高线数据
plt.clabel(C, inline=True, fontsize=10)
# ---------------------

x = np.matrix([[-0.2],
               [0.4]])

tol = 0.00001
xv = [x[0, 0]]
yv = [x[1, 0]]

plt.plot(x[0, 0], x[1, 0], marker='o')

for t in range(6000):
    delta = delta_grad(x[0, 0], x[1, 0])
    if abs(delta[0, 0]) < tol and abs(delta[1, 0]) < tol:
        break
    x = x - delta
    xv.append(x[0, 0])
    yv.append(x[1, 0])

plt.plot(xv, yv, label='track')
# plt.plot(xv, yv, label='track', marker='o')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Gradient for Rosenbrock Function')
plt.legend()
plt.show()

  • 牛顿法
    x n + 1 = x n − [ H f ( x n ) − 1 ∇ f ( x n ) , n ≥ 0. x_{n+1}=x_n-[\bold{H}f(x_n)^{-1}\nabla f(x_n),\quad n\geq0. xn+1=xn[Hf(xn)1f(xn),n0.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker


def f(x, y):
    return (1 - x) ** 2 + 100 * (y - x * x) ** 2


def H(x, y):
    return np.matrix([[1200 * x * x - 400 * y + 2, -400 * x],
                      [-400 * x, 200]])


def grad(x, y):
    return np.matrix([[2 * x - 2 + 400 * x * (x * x - y)],
                      [200 * (y - x * x)]])


def delta_newton(x, y):
    alpha = 1.0
    delta = alpha * H(x, y).I * grad(x, y)
    return delta


# ----- 绘制等高线 -----
# 数据数目
n = 256
# 定义x, y
x = np.linspace(-1, 1.1, n)
y = np.linspace(-1, 1.1, n)

# 生成网格数据
X, Y = np.meshgrid(x, y)

plt.figure()
# 填充等高线的颜色, 8是等高线分为几部分
plt.contourf(X, Y, f(X, Y), 5, alpha=0, cmap=plt.cm.hot)
# 绘制等高线
C = plt.contour(X, Y, f(X, Y), 8, locator=ticker.LogLocator(), colors='black', linewidth=0.01)
# 绘制等高线数据
plt.clabel(C, inline=True, fontsize=10)
# ---------------------

x = np.matrix([[-0.3],
               [0.4]])

tol = 0.00001
xv = [x[0, 0]]
yv = [x[1, 0]]

plt.plot(x[0, 0], x[1, 0], marker='o')

for t in range(100):
    delta = delta_newton(x[0, 0], x[1, 0])
    if abs(delta[0, 0]) < tol and abs(delta[1, 0]) < tol:
        break
    x = x - delta
    xv.append(x[0, 0])
    yv.append(x[1, 0])

plt.plot(xv, yv, label='track')
# plt.plot(xv, yv, label='track', marker='o')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Newton\'s Method for Rosenbrock Function')
plt.legend()
plt.show()

引用自: SpringHerald.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
随机森林是一种集成学习方法,它由多个决策树组成。每个决策树都是一个分类器,通过对数据集中的观察进行分类来进行预测。因此,随机森林可以用于分类问题。\[2\] 在Python中,可以使用scikit-learn库来实现随机森林分类器。下面是一个使用随机森林进行三分类的示例代码: ```python from sklearn.ensemble import RandomForestClassifier # 创建随机森林分类器对象 rf_classifier = RandomForestClassifier(n_estimators=100) # 训练模型 rf_classifier.fit(X_train, y_train) # 预测 y_pred = rf_classifier.predict(X_test) ``` 在这个示例中,`n_estimators`参数指定了随机森林中决策树的数量。`X_train`和`y_train`是训练数据集的特征和标签,`X_test`是测试数据集的特征。`fit`方法用于训练模型,`predict`方法用于进行预测。 关于为什么随机森林不能使用`decision_function`方法,是因为`decision_function`方法是用于支持向量机(SVM)等模型的,它返回样本到决策边界的距离。而随机森林是基于决策树的集成模型,它的预测结果是通过多个决策树投票得出的,并不涉及到距离的计算。因此,随机森林没有`decision_function`方法。如果想要获取样本到决策边界的距离,可以考虑使用其他模型,如支持向量机。 #### 引用[.reference_title] - *1* *2* [python中实现随机森林_Python中的随机森林](https://blog.csdn.net/cumei1658/article/details/107363668)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [随机森林算法(Random Forest)Python实现](https://blog.csdn.net/weixin_43734080/article/details/122268826)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值