鱼C论坛

 找回密码
 立即注册
查看: 4096|回复: 4

同样的代码在python的IDLE中正常运行,但是在pycharm中运行报错

[复制链接]
发表于 2020-4-7 16:50:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 amoschen 于 2020-4-7 16:50 编辑

今天在练习python的过程遇到一个问题,就是下面的这段代码,在python自带的IDLE中能够正常运行,但是在pycharm中运行就会报错,有没有大神予以解答的?


#-*- coding: utf-8 -*-
#数据规范化
import pandas as pd

datafile = 'E:\学习文件\个人学习资料\Python\书籍\《Python数据分析与挖掘实战》源数据和代码' \
            '\Python-2+|+++÷+d-++=-|++\chapter4\demo\data\discretization_data.xls' #参数初始化
data = pd.read_excel(datafile) #读取数据
data = data[u'肝气郁结证型系数'].copy()
k = 4

d1 = pd.cut(data, k, labels = range(k)) #等宽离散化,各个类比依次命名为0,1,2,3

#等频率离散化
w = [1.0*i/k for i in range(k+1)]
w = data.describe(percentiles = w)[4:4+k+1] #使用describe函数自动计算分位数
w[0] = w[0]*(1-1e-10)
d2 = pd.cut(data, w, labels = range(k))

from sklearn.cluster import KMeans #引入KMeans
kmodel = KMeans(n_clusters = k, n_jobs = 4) #建立模型,n_jobs是并行数,一般等于CPU数较好
kmodel.fit(data.values.reshape((len(data), 1))) #训练模型
c = pd.DataFrame(kmodel.cluster_centers_).sort_values(0) #输出聚类中心,并且排序(默认是随机序的)
w = c.rolling(2).mean().iloc[1:] #相邻两项求中点,作为边界点
w = [0] + list(w[0]) + [data.max()] #把首末边界点加上
d3 = pd.cut(data, w, labels = range(k))

def cluster_plot(d, k): #自定义作图函数来显示聚类结果
  import matplotlib.pyplot as plt
  plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签
  plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
  
  plt.figure(figsize = (8, 3))
  for j in range(0, k):
    plt.plot(data[d==j], [j for i in d[d==j]], 'o')
  
  plt.ylim(-0.5, k-0.5)
  return plt

cluster_plot(d1, k).show()

cluster_plot(d2, k).show()
cluster_plot(d3, k).show()


                               
登录/注册后可看大图

以下为pycharm中的报错信息
E:\学习文件\个人学习资料\Python\学习代码\environment\Scripts\python.exe E:/学习文件/个人学习资料/Python/书籍/《Python数据分析与挖掘实战》源数据和代码/Python-2+|+++÷+d-++=-|++/chapter4/demo/code/4-3_data_discretization.py
exception calling callback for <Future at 0xdee9c88 state=finished raised BrokenProcessPool>
joblib.externals.loky.process_executor._RemoteTraceback: 
'''
Traceback (most recent call last):
  File "D:\python\lib\site-packages\joblib\externals\loky\process_executor.py", line 391, in _process_worker
    call_item = call_queue.get(block=True, timeout=timeout)
  File "D:\python\lib\multiprocessing\queues.py", line 99, in get
    if not self._rlock.acquire(block, timeout):
PermissionError: [WinError 5] 拒绝访问。
'''

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\joblib\externals\loky\_base.py", line 625, in _invoke_callbacks
    callback(self)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 340, in __call__
    self.parallel.dispatch_next()
  File "D:\python\lib\site-packages\joblib\parallel.py", line 769, in dispatch_next
    if not self.dispatch_one_batch(self._original_iterator):
  File "D:\python\lib\site-packages\joblib\parallel.py", line 835, in dispatch_one_batch
    self._dispatch(tasks)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 754, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "D:\python\lib\site-packages\joblib\_parallel_backends.py", line 551, in apply_async
    future = self._workers.submit(SafeFunction(func))
  File "D:\python\lib\site-packages\joblib\externals\loky\reusable_executor.py", line 160, in submit
    fn, *args, **kwargs)
  File "D:\python\lib\site-packages\joblib\externals\loky\process_executor.py", line 1027, in submit
    raise self._flags.broken
joblib.externals.loky.process_executor.BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable.
exception calling callback for <Future at 0xdefaf98 state=finished raised BrokenProcessPool>
joblib.externals.loky.process_executor._RemoteTraceback: 
'''
Traceback (most recent call last):
  File "D:\python\lib\site-packages\joblib\externals\loky\process_executor.py", line 391, in _process_worker
    call_item = call_queue.get(block=True, timeout=timeout)
  File "D:\python\lib\multiprocessing\queues.py", line 99, in get
    if not self._rlock.acquire(block, timeout):
PermissionError: [WinError 5] 拒绝访问。
'''

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\joblib\externals\loky\_base.py", line 625, in _invoke_callbacks
    callback(self)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 340, in __call__
    self.parallel.dispatch_next()
  File "D:\python\lib\site-packages\joblib\parallel.py", line 769, in dispatch_next
    if not self.dispatch_one_batch(self._original_iterator):
  File "D:\python\lib\site-packages\joblib\parallel.py", line 835, in dispatch_one_batch
    self._dispatch(tasks)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 754, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "D:\python\lib\site-packages\joblib\_parallel_backends.py", line 551, in apply_async
    future = self._workers.submit(SafeFunction(func))
  File "D:\python\lib\site-packages\joblib\externals\loky\reusable_executor.py", line 160, in submit
    fn, *args, **kwargs)
  File "D:\python\lib\site-packages\joblib\externals\loky\process_executor.py", line 1027, in submit
    raise self._flags.broken
  File "D:\python\lib\site-packages\joblib\externals\loky\_base.py", line 625, in _invoke_callbacks
    callback(self)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 340, in __call__
    self.parallel.dispatch_next()
  File "D:\python\lib\site-packages\joblib\parallel.py", line 769, in dispatch_next
    if not self.dispatch_one_batch(self._original_iterator):
  File "D:\python\lib\site-packages\joblib\parallel.py", line 835, in dispatch_one_batch
    self._dispatch(tasks)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 754, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "D:\python\lib\site-packages\joblib\_parallel_backends.py", line 551, in apply_async
    future = self._workers.submit(SafeFunction(func))
  File "D:\python\lib\site-packages\joblib\externals\loky\reusable_executor.py", line 160, in submit
    fn, *args, **kwargs)
  File "D:\python\lib\site-packages\joblib\externals\loky\process_executor.py", line 1027, in submit
    raise self._flags.broken
joblib.externals.loky.process_executor.BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable.
ERROR: The process "6664" not found.
ERROR: The process "8120" not found.
ERROR: The process "7940" not found.
ERROR: The process "6636" not found.
joblib.externals.loky.process_executor._RemoteTraceback: 
'''
Traceback (most recent call last):
  File "D:\python\lib\site-packages\joblib\externals\loky\process_executor.py", line 391, in _process_worker
    call_item = call_queue.get(block=True, timeout=timeout)
  File "D:\python\lib\multiprocessing\queues.py", line 99, in get
    if not self._rlock.acquire(block, timeout):
PermissionError: [WinError 5] 拒绝访问。
'''

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "E:/学习文件/个人学习资料/Python/书籍/《Python数据分析与挖掘实战》源数据和代码/Python-2+|+++÷+d-++=-|++/chapter4/demo/code/4-3_data_discretization.py", line 21, in <module>
    kmodel.fit(data.values.reshape((len(data), 1))) #训练模型
  File "D:\python\lib\site-packages\sklearn\cluster\_kmeans.py", line 956, in fit
    for seed in seeds)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 1017, in __call__
    self.retrieve()
  File "D:\python\lib\site-packages\joblib\parallel.py", line 909, in retrieve
    self._output.extend(job.get(timeout=self.timeout))
  File "D:\python\lib\site-packages\joblib\_parallel_backends.py", line 562, in wrap_future_result
    return future.result(timeout=timeout)
  File "D:\python\lib\concurrent\futures\_base.py", line 432, in result
    return self.__get_result()
  File "D:\python\lib\concurrent\futures\_base.py", line 384, in __get_result
    raise self._exception
  File "D:\python\lib\site-packages\joblib\externals\loky\_base.py", line 625, in _invoke_callbacks
    callback(self)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 340, in __call__
    self.parallel.dispatch_next()
  File "D:\python\lib\site-packages\joblib\parallel.py", line 769, in dispatch_next
    if not self.dispatch_one_batch(self._original_iterator):
  File "D:\python\lib\site-packages\joblib\parallel.py", line 835, in dispatch_one_batch
    self._dispatch(tasks)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 754, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "D:\python\lib\site-packages\joblib\_parallel_backends.py", line 551, in apply_async
    future = self._workers.submit(SafeFunction(func))
  File "D:\python\lib\site-packages\joblib\externals\loky\reusable_executor.py", line 160, in submit
    fn, *args, **kwargs)
  File "D:\python\lib\site-packages\joblib\externals\loky\process_executor.py", line 1027, in submit
    raise self._flags.broken
  File "D:\python\lib\site-packages\joblib\externals\loky\_base.py", line 625, in _invoke_callbacks
    callback(self)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 340, in __call__
    self.parallel.dispatch_next()
  File "D:\python\lib\site-packages\joblib\parallel.py", line 769, in dispatch_next
    if not self.dispatch_one_batch(self._original_iterator):
  File "D:\python\lib\site-packages\joblib\parallel.py", line 835, in dispatch_one_batch
    self._dispatch(tasks)
  File "D:\python\lib\site-packages\joblib\parallel.py", line 754, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "D:\python\lib\site-packages\joblib\_parallel_backends.py", line 551, in apply_async
    future = self._workers.submit(SafeFunction(func))
  File "D:\python\lib\site-packages\joblib\externals\loky\reusable_executor.py", line 160, in submit
    fn, *args, **kwargs)
  File "D:\python\lib\site-packages\joblib\externals\loky\process_executor.py", line 1027, in submit
    raise self._flags.broken
joblib.externals.loky.process_executor.BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable.

Process finished with exit code 1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-7 17:53:20 | 显示全部楼层
100+行的 traceback ,有点壮观
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-7 18:46:42 | 显示全部楼层
真的不建议用pycharm,本身python就是轻巧的软件,用一些高级的编辑器也不会使代码更牛逼,用点简单的吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 00:48:17 | 显示全部楼层
本帖最后由 cug_cui 于 2020-4-8 00:49 编辑
datafile = 'E:\学习文件\个人学习资料\Python\书籍\《Python数据分析与挖掘实战》源数据和代码' \
            '\Python-2+|+++÷+d-++=-|++\chapter4\demo\data\discretization_data.xls' #参数初始化
这个文件路径是不是不对啊?这应该是一个字符串,像这样的
'E:\Programme\python\pyc\pylearn\doc\record.txt'
就算是换了行,也不应该有四个单引号。
而且你这路径还带中文,层次还这么深,难保不出错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-12 13:14:30 | 显示全部楼层
datafile = 'E:\学习文件\个人学习资料\Python\书籍\《Python数据分析与挖掘实战》源数据和代码' \
            '\Python-2+|+++÷+d-++=-|++\chapter4\demo\data\discretization_data.xls' #参数初始化
这么长的路径,是人写的路径吗?况且,windows是不让再命名里有+,-这种特殊字符,你这样写不是故意对windows挑衅吗?还有那个换行符,怎么可能有四个字符串呢?有换行符那个路径应该这么写:
 'E:\学习文件\个人学习资料\Python\书籍\《Python数据分析与挖掘实战》源数据和代码\
            \Python-2+|+++÷+d-++=-|++\chapter4\demo\data\discretization_data.xls'
或者,换行符也可以改成小括号。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-26 09:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表