You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows7下运行KMeans遇‘无法检测物理核心数’问题求助

问题

在64位Windows 7系统上运行Python 3.8.10,执行以下sklearn KMeans示例代码:

from sklearn.cluster import KMeans
import numpy as np

X = np.array([[1, 2], [1, 4], [1, 0],
              [10, 2], [10, 4], [10, 0]])

kmeans = KMeans(n_clusters=2, random_state=0, n_init="auto").fit(X)
kmeans.labels_
kmeans.predict([[0, 0], [12, 3]])
kmeans.cluster_centers_

出现如下警告且代码无法运行:

Warning (from warnings module):
File "C:\Users\Maria\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\externals\loky\backend\context.py", line 136
warnings.warn(
UserWarning: Could not find the number of physical cores for the following reason:
[WinError 2] El sistema no puede encontrar el archivo especificado
Returning the number of logical cores instead. You can silence this warning by setting LOKY_MAX_CPU_COUNT to the number of cores you want to use.
File "C:\Users\Maria\AppData\Local\Programs\Python\Python38\lib\site-packages\joblib\externals\loky\backend\context.py", line 257, in _count_physical_cores
cpu_info = subprocess.run(
File "C:\Users\Maria\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 493, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\Maria\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 858, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Maria\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1311, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,

已尝试将LOKY_MAX_CPU_COUNT设置为2(PC核心数),但问题仍未解决,求解决思路。

解决思路

  • 确认环境变量设置对不对
    得保证LOKY_MAX_CPU_COUNT是系统级或者当前Python环境的环境变量,不能只在代码里临时瞎设。试试这么验证:

    1. 右键「计算机」→「属性」→「高级系统设置」→「环境变量」,在系统变量里加LOKY_MAX_CPU_COUNT,值设为2,重启Python或者IDE再跑代码。
    2. 如果想临时设置,得在代码最开头就加:
      import os
      os.environ["LOKY_MAX_CPU_COUNT"] = "2"
      
      记住要放在导入sklearn和joblib之前。
  • 降级loky版本
    新版本loky在Win7上可能有兼容问题,试试降到2.5.1版本:

    pip install loky==2.5.1
    
  • 让KMeans单线程跑
    初始化KMeans的时候指定n_jobs=1,跳过loky的核心检测逻辑:

    kmeans = KMeans(n_clusters=2, random_state=0, n_init="auto", n_jobs=1).fit(X)
    
  • 检查系统文件有没有损坏
    报错里的WinError 2是说找不到指定文件,大概率是系统里的wmic.exe丢了或者坏了(loky靠它查核心数)。可以从正常的Win7系统里复制wmic.exe到C:\Windows\System32,或者跑系统修复命令:

    sfc /scannow
    

内容的提问来源于stack exchange,提问作者abeh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 10:07:45