, .
:
- Windows 98/2000/XP;
- Python 2.6;
- SQL Server 2005.
Python 2.6 . , , . .
Apriori, 3.1, , , . , (. 3.2), (. 3.3), (. 3.4).
, (. 3.5), , (. 3.6).
3.1
3.2
3.3 − ( 2-5)
3.4 −
3.5
3.6
, , . :
) .
. , (. 3.7).
3.7
, . 1 3.8, 2 3.9, 3 3.10.
3.8 1
3.9 2
3.10 3
, . (. 3.11).
3.11
, , (. 3.12).
|
|
3.12
, , , (. 3.6). , , .
) .
, 3.6. ( 3.13), .
3.13
. , , , , . , , ( ), ( , ).
, . . , .
SQL SERVER, Python 2.6. . . , . , .
1. Agrawal R. Mining Association Rules Between Sets of Items in Large Databases. / R. Agrawal, T. Imielinski, A. Swami. // SIGMOD Conference 1993. − . 207-216.
2. Hipp J. Algorithms for association rule mining A general survey and comparison. / J. Hipp, U. Güntzer, G. Nakhaeizadeh. // SIGKDD Explorations. − 2(2). 2000. . 1-58.
3. Omiecinski E. Alternative interest measures for mining associations in databases. / E. Omiecinski. // IEEE Transactions on Knowledge and Data Engineering. 2003. 15(1). . 57-69.
4. : [. ]. − : http://wiki.auditory.ru/.
5. : [. ]. − : www.rammus.ru/products/arda/.
6. : [. ]. − : www.rammus.ru/products/arda/article_review_ar_applications/.
7. Association rule learning: [. ]. − : http://en.wikipedia.org/wiki/Association_rule_learning/.
8. : [. ]. − : www.intuit.ru/department/database/datamining/15/.
9. : [. ].− : www.basegroup.ru/library/analysis/association_rules/intro/.
|
|
10. Apriori : [. ]. − : www.basegroup.ru/library/association_rule/.
11. Apriori: [. ]. − : ru.wikipedia/Apriori/.
12. : [. ]. − : www.copy-market.ru/index.php?page...dannyh/.
13. : [. ].− : http://www.copy-market.ru/personalizatsiya-dannyh/.
14. : [. ]. − : www.lscorp.ru/.
15. Data Mining: [. ]. − : www.int.ru/datamining/.
16. Data Mining : [. ]. − : www.inftech.webservis.ru/it/database/datamining/ar2.html/.
17. Apriori algorithm source code: [. ]. − : msug.vn.ua/blogs/datamining/archive/2009/08/02/apriori-algorithm/.
18. Data Mining: [. ]. − : http://habrahabr.ru/blogs/data_mining/66016/.
19. . SQL. / . , . , . . − .: , 1998. 565 .
20. SQL. [. ]. − : www.citforum.ru/database/sqlbook/index.shtml/.
qt
#!/usr/bin/python2.6
# -*- coding: utf-8 -*-
FROM __future__ IMPORT unicode_literals
IMPORT platform
IMPORT sys
FROM PyQt4 IMPORT QtCore, QtGui
IMPORT resource_rc
FROM models IMPORT Category, Item
FROM FormResult IMPORT FormResult
FROM FormTransactions IMPORT FormTransactions
FROM FormCategories IMPORT FormCategories
FROM FormItem IMPORT FormItem
FROM FormCart IMPORT FormCart
FROM FormRules IMPORT FormRules
FROM FormPopular IMPORT FormPopular
FROM FormSupp IMPORT FormSupp
__version__ = "1.0.0"
class Form(QtGui.QDialog):
def __init__(self, parent=NONE):
SUPER(Form, self).__init__(parent)
addAction = QtGui.QAction(QtGui.QIcon(":/edit_add.png"), "&", self,
triggered=self.add_item)
delAction= QtGui.QAction(QtGui.QIcon(":/remove.png"), "&", self,
triggered=self.delete_item)
categoryAction=QtGui.QAction(QtGui.QIcon(":/category.png"), "&", self,
triggered=self.show_categories)
TRANSACTION=QtGui.QAction(QtGui.QIcon(":/database.png"), "&", self,
triggered=self.show_transactions)
popularAction=QtGui.QAction(QtGui.QIcon(":/chart_bar.png"), "& ", self,
triggered=self.show_popular)
rulesAction=QtGui.QAction(QtGui.QIcon(":/calculator.png"), "&", self,
triggered=self.show_rules)
cartAction=QtGui.QAction(QtGui.QIcon(":/cart2.png"), "&", self,
triggered=self.show_cart)
itemsAction=QtGui.QAction(QtGui.QIcon(":/refresh.png"), "&", self,
triggered=self.show_items)
aboutAction=QtGui.QAction(QtGui.QIcon(":/help.png"),"& ", self,
triggered=self.help_about)
toolBar = QtGui.QToolBar()
#QStyle.SH_TitleBar_NoBorder
toolBar.addAction(addAction)
toolBar.addAction(delAction)
toolBar.addAction(categoryAction)
toolBar.addAction(TRANSACTION)
toolBar.addAction(popularAction)
toolBar.addAction(rulesAction)
toolBar.addAction(cartAction)
toolBar.addAction(itemsAction)
toolBar.addAction(aboutAction)
#init grid
self.tableWidget = QtGui.QTableWidget()
self.refreshTable()
layout = QtGui.QVBoxLayout()
layout.addWidget(toolBar)
layout.addWidget(self.tableWidget)
self.setLayout(layout)
self.connect(self.tableWidget,
QtCore.SIGNAL("itemDoubleClicked(QTableWidgetItem*)"),
|
|
self.edit_item)
self.setWindowTitle(u"Apriori. ")
self.setGeometry(250, 250, 950, 400)
screen=QtGui.QDesktopWidget().screenGeometry()
size=self.geometry()
self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2)
def refreshTable(self):
self.tableWidget.clear()
self.tableWidget.setSortingEnabled(True)
self.tableWidget.setRowCount(0)
self.tableWidget.enabledChange(False)
self.tableWidget.setSelectionMode(QtGui.QAbstractItemView.SingleSelection); self.tableWidget.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows);
self.tableWidget.setEditTriggers(QtGui.QTableWidget.NoEditTriggers)
elf.tableWidget.setColumnCount(3)
self.tableWidget.setHorizontalHeaderLabels(["","",''])
self.tableWidget.setColumnWidth(0, 40)
self.tableWidget.setColumnWidth(1, 400)
self.tableWidget.setColumnWidth(2, 400)
for item in Item.get():
no=self.tableWidget.rowCount()
self.tableWidget.insertRow(no)
self.tableWidget.setItem(no, 0, QtGui.QTableWidgetItem(str(item.id)))
self.tableWidget.setItem(no, 1, QtGui.QTableWidgetItem(item.name))
self.tableWidget.setItem(no, 2, QtGui.QTableWidgetItem(Category.get (item.category_id).get_name()))
self.tableWidget.setRowHeight(no, 20)
def show_rules(self):
forms = FormSupp()
if forms.exec_():
form=FormRules(minsupp=forms.minText.text().toInt()[0], maxsupp=forms.maxText.text().toInt()[0])
form.exec_()
def show_popular(self):
forms = FormSupp()
if forms.exec_():
form=FormPopular(minsupp=forms.minText.text().toInt()[0], maxsupp=forms.maxText.text().toInt()[0])
form.exec_()
def show_cart(self):
form = FormCart()
form.exec_()
def add_item(self):
form = FormItem()
form.exec_()
self.refreshTable()
def edit_item(self):
no = self.tableWidget.currentRow()
if no!= -1:
id = self.tableWidget.item(no, 0).text()
form = FormItem(model=Item.get(id))
form.exec_()
self.refreshTable()
def delete_item(self):
reply = QtGui.QMessageBox.question(self,
"",
" ! ?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
no = self.tableWidget.currentRow()
no!= -1:
id = self.tableWidget.item(no, 0).text()
Item(id=id).delete()
self.refreshTable()
def show_items(self):
form = FormResult()
form.exec_()
def show_transactions(self):
form = FormTransactions()
form.exec_()
def show_categories(self):
form = FormCategories()
form.exec_()
def help_about(self):
QtGui.QMessageBox.about(self, "Apriori ",
"""<b>Apriori</b> v {0}
<p>© 2010, .
.
<p>Python {1} Qt {2} PyQt {3} on {4}""".format(
__version__, platform.python_version(),
QtCore.QT_VERSION_STR, QtCore.PYQT_VERSION_STR,
platform.system()))
app = QtGui.QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
Models
MPORT ConfigParser, os
FROM autumn.db.connection IMPORT autumn_db
FROM autumn.model IMPORT Model
FROM autumn.db.relations IMPORT ForeignKey, OneToMany
FROM autumn IMPORT validators
IMPORT DATETIME
config = ConfigParser.ConfigParser()
config.readfp(OPEN('app.cfg'))
autumn_db.conn.connect('mysql', HOST=config.get('Database', 'host'), \
USER=config.get('Database', 'user'), \
db=config.get('Database', 'db'), \
passwd=config.get('Database', 'passwd'), \
CHARSET='utf8')
class ORDER(Model):
items = OneToMany('OrderItem', 'order_id')
|
|
class Meta:
TABLE = 'orders'
class OrderItem(Model):
ORDER = ForeignKey(ORDER, 'order_id')
class Meta:
TABLE = 'order_items'
class Item(Model):
class Meta:
TABLE = 'items'
class Category(Model):
class Meta:
TABLE = 'categories'
def get_name(self):
IF self.lvl == 1:
RETURN self.name
IF self.lvl == 2:
sub_category = Category.get(self.parent_id)
RETURN sub_category.name + ' -> ' + self.name
IF self.lvl == 3:
RETURN sub_category2.name + ' -> ' + sub_category.name + ' -> ' + self.name
def get_parent_name(self):
IF self.lvl == 2:
sub_category = Category.get(self.parent_id)
RETURN sub_category.name
IF self.lvl == 3:
sub_category = Category.get(self.parent_id)
sub_category2 = Category.get(sub_category.parent_id)
RETURN sub_category2.name + ' -> ' + sub_category.name
Apriori
-*- coding: utf-8 -*-
FROM models IMPORT ORDER, OrderItem
class Apriori:
def __init__(self, minsupp=0, maxsupp=1000):
self.orders = NONE
self.items = NONE
self.max_items = 0
self.minsupp = minsupp
self.maxsupp = maxsupp
def transactions(self):
""" """
orders = []
FOR ORDER IN Order.get():
_items = []
FOR item IN order.items:
orders.append(_items)
self.orders = orders
max_items = 0
FOR ORDER IN orders:
IF len(ORDER) > max_items: max_items = len(ORDER)
self.max_items = max_items
RETURN self.orders
def items_(self):
items = {}
IF self.orders == NONE:
self.transactions()
FOR ORDER IN self.orders:
FOR item IN ORDER:
IF item NOT IN items:
items[item] = 1
ELSE:
items[item] = items[item] + 1
RETURN self.items
def calc(self):
IF self.items == NONE:
self.items_()
k = {}
result = []
k[1] = [{'elements': [d], 'cnt': n} FOR d, n IN self.items.iteritems()]
FOR NO IN xrange(2, self.max_items):
k[NO] = self.Apriorigen(k[NO 1], self.orders, self.minsupp, self.maxsupp)
result = result + k[NO]
RETURN result
def Apriorigen(self, DATA, orders, minsupp, maxsupp):
""" k+1 .
k DATA
"""
IF candidate == tmp:
is_exist = TRUE
FOR c IN candidates:
IF c['elements'] == item:
c['cnt'] = c['cnt'] + 1
break
ELSE:
candidates.append({'cnt': 1, 'elements': item[:]})
FOR item IN candidates:
supp = self.find_supp(item['elements'][:len(item['elements']) 1], DATA)
item['supp'] = FLOAT(item['cnt']) / supp * 100
candidates_filter = [line FOR line IN candidates IF line['cnt'] >= minsupp AND line['cnt'] <= maxsupp]
RETURN candidates_filter
def find_supp(self, el, DATA):
FOR i IN DATA:
IF el == i['elements']:
RETURN i['cnt']
RETURN 0
def popular(self):
k = self.calc()
items = sorted(k, KEY = lambda X: -X['supp'])
RETURN items
Allbest.ru