使用sklearn预测走势_使用python+sklearn实现梯度提升回归的预测间隔

本示例说明了如何使用分位数回归(quantile regression)来创建预测间隔(prediction intervals)。

6512145f5d7e5b7e06b47212e817bfab.png
sphx_glr_plot_gradient_boosting_quantile_001
import numpy as npimport matplotlib.pyplot as pltfrom sklearn.ensemble import GradientBoostingRegressor
np.random.seed(1)def f(x):"""The function to predict."""return x * np.sin(x)#----------------------------------------------------------------------# 首先是无噪声的情况
X = np.atleast_2d(np.random.uniform(0, 10.0, size=100)).T
X = X.astype(np.float32)# 观察数据
y = f(X).ravel()
dy = 1.5 + 1.0 * np.random.random(y.shape)
noise = np.random.normal(0, dy)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Stacking是一种集成学习方法,可以将多个模型的预测结果结合起来,得到更好的预测效果。在使用Python和scikit-learn库实现Stacking方法时,需要进行以下步骤: 1. 导入必要的库和数据集。 ```python import numpy as np import pandas as pd from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.neighbors import KNeighborsClassifier from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC from sklearn.metrics import accuracy_score from sklearn.model_selection import cross_val_score, KFold from sklearn.model_selection import GridSearchCV from mlxtend.classifier import StackingClassifier iris = load_iris() X, y = iris.data[:, 1:3], iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) ``` 2. 定义基本模型和元模型。 ```python clf1 = KNeighborsClassifier(n_neighbors=3) clf2 = DecisionTreeClassifier() clf3 = RandomForestClassifier(n_estimators=100) clf4 = SVC(kernel='linear', probability=True) lr = LogisticRegression() ``` 3. 定义Stacking模型,并进行交叉验证。 ```python sclf = StackingClassifier(classifiers=[clf1, clf2, clf3, clf4], meta_classifier=lr) kfold = KFold(n_splits=10, shuffle=True, random_state=42) for clf, label in zip([clf1, clf2, clf3, clf4, sclf], ['KNN', 'Decision Tree', 'Random Forest', 'SVM', 'StackingClassifier']): scores = cross_val_score(clf, X, y, cv=kfold, scoring='accuracy') print("Accuracy: %0.2f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label)) ``` 4. 对Stacking模型进行调参。 ```python params = {'kneighborsclassifier__n_neighbors': [1, 3, 5], 'decisiontreeclassifier__max_depth': [1, 2], 'randomforestclassifier__max_depth': [1, 2], 'meta-logisticregression__C': [0.1, 1.0, 10.0]} grid = GridSearchCV(estimator=sclf, param_grid=params, cv=kfold, refit=True) grid.fit(X_train, y_train) print("Best parameters set found on development set:") print(grid.best_params_) print("Grid scores on development set:") means = grid.cv_results_['mean_test_score'] stds = grid.cv_results_['std_test_score'] for mean, std, params in zip(means, stds, grid.cv_results_['params']): print("%0.3f (+/-%0.03f) for %r" % (mean, std * 2, params)) ``` 5. 计算Stacking模型在测试集上的准确率。 ```python y_pred = grid.predict(X_test) print('Accuracy: %.2f' % accuracy_score(y_test, y_pred)) ``` 通过以上步骤,我们就可以使用Python和scikit-learn库实现Stacking方法来组合预测了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值