TypeError:__init__() 得到了一个意外的关键字参数“categorical_features”:onehotencoder

Posted

技术标签:

【中文标题】TypeError:__init__() 得到了一个意外的关键字参数“categorical_features”:onehotencoder【英文标题】:TypeError: __init__() got an unexpected keyword argument 'categorical_features' : onehotencoder 【发布时间】:2021-12-12 09:28:00 【问题描述】:

这是我的代码,我无法解决有关“categorical_features”的错误,我无法解决它我正在使用这个 github repo https://github.com/AarohiSingla/ResNet50/blob/master/3-resnet50_rooms_dataset.ipynb 它在哪里成功实施,但在我的机器上出现错误

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os

dataset_path = os.listdir('rooms_dataset')

#list of directories in the folder

room_types = os.listdir('rooms_dataset')

print(room_types) 

#what kinds of rooms are in this dataset
print("Types of rooms found" , len(room_types))

#storing all the images together in a single list
rooms = []

for item in room_types:
    #Get all the file names
    all_rooms = os.listdir('rooms_dataset' + '/' + item)
    
#Adding items to the list
for room in all_rooms:
    rooms.append((item, str('rooms_dataset' + '/' + item) + '/' + room))

rooms_df = pd.DataFrame(data = rooms , columns = ['room type' , 'image'])
print(rooms_df.head())

print("Total number of rooms in the dataset : " , len(rooms_df))

#Total number of images in each category
room_count = rooms_df['room type'].value_counts()

import cv2
path = 'rooms_dataset/'


im_size = 224

#storing images after recycling it
images = []

#storing labels
labels = []

for i in room_types:
    data_path = path + str(i)  # entered in 1st folder and then 2nd folder and then 3rd folder
    filenames = [i for i in os.listdir(data_path) ]
   # print(filenames)  # will get the names of all images
    for f in filenames:
        img = cv2.imread(data_path + '/' + f)  # reading that image as array
        #print(img)  # will get the image as an array
        img = cv2.resize(img, (im_size, im_size))
        images.append(img)
        labels.append(i)
        
images = np.array(images)
images.shape

# dividing array pixels by 255 for simplicity
images = images.astype('float32') / 255.0

from sklearn.preprocessing import LabelEncoder , OneHotEncoder
y=rooms_df['room type'].values
#print(y[:5])

# for y
y_labelencoder = LabelEncoder ()
y = y_labelencoder.fit_transform (y)
#print (y)

y=y.reshape(-1,1)
onehotencoder = OneHotEncoder(categorical_features=[0])  #Converted  scalar output into vector output where the correct class will be 1 and other will be 0
Y= onehotencoder.fit_transform(y)
Y.shape  #(393, 3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-657727820e24> in <module>
      9 
     10 y=y.reshape(-1,1)
---> 11 onehotencoder = OneHotEncoder(categorical_features=[0])  #Converted  scalar output into vector output where the correct class will be 1 and other will be 0
     12 Y= onehotencoder.fit_transform(y)
     13 Y.shape  #(393, 3)

c:\users\shri\appdata\local\programs\python\python37\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     71                           FutureWarning)
     72         kwargs.update(k: arg for k, arg in zip(sig.parameters, args))
---> 73         return f(**kwargs)
     74     return inner_f
     75 

TypeError: __init__() got an unexpected keyword argument 'categorical_features'

【问题讨论】:

【参考方案1】:

根据您的sklearn 版本,参数categorical_features 不再存在,所以不妨试试这样:

y = y.reshape(-1,1)
onehotencoder = OneHotEncoder()  # Converted  scalar output into vector output where the correct class will be 1 and other will be 0
Y = onehotencoder.fit_transform(y)
Y.shape  #(393, 3)

【讨论】:

以上是关于TypeError:__init__() 得到了一个意外的关键字参数“categorical_features”:onehotencoder的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: __init__() 得到了一个意外的关键字参数“编码”

TypeError: __init__() 得到了一个意外的关键字参数 '__no_builder' Kivy

sklearn.cluster.KMeans 得到“TypeError:__init__() 得到了一个意外的关键字参数‘n_jobs’”

TypeError:__init__() 得到了一个意外的关键字参数“categorical_features”:onehotencoder

Tensorflow 错误:TypeError:__init__() 得到了一个意外的关键字参数“dct_method”[关闭]

如何使用 keras 加载保存的模型? (错误: : TypeError: __init__() 得到了一个意外的关键字参数“可训练”)