简体   繁体   中英

Accessing data from python dictionary

import numpy as np

def unpickle(file):
   import pickle
   with open(file, 'rb') as fo:
   dict = pickle.load(fo, encoding='bytes')
   return dict

 train = []
for j in range (1,6):
train.append(unpickle('/Users/sachalabdullah/Desktop/cifar-10-batches-py/data_batch_'+str(j)))

test = unpickle ("/Users/sachalabdullah/Desktop/cifar-10-batches-py/test_batch”)

I have loaded CIFAR_10 since I am new to python I don't know how to access the data from dictionary.

There is another thing which is confusing me that I have appended all the five batches of training data in train suppose I have access only the labels and images from data set so if I am accessing it I would be getting data from all five batches or I need to access images and labels separately for every batch ?

Is there any Matlab equivalent to this, if I wanted to get column 1 and 2 from a matrix I would do A(:, [1,2]) , or there is not?

First create your dump file.

file = open('filename', 'r')
obj = file.read()
pickle.dump(obj, open('file.pickle', 'wb'))

then

with open(file.pickle, 'rb') as fo:
   dict = pickle.load(fo, encoding='bytes')

To access keys in a dictionary:

mydict={'mykey':['value1','value2']}

#access mykey from mydict:
mydict['mykey']

to load that data properly into a dict I'd do this:

def unpickle(file):
   import pickle
   dict={}
   with open(file, 'rb') as fo:
   dict[fo] = pickle.load(fo, encoding='bytes')
   return dict

#how to access the data in your train[j] dict:
[v for v in train[j].values()]

the python equivalent to get the first two rows of a matrix:

A=np.matrix([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
A[:,[0,1]]

keep in mind that the index starts at 0 for python. matlab index starts at 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM