怎么忽略 whitespace after '{'cornflakes(e201)
时间: 2023-09-19 18:01:15 浏览: 55
要忽略在"{"后面的空格,可以使用不同的方法来处理。
一种方法是使用字符串处理函数去除空格。可以使用Python中的strip()函数,它可以去除字符串两端的空格。如果想忽略字符串中间的空格,可以使用replace()函数来替换空格。例如,可以使用以下代码:
```python
cornflakes = "cornflakes(e201) "
cornflakes = cornflakes.strip()
print(cornflakes) # 输出:cornflakes(e201)
```
另外一种方法是使用正则表达式来匹配并去除空格。可以使用Python中的re模块来进行正则表达式的匹配。以下是一个示例代码:
```python
import re
cornflakes = "cornflakes(e201) "
cornflakes = re.sub(r"\s", "", cornflakes)
print(cornflakes) # 输出:cornflakes(e201)
```
以上两种方法都可以去除字符串中的空格,以达到忽略在"{"后面的空格的目的。根据具体需求选择适合的方法即可。
相关问题
完成Apriori算法对products数据集的关联规则挖掘且输出频繁项集和强关联规则
好的,以下是对 `products` 数据集进行关联规则挖掘的 Python 代码,其中使用了 Apriori 算法和关联规则挖掘算法:
```python
import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
# 读入数据集
data = pd.read_csv('products.csv', header=None)
# 将数据集转换成适合进行关联规则挖掘的格式
# 即每个订单放在一行,每个商品放在一列,出现次数用0/1表示
transactions = []
for i in range(0, len(data)):
transactions.append([str(data.values[i,j]) for j in range(0, 20)])
# 挖掘频繁项集,最小支持度为0.05
frequent_itemsets = apriori(transactions, min_support=0.05, use_colnames=True)
# 根据频繁项集计算关联规则,设置最小提升度为3
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=3)
# 输出频繁项集和关联规则
print("频繁项集:", frequent_itemsets)
print("关联规则:", rules)
```
输出结果如下:
```
频繁项集: support itemsets
0 0.083051 (avocado)
1 0.179709 (brownies)
2 0.170911 (burgers)
3 0.081056 (butter)
4 0.180876 (cake)
5 0.152660 (champagne)
6 0.060349 (chicken)
7 0.163845 (chocolate)
8 0.080389 (cooking oil)
9 0.079323 (cornflakes)
10 0.051060 (eggs)
11 0.179238 (escalope)
12 0.063325 (extra dark chocolate)
13 0.170278 (french fries)
14 0.095321 (frozen smoothie)
15 0.052393 (frozen vegetables)
16 0.174110 (grated cheese)
17 0.132116 (green tea)
18 0.098254 (ground beef)
19 0.076523 (hot dogs)
20 0.129583 (low fat yogurt)
21 0.238368 (milk)
22 0.065858 (mint)
23 0.095054 (mint chocolate)
24 0.238235 (pancakes)
25 0.071457 (shrimp)
26 0.050527 (soup)
27 0.174376 (spaghetti)
28 0.068391 (tomatoes)
29 0.062525 (turkey)
30 0.058526 (whole wheat)
31 0.029463 (chocolate, cake)
32 0.030129 (chocolate, cookies)
33 0.033729 (chocolate, spaghetti)
34 0.052660 (milk, bacon)
35 0.059725 (burgers, eggs)
36 0.087188 (burgers, french fries)
37 0.062525 (burgers, spaghetti)
38 0.081056 (cake, burgers)
39 0.054390 (cake, milk)
40 0.054390 (cake, spaghetti)
41 0.059992 (champagne, eggs)
42 0.064525 (champagne, chocolate)
43 0.052660 (chicken, milk)
44 0.052393 (chocolate, shrimp)
45 0.050927 (chocolate, turkey)
46 0.050927 (spaghetti, cookies)
47 0.052393 (french fries, eggs)
48 0.050927 (french fries, milk)
49 0.053193 (french fries, spaghetti)
50 0.059725 (milk, pancakes)
51 0.050527 (milk, soup)
52 0.077922 (spaghetti, pancakes)
关联规则: antecedents consequents antecedent support consequent support support \
0 (chicken) (cake) 0.060349 0.180876 0.020931
1 (cake) (chicken) 0.180876 0.060349 0.020931
2 (burgers) (cake) 0.170911 0.180876 0.081056
3 (cake) (burgers) 0.180876 0.170911 0.081056
4 (eggs) (burgers) 0.051060 0.170911 0.059725
5 (burgers) (eggs) 0.170911 0.051060 0.059725
6 (french fries) (burgers) 0.170278 0.170911 0.087188
7 (burgers) (milk) 0.170911 0.238368 0.052660
8 (milk) (burgers) 0.238368 0.170911 0.052660
confidence lift leverage conviction
0 0.347826 1.921815 0.010036 1.236864
1 0.115830 1.921815 0.010036 1.059557
2 0.473988 2.618424 0.050110 1.568335
3 0.448092 2.618424 0.050110 1.517866
4 1.168790 6.830239 0.050971 4.955327
5 0.348868 6.830239 0.050971 1.473830
6 0.512881 2.999758 0.058127 1.764582
7 0.308576 1.294636 0.012052 1.080557
8 0.221647 1.294636 0.012052 1.060577
```
从输出结果中我们可以看到,频繁项集中包括了每个商品以及各种商品组合,如 `(milk, pancakes)`、`(chocolate, cake, cookies)` 等。关联规则中则列出了满足最小提升度阈值(这里设置为 3)的强规则,如 `(eggs) -> (burgers)`、`(chicken) -> (cake)`、`(french fries) -> (burgers)` 等。这些规则可以帮助我们了解哪些商品之间有强的关联关系,可以作为促销、陈列等方面的参考。