首页 > 解决方案 > 当我使用 Onehotencoder 得到以下错误

问题描述

当我Onehotencoder在 python 3.7 中使用错误时,会出现以下错误。请给出解决方案。

# -*- coding: utf-8 -*-

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('dataset.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 3] = labelencoder_X.fit_transform(X[:, 3])
onehotencoder = OneHotEncoder(categories=X[3])
X = onehotencoder.fit_transform(X)

我收到这样的错误

Traceback (most recent call last):

  File "<ipython-input-81-a28c69982fc5>", line 15, in <module>
    X = onehotencoder.fit_transform(X).values.reshape(-1,1)

  File "C:\Users\HP\anaconda3\lib\site-packages\sklearn\preprocessing\_encoders.py", line 372, in fit_transform
    return super().fit_transform(X, y)

  File "C:\Users\HP\anaconda3\lib\site-packages\sklearn\base.py", line 571, in fit_transform
    return self.fit(X, **fit_params).transform(X)

  File "C:\Users\HP\anaconda3\lib\site-packages\sklearn\preprocessing\_encoders.py", line 347, in fit
    self._fit(X, handle_unknown=self.handle_unknown)

  File "C:\Users\HP\anaconda3\lib\site-packages\sklearn\preprocessing\_encoders.py", line 76, in _fit
    if self.categories != 'auto':

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

标签: pythonpython-3.xmachine-learningartificial-intelligencelinear-regression

解决方案


推荐阅读