前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Tensorflow小技巧(一)

Tensorflow小技巧(一)

原创
作者头像
XianxinMao
修改2021-07-29 10:52:49
2650
修改2021-07-29 10:52:49
举报
文章被收录于专栏:深度学习框架深度学习框架

how-do-i-select-rows-from-a-dataframe-based-on-column-values

To select rows whose column value equals a scalar, some_value, use ==:

代码语言:javascript
复制
df.loc[df['column_name'] == some_value]

To select rows whose column value is in an iterable, some_values, use isin:

代码语言:javascript
复制
df.loc[df['column_name'].isin(some_values)]

how-do-i-sort-a-dictionary-by-value

代码语言:javascript
复制
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
dict(sorted(x.items(), key=lambda item: item[1]))

how-can-i-count-the-occurrences-of-a-list-item

代码语言:javascript
复制
from collections import Counter
​
l = ["a","b","b"]
Counter(l)

pandas.DataFrame.drop_duplicates

代码语言:javascript
复制
df = pd.DataFrame({
...     'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
...     'style': ['cup', 'cup', 'cup', 'pack', 'pack'],
...     'rating': [4, 4, 3.5, 15, 5]
... })
​
df.drop_duplicates(subset=['brand'])

tf.data.Dataset-----as_numpy_iterator()

Returns an iterator which converts all elements of the dataset to numpy.

代码语言:javascript
复制
dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
for element in dataset.as_numpy_iterator():
  print(element)

tf.data.Dataset

The tf.data.Dataset API supports writing descriptive and efficient input pipelines. Dataset usage follows a common pattern:

  1. Create a source dataset from your input data.
  2. Apply dataset transformations to preprocess the data.
  3. Iterate over the dataset and process the elements.

Iteration happens in a streaming fashion, so the full dataset does not need to fit into memory.

The simplest way to create a dataset is to create it from a python list:

代码语言:javascript
复制
dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
for element in dataset:
  print(element)

Once you have a dataset, you can apply transformations to prepare the data for your model:

代码语言:javascript
复制
dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
dataset = dataset.map(lambda x: x*2)
list(dataset.as_numpy_iterator())

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • how-do-i-select-rows-from-a-dataframe-based-on-column-values
  • how-do-i-sort-a-dictionary-by-value
  • how-can-i-count-the-occurrences-of-a-list-item
  • pandas.DataFrame.drop_duplicates
  • tf.data.Dataset-----as_numpy_iterator()
  • tf.data.Dataset
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档