简体   繁体   English

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

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

I am using python 2.7. 我正在使用python 2.7。 Documentation for SVC . SVC的文档。

When I try the following: 当我尝试以下操作时:

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

It throws the following error: 它引发以下错误:

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

So I thought I would try this: 所以我想我会尝试一下:

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

Still doesn't recognize the kernel. 仍然无法识别内核。 What am I doing wrong? 我究竟做错了什么?

What you are doing should work in the newest versions of Python 2.7 and scikit-learn without having to resort to manually dealing with string conversion, so this sounds like a Python environment gone awry. 您所做的工作应该可以在最新版本的python 2.7和scikit-learn中运行,而不必求助于手动处理字符串转换,因此这听起来像Python环境出了问题。

If you are using conda to manage your environments, you can try creating one from scratch through the following steps: 如果您使用conda来管理环境,则可以尝试通过以下步骤从头开始创建一个环境:

  1. Open Anaconda Prompt (or any command prompt from which you can run conda). 打开Anaconda Prompt(或从中可以运行conda的任何命令提示符)。

  2. Run conda create --name py27sklearn to create a new environment 运行conda create --name py27sklearn创建一个新环境

  3. Activate that environment by running activate py27sklearn (or conda activate py27sklearn ) 通过运行activate py27sklearn激活该环境(或conda activate py27sklearn

  4. Install Python 2.7 by running conda install python=2.7 . 通过运行conda install python=2.7

  5. Install scikit-learn by running conda install scikit-learn . 通过运行conda install scikit-learn

  6. Run a Python interpreter by running python . 通过运行python来运行Python解释器。

  7. Verify that your code runs as expected. 验证您的代码是否按预期运行。

You should see something like the following: 您应该看到类似以下的内容:

(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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM