国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python機器學習庫scikit-learn:SVR的基本應用

2019-11-06 07:37:12
字體:
來源:轉載
供稿:網友

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 divisionimport timeimport numpy as npfrom sklearn.svm import SVRfrom sklearn.model_selection import GridSearchCVfrom sklearn.model_selection import learning_curveimport matplotlib.pyplot as pltrng = 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#初始化SVRsvr = 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() - t0t0 = 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下降圖,我仿佛又回到了學生時代。。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 承德县| 台北县| 大竹县| 淮北市| 龙泉市| 泽普县| 安图县| 汽车| 大同县| 容城县| 镇平县| 哈密市| 亳州市| 连江县| 大新县| 铜川市| 喜德县| 郸城县| 南投市| 子长县| 普兰县| 开封县| 且末县| 贡嘎县| 神农架林区| 包头市| 平顶山市| 合山市| 长白| 南靖县| 娱乐| 扎囊县| 玛多县| 萝北县| 奉化市| 巴塘县| 柳河县| 喜德县| 罗城| 班玛县| 镇巴县|