【发布时间】:2017-08-14 18:57:35
【问题描述】:
我一直在研究并且正在努力寻找解决这个问题的最佳方法。我有一个训练数据集和一个测试数据集。测试数据集缺少训练数据集具有的两个特征列(Channel 和 Sector - 都由 4 个类组成)。
我已经根据数据构建了一个决策树,但是当我需要同时在这两个通道或扇区上进行训练时,我只能使用它在通道或扇区上进行训练。
谁能给我一个在 python 中实现多类多输出机器学习的建议?
import os
import subprocess
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier, export_graphviz
def getPath(thisFile):
if os.path.exists(thisFile):
df = pd.read_csv(thisFile, header=0)
else:
return
return df
def visualize_tree(tree, feature_names):
with open("dt.dot", 'w') as f:
export_graphviz(tree, out_file=f,
feature_names=feature_names)
command = ["dot", "-Tpng", "dt.dot", "-o", "dt.png"]
try:
subprocess.check_call(command)
except:
exit("Could not run dot, ie graphviz, to "
"produce visualization")
data = np.loadtxt("newTrain2.csv", delimiter=',')
X = data[:, 1:4]
quantity = data[:, 2]
for i in range(len(quantity)):
if quantity[i] < 30:
quantity[i] = 1
if quantity[i] >= 25 and quantity[i] < 75:
quantity[i] = 2
if quantity[i] >= 75 and quantity[i] < 250:
quantity[i] = 3
if quantity[i] > 250:
quantity[i] = 4
revenue = data[:, 3]
for i in range(len(revenue)):
if revenue[i] < 1000:
revenue[i] = 1
if revenue[i] >= 1000 and revenue[i] < 4000:
revenue[i] = 2
if revenue[i] >= 4000 and revenue[i] < 10000:
revenue[i] = 3
if revenue[i] > 10000:
revenue[i] = 4
X[:, 1] = quantity
X[:, 2] = revenue
targets = data[:,4]
thisTree = DecisionTreeClassifier(min_samples_split=30, random_state=99)
thisTree.fit(X, targets)
visualize_tree(thisTree, ["product", "quantity", "revenue"])
【问题讨论】:
标签: python machine-learning neural-network deep-learning decision-tree