简体   繁体   English

需要帮助来理解这一行代码(字典,键,熊猫,numpy)

[英]Need help understanding this line of code (dictionaries, keys, pandas, numpy)

I'm attempting a Google Crash Course to learn TensorFlow and Machine Learning. 我正在尝试Google Crash课程来学习TensorFlow和机器学习。 I am having trouble comprehending one of the lines from their coding examples . 我很难理解他们的编码示例中的其中一行。

def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):
"""Trains a linear regression model of one feature.

Args:
  features: pandas DataFrame of features
  targets: pandas DataFrame of targets
  batch_size: Size of batches to be passed to the model
  shuffle: True or False. Whether to shuffle the data.
  num_epochs: Number of epochs for which data should be repeated. None = repeat indefinitely
Returns:
  Tuple of (features, labels) for next data batch
"""

# Convert pandas data into a dict of np arrays.
features = {key:np.array(value) for key,value in dict(features).items()}

I need help understanding that last line of code. 我需要帮助来了解最后一行代码。

features = {key:np.array(value) for key,value in dict(features).items()}

I've researched dictionaries in an attempt to understand it myself, but it's still a bit much for me to grasp. 我已经研究过字典以试图自己理解它,但是我仍然要掌握很多东西。 I've attempted to write the same line of code in a way I can understand: 我试图以一种我能理解的方式编写同一行代码:

np_dict_array = dict(features).items()

for key,value in np_dict_array:
    features += np_dict_array[key]

I do not think I am rewriting the code correctly. 我认为我没有正确重写代码。 To get specific, I need help understanding what this does in that line of code: 具体来说,我需要帮助来了解这行代码的作用:

key:np.array(value)

If anybody could explain what that line of code is doing, or (bonus points) rewrite it in a novice-friendly way, I would greatly appreciate it! 如果有人可以解释该代码行的功能,或者(奖励积分)以新手友好的方式重写它,我将不胜感激!

features = {key:np.array(value) for key,value in dict(features).items()} features = {key:np.array(value)for key,dict(features).items()中的值}

It is a dictionary comprehension . 这是字典的理解 It converts all values in dict(features) to a Numpy array. 它将dict(features)中的所有值转换为Numpy数组。

key:np.array(value) 键:np.array(值)

This is how you assign key value pairs to a dictionary. 这是将键值对分配给字典的方式。

Alternate syntax: 备用语法:

features = {}
for key, value in dict(features).items():
    features[key] = np.array(value)

Comprehensions are popular as they reduce this sort of common pattern down to a single line. 理解很普遍,因为它们将这种常见的模式减少为一行。 However, it is sometimes tempting to try to do too much in a comprehension as complexity grows. 但是,有时随着复杂性的提高,尝试去做太多事情是很诱人的。

This is a 'dictionary comprehension' - modelled on a list comprehension, but making a new dictionary instead. 这是一种“字典理解”-以列表理解为模型,但改为制作新字典。

features = {key:np.array(value) for key,value in dict(features).items()}

take things from the inside out: 从内而外取东西:

dict(features)    # make a dictionary from the `features` argument
    .items()      # make a list of (key,value) tuples
for key,value     # iterate on those tuples
np.array(value)   # make a numpy array from the value
key:...           # make a new entry in the new dictionary

In sum, it makes a dictionary from features , making sure that the value for each item is a numpy array. 总而言之,它会根据features创建一个字典,确保每一项的value都是一个numpy数组。

fdict = dict(features)
adict = dict()   # empty dictionary
for key,value in fdict.items():
    adict[key] = np.array(value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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