使用scikit-learn来分类银行交易的多个输出

时间:2017-02-14 18:06:02

标签: python scikit-learn classification text-classification

...背景

我正在尝试创建一个分类器,它将尝试根据以前的分类帐cli条目和下载的银行对帐单中提供的交易描述自动创建分类帐cli条目。

我的想法是,我将从现有的分类帐-cli文件中解析条目,并提取功能和标签并使用它来学习。然后,当我导入新的交易时,我会使用之前提取的功能来预测两件事.A)分类帐目的地帐户和B)收款人。

我已经做了一大笔谷歌搜索,我认为这让我走得很远,但我不确定我是否以正确的方式接近这一点,因为我在尊重分类方面真的是绿色的,或者我是否理解了所有内容以做出适当的决定这将产生令人满意的结果。如果我的分类器无法预测分类帐帐户和收款人,我会根据需要提示这些值。

我已经将提供给这个问题的答案用作模板,并通过添加银行业务描述而不是提及纽约或伦敦的东西进行修改...... use scikit-learn to classify into multiple categories

每个分类帐条目由一个收款人和一个目的地帐户组成。

当我尝试我的解决方案时(类似于上面链接中提供的解决方案)我期望对于每个输入样本我会得到一个预测的分类帐目标帐户和一个预测的收款人。对于一些样本我确实得到了这个,但对于其他人我只预测了一个分类帐目的地帐户或收款人预测。这是预期的吗?如果分类帐目标帐户或收款人只返回一个值,我怎么知道?

此外,我不确定我尝试做的是否被视为多级,多标签或多输出?

非常感谢任何帮助。

这是我当前的脚本和输出:

#! /usr/bin/env python3

import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier
from sklearn import preprocessing

X_train = np.array(["POS MERCHANDISE",
"POS MERCHANDISE TIM HORTONS #57",
"POS MERCHANDISE LCBO/RAO #0266",
"POS MERCHANDISE RONA HOME & GAR",
"SPORT CHEK #264 NEPEAN ON",
"LOBLAWS 1035 NEPEAN ON",
"FARM BOY #90 NEPEAN ON",
"WAL-MART #3638 NEPEAN ON",
"COSTCO GAS W1263 NEPEAN ON",
"COSTCO WHOLESALE W1263 NEPEAN ON",
"FARM BOY #90",
"LOBLAWS 1035",
"YIG ROSS 819",
"POS MERCHANDISE STARBUCKS #456"
])
y_train_text = [["HOMESENSE","Expenses:Shopping:Misc"],
["TIM HORTONS","Expenses:Food:Dinning"],
["LCBO","Expenses:Food:Alcohol-tobacco"],
["RONA HOME & GARDEN","Expenses:Auto"],
["SPORT CHEK","Expenses:Shopping:Clothing"],
["LOBLAWS","Expenses:Food:Groceries"],
["FARM BOY","Expenses:Food:Groceries"],
["WAL-MART","Expenses:Food:Groceries"],
["COSTCO GAS","Expenses:Auto:Gas"],
["COSTCO","Expenses:Food:Groceries"],
["FARM BOY","Expenses:Food:Groceries"],
["LOBLAWS","Expenses:Food:Groceries"],
["YIG","Expenses:Food:Groceries"],
["STARBUCKS","Expenses:Food:Dinning"]]

X_test = np.array(['POS MERCHANDISE STARBUCKS #123',
                   'STARBUCKS #589',
                   'POS COSTCO GAS',
                   'COSTCO WHOLESALE',
                   "TIM HORTON'S #58",
                   'BOSTON PIZZA',
                   'TRANSFER OUT',
                   'TRANSFER IN',
                   'BULK BARN',
                   'JACK ASTORS',
                   'WAL-MART',
                   'WALMART'])

#target_names = ['New York', 'London']

lb = preprocessing.MultiLabelBinarizer()
Y = lb.fit_transform(y_train_text)

classifier = Pipeline([
    ('vectorizer', CountVectorizer()),
    ('tfidf', TfidfTransformer()),
    ('clf', OneVsRestClassifier(LinearSVC()))])

classifier.fit(X_train, Y)
predicted = classifier.predict(X_test)
all_labels = lb.inverse_transform(predicted)

for item, labels in zip(X_test, all_labels):
    print ('%s => %s' % (item, ', '.join(labels)))

输出:

POS MERCHANDISE STARBUCKS #123 => Expenses:Food:Dinning
STARBUCKS #589 => Expenses:Food:Dinning, STARBUCKS
POS COSTCO GAS => COSTCO GAS, Expenses:Auto:Gas
COSTCO WHOLESALE => COSTCO, Expenses:Food:Groceries
TIM HORTON'S #58 => Expenses:Food:Dinning
BOSTON PIZZA => Expenses:Food:Groceries
TRANSFER OUT => Expenses:Food:Groceries
TRANSFER IN => Expenses:Food:Groceries
BULK BARN => Expenses:Food:Groceries
JACK ASTORS => Expenses:Food:Groceries
WAL-MART => Expenses:Food:Groceries, WAL-MART
WALMART => Expenses:Food:Groceries

正如您所看到的,某些预测仅提供分类帐目的地帐户,而BULK BARN等部分似乎默认为“费用:食品:杂货”。

对于预测收款人,它实际上只是基于交易描述以及过去映射到的收款人,并且不会受到使用的目标分类帐帐户的影响。对于预测分类帐目的地帐户可能更复杂,因为它可以基于描述以及其他可能的特征,例如金额或星期或交易日。例如,购买200美元或更少的Costco(主要销售散装食品加上大型电子产品和家具)的购买很可能被视为购买超过200美元的杂货可能被视为家用电器或电子产品。也许我应该训练两个单独的分类器?

以下是我正在解析的leger条目示例,用于获取我将用于功能的数据以及识别分类帐目标帐户和收款人的类。

2017/01/01 * TIM HORTONS --payee
    ;描述:_POS MERCHANDISE TIM HORTONS#57 - 交易描述
    费用:食物:餐饮 - 目的地帐户$ 5.00
    资产:现金

斜体部分是我解析的部分。我想基于将新交易的银行交易描述与存储在'描述中的先前交易相关联的描述进行匹配来分配目的地账户(例如费用:食品:餐饮)和收款人(例如TIM HORTONS)。 '分类帐条目的标记。

1 个答案:

答案 0 :(得分:1)

关于你的最后评论:

 Training set------->  1st classifier  <------- new data input
                           |                
                           |            
                           | 
                    output labelled data (payee)
                           +              
                     other features
                           |
New training set---> 2d classifier   
                           |
                           |
                    output labelled data (ledger account)

Training set------->  1st classifier  <------- new data input
                           |                
                           |            
                           | 
                         output :
       multi-labelled data (payee and ledger account)

修改 有2个独立的分类器:

Training set for payee (with all relative feature)
            |
            |
      1st classifier <--------new input data
            |
            | 
       output labelled data (payee)


Training set for ledger account (with all relative feature)
            |
            |
      2d classifier <--------new input data
            |
            | 
       output labelled data (ledger account)