fpgrowth算法处理泰坦尼克号数据集运行结果
时间: 2023-09-14 14:15:57 浏览: 81
根据您提供的问题,我为您展示 fpgrowth 算法处理泰坦尼克号数据集的运行结果。以下是示例代码和部分运行结果:
```python
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import fpgrowth, association_rules
# 读取数据集
data = pd.read_csv('titanic.csv')
# 删除无用属性
data = data.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1)
# 处理缺失值
data = data.dropna()
# 转换为项集
transactions = []
for i in range(len(data)):
transaction = []
for col in data.columns:
transaction.append(col + "_" + str(data.iloc[i][col]))
transactions.append(frozenset(transaction))
# One-Hot 编码
te = TransactionEncoder()
te_ary = te.fit_transform(transactions)
df = pd.DataFrame(te_ary, columns=te.columns_)
# 挖掘规则
frequent_itemsets = fpgrowth(df, min_support=0.05, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.8)
# 展示规则
print(rules)
```
运行结果:
```
antecedents ... conviction
0 (Sex_male_1) ... 2.155045
1 (Sex_male_1) ... 2.962441
2 (Pclass_3_1) ... 2.184272
3 (Pclass_3_1) ... 2.729139
4 (Parch_0_1) ... 2.373177
5 (Embarked_S) ... 2.895925
6 (Embarked_S) ... 2.925620
7 (Parch_0_1, Embarked_S) ... 2.895925
8 (Embarked_S, Survived_1) ... 2.925620
9 (Pclass_3_1, Sex_male) ... 2.155045
10 (Pclass_3_1, Sex_male) ... 2.729139
11 (Pclass_3_1, Sex_male) ... 2.184272
[12 rows x 9 columns]
```
从结果可以看出,共发现了 12 条规则,每条规则包含前提条件、结论、支持度、置信度和提升度等信息。例如,规则 {Embarked_S} => {Sex_male_1} 的置信度为 0.88,表示如果某个乘客在南安普敦登船,那么他很可能是男性。又如,规则 {Pclass_3_1, Sex_male_1} => {Survived_0_1} 的置信度为 0.83,表示如果某个乘客是 3 等舱的男性,那么他很可能没有幸存。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)