如何让 SciKit-Learn 在 SVC 中识别我的内核?

How to get SciKit-Learn to recognize my kernel in SVC?

我正在使用 python 2.7。 SVC.

的文档

当我尝试以下操作时:

from sklearn.svm import SVC
base_learner = SVC(random_state=4,probability=True)

它抛出以下错误:

TypeError: Argument 'kernel' has incorrect type (expected str, got unicode)

所以我想我会试试这个:

from builtins import str
from sklearn.svm import SVC
base_learner = SVC(kernel=str('rbf'), random_state=4,probability=True)

仍然无法识别内核。我做错了什么?

你所做的应该在最新版本的 Python 2.7 和 scikit-learn 中工作,而不必诉诸手动处理字符串转换,所以这听起来像是 Python 环境出了问题.

如果您使用 conda 来管理您的环境,您可以尝试通过以下步骤从头开始创建一个:

  1. 打开 Anaconda Prompt(或任何可以 运行 conda 的命令提示符)。

  2. 运行conda create --name py27sklearn创建新环境

  3. 通过运行宁activate py27sklearn(或conda activate py27sklearn)激活该环境

  4. 通过 运行ning conda install python=2.7 安装 Python 2.7。

  5. 通过 运行ning conda install scikit-learn 安装 scikit-learn。

  6. 运行 运行ning python 的 Python 解释器。

  7. 验证您的代码 运行 是否符合预期。

您应该会看到如下内容:

(py27sklearn) $ python
Python 2.7.15 |Anaconda, Inc.| (default, May  1 2018, 18:37:09) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from sklearn.svm import SVC
>>> SVC(random_state=4, probability=True)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=True, random_state=4, shrinking=True, tol=0.001,
  verbose=False)