本文整理汇总了Python中category_encoders.ordinal.OrdinalEncoder类的典型用法代码示例。如果您正苦于以下问题:Python OrdinalEncoder类的具体用法?Python OrdinalEncoder怎么用?Python OrdinalEncoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OrdinalEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: HelmertEncoder
class HelmertEncoder(BaseEstimator, TransformerMixin):
"""
"""
def __init__(self, verbose=0, cols=None, drop_invariant=False):
"""
:param verbose:
:param cols:
:return:
"""
self.drop_invariant = drop_invariant
self.drop_cols = []
self.verbose = verbose
self.cols = cols
self.ordinal_encoder = OrdinalEncoder(verbose=verbose, cols=cols)
def fit(self, X, y=None, **kwargs):
"""
:param X:
:param y:
:param kwargs:
:return:
"""
self.ordinal_encoder = self.ordinal_encoder.fit(X)
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
self.drop_cols = [x for x in X_temp.columns.values if X_temp[x].var() <= 10e-5]
return self
def transform(self, X):
"""
:param X:
:return:
"""
if not isinstance(X, pd.DataFrame):
X = pd.DataFrame(X)
if self.cols == []:
return X
X = self.ordinal_encoder.transform(X)
X = helmert_coding(X, cols=self.cols)
if self.drop_invariant:
for col in self.drop_cols:
X.drop(col, 1, inplace=True)
return X
示例2: fit
def fit(self, X, y, **kwargs):
"""Fit encoder according to X and y.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples
and n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
Returns
-------
self : encoder
Returns self.
"""
# unite the input into pandas types
X = util.convert_input(X)
y = util.convert_input_vector(y, X.index)
if X.shape[0] != y.shape[0]:
raise ValueError("The length of X is " + str(X.shape[0]) + " but length of y is " + str(y.shape[0]) + ".")
self._dim = X.shape[1]
# if columns aren't passed, just use every string column
if self.cols is None:
self.cols = util.get_obj_cols(X)
else:
self.cols = util.convert_cols_to_list(self.cols)
if self.handle_missing == 'error':
if X[self.cols].isnull().any().bool():
raise ValueError('Columns to be encoded can not contain null')
self.ordinal_encoder = OrdinalEncoder(
verbose=self.verbose,
cols=self.cols,
handle_unknown='value',
handle_missing='value'
)
self.ordinal_encoder = self.ordinal_encoder.fit(X)
X_ordinal = self.ordinal_encoder.transform(X)
self.mapping = self.fit_target_encoding(X_ordinal, y)
X_temp = self.transform(X, override_return_df=True)
self.feature_names = list(X_temp.columns)
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
generated_cols = util.get_generated_cols(X, X_temp, self.cols)
self.drop_cols = [x for x in generated_cols if X_temp[x].var() <= 10e-5]
try:
[self.feature_names.remove(x) for x in self.drop_cols]
except KeyError as e:
if self.verbose > 0:
print("Could not remove column from feature names."
"Not found in generated cols.\n{}".format(e))
return self
示例3: fit
def fit(self, X, y=None, **kwargs):
"""
:param X:
:param y:
:param kwargs:
:return:
"""
# if the input dataset isn't already a dataframe, convert it to one (using default column names)
if not isinstance(X, pd.DataFrame):
X = pd.DataFrame(X)
# if columns aren't passed, just use every string column
if self.cols is None:
self.cols = get_obj_cols(X)
# train an ordinal pre-encoder
self.ordinal_encoder = OrdinalEncoder(verbose=self.verbose, cols=self.cols)
self.ordinal_encoder = self.ordinal_encoder.fit(X)
# drop all output columns with 0 variance.
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
self.drop_cols = [x for x in X_temp.columns.values if X_temp[x].var() <= 10e-5]
return self
示例4: __init__
def __init__(self, verbose=0, cols=None):
"""
:param verbose:
:param cols:
:return:
"""
self.verbose = verbose
self.cols = cols
self.ordinal_encoder = OrdinalEncoder(verbose=verbose, cols=cols)
示例5: BinaryEncoder
class BinaryEncoder(BaseEstimator, TransformerMixin):
"""
Binary encoding encodes the integers as binary code with one column per digit.
"""
def __init__(self, verbose=0, cols=None):
"""
:param verbose:
:param cols:
:return:
"""
self.verbose = verbose
self.cols = cols
self.ordinal_encoder = OrdinalEncoder(verbose=verbose, cols=cols)
def fit(self, X, y=None, **kwargs):
"""
:param X:
:param y:
:param kwargs:
:return:
"""
self.ordinal_encoder = self.ordinal_encoder.fit(X)
return self
def transform(self, X):
"""
:param X:
:return:
"""
if not isinstance(X, pd.DataFrame):
X = pd.DataFrame(X)
X = self.ordinal_encoder.transform(X)
return binary(X, cols=self.cols)
示例6: BackwardDifferenceEncoder
class BackwardDifferenceEncoder(BaseEstimator, TransformerMixin):
"""
"""
def __init__(self, verbose=0, cols=None):
"""
:param verbose:
:param cols:
:return:
"""
self.verbose = verbose
self.cols = cols
self.ordinal_encoder = OrdinalEncoder(verbose=verbose, cols=cols)
def fit(self, X, y=None, **kwargs):
"""
:param X:
:param y:
:param kwargs:
:return:
"""
self.ordinal_encoder = self.ordinal_encoder.fit(X)
return self
def transform(self, X):
"""
:param X:
:return:
"""
if not isinstance(X, pd.DataFrame):
X = pd.DataFrame(X)
X = self.ordinal_encoder.transform(X)
return backward_difference_coding(X, cols=self.cols)
示例7: fit
def fit(self, X, y=None, **kwargs):
"""Fit encoder according to X and y.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples
and n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
Returns
-------
self : encoder
Returns self.
"""
# if the input dataset isn't already a dataframe, convert it to one (using default column names)
# first check the type
X = convert_input(X)
self._dim = X.shape[1]
# if columns aren't passed, just use every string column
if self.cols is None:
self.cols = get_obj_cols(X)
# train an ordinal pre-encoder
self.ordinal_encoder = OrdinalEncoder(
verbose=self.verbose,
cols=self.cols,
impute_missing=self.impute_missing,
handle_unknown=self.handle_unknown
)
self.ordinal_encoder = self.ordinal_encoder.fit(X)
for col in self.cols:
self.digits_per_col[col] = self.calc_required_digits(X, col)
# do a transform on the training data to get a column list
X_t = self.transform(X, override_return_df=True)
self._encoded_columns = X_t.columns.values
# drop all output columns with 0 variance.
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
self.drop_cols = [x for x in X_temp.columns.values if X_temp[x].var() <= 10e-5]
return self
示例8: __init__
def __init__(self, verbose=0, cols=None, drop_invariant=False):
"""
:param verbose:
:param cols:
:return:
"""
self.drop_invariant = drop_invariant
self.drop_cols = []
self.verbose = verbose
self.cols = cols
self.ordinal_encoder = OrdinalEncoder(verbose=verbose, cols=cols)
示例9: fit
def fit(self, X, y=None, **kwargs):
"""Fits an ordinal encoder to produce a consistent mapping across applications and optionally finds
generally invariant columns to drop consistently.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples
and n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
Returns
-------
self : encoder
Returns self.
"""
# if the input dataset isn't already a dataframe, convert it to one (using default column names)
# first check the type
if not isinstance(X, pd.DataFrame):
if isinstance(X, list):
X = pd.DataFrame(np.array(X))
elif isinstance(X, (np.generic, np.ndarray)):
X = pd.DataFrame(X)
else:
raise ValueError('Unexpected input type: %s' % (str(type(X))))
self._dim = X.shape[1]
# if columns aren't passed, just use every string column
if self.cols is None:
self.cols = get_obj_cols(X)
# train an ordinal pre-encoder
self.ordinal_encoder = OrdinalEncoder(verbose=self.verbose, cols=self.cols)
self.ordinal_encoder = self.ordinal_encoder.fit(X)
# drop all output columns with 0 variance.
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
self.drop_cols = [x for x in X_temp.columns.values if X_temp[x].var() <= 10e-5]
return self
示例10: fit
def fit(self, X, y=None, **kwargs):
"""Fit encoder according to X and y.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples
and n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
Returns
-------
self : encoder
Returns self.
"""
# first check the type
if not isinstance(X, pd.DataFrame):
if isinstance(X, list):
X = pd.DataFrame(np.array(X))
elif isinstance(X, (np.generic, np.ndarray)):
X = pd.DataFrame(X)
else:
raise ValueError('Unexpected input type: %s' % (str(type(X))))
self._dim = X.shape[1]
# if columns aren't passed, just use every string column
if self.cols is None:
self.cols = get_obj_cols(X)
self.ordinal_encoder = OrdinalEncoder(verbose=self.verbose, cols=self.cols)
self.ordinal_encoder = self.ordinal_encoder.fit(X)
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
self.drop_cols = [x for x in X_temp.columns.values if X_temp[x].var() <= 10e-5]
return self
示例11: fit
def fit(self, X, y=None, **kwargs):
"""Fit encoder according to X and y.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples
and n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
Returns
-------
self : encoder
Returns self.
"""
# first check the type
X = convert_input(X)
self._dim = X.shape[1]
# if columns aren't passed, just use every string column
if self.cols is None:
self.cols = get_obj_cols(X)
self.ordinal_encoder = OrdinalEncoder(
verbose=self.verbose,
cols=self.cols,
impute_missing=self.impute_missing,
handle_unknown=self.handle_unknown
)
self.ordinal_encoder = self.ordinal_encoder.fit(X)
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
self.drop_cols = [x for x in X_temp.columns.values if X_temp[x].var() <= 10e-5]
return self
示例12: fit
def fit(self, X, y=None, **kwargs):
"""Fit encoder according to X and y.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples
and n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
Returns
-------
self : encoder
Returns self.
"""
# if the input dataset isn't already a dataframe, convert it to one (using default column names)
# first check the type
X = convert_input(X)
self._dim = X.shape[1]
# if columns aren't passed, just use every string column
if self.cols is None:
self.cols = get_obj_cols(X)
# train an ordinal pre-encoder
self.ordinal_encoder = OrdinalEncoder(verbose=self.verbose, cols=self.cols)
self.ordinal_encoder = self.ordinal_encoder.fit(X)
# drop all output columns with 0 variance.
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
self.drop_cols = [x for x in X_temp.columns.values if X_temp[x].var() <= 10e-5]
return self
示例13: fit
def fit(self, X, y=None, **kwargs):
"""
:param X:
:param y:
:param kwargs:
:return:
"""
# if columns aren't passed, just use every string column
if self.cols is None:
self.cols = get_obj_cols(X)
self.ordinal_encoder = OrdinalEncoder(verbose=self.verbose, cols=self.cols)
self.ordinal_encoder = self.ordinal_encoder.fit(X)
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
self.drop_cols = [x for x in X_temp.columns.values if X_temp[x].var() <= 10e-5]
return self
示例14: BaseNEncoder
#.........这里部分代码省略.........
self.digits_per_col = {}
def fit(self, X, y=None, **kwargs):
"""Fit encoder according to X and y.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples
and n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
Returns
-------
self : encoder
Returns self.
"""
# if the input dataset isn't already a dataframe, convert it to one (using default column names)
# first check the type
X = convert_input(X)
self._dim = X.shape[1]
# if columns aren't passed, just use every string column
if self.cols is None:
self.cols = get_obj_cols(X)
# train an ordinal pre-encoder
self.ordinal_encoder = OrdinalEncoder(
verbose=self.verbose,
cols=self.cols,
impute_missing=self.impute_missing,
handle_unknown=self.handle_unknown
)
self.ordinal_encoder = self.ordinal_encoder.fit(X)
for col in self.cols:
self.digits_per_col[col] = self.calc_required_digits(X, col)
# do a transform on the training data to get a column list
X_t = self.transform(X, override_return_df=True)
self._encoded_columns = X_t.columns.values
# drop all output columns with 0 variance.
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
self.drop_cols = [x for x in X_temp.columns.values if X_temp[x].var() <= 10e-5]
return self
def transform(self, X, override_return_df=False):
"""Perform the transformation to new categorical data.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Returns
-------
示例15: BinaryEncoder
class BinaryEncoder(BaseEstimator, TransformerMixin):
"""
Binary encoding encodes the integers as binary code with one column per digit.
"""
def __init__(self, verbose=0, cols=None, drop_invariant=False):
"""
:param verbose:
:param cols:
:return:
"""
self.drop_invariant = drop_invariant
self.drop_cols = []
self.verbose = verbose
self.cols = cols
self.ordinal_encoder = OrdinalEncoder(verbose=verbose, cols=cols)
def fit(self, X, y=None, **kwargs):
"""
:param X:
:param y:
:param kwargs:
:return:
"""
# if the input dataset isn't already a dataframe, convert it to one (using default column names)
if not isinstance(X, pd.DataFrame):
X = pd.DataFrame(X)
# if columns aren't passed, just use every string column
if self.cols is None:
self.cols = get_obj_cols(X)
# train an ordinal pre-encoder
self.ordinal_encoder = self.ordinal_encoder.fit(X)
# drop all output columns with 0 variance.
if self.drop_invariant:
self.drop_cols = []
X_temp = self.transform(X)
self.drop_cols = [x for x in X_temp.columns.values if X_temp[x].var() <= 10e-5]
return self
def transform(self, X):
"""
:param X:
:return:
"""
if not isinstance(X, pd.DataFrame):
X = pd.DataFrame(X)
if self.cols == []:
return X
X = self.ordinal_encoder.transform(X)
X = binary(X, cols=self.cols)
if self.drop_invariant:
for col in self.drop_cols:
X.drop(col, 1, inplace=True)
return X