Jupyter 错误 - 对于给定的一段代码,“内核似乎已经死亡,它将自动重新启动”

Posted

技术标签:

【中文标题】Jupyter 错误 - 对于给定的一段代码,“内核似乎已经死亡,它将自动重新启动”【英文标题】:Jupyter error - 'the kernel appears to have died, it will restart automatically' for a given piece of code 【发布时间】:2019-12-22 18:40:16 【问题描述】:

每当我运行此代码时,它都会成功运行,但是在执行最后 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)

请帮助解决这个问题。 我尝试了所有方法,但都没有成功。

【问题讨论】:

frequent_itemsets = apriori(basket_sets, min_support=0.01, use_colnames=True) 导致MemoryError: Unable to allocate array with shape (46185705, 3, 123) and data type int64。此命令使用 8GB 内存。我的猜测是,在收到错误消息之前,您的笔记本电脑因内存不足而崩溃。 如前所述,这是由于内存错误。似乎你有太多独特的项目。在具有高内存内核(大约 25GB RAM)的 Google Colabs 上运行笔记本应该可以解决问题。 @skillsmuggler 我的系统有 36GB 内存,所以使用 Google Colabs 不太可能解决问题。basket_sets123 rows × 1619 columns 您能说说您实际尝试过的内容吗?数组有多大? @skillsmuggler 在 Google colab 中读取 xlsx 文件的任何代码。我试图使用它,但我遇到的错误很少。 【参考方案1】:

我和你有同样的问题。

我将最小支持更改为 0.5 或其他大于 0.01 的东西,它起作用了!

我认为这是因为有很多输出。 我希望这对你也有帮助。

【讨论】:

以上是关于Jupyter 错误 - 对于给定的一段代码,“内核似乎已经死亡,它将自动重新启动”的主要内容,如果未能解决你的问题,请参考以下文章

从脚本之家上看到的一段音频播放代码

如何在 jupyter notebook 中运行 parse_args()?收到错误 SystemExit: 2

脚本控制Jupyter启动停止重启

脚本控制Jupyter启动停止重启

在有条件的一段时间内测试文件存在是不是有任何陷阱?

一段递归代码引发的对于传参以及关于基本类型的一点了解