Apriori 算法对鸢尾花数据进行关联规则挖掘代码
时间: 2023-12-26 21:06:45 浏览: 67
由于 Apriori 算法是一种无监督学习方法,所以并不适用于对鸢尾花数据进行关联规则挖掘。关联规则挖掘通常用于挖掘大规模数据集中的频繁项集和关联规则,而鸢尾花数据集只包含150个样本,因此使用关联规则挖掘算法并不切实际。对于鸢尾花数据集,我们通常会使用监督学习算法,如决策树、逻辑回归等。
以下是使用决策树算法对鸢尾花数据进行分类的示例代码:
```python
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
# 加载数据集
iris = load_iris()
X, y = iris.data, iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练决策树模型
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
# 在测试集上评估模型性能
score = model.score(X_test, y_test)
print("Accuracy:", score)
```
该代码加载鸢尾花数据集,将其划分为训练集和测试集,然后使用决策树算法训练模型,并在测试集上评估模型性能。
相关问题
Apriori 算法对载入的鸢尾花数据进行关联规则挖掘代码
好的,以下是使用 Apriori 算法对鸢尾花数据进行关联规则挖掘的 Python 代码示例:
```python
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
# 载入鸢尾花数据
iris = pd.read_csv('iris.csv')
# 将数据按照 species 列进行分组
grouped = iris.groupby('species')['sepal_length', 'sepal_width', 'petal_length', 'petal_width'].apply(list)
# 将数据转换成适合 Apriori 算法的格式
te = TransactionEncoder()
te_ary = te.fit(grouped).transform(grouped)
df = pd.DataFrame(te_ary, columns=te.columns_)
# 使用 Apriori 算法计算频繁项集
frequent_itemsets = apriori(df, min_support=0.5, use_colnames=True)
# 根据频繁项集计算关联规则
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
# 输出结果
print(rules[['antecedents', 'consequents', 'support', 'confidence', 'lift']])
```
运行以上代码后,会输出每个关联规则的前件、后件、支持度、置信度和提升度等信息。
使用Apriori算法对商品订单数据进行关联规则挖掘代码python
以下是使用Apriori算法对商品订单数据进行关联规则挖掘的Python代码:
```python
import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
# 读取数据
data = pd.read_csv('order_data.csv', header=None)
# 将数据转换为one-hot编码格式
data_hot_encoded = data.stack().str.get_dummies().sum(level=0)
# 使用Apriori算法找到频繁项集
frequent_itemsets = apriori(data_hot_encoded, min_support=0.1, use_colnames=True)
# 根据频繁项集计算关联规则
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
# 输出关联规则
print(rules)
```
其中,`order_data.csv`是包含订单数据的CSV文件,每行表示一个订单,每个订单包含多个商品。`min_support`参数用于控制频繁项集的最小支持度,`metric`参数用于控制关联规则的评价指标,这里使用了Lift指标,`min_threshold`参数用于控制关联规则的最小阈值。最后,输出的`rules`变量包含了所有满足条件的关联规则。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)