파이썬/Numpy

[Python] AttributeError: 'NoneType' object has no attribute 'split'

easysheep 2023. 4. 14. 14:16

1. 증상

Numpy 와 Kmeans를 같이 사용하면 다음과 같은 오류가 발생하였다..

import numpy as np 
from sklearn.cluster import KMeans
allLocations = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
kmeanModel = KMeans(n_clusters=2, random_state=0)
kmeanModel.fit(allLocations)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[40], line 3
      1 allLocations = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
      2 kmeanModel = KMeans(n_clusters=2, random_state=0)
----> 3 kmeanModel.fit(allLocations)

File /opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:1455, in KMeans.fit(self, X, y, sample_weight)
   1453 else:
   1454     kmeans_single = _kmeans_single_lloyd
-> 1455     self._check_mkl_vcomp(X, X.shape[0])
   1457 best_inertia, best_labels = None, None
   1459 for i in range(self._n_init):
   1460     # Initialize centers

File /opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:911, in _BaseKMeans._check_mkl_vcomp(self, X, n_samples)
    909 n_active_threads = int(np.ceil(n_samples / CHUNK_SIZE))
    910 if n_active_threads < self._n_threads:
--> 911     modules = threadpool_info()
    912     has_vcomp = "vcomp" in [module["prefix"] for module in modules]
    913     has_mkl = ("mkl", "intel") in [
    914         (module["internal_api"], module.get("threading_layer", None))
    915         for module in modules
    916     ]

File /opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/sklearn/utils/fixes.py:150, in threadpool_info()
    148     return controller.info()
    149 else:
--> 150     return threadpoolctl.threadpool_info()

File /opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/threadpoolctl.py:124, in threadpool_info()
    107 @_format_docstring(USER_APIS=list(_ALL_USER_APIS),
    108                    INTERNAL_APIS=_ALL_INTERNAL_APIS)
    109 def threadpool_info():
    110     """Return the maximal number of threads for each detected library.
    111 
    112     Return a list with all the supported modules that have been found. Each
   (...)
    122     In addition, each module may contain internal_api specific entries.
    123     """
--> 124     return _ThreadpoolInfo(user_api=_ALL_USER_APIS).todicts()

File /opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/threadpoolctl.py:340, in _ThreadpoolInfo.__init__(self, user_api, prefixes, modules)
    337     self.user_api = [] if user_api is None else user_api
    339     self.modules = []
--> 340     self._load_modules()
    341     self._warn_if_incompatible_openmp()
    342 else:

File /opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/threadpoolctl.py:371, in _ThreadpoolInfo._load_modules(self)
    369 """Loop through loaded libraries and store supported ones"""
    370 if sys.platform == "darwin":
--> 371     self._find_modules_with_dyld()
    372 elif sys.platform == "win32":
    373     self._find_modules_with_enum_process_module_ex()

File /opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/threadpoolctl.py:428, in _ThreadpoolInfo._find_modules_with_dyld(self)
    425 filepath = filepath.decode("utf-8")
    427 # Store the module if it is supported and selected
--> 428 self._make_module_from_path(filepath)

File /opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/threadpoolctl.py:515, in _ThreadpoolInfo._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)

File /opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/threadpoolctl.py:606, in _Module.__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()

File /opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/threadpoolctl.py:646, in _OpenBLASModule.get_version(self)
    643 get_config = getattr(self._dynlib, "openblas_get_config",
    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'

2. 원인

threadpoolctl 2.1.0 에 이슈가 있어서 안된다고 한다...

 

3. 해결 방법 및 출처

출처:

https://github.com/scikit-learn/scikit-learn/issues/22689

 

kMeans stopped working with numpy 1.22.2 · Issue #22689 · scikit-learn/scikit-learn

Describe the bug kMeans is not working anymore with numpy 1.22.2 Probably similiar to (#22683) but not sure if it is the same fix Steps/Code to Reproduce allLocations = np.array([[1, 2], [1, 4], [1...

github.com

주피터 노트북을 키고 있으면 종료 후 !!(키고 했더니 안되었었다.)

다음의 명령어를 적어준다.

# base를 사용중이면 안적어주어도 된다..
conda activate [name] # 만약 사용하는 가상환경이 있으면 , [name] 대신 적어 놓고 활성화 하자..



# 원인인 threadpoolctl를 3.x 이상 버전으로 업그레이드해주자.
pip install threadpoolctl==3.1.0

import numpy as np 
from sklearn.cluster import KMeans
allLocations = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
kmeanModel = KMeans(n_clusters=2, random_state=0)
kmeanModel.fit(allLocations)