使用SKlearn(Sci 您所在的位置:网站首页 svr模型是什么 使用SKlearn(Sci

使用SKlearn(Sci

#使用SKlearn(Sci| 来源: 网络整理| 查看: 265

今天了解到sklearn这个库,简直太酷炫,一行代码完成机器学习。

贴一个自动生成数据,SVR进行数据拟合的代码,附带网格搜索(GridSearch, 帮助你选择合适的参数)以及模型保存、读取以及结果绘制。

from sklearn.svm import SVR from sklearn.externals import joblib from sklearn.model_selection import GridSearchCV import numpy as np import matplotlib.pyplot as plt rng = np.random # svr = joblib.load('svr.pkl') # 读取模型 x = rng.uniform(1, 100, (100, 1)) y = 5 * x + np.sin(x) * 5000 + 2 + np.square(x) + rng.rand(100, 1) * 5000 # 自动选择合适的参数 svr = GridSearchCV(SVR(), param_grid={"kernel": ("linear", 'rbf'), "C": np.logspace(-3, 3, 7), "gamma": np.logspace(-3, 3, 7)}) svr.fit(x, y) # joblib.dump(svr, 'svr.pkl') # 保存模型 xneed = np.linspace(0, 100, 100)[:, None] y_pre = svr.predict(xneed)# 对结果进行可视化: plt.scatter(x, y, c='k', label='data', zorder=1) # plt.hold(True) plt.plot(xneed, y_pre, c='r', label='SVR_fit') plt.xlabel('data') plt.ylabel('target') plt.title('SVR versus Kernel Ridge') plt.legend() plt.show() print(svr.best_params_)

 

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有