python中的sklearn.svm.svr_python机器学习库scikit-learn:SVR的基本应用

本文介绍了如何使用Python的scikit-learn库进行支持向量回归(SVR)。通过随机生成数据并用sin函数映射,展示了如何训练和测试SVR模型,并对训练和预测过程的耗时进行了可视化。此外,还探讨了学习曲线,观察模型性能随着训练集大小的变化情况。
摘要由CSDN通过智能技术生成

scikit-learn是python的第三方机器学习库,里面集成了大量机器学习的常用方法。例如:贝叶斯,svm,knn等。

scikit-learn的官网 : http://scikit-learn.org/stable/index.html点击打开链接

SVR是支持向量回归(support vector regression)的英文缩写,是支持向量机(SVM)的重要的应用分支。

scikit-learn中提供了基于libsvm的SVR解决方案。

PS:libsvm是台湾大学林智仁教授等开发设计的一个简单、易于使用和快速有效的SVM模式识别与回归的软件包。

我们自己随机产生一些值,然后使用sin函数进行映射,使用SVR对数据进行拟合

from __future__ import division

import time

import numpy as np

from sklearn.svm import SVR

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import learning_curve

import matplotlib.pyplot as plt

rng = np.random.RandomState(0)

#############################################################################

# 生成随机数据

X = 5 * rng.rand(10000, 1)

y = np.sin(X).ravel()

# 在标签中对每50个结果标签添加噪声

y[::50] += 2 * (0.5 - rng.rand(int(X.shape[0]/50)))

X_plot = np.linspace(0, 5, 100000)[:, None]

#############################################################################

# 训练SVR模型

#训练规模

train_size = 100

#初始化SVR

svr = GridSearchCV(SVR(kernel='rbf', gamma=0.1), cv=5,

param_grid={"C": [1e0, 1e1, 1e2, 1e3],

"gamma": np.logspace(-2, 2, 5)})

#记录训练时间

t0 = time.time()

#训练

svr.fit(X[:train_size], y[:train_size])

svr_fit = time.time() - t0

t0 = time.time()

#测试

y_svr = svr.predict(X_plot)

svr_predict = time.time() - t0

然后我们对结果进行可视化处理

#############################################################################

# 对结果进行显示

plt.scatter(X[:100], y[:100], c='k', label='data', zorder=1)

plt.hold('on')

plt.plot(X_plot, y_svr, c='r',

label='SVR (fit: %.3fs, predict: %.3fs)' % (svr_fit, svr_predict))

plt.xlabel('data')

plt.ylabel('target')

plt.title('SVR versus Kernel Ridge')

plt.legend()

plt.figure()

##############################################################################

# 对训练和测试的过程耗时进行可视化

X = 5 * rng.rand(1000000, 1)

y = np.sin(X).ravel()

y[::50] += 2 * (0.5 - rng.rand(int(X.shape[0]/50)))

sizes = np.logspace(1, 4, 7)

for name, estimator in {

"SVR": SVR(kernel='rbf', C=1e1, gamma=10)}.items():

train_time = []

test_time = []

for train_test_size in sizes:

t0 = time.time()

estimator.fit(X[:int(train_test_size)], y[:int(train_test_size)])

train_time.append(time.time() - t0)

t0 = time.time()

estimator.predict(X_plot[:1000])

test_time.append(time.time() - t0)

plt.plot(sizes, train_time, 'o-', color="b" if name == "SVR" else "g",

label="%s (train)" % name)

plt.plot(sizes, test_time, 'o--', color="r" if name == "SVR" else "g",

label="%s (test)" % name)

plt.xscale("log")

plt.yscale("log")

plt.xlabel("Train size")

plt.ylabel("Time (seconds)")

plt.title('Execution Time')

plt.legend(loc="best")

################################################################################

# 对学习过程进行可视化

plt.figure()

svr = SVR(kernel='rbf', C=1e1, gamma=0.1)

train_sizes, train_scores_svr, test_scores_svr = \

learning_curve(svr, X[:100], y[:100], train_sizes=np.linspace(0.1, 1, 10),

scoring="neg_mean_squared_error", cv=10)

plt.plot(train_sizes, -test_scores_svr.mean(1), 'o-', color="r",

label="SVR")

plt.xlabel("Train size")

plt.ylabel("Mean Squared Error")

plt.title('Learning curves')

plt.legend(loc="best")

plt.show()

看见了熟悉的LOSS下降图,我仿佛又回到了学生时代。。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM),用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据表,使用类的实例表示表的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据,这大大简化了数据操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据引擎和连接池: SQLAlchemy 支持多种数据后端,并且为每种后端提供了对应的数据引擎。 它还提供了连接池管理功能,以优化数据连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值