python中的sklearn.feature_extraction dictvectorizer

class  sklearn.feature_extraction. DictVectorizer ( dtype=<type 'numpy.float64'>separator='='sparse=True, sort=True ) [source]

Transforms lists of feature-value mappings to vectors.

This transformer turns lists of mappings (dict-like objects) of feature names to feature values into Numpy arrays or scipy.sparse matrices for use with scikit-learn estimators.

When feature values are strings, this transformer will do a binary one-hot (aka one-of-K) coding: one boolean-valued feature is constructed for each of the possible string values that the feature can take on. For instance, a feature “f” that can take on the values “ham” and “spam” will become two features in the output, one signifying “f=ham”, the other “f=spam”.

However, note that this transformer will only do a binary one-hot encoding when feature values are of type string. If categorical features are represented as numeric values such as int, the DictVectorizer can be followed by OneHotEncoder to complete binary one-hot encoding.

Features that do not occur in a sample (mapping) will have a zero value in the resulting array/matrix.

Read more in the User Guide.

Parameters:

dtype : callable, optional

The type of feature values. Passed to Numpy array/scipy.sparse matrix constructors as the dtype argument.

separator : string, optional

Separator string used when constructing new features for one-hot coding.

sparse : boolean, optional.

Whether transform should produce scipy.sparse matrices. True by default.

sort : boolean, optional.

Whether feature_names_ and vocabulary_ should be sorted when fitting. True by default.

Attributes:

vocabulary_ : dict

A dictionary mapping feature names to feature indices.

feature_names_ : list

A list of length n_features containing the feature names (e.g., “f=ham” and “f=spam”).

See also

FeatureHasher
performs vectorization using only a hash function.
sklearn.preprocessing.OneHotEncoder
handles nominal/categorical features encoded as columns of integers.

Examples

>>> from sklearn.feature_extraction import DictVectorizer>>> v = DictVectorizer(sparse=False)>>> D = [{'foo': 1, 'bar': 2}, {'foo': 3, 'baz': 1}]>>> X = v.fit_transform(D)>>> Xarray([[ 2.,  0.,  1.],       [ 0.,  1.,  3.]])>>> v.inverse_transform(X) ==         [{'bar': 2.0, 'foo': 1.0}, {'baz': 1.0, 'foo': 3.0}]True>>> v.transform({'foo': 4, 'unseen_feature': 3})array([[ 0.,  0.,  4.]])

Methods

fit(X[, y]) Learn a list of feature name -> indices mappings.
fit_transform(X[, y]) Learn a list of feature name -> indices mappings and transform X.
get_feature_names() Returns a list of feature names, ordered by their indices.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X[, dict_type]) Transform array or sparse matrix X back to feature mappings.
restrict(support[, indices]) Restrict the features to those in support using feature selection.
set_params(\*\*params) Set the parameters of this estimator.
transform(X[, y]) Transform feature->value dicts to array or sparse matrix.
__init__ ( dtype=<type 'numpy.float64'>separator='='sparse=Truesort=True ) [source]
fit ( Xy=None ) [source]

Learn a list of feature name -> indices mappings.

Parameters:

X : Mapping or iterable over Mappings

Dict(s) or Mapping(s) from feature names (arbitrary Python objects) to feature values (strings or convertible to dtype).

y : (ignored)

Returns:

self :

fit_transform ( Xy=None ) [source]

Learn a list of feature name -> indices mappings and transform X.

Like fit(X) followed by transform(X), but does not require materializing X in memory.

Parameters:

X : Mapping or iterable over Mappings

Dict(s) or Mapping(s) from feature names (arbitrary Python objects) to feature values (strings or convertible to dtype).

y : (ignored)

Returns:

Xa : {array, sparse matrix}

Feature vectors; always 2-d.

get_feature_names ( ) [source]

Returns a list of feature names, ordered by their indices.

If one-of-K coding is applied to categorical features, this will include the constructed feature names but not the original ones.

get_params ( deep=True ) [source]

Get parameters for this estimator.

Parameters:

deep : boolean, optional

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params : mapping of string to any

Parameter names mapped to their values.

inverse_transform ( Xdict_type=<type 'dict'> ) [source]

Transform array or sparse matrix X back to feature mappings.

X must have been produced by this DictVectorizer’s transform or fit_transform method; it may only have passed through transformers that preserve the number of features and their order.

In the case of one-hot/one-of-K coding, the constructed feature names and values are returned rather than the original ones.

Parameters:

X : {array-like, sparse matrix}, shape = [n_samples, n_features]

Sample matrix.

dict_type : callable, optional

Constructor for feature mappings. Must conform to the collections.Mapping API.

Returns:

D : list of dict_type objects, length = n_samples

Feature mappings for the samples in X.

restrict ( supportindices=False ) [source]

Restrict the features to those in support using feature selection.

This function modifies the estimator in-place.

Parameters:

support : array-like

Boolean mask or list of indices (as returned by the get_support member of feature selectors).

indices : boolean, optional

Whether support is a list of indices.

Returns:

self :

Examples

>>> from sklearn.feature_extraction import DictVectorizer>>> from sklearn.feature_selection import SelectKBest, chi2>>> v = DictVectorizer()>>> D = [{'foo': 1, 'bar': 2}, {'foo': 3, 'baz': 1}]>>> X = v.fit_transform(D)>>> support = SelectKBest(chi2, k=2).fit(X, [0, 1])>>> v.get_feature_names()['bar', 'baz', 'foo']>>> v.restrict(support.get_support())DictVectorizer(dtype=..., separator='=', sort=True,        sparse=True)>>> v.get_feature_names()['bar', 'foo']
set_params ( **params ) [source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns: self :
transform ( Xy=None ) [source]

Transform feature->value dicts to array or sparse matrix.

Named features not encountered during fit or fit_transform will be silently ignored.

Parameters:

X : Mapping or iterable over Mappings, length = n_samples

Dict(s) or Mapping(s) from feature names (arbitrary Python objects) to feature values (strings or convertible to dtype).

y : (ignored)

Returns:

Xa : {array, sparse matrix}

Feature vectors; always 2-d.


改进这段代码 import pandas as pd from sklearn.feature_extraction import DictVectorizer from sklearn import tree from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt filepath='E:\\《python与数据科学》考核方式和考核说明\\银行营销数据_训练集和测试集.xlsx' data=pd.read_excel(filepath,sheet_name=0) vec_x=DictVectorizer(sparse = False) vec_y=DictVectorizer(sparse = False) x_feature = data[['duration','emp.var.rate','nr.employed']] x_train = vec_x.fit_transform(x_feature.to_dict(orient='records')) y_feature = data[['y']] y_train = vec_y.fit_transform(y_feature.to_dict(orient='records')) print('show feature\n',x_feature) print('show vector\n',x_train) print('show vector name\n',vec_x.get_feature_names_out()) print('show feature\n',y_feature) print('show vector\n',y_train) print('show vector name\n',vec_y.get_feature_names_out()) clf = tree.DecisionTreeClassifier(criterion='gini') clf.fit(x_train,y_train) plt.figure(figsize=(30,10),facecolor='yellow') tree.plot_tree(clf,filled = True); plt.show() r=tree.export_text(clf,feature_names=list(vec_x.get_feature_names_out())) print(r) filepath1='E:\\《python与数据科学》考核方式和考核说明\\银行营销数据_待分析.xlsx' data1=pd.read_excel(filepath1,sheet_name=0) data['考试学号']=data['考试学号'].astype("str") data1=data1[data1['考试学号'] == 2020051507220] x_feature = data1[['duration','emp.var.rate','nr.employed']] x_test = vec_x.fit_transform(x_feature.to_dict(orient='records')) test_predict = clf.predict(x_test) print(test_predict) print(vec_y.get_feature_names_out())
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值