在学习使用sklearn做单机特征工程这篇文章时,发现在计算互信息时from minepy import MINE
代码运行出错ModuleNotFoundError: No module named 'minepy'
尝试解决错误第一步
根据ModuleNotFoundError: No module named 'minepy'
提示,缺少minepy
模块,于是到Anaconda环境下执行命令pip install minepy
:
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting minepy
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/84/92/84b624219daa189a44a06641a165e1099ec9a2c8e2dff24f748025d798e2/minepy-1.2.6.tar.gz (496 kB)
Preparing metadata (setup.py) ... done
Requirement already satisfied: numpy>=1.3.0 in f:\anaconda\envs\sklearn-env\lib\site-packages (from minepy) (1.23.0)
Building wheels for collected packages: minepy
Building wheel for minepy (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py bdist_wheel did not run successfully.
│ exit code: 1
╰─> [10 lines of output]
running bdist_wheel
running build
running build_py
creating build
creating build\lib.win-amd64-cpython-39
creating build\lib.win-amd64-cpython-39\minepy
copying minepy\__init__.py -> build\lib.win-amd64-cpython-39\minepy
running build_ext
building 'minepy.mine' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for minepy
Running setup.py clean for minepy
Failed to build minepy
Installing collected packages: minepy
Running setup.py install for minepy ... error
error: subprocess-exited-with-error
× Running setup.py install for minepy did not run successfully.
│ exit code: 1
╰─> [12 lines of output]
running install
F:\anaconda\envs\sklearn-env\lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
running build
running build_py
creating build
creating build\lib.win-amd64-cpython-39
creating build\lib.win-amd64-cpython-39\minepy
copying minepy\__init__.py -> build\lib.win-amd64-cpython-39\minepy
running build_ext
building 'minepy.mine' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure
× Encountered error while trying to install package.
╰─> minepy
note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.
安装失败!
尝试解决错误第二步
进入minepy
的官网:
https://www.lfd.uci.edu/~gohlke/pythonlibs/
这个网站包含了很多Python扩展包的非官方Windows二进制文件,还真有minepy
:
结合自己的Python版本,我下载的是:
minepy-1.2.6-cp39-cp39-win_amd64.whl
把它下载下来后,我把它拷贝到了:
F:\anaconda\envs\sklearn-env\Scripts
文件夹里面。
在Anaconda的Prompt
中直接用命令:
pip install F:\anaconda\envs\sklearn-env\Scripts\minepy-1.2.6-cp39-cp39-win_amd64.whl
最后的结果为:
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Processing f:\anaconda\envs\sklearn-env\scripts\minepy-1.2.6-cp39-cp39-win_amd64.whl
Requirement already satisfied: numpy>=1.3.0 in f:\anaconda\envs\sklearn-env\lib\site-packages (from minepy==1.2.6) (1.23.0)
Installing collected packages: minepy
Successfully installed minepy-1.2.6
安装成功!
测试
我们常用的代码如下:
from sklearn.feature_selection import SelectKBest
from minepy import MINE
from sklearn.datasets import load_iris
iris=load_iris()
# 由于MINE的设计不是函数式的,定义mic方法将其为函数式的,返回一个二元组,二元组的第2项设置成固定的P值0.5
def mic(x, y):
m = MINE()
m.compute_score(x, y)
return (m.mic(), 0.5)
#选择K个最好的特征,返回特征选择后的数据
SelectKBest(lambda X, Y: np.array(map(lambda x:mic(x, Y), X.T)).T, k=2).fit_transform(iris.data,iris.target)
我们跑出来的结果错误:
float() argument must be a string or a number, not 'map'
解决办法:
将获取到的map值放入list列表中,然后再使用。
即:
SelectKBest(lambda X, Y: np.array(list(map(lambda x:mic(x, Y), X.T))).T[0], k=2).fit_transform(iris.data,iris.target)
我们的运行结果为:
运行成功,全部搞定!