build-in函数sorted()的应用

首先help(sorted)一下,得到如下信息:

此函数有三个参数:

  iterable---可迭代的list

  key---决定按照排列的对象

  reverse--决定是升序还是降序

  默认是按照升序对列表元素进行排列

简单的例子:

l = [a, d, c, b]
new_l = sorted(l)
print(new_l)

输出:
['a', 'b', 'c', 'd']

但是当我们进行复杂的排序时,key就能大显身手了!

比如我们将以下元组按照优先级为第一项、第二项、第三项进行排序

  Tom,19,80

  John,20,90

  Jony,17,91

  Jony,17,93

  Json,21,85

我们就可以对key参数进行操作,key  =  operator.itemgetter(0, 1, 2) ,大功告成!

扩展:

  operator模块的itemgetter()函数,此函数返回的是一个一个函数,必须作用在一个对象上

  也就是

    After f = itemgetter(2), the call f(r) returns r[2]

    After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])

转载于:https://www.cnblogs.com/yqs-0705/p/10346381.html

FP-growth算法是一种用于频繁模式挖掘的算法,常用于数据挖掘、市场分析和推荐系统等领域。下面是使用Python实现FP-growth算法的代码: 首先,需要导入相应的库: ``` from collections import defaultdict from itertools import chain from typing import List, Tuple ``` 接着,定义一些常量: ``` # 定义FP树节点 class FPTreeNode: def __init__(self, item=None, count=1, parent=None): self.item = item self.count = count self.parent = parent self.children = defaultdict(FPTreeNode) # 定义FP树 class FPTree: def __init__(self, transactions, support, root_value, root_count): self.frequent_items = self.find_frequent_items(transactions, support) self.headers = self.build_header_table(self.frequent_items) self.root = self.build_fptree(transactions, root_value, root_count, self.frequent_items, self.headers) # 定义FP-growth算法 class FPGrowth: def __init__(self, min_support=0.5, min_confidence=0.5): self.min_support = min_support self.min_confidence = min_confidence # 定义函数:寻找频繁项集 def find_frequent_items(self, transactions, support): items = defaultdict(lambda: 0) for transaction in transactions: for item in transaction: items[item] += 1 # 去除不符合最小支持度的项 items = dict((item, support) for item, support in items.items() if support >= support * len(transactions)) # 返回频繁项集 return items ``` 接着,实现构建FP树的函数: ``` # 定义函数:构建FP树 def build_fptree(self, transactions, root_value, root_count, frequent_items, headers): root = FPTreeNode(item=root_value, count=root_count) for transaction in transactions: sorted_items = sorted([item for item in transaction if item in frequent_items], key=lambda item: frequent_items[item], reverse=True) if len(sorted_items) > 0: self.insert_tree(sorted_items, root, headers) return root # 定义函数:插入节点到FP树中 def insert_tree(self, items, node, headers): if items[0] in node.children: child = node.children[items[0]] else: child = FPTreeNode(item=items[0], parent=node) headers[items[0]].append(child) node.children[items[0]] = child if len(items) > 1: self.insert_tree(items[1:], child, headers) child.count += 1 ``` 最后,实现FP-growth算法的主函数: ``` # 定义函数:寻找频繁模式 def find_frequent_patterns(self, transactions): if not transactions: return None # 构建FP树 support = self.min_support root_value = 'null' root_count = len(transactions) fp_tree = FPTree(transactions, support, root_value, root_count) # 寻找频繁项集和条件模式基 frequent_patterns = defaultdict(int) conditional_patterns = defaultdict(list) self.mine_patterns(fp_tree, fp_tree.header_table, frequent_patterns, conditional_patterns) # 返回频繁模式 return frequent_patterns # 定义函数:挖掘频繁项集 def mine_patterns(self, tree, headers, frequent_patterns, conditional_patterns): sorted_items = [item[0] for item in sorted(headers.items(), key=lambda x: x[1][0].count)] for item in sorted_items: base_patterns = [path(item_node) for item_node in headers[item]] frequent_patterns.update({tuple(pattern): headers[item][0].count for pattern in base_patterns}) conditional_tree = self.build_conditional_tree(base_patterns, headers[item]) if conditional_tree: self.mine_patterns(conditional_tree, conditional_tree.header_table, frequent_patterns, conditional_patterns) ``` 这样,我们就完成了使用Python实现FP-growth算法的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值