Tensorflow小技巧(一)

简介: Tensorflow小技巧(一)

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

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

df.loc[df['column_name'] == some_value]

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

df.loc[df['column_name'].isin(some_values)]

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

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

from collections import Counter

l = ["a","b","b"]
Counter(l)

pandas.DataFrame.drop_duplicates

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.

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:

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:

dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
dataset = dataset.map(lambda x: x*2)
list(dataset.as_numpy_iterator())
目录
相关文章
|
Android开发
如何用Airtest脚本无线连接Android设备?
如何用Airtest脚本无线连接Android设备?
217 0
|
自然语言处理 物联网 API
检索增强生成(RAG)实践:基于LlamaIndex和Qwen1.5搭建智能问答系统
检索增强生成(RAG)实践:基于LlamaIndex和Qwen1.5搭建智能问答系统
检索增强生成(RAG)实践:基于LlamaIndex和Qwen1.5搭建智能问答系统
|
Go
golang力扣leetcode 47.全排列II
golang力扣leetcode 47.全排列II
117 0
|
easyexcel UED
利用fd子系统实现图案与图片显示方法
利用fd子系统实现图案与图片显示方法
89 1
|
存储 Linux API
select、poll、epoll、多线程实现并发请求处理
select、poll、epoll、多线程实现并发请求处理
167 0
|
存储 JavaScript API
Pinia介绍及简单示例
Pinia介绍及简单示例
415 0
|
SQL Java 数据库
MyBatis-Plus 条件构造器
MyBatis-Plus 条件构造器
341 0
|
存储 缓存 安全
C++初阶之一篇文章教会你list(理解和使用)(上)
在C++标准库中,std::list 是一个双向链表容器,用于存储一系列元素。与 std::vector 和 std::deque 等容器不同,std::list 使用链表的数据结构来组织元素,因此在某些操作上具有独特的优势和性能特点。以下是关于 std::list 的详细介绍:
|
Go
golang力扣leetcode 969.煎饼排序
golang力扣leetcode 969.煎饼排序
64 0