代码之家  ›  专栏  ›  技术社区  ›  van neilsen

Ubuntu 18.04 LTS中的支持向量回归(SVR)图无图

  •  1
  • van neilsen  · 技术社区  · 6 年前

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

    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高级副总裁.py 不输出任何内容。 我错过了要导入的内容吗?或者我们不能画出这张图? 我刚开始学机器

    2 回复  |  直到 6 年前
        1
  •  0
  •   Gambit1614    6 年前

    否则,我复制了您的代码并删除了 matplotlib.use(“agg”) ,它在Ubuntu 18.04,matplotlib version 2.2.2上为我工作。你能指定你使用的版本吗?

    将numpy导入为np Y[::5]+=3*(0.5-np.随机.rand(8)) #拟合回归模型 Lw=2 plt.plot(x,y_poly,color=“cornflowerblue”,lw=lw,label=“多项式模型”)。 图例()
    herehere

    matplotlib.use("Agg")它在Ubuntu18.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()
    

    enter image description here

        2
  •  0
  •   Sergey Kovalev    6 年前

    Matplotlib可以使用几个“后端”中的一个来生成图形。这些后端做不同的事情。在您的情况下,您指定 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