本文整理匯總了Python中sklearn.preprocessing.label.LabelEncoder.classes_方法的典型用法代碼示例。如果您正苦於以下問題:Python LabelEncoder.classes_方法的具體用法?Python LabelEncoder.classes_怎麽用?Python LabelEncoder.classes_使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.preprocessing.label.LabelEncoder
的用法示例。
在下文中一共展示了LabelEncoder.classes_方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: load_dataset
# 需要導入模塊: from sklearn.preprocessing.label import LabelEncoder [as 別名]
# 或者: from sklearn.preprocessing.label.LabelEncoder import classes_ [as 別名]
def load_dataset(self):
with open(self.file_name) as f:
dataset = arff.load(f)
if self.label_attribute is None:
self.label_attribute = dataset["attributes"][-1][0]
data = list(numpy.asarray(dataset["data"]).transpose())
labels = None
row = 0
for attribute_name, attribute_type in dataset["attributes"]:
if attribute_name == self.label_attribute:
# Labels found!
labels = data.pop(row)
continue
# Nominal attribute
if isinstance(attribute_type, list):
# Convert None in '?' for next check and to make label_binarize work
for j in range(len(data[row])):
if data[row][j] is None:
data[row][j] = "?"
if numpy.all(data[row] == "?"):
# If no data is present, just remove the row
data.pop(row)
continue
if self.binarize:
data[row] = numpy.asarray(label_binarize(data[row], attribute_type), dtype=numpy.float64)
else:
encoder = LabelEncoder()
encoder.classes_ = attribute_type
if "?" not in encoder.classes_:
encoder.classes_.insert(0, "?")
data[row] = encoder.transform(data[row]).reshape((len(data[row]), 1)).astype(numpy.float64)
else:
# Numeric attributes: check for nan values
data[row] = data[row].astype(numpy.float64)
nans = numpy.isnan(data[row])
if numpy.all(nans):
# If everything is nan, remove the feature
data.pop(row)
continue
if numpy.any(nans):
mean = data[row][numpy.invert(nans)].sum() / numpy.invert(nans).sum()
data[row][nans] = mean
# Reshape to do hstack later
data[row] = data[row].reshape((len(data[row]), 1))
# Go to next row only if we have NOT removed the current one
row += 1
instances = numpy.hstack(tuple(data))
useless_indices = numpy.where(instances.var(axis=0) == 0)
instances = numpy.delete(instances, useless_indices, axis=1)
return instances, labels