1

I am using Python 2.7.15rc1 in Ubuntu 18.04 LTS. I was trying to plot graph of Support Vector Regression but I dint get any output.

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 outputs nothing. Did I missed something to import? or we can not plot graph of this? I am new to machine learning

van neilsen
  • 547
  • 8
  • 21

2 Answers2

0

Matplotlib can use one of several "backends" for producing graphs. Those backends do different things. In your case you specified Agg backend that is used to write PNG files:

matplotlib.use("Agg")

So the solution is to remove that line to use the default backend for your system or choose a backend that produces graphs on screen. You might these first:

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

See https://matplotlib.org/faq/usage_faq.html#what-is-a-backend for the complete list of backends.

Sergey Kovalev
  • 9,110
  • 2
  • 28
  • 32
0

You just need to add %matplotlib inline at the top of your code if you are running on Jupyter Ipython notebook. You can read more about it here and here.

Otherwise, I copied your code and removed matplotlib.use("Agg") , it works for me on Ubuntu 18.04, matplotlib version 2.2.2. Can you specify which version you are using ?

Also here is the code,

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()

enter image description here

Gambit1614
  • 8,547
  • 1
  • 25
  • 51
  • Python 2.7.15rc1, Ubuntu 18.04 LTS and matplotlib : 2.2.2. I tried removing `matplotlib.use("Agg")` but I got error __tkinter.TclError: no display name and no $DISPLAY environment variable._ I am using putty to connect my machine and running _python svm.py_. Is this correct? – van neilsen Jun 16 '18 at 13:47
  • In your case it would be better to save you figure I think. I am not sure but some environment variable needs to be set to view the png generated. – Gambit1614 Jun 16 '18 at 13:48
  • In _/etc/matplotlibrc_ _backend : TkAgg_ is only enabled, rest of all lines are commented. is that fine? – van neilsen Jun 16 '18 at 14:07
  • I think you need to use ssh with -X arguement to enable display forwarding. Something like ssh -X system-id – Gambit1614 Jun 16 '18 at 14:09
  • I think this should help https://stackoverflow.com/questions/3453188/matplotlib-display-plot-on-a-remote-machine – Gambit1614 Jun 16 '18 at 14:10
  • Getting `/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display` `ImportError: No module named _backend_gdk` – van neilsen Jun 16 '18 at 14:22
  • What is the configuration of the remote machine ? – Gambit1614 Jun 16 '18 at 14:28
  • As per the link shared I need to install Xvfb which doesn't show output. But I need graph to be plotted. – van neilsen Jun 16 '18 at 14:28
  • It is a VM with 4GB RAM where I am running all this. I tried directly in VM terminal but still no luck. – van neilsen Jun 16 '18 at 14:30
  • 1
    I think you will have to save the plot using https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html – Gambit1614 Jun 16 '18 at 14:39
  • Thanks matplotlib.pyplot.savefig worked for me. But still I am thinking why output graph plot is not getting displayed. – van neilsen Jun 16 '18 at 14:48