Jupyter错误'内核似乎已经死亡,它将自动重新启动'为一个给定的代码块

2024-05-11 06:11:06 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我运行这段代码时,它都会成功运行,但在执行最后4行代码时,内核就会死掉,其中Apriori算法用于市场篮子分析: 数据集:https://archive.ics.uci.edu/ml/datasets/Online+Retail

import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
import pandas as pd
import numpy as np 
import seaborn as sns
import matplotlib.pyplot as plt
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
import mlxtend as ml
print("test1")

retail_df=pd.read_excel("Online Retail.xlsx",sheet_name="Online Retail")
retail_df.head()

rslt_df = retail_df[retail_df['Quantity'] > 5]
rslt_df=rslt_df.iloc[:10000]

rslt_df.shape
rslt_df.head()

df = rslt_df.groupby(['Quantity','Description']).size().reset_index(name='count')
df.head()

basket = df.groupby(['Quantity', 'Description'])['count'].sum().unstack().reset_index().fillna(0).set_index('Quantity')
basket

#The encoding function
def encode_units(x):
    if x <= 0:
        return 0
    if x >= 1:
        return 1
basket_sets = basket.applymap(encode_units)
basket_sets

**#THE NOTEBOOK CRASHES FOR THE BELOW 4 LINES OF CODE**

frequent_itemsets = apriori(basket_sets, min_support=0.01, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="lift")
rules.sort_values('confidence', ascending = False, inplace = True)
rules.head(10)

请帮忙解决这个问题。 我试过各种方法,但都不管用。你知道吗


Tags: importtruedfindexasheadrulesquantity
1条回答
网友
1楼 · 发布于 2024-05-11 06:11:06

我和你有同样的问题。你知道吗

我把最小支持值改为0.5或者其他一些大于0.01的值,结果成功了!你知道吗

我想是因为产量很大。 我希望这对你也有帮助。你知道吗

相关问题 更多 >