【发布时间】:2021-01-22 05:05:37
【问题描述】:
我正在尝试在 python 中编写一个带有两个 def 的类,这有助于我在不使用任何库的情况下从头开始执行 ONE_HOT ENCODING,没有类我的代码可以工作并执行 ONE_HOT_ENCODING 但在 class 内部,我遇到了我无法解决的问题如果有人可以提供帮助,我将不胜感激。 我的数据在这里:https://www.dropbox.com/s/z5y1mucs3h93izp/car.csv?dl=0 '''
import pandas as pd
import numpy as np
dataset = np.loadtxt(r'C:\Users\User\Downloads\car.csv', skiprows=1, delimiter=',', dtype='object', encoding='UTF-8')
class one_hot():
def __init__(self,dataset = None ,column = None):
self.dataset = dataset
self.column = column
def my_label_encoder( self,column):
for index, label in enumerate(set(dataset[:, column])):
dataset[dataset[:, column] == label, column] = index
def one_hot_encoder(self,column):
ncategory = np.max(dataset[:, column]) + 1
print(ncategory)
ohe = np.zeros((dataset.shape[0], ncategory))
print(ohe)
for index, category in enumerate(dataset[:, column]):
print(index ,'index')
print(category,'category')
ohe[index, category] = 1
print(ohe)
a = pd.DataFrame(ohe)
return a
e = one_hot()
b = e.my_label_encoder(dataset,1)
c = b.one_hot_encoder(dataset,1)
''' 我得到这个错误:
TypeError: my_label_encoder() takes 2 positional arguments but 3 were given
如果你能帮助我,我会很高兴。
【问题讨论】:
-
您发布的代码包含缩进错误,请编辑您的帖子以使缩进正确
-
@AsafAmnony 希望现在没事吧?
标签: python machine-learning deep-learning one-hot-encoding