繁体   English   中英

支持向量回归(SVR)在Ubuntu 18.04 LTS中未绘制任何图形

[英]Support Vector Regression (SVR) plots no graph in Ubuntu 18.04 LTS

我在Ubuntu 18.04 LTS中使用Python 2.7.15rc1。 我试图绘制支持向量回归图,但没有得到任何输出。

import matplotlib
matplotlib.use("Agg")
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt

#Generate Sample data
x = np.sort(5 * np.random.rand(40, 1), axis = 0)
y = np.sin(x).ravel()

#Add noise to targets
y[::5] += 3 * (0.5 - np.random.rand(8))

#create classifier regression model
svr_rbf = SVR(kernel="rbf", C=1000, gamma=0.1)
svr_lin = SVR(kernel="linear", C=1000, gamma=0.1)
svr_poly = SVR(kernel="poly", C=1000, gamma=0.1)

#Fit regression model
y_rbf = svr_rbf.fit(x,y).predict(x)
y_lin = svr_lin.fit(x,y).predict(x)
y_poly = svr_poly.fit(x,y).predict(x)

#Plotting of results
lw = 2
plt.scatter(x, y, color="darkorange", label="data")
plt.plot(x, y_rbf, color="navy", lw=lw, label="RBF Model")
plt.plot(x, y_lin, color="c", lw=lw, label="Linear Model")
plt.plot(x, y_poly, color="cornflowerblue", lw=lw, label="Polynomial Model")
plt.xlabel("data")
plt.ylabel("target")
plt.title("Support Vector Regression")
plt.legend()
plt.show()

python svm.py不输出任何内容。 我错过了进口什么吗? 还是我们无法绘制此图? 我是机器学习的新手

Matplotlib可以使用几种“后端”之一来生成图形。 这些后端做不同的事情。 在您的情况下,您指定了用于写入PNG文件的Agg后端:

matplotlib.use("Agg")

因此,解决方案是删除该行以使用系统的默认后端,或者选择在屏幕上生成图形的后端。 您可以先进行以下操作:

matplotlib.use("GTK3Agg")
matplotlib.use("WXAgg")
matplotlib.use("TkAgg")
matplotlib.use("Qt5Agg")

有关后端的完整列表,请参阅https://matplotlib.org/faq/usage_faq.html#what-is-a-backend

如果您在Jupyter Ipython笔记本上运行,则只需在代码顶部添加%matplotlib inline 您可以在这里这里阅读更多有关它的信息

否则,我复制了您的代码并删除了matplotlib.use("Agg") ,它对我适用于Ubuntu 18.04,matplotlib版本2.2.2。 您可以指定使用的版本吗?

这也是代码,

import matplotlib
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt

#Generate Sample data
x = np.sort(5 * np.random.rand(40, 1), axis = 0)
y = np.sin(x).ravel()

#Add noise to targets
y[::5] += 3 * (0.5 - np.random.rand(8))

#create classifier regression model
svr_rbf = SVR(kernel="rbf", C=1000, gamma=0.1)
svr_lin = SVR(kernel="linear", C=1000, gamma=0.1)
svr_poly = SVR(kernel="poly", C=1000, gamma=0.1)

#Fit regression model
y_rbf = svr_rbf.fit(x,y).predict(x)
y_lin = svr_lin.fit(x,y).predict(x)
y_poly = svr_poly.fit(x,y).predict(x)

#Plotting of results
lw = 2
plt.scatter(x, y, color="darkorange", label="data")
plt.plot(x, y_rbf, color="navy", lw=lw, label="RBF Model")
plt.plot(x, y_lin, color="c", lw=lw, label="Linear Model")
plt.plot(x, y_poly, color="cornflowerblue", lw=lw, label="Polynomial Model")
plt.xlabel("data")
plt.ylabel("target")
plt.title("Support Vector Regression")
plt.legend()
plt.show()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM