【问题标题】:plotting a 3d graph of a regressor made with sklearn绘制使用 sklearn 制作的回归量的 3d 图
【发布时间】:2021-03-15 17:47:22
【问题描述】:

我一直在使用这个tutorial 来学习决策树学习,现在我正在尝试了解它如何与更高维数据集一起工作。

目前,我的回归器预测您传递给它的 (x,y) 对的 Z 值。

import numpy as np 
import matplotlib.pyplot as plt 
from sklearn.tree import DecisionTreeRegressor 
from mpl_toolkits import mplot3d
dataset = np.array( 
[['Asset Flip', 100,100, 1000], 
['Text Based', 500,300, 3000], 
['Visual Novel', 1500,500, 5000], 
['2D Pixel Art', 3500,300, 8000], 
['2D Vector Art', 5000,900, 6500], 
['Strategy', 6000,600, 7000], 
['First Person Shooter', 8000,500, 15000], 
['Simulator', 9500,400, 20000], 
['Racing', 12000,300, 21000], 
['RPG', 14000,150, 25000], 
['Sandbox', 15500,200, 27000], 
['Open-World', 16500,500, 30000], 
['MMOFPS', 25000,600, 52000], 
['MMORPG', 30000,700, 80000] 
]) 
X = dataset[:, 1:3].astype(int) 
y = dataset[:, 3].astype(int) 
regressor = DecisionTreeRegressor(random_state = 0) 
regressor.fit(X, y) 

我想使用 3d 图表将其可视化,但我一直在努力解决 regressor.predict() 期望其输入的方式与 matplotlib 线框等程序期望其输入的方式。结果我无法让它们一起工作。

【问题讨论】:

    标签: python matplotlib scikit-learn 3d


    【解决方案1】:

    试试这个,我没有安装所有的包,所以我在google colab上测试了这个。让我知道这是否符合您的预期。

    from mpl_toolkits.mplot3d import Axes3D
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    # to just see the prediction results of your data
    #ax.scatter(X[:, 0], X[:, 1], regressor.predict(regressor.predict(X)), c='g')
    
    samples = 10
    xx, yy = np.meshgrid(np.linspace(min(X[:,0]), max(X[:,0]), samples), np.linspace(min(X[:,1]), max(X[:,1]), samples))
    # to see the decision boundaries(not the right word for a decision tree regressor, I think)
    ax.plot_wireframe(xx, yy, regressor.predict(np.hstack((xx.reshape(-1,1), yy.reshape(-1,1)))).reshape(xx.shape))
    ax.set_xlabel('x-axis')
    ax.set_ylabel('y-axis')
    ax.set_zlabel('z-axis(predictions)')
    

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 2020-02-14
      • 2022-01-25
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多