python3 learn to use api
Extract features from samples of dictionary-type data structures and convert them into vector form
Source code git: https://github.com/linyi0604/MachineLearning
Code:
1 from sklearn.feature_extraction import DictVectorizer 2 3 ''' 4 Dictionary feature extractor: 5 Extract and vectorize the dictionary data structure 6 Category type features are vectorized using 0 1 binary method with the help of prototype feature names 7 Numeric type features remain unchanged Variable 8 ''' 9 10 #Define a list of dictionaries to represent multiple data samples 11 measurements = [ 12 { " city " : " Dubai " , " temperature " : 33.0 }, 13 { " city " :" London " , " temperature " : 12.0 }, 14 { " city " : " San Fransisco " , " temperature " : 18.0 }, 15 ] 16 17 #Initialize dictionary feature extractor 18 vec = DictVectorizer() 19 data = vec. fit_transform(measurements).toarray() 20 #View the extracted feature values 21 print (data) 22 ''' 23 [[ 1. 0. 0. 33.] 24 [ 0. 1. 0. 12.] 25 [ 0. 0. 1. 18.]] 26 ''' 27 #View the meaning of the extracted features 28 print (vec.get_feature_names()) 29 ''' 30 [' city=Dubai', 'city=London', 'city=San Fransisco', 'temperature'] 31 '''