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

问题描述 投票:0回答:1

我正在使用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 string python-2.7 scikit-learn python-unicode
1个回答
1
投票

你正在做什么应该在最新版本的Python 2.7和scikit-learn中工作而不必手动处理字符串转换,所以这听起来像是一个Python环境出错了。

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

  1. 打开Anaconda Prompt(或任何可以运行conda的命令提示符)。
  2. 运行conda create --name py27sklearn来创建一个新环境
  3. 通过运行activate py27sklearn(或conda activate py27sklearn)激活该环境
  4. 通过运行conda install python=2.7安装Python 2.7。
  5. 通过运行conda install scikit-learn安装scikit-learn。
  6. 运行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)
© www.soinside.com 2019 - 2024. All rights reserved.