• 使用SKlearn(Sci-Kit Learn)进行SVR模型学习


    今天了解到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_)

  • 相关阅读:
    OAuth2.0 基础概述
    Ubuntu安装Gogs服务
    ASP.NET WebAPI 生成帮助文档与使用Swagger服务测试
    ASP.NET MVC 中的路由
    升级Ghost
    搭建Golang开发环境
    TDD并不是看上去的那么美
    .NET Framework 源码查看与调试
    在 ASP.NET MVC 中使用异步控制器
    SpringMVC+FreeMarker+Mybatis 整合
  • 原文地址:https://www.cnblogs.com/zinyy/p/9535069.html
Copyright © 2020-2023  润新知