'OneHotEncoder' object has no attribute 'get'
时间: 2023-09-18 11:14:32 浏览: 223
The error message "'OneHotEncoder' object has no attribute 'get'" typically occurs when you try to call the 'get' method on an instance of the OneHotEncoder class in scikit-learn.
The OneHotEncoder class in scikit-learn does not have a 'get' method. Instead, you should use the 'transform' method to convert your input data to the one-hot encoded representation.
Here's an example of how to use the OneHotEncoder class to one-hot encode a categorical variable:
```python
from sklearn.preprocessing import OneHotEncoder
import numpy as np
# create some categorical data
data = np.array([['red', 'small'], ['blue', 'large'], ['green', 'medium']])
# create an instance of the OneHotEncoder class
enc = OneHotEncoder()
# fit the encoder to the data and transform the data to one-hot encoding
one_hot_data = enc.fit_transform(data).toarray()
print(one_hot_data)
```
In this example, the 'fit_transform' method of the OneHotEncoder class is used to fit the encoder to the data and transform the data to a one-hot encoded representation. The resulting one-hot encoded data is then printed to the console.
阅读全文