Python中的多输出多类机器学习

时间:2017-03-22 15:01:57

标签: python machine-learning neural-network deep-learning decision-tree

我一直在研究,并且正在努力寻找解决这个问题的最佳方法。我有一个训练数据集和一个测试数据集。测试数据集缺少训练数据集具有的两个要素列(通道和扇区 - 均由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"])

1 个答案:

答案 0 :(得分:0)

一种方法是仅将两个缺失列转换为两个类,将两个类组合在一起。如果每列有4个不同的类,则合并列将具有4 * 4 = 16个类。