MCPcopy Create free account
hub / github.com/apple/coremltools / create_feature_vectorizer

Function create_feature_vectorizer

coremltools/models/feature_vectorizer.py:14–98  ·  view source on GitHub ↗

Create a feature vectorizer from input features. This returns a 2-tuple ``(spec, num_dimension)`` for a feature vectorizer that puts everything into a single array with a length equal to the total size of all the input features. Parameters ---------- input_features: [list o

(input_features, output_feature_name, known_size_map={})

Source from the content-addressed store, hash-verified

12
13
14def create_feature_vectorizer(input_features, output_feature_name, known_size_map={}):
15 """
16 Create a feature vectorizer from input features. This returns a 2-tuple
17 ``(spec, num_dimension)`` for a feature vectorizer that puts everything into a
18 single array with a length equal to the total size of all the input features.
19
20 Parameters
21 ----------
22 input_features: [list of 2-tuples]
23 Name(s) of the input features, given as a list of ``('name', datatype)``
24 tuples. The datatypes entry is one of the data types defined in the
25 ``datatypes`` module. Allowed ``datatypes`` are ``datatype.Int64``,
26 ``datatype.Double``, ``datatypes.Dictionary``, and ``datatype.Array``.
27
28 If the feature is a dictionary type, then the dictionary must have integer
29 keys, and the number of dimensions to expand it into must be provided by
30 ``known_size_map``.
31
32 Feature indices in the final array are counted sequentially from the
33 from 0 through the total number of features.
34
35
36 output_feature_name: str
37 The name of the output feature. The type is an Array
38 List of the output features of the network.
39
40 known_size_map:
41 A dictionary mapping the feature name to the expanded size in the final
42 array. This is most useful for specifying the size of sparse vectors
43 given as dictionaries of index to value.
44
45 """
46
47 spec = proto.Model_pb2.Model()
48 spec.specificationVersion = SPECIFICATION_VERSION
49
50 input_features = process_or_validate_features(input_features)
51
52 feature_vectorizer = spec.featureVectorizer
53
54 num_output_dimensions = 0
55
56 for n, ft in input_features:
57 if n in known_size_map:
58 dim = known_size_map[n]
59
60 if ft.num_elements is not None:
61 if dim != ft.num_elements:
62 raise ValueError(
63 "In feature {}, override size {} not compatible with inherent "
64 "value size {}.".format(n, dim, ft.num_elements)
65 )
66 else:
67 if ft.num_elements is None:
68 raise ValueError(
69 "In feature {}, inherent size unknown so must be manually supplied.".format(
70 n
71 )

Callers 3

convertFunction · 0.85
_convert_sklearn_modelFunction · 0.85
convertFunction · 0.85

Calls 10

isinstanceFunction · 0.85
is_valid_feature_listFunction · 0.85
TypeErrorClass · 0.85
ArrayMethod · 0.80
addMethod · 0.65
lenFunction · 0.50
ModelMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected