numpy sklearn KMeans不起作用,因为我只得到“NoneType”对象,该对象在非空数组上没有属性“split”

tyu7yeag  于 2023-04-06  发布在  其他
关注(0)|答案(5)|浏览(1472)

我不知道什么是错的,但突然KMeanssklearn不工作了,我不知道我做错了什么。有人遇到过这个问题吗?或者知道我如何修复它?

from sklearn.cluster import KMeans

kmeanModel = KMeans(n_clusters=k, random_state=0)
kmeanModel.fit(allLocations)

allLocations看起来像这样:

array([[12.40236   , 51.38086   ],
       [12.40999   , 51.38494   ],
       [12.40599   , 51.37284   ],
       [12.28692   , 51.32039   ],
       [12.41349   , 51.34443   ], ...])

并且allLocations.dtype给出dtype('float64')
scikit-learn版本是1.0.2,NumPy版本是1.22.2,我使用的是Jupyter笔记本。
The Error说道:
'NoneType' object has no attribute 'split'
整个错误看起来像这样:

AttributeError                            Traceback (most recent call last)
<ipython-input-30-db8e8220c8b9> in <module>
     12 for k in K:
     13     kmeanModel = KMeans(n_clusters=k, random_state=0)
---> 14     kmeanModel.fit(allLocations)
     15     distortions.append(kmeanModel.inertia_)
     16 #Plotting the distortions
    
~\anaconda3\lib\site-packages\sklearn\cluster\_kmeans.py in fit(self, X, y, sample_weight)
   1169         if self._algorithm == "full":
   1170             kmeans_single = _kmeans_single_lloyd
-> 1171             self._check_mkl_vcomp(X, X.shape[0])
   1172         else:
   1173             kmeans_single = _kmeans_single_elkan
    
~\anaconda3\lib\site-packages\sklearn\cluster\_kmeans.py in _check_mkl_vcomp(self, X, n_samples)
   1026         active_threads = int(np.ceil(n_samples / CHUNK_SIZE))
   1027         if active_threads < self._n_threads:
-> 1028             modules = threadpool_info()
   1029             has_vcomp = "vcomp" in [module["prefix"] for module in modules]
   1030             has_mkl = ("mkl", "intel") in [
    
~\anaconda3\lib\site-packages\sklearn\utils\fixes.py in threadpool_info()
    323         return controller.info()
    324     else:
--> 325         return threadpoolctl.threadpool_info()
    326 
    327 
    
~\anaconda3\lib\site-packages\threadpoolctl.py in threadpool_info()
    122     In addition, each module may contain internal_api specific entries.
    123     """
--> 124     return _ThreadpoolInfo(user_api=_ALL_USER_APIS).todicts()
    125 
    126 
    
~\anaconda3\lib\site-packages\threadpoolctl.py in __init__(self, user_api, prefixes, modules)
    338 
    339             self.modules = []
--> 340             self._load_modules()
    341             self._warn_if_incompatible_openmp()
    342         else:
    
~\anaconda3\lib\site-packages\threadpoolctl.py in _load_modules(self)
    371             self._find_modules_with_dyld()
    372         elif sys.platform == "win32":
--> 373             self._find_modules_with_enum_process_module_ex()
    374         else:
    375             self._find_modules_with_dl_iterate_phdr()
    
~\anaconda3\lib\site-packages\threadpoolctl.py in _find_modules_with_enum_process_module_ex(self)
    483 
    484                 # Store the module if it is supported and selected
--> 485                 self._make_module_from_path(filepath)
    486         finally:
    487             kernel_32.CloseHandle(h_process)
    
~\anaconda3\lib\site-packages\threadpoolctl.py in _make_module_from_path(self, filepath)
    513             if prefix in self.prefixes or user_api in self.user_api:
    514                 module_class = globals()[module_class]
--> 515                 module = module_class(filepath, prefix, user_api, internal_api)
    516                 self.modules.append(module)
    517 
    
~\anaconda3\lib\site-packages\threadpoolctl.py in __init__(self, filepath, prefix, user_api, internal_api)
    604         self.internal_api = internal_api
    605         self._dynlib = ctypes.CDLL(filepath, mode=_RTLD_NOLOAD)
--> 606         self.version = self.get_version()
    607         self.num_threads = self.get_num_threads()
    608         self._get_extra_info()
    
~\anaconda3\lib\site-packages\threadpoolctl.py in get_version(self)
    644                              lambda: None)
    645         get_config.restype = ctypes.c_char_p
--> 646         config = get_config().split()
    647         if config[0] == b"OpenBLAS":
    648             return config[1].decode("utf-8")
    
AttributeError: 'NoneType' object has no attribute 'split'
uinbv5nw

uinbv5nw1#

threadpoolctl升级到版本〉3。

这适用于numpy的所有版本。

8fq7wneg

8fq7wneg2#

我最近开始收到同样的错误。它可能与macOS从Sierra升级到 Catalina 有关,但我发现当n_clusters = 1时,它在计算kMeans时出现问题。在下面的代码中,我将范围更改为2:10而不是1:10,它开始工作。

K = range(2,10)

for k in K:

  kmeanModel = KMeans(n_clusters=k)

  kmeanModel.fit(data)

  distortions.append(kmeanModel.inertia_)
7fyelxc5

7fyelxc53#

将numpy降级到1.21.4后,它又可以工作了

brqmpdu1

brqmpdu14#

我将threadpoolctl从版本2.2.0升级到版本3.1.0,这解决了问题

fdbelqdn

fdbelqdn5#

以下选项均不适用于MacOS Ventura:已将threadpoolctl从版本2.2.0升级到版本3.1.0将numpy降级到1.21.4
我解决了这个问题,从木星笔记本转移到谷歌可乐。

相关问题