支持向量回归 (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 可以使用几种 "backends" 之一来生成图形。这些后端做不同的事情。在您的情况下,您指定了 Agg
用于写入 PNG 文件的后端:
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 notebook 上 运行,则只需在代码顶部添加 %matplotlib inline
。您可以阅读更多相关信息 and here。
否则,我复制了你的代码并删除了 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()
我在 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 可以使用几种 "backends" 之一来生成图形。这些后端做不同的事情。在您的情况下,您指定了 Agg
用于写入 PNG 文件的后端:
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 notebook 上 运行,则只需在代码顶部添加 %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()