python - 当我使用 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()
解决方案
推荐阅读
- javascript - 如何使用 Promise all() 同时执行这两个函数?
- javascript - window.print() 在页面打印后从复选框中删除复选标记
- asterisk - PJSIP 在应该回复 404 时回复 503
- python - bs4 - 根据其他值提取特定的 href
- ios - 如何访问 iOS 应用程序中的通用链接?
- css - CSS Grid 中的 Chrome / Firefox 百分比高度差异
- go - golang 模块无法将单个依赖项更新到我的 .mod 文件
- oracle11g - 如何避免 Oracle 自动舍入 20 位小数?
- object - 搅拌机 - 组合对象 (CTRL+J) 破坏质量
- c# - 如何在不实现的情况下创建包含 OrderBy 的 IQueryable