Tensorflow-datasetI使用_1.基础api使用

本文详细介绍如何使用TensorFlow和Python进行数据处理和机器学习,包括数据集的构建、重复与批处理、数据集的交织处理,以及如何利用numpy、pandas等库进行数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import matplotlib as mpl #Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用

import matplotlib.pyplot as plt #Python数据可视化matplotlib.pyplot

#%matplotlib inline #在使用jupyter notebook 或者 jupyter qtconsole的时候,经常会用到%matplotlib inline。其作用就是在你调用plot()进行画图或者直接输入Figure的实例对象的时候,会自动的显示并把figure嵌入到console中。

import numpy as np#数值计算扩展。这种工具可用来存储和处理大型矩阵

import sklearn#机器学习中常用的第三方模块,对常用的机器学习方法进行了封装,包括回归(Regression)、降维(Dimensionality Reduction)、分类(Classfication)、聚类(Clustering)等方法。
    
import pandas as pd#是python的一个数据分析包
import os #系统编程的操作模块,可以处理文件和目录
import sys #sys模块包含了与Python解释器和它的环境有关的函数
import time 
import tensorflow as tf

from tensorflow import keras
##################################################################################################
#选择GPU
#os.environ["CUDA_VISIBLE_DEVICES"] = "0"

##################################################################################################

print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
    print(module.__name__, module.__version__)

"""output:
    2.0.0
    sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)
    matplotlib 3.1.1
    numpy 1.16.5
    pandas 0.25.1
    sklearn 0.21.3
    tensorflow 2.0.0
"""

##################################################################################################
#从内存中构建数据集,参数可以是普通的列表、numpy数组、字典
dataset = tf.data.Dataset.from_tensor_slices(np.arange(10))
print(dataset)

"""ouput:
    <TensorSliceDataset shapes: (), types: tf.int64>
"""

for item in dataset:
    print(item)

"""
output:tf.Tensor(0, shape=(), dtype=int64)
        tf.Tensor(1, shape=(), dtype=int64)
        tf.Tensor(2, shape=(), dtype=int64)
        tf.Tensor(3, shape=(), dtype=int64)
        tf.Tensor(4, shape=(), dtype=int64)
        tf.Tensor(5, shape=(), dtype=int64)
        tf.Tensor(6, shape=(), dtype=int64)
        tf.Tensor(7, shape=(), dtype=int64)
        tf.Tensor(8, shape=(), dtype=int64)
        tf.Tensor(9, shape=(), dtype=int64)
"""

##################################################################################################

# 1. repeat epoch   对数据集重复的读取,相当于多个epoch
# 2. get batch  从数据中取批次大小为7(batchsize为7)


dataset = dataset.repeat(3).batch(7)
for item in dataset:
    print(item)

"""
output:
    tf.Tensor([0 1 2 3 4 5 6], shape=(7,), dtype=int64)
    tf.Tensor([7 8 9 0 1 2 3], shape=(7,), dtype=int64)
    tf.Tensor([4 5 6 7 8 9 0], shape=(7,), dtype=int64)
    tf.Tensor([1 2 3 4 5 6 7], shape=(7,), dtype=int64)
    tf.Tensor([8 9], shape=(2,), dtype=int64)
"""

##################################################################################################
# interleave: 表示从数据集中每一个进行处理,处理后的结果再进行合并一个新的数据集
# 常用:现有的dataset中存储的是文件名,用interleave把其中每一个文件名对应的文件进行读取,然后进行合并形成一个新的数据集
# 文件dataset -> 具体数据集

dataset2 = dataset.interleave(
    lambda v: tf.data.Dataset.from_tensor_slices(v), # 转换函数
    cycle_length=5,  # cycle_length #并行处理数据集中5个数据
    block_length=5,  # block_length #每次从转换函数中取多少个元素
)
for item in dataset2:
    print(item)
"""
output:
    tf.Tensor(0, shape=(), dtype=int32)
    tf.Tensor(1, shape=(), dtype=int32)
    tf.Tensor(2, shape=(), dtype=int32)
    tf.Tensor(3, shape=(), dtype=int32)
    tf.Tensor(4, shape=(), dtype=int32) #从第一个取5个
    tf.Tensor(7, shape=(), dtype=int32)
    tf.Tensor(8, shape=(), dtype=int32)
    tf.Tensor(9, shape=(), dtype=int32)
    tf.Tensor(0, shape=(), dtype=int32)
    tf.Tensor(1, shape=(), dtype=int32) # 从第二个取5个
    tf.Tensor(4, shape=(), dtype=int32)
    tf.Tensor(5, shape=(), dtype=int32)
    tf.Tensor(6, shape=(), dtype=int32)
    tf.Tensor(7, shape=(), dtype=int32)
    tf.Tensor(8, shape=(), dtype=int32) # 从第三个取5个
    tf.Tensor(1, shape=(), dtype=int32)
    tf.Tensor(2, shape=(), dtype=int32)
    tf.Tensor(3, shape=(), dtype=int32)
    tf.Tensor(4, shape=(), dtype=int32)
    tf.Tensor(5, shape=(), dtype=int32) # 从第四个取5个
    tf.Tensor(8, shape=(), dtype=int32) 
    tf.Tensor(9, shape=(), dtype=int32)
    tf.Tensor(5, shape=(), dtype=int32)
    tf.Tensor(6, shape=(), dtype=int32)
    tf.Tensor(2, shape=(), dtype=int32) #从第5个取两个(总共就两个)然后再从第一个取3个
    tf.Tensor(3, shape=(), dtype=int32)
    tf.Tensor(9, shape=(), dtype=int32)
    tf.Tensor(0, shape=(), dtype=int32)
    tf.Tensor(6, shape=(), dtype=int32)
    tf.Tensor(7, shape=(), dtype=int32)

"""
##################################################################################################
#参数是元组
x = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array(['cat', 'dog', 'fox'])

# x,y第一个维度的个数必须是相等的,[1,2] 对应cat 等
dataset3 = tf.data.Dataset.from_tensor_slices((x, y)) #(x,y)是元组
print(dataset3)

for item_x, item_y in dataset3:
    print(item_x.numpy(), item_y.numpy())
    print(item_x,item_y)
"""output:
    <TensorSliceDataset shapes: ((2,), ()), types: (tf.int32, tf.string)>
    
    [1 2] b'cat'
    tf.Tensor([1 2], shape=(2,), dtype=int32) tf.Tensor(b'cat', shape=(), dtype=string)
    [3 4] b'dog'
    tf.Tensor([3 4], shape=(2,), dtype=int32) tf.Tensor(b'dog', shape=(), dtype=string)
    [5 6] b'fox'
    tf.Tensor([5 6], shape=(2,), dtype=int32) tf.Tensor(b'fox', shape=(), dtype=string)
"""

##################################################################################################
#参数是字典
dataset4 = tf.data.Dataset.from_tensor_slices({"feature": x,
                                               "label": y})

for item in dataset4:
    print(item["feature"].numpy(), item["label"].numpy())
    print(item["feature"],item["label"])

"""output:
    [1 2] b'cat'
    tf.Tensor([1 2], shape=(2,), dtype=int32) tf.Tensor(b'cat', shape=(), dtype=string)
    [3 4] b'dog'
    tf.Tensor([3 4], shape=(2,), dtype=int32) tf.Tensor(b'dog', shape=(), dtype=string)
    [5 6] b'fox'
    tf.Tensor([5 6], shape=(2,), dtype=int32) tf.Tensor(b'fox', shape=(), dtype=string)
"""


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值