gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:14:00 25 4
gpt4 key购买 nike

背景...

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

我的想法是,我将解析现有 ledger-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)似乎默认为“费用:食品:杂货”。

为了预测收款人,它实际上只是基于交易描述和过去映射到的收款人,不会受到使用的目标分类帐帐户的影响。预测分类账目标账户可能会涉及更多,因为它可以基于描述,以及其他可能的特征,如金额、星期几或交易月份。例如,在 Costco(主要出售散装食品以及大型电子产品和家具)购买的 200 美元或以下的商品很可能被视为杂货,而超过 200 美元的购买可能被视为家居用品或电子产品。也许我应该训练两个独立的分类器?

这是我正在解析的一个总账条目示例,以获取我将用于功能的数据并识别总账目标账户和收款人的类别。

2017/01/01 * TIM HORTONS --payee
;描述:_POS MERCHANDISE TIM HORTONS #57 -- 交易描述
费用:食品:晚餐 -- 目标账户 $ 5.00
Assets :现金

斜体部分是我解析的部分。我想根据新交易的银行交易描述与存储在'Desc ' 分类帐条目的标签。

最佳答案

关于你最后的评论:

 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)

关于python - 使用 scikit-learn 对银行交易的多个输出进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42233232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com