【发布时间】:2026-01-06 20:30:01
【问题描述】:
此代码适合 python 中的回归树。我想将此基于文本的输出转换为表格格式。
已对此 (Convert a decision tree to a table) 进行了调查,但给定的解决方案不起作用。
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeRegressor
from sklearn import tree
dataset = np.array(
[['Asset Flip', 100, 1000],
['Text Based', 500, 3000],
['Visual Novel', 1500, 5000],
['2D Pixel Art', 3500, 8000],
['2D Vector Art', 5000, 6500],
['Strategy', 6000, 7000],
['First Person Shooter', 8000, 15000],
['Simulator', 9500, 20000],
['Racing', 12000, 21000],
['RPG', 14000, 25000],
['Sandbox', 15500, 27000],
['Open-World', 16500, 30000],
['MMOFPS', 25000, 52000],
['MMORPG', 30000, 80000]
])
X = dataset[:, 1:2].astype(int)
y = dataset[:, 2].astype(int)
regressor = DecisionTreeRegressor(random_state = 0)
regressor.fit(X, y)
text_rule = tree.export_text(regressor )
print(text_rule)
我得到的输出是这样的
print(text_rule)
|--- feature_0 <= 20750.00
| |--- feature_0 <= 7000.00
| | |--- feature_0 <= 1000.00
| | | |--- feature_0 <= 300.00
| | | | |--- value: [1000.00]
| | | |--- feature_0 > 300.00
| | | | |--- value: [3000.00]
| | |--- feature_0 > 1000.00
| | | |--- feature_0 <= 2500.00
| | | | |--- value: [5000.00]
| | | |--- feature_0 > 2500.00
| | | | |--- feature_0 <= 4250.00
| | | | | |--- value: [8000.00]
| | | | |--- feature_0 > 4250.00
| | | | | |--- feature_0 <= 5500.00
| | | | | | |--- value: [6500.00]
| | | | | |--- feature_0 > 5500.00
| | | | | | |--- value: [7000.00]
| |--- feature_0 > 7000.00
| | |--- feature_0 <= 13000.00
| | | |--- feature_0 <= 8750.00
| | | | |--- value: [15000.00]
| | | |--- feature_0 > 8750.00
| | | | |--- feature_0 <= 10750.00
| | | | | |--- value: [20000.00]
| | | | |--- feature_0 > 10750.00
| | | | | |--- value: [21000.00]
| | |--- feature_0 > 13000.00
| | | |--- feature_0 <= 16000.00
| | | | |--- feature_0 <= 14750.00
| | | | | |--- value: [25000.00]
| | | | |--- feature_0 > 14750.00
| | | | | |--- value: [27000.00]
| | | |--- feature_0 > 16000.00
| | | | |--- value: [30000.00]
|--- feature_0 > 20750.00
| |--- feature_0 <= 27500.00
| | |--- value: [52000.00]
| |--- feature_0 > 27500.00
| | |--- value: [80000.00]
我想将此规则转换为类似于以下形式的 pandas 表。如何做到这一点?
规则的情节版本是这样的(供参考)。请注意,我在表格中显示了规则的最左侧部分。
【问题讨论】:
-
你能分享一个你正在寻找的输出的例子吗?
-
@quizzical_panini 添加了输出格式以及规则的可视化表示。