Skip to content Skip to sidebar Skip to footer

Number Of Features In Dictionary

I am working on loading a dataset from a pickle file like this ''' Load the dictionary containing the dataset ''' with open('final_project_dataset.pkl', 'r') as data_file: data

Solution 1:

Try this.

no_of_features = len(data_dict[data_dict.keys()[0]])  

This will work only if all your keys in data_dict have same number of features.

or simply

no_of_features = len(data_dict['GLISAN JR BEN F'])  

Solution 2:

""" Load the dictionary containing the dataset """withopen("final_project_dataset.pkl", "r") as data_file:
  data_dict = pickle.load(data_file)
  printlen(data_dict)

Solution 3:

I think you want to find out the size of the set of all unique field names used in the row dictionaries. You can find that like this:

data_dict = {
    'red':{'alpha':1,'bravo':2,'golf':3,'kilo':4},
    'green':{'bravo':1,'delta':2,'echo':3},
    'blue':{'foxtrot':1,'tango':2}
}   
unique_features = set(
    feature
    for row_dict in data_dict.values()
    for feature in row_dict.keys()
)
print(unique_features)
# {'golf', 'delta', 'foxtrot', 'alpha', 'bravo', 'echo', 'tango', 'kilo'}
print(len(unique_features))
# 8

Solution 4:

Apply sum to the len of each nested dictionary:

sum(len(v) for _, v in data_dict.items())

v represents a nested dictionary object.

Dictionaries will naturally return their keys when you call an iterator on them (or something of that sort), so calling len will return the number of keys in each nested dictionary, viz. number of features.

If the features may be duplicated across nested objects, then collect them in a set and apply len

len(set(f for v in data_dict.values() for f in v.keys()))

Solution 5:

Here is the answer https://discussions.udacity.com/t/lesson-5-number-of-features/44253/4

where we choose 1 person in this case SKILLING JEFFREY K within the database called enron_data. and then we print the lenght of the keys in the dictionary.

printlen(enron_data["SKILLING JEFFREY K"].keys())

Post a Comment for "Number Of Features In Dictionary"