我使用 dataset = tf.data.experimental.make_csv_dataset() 函式從 csv 檔案創建資料集,但我的資料集具有分類和數字特征。
dataset=
color price weight
red 120 1.2
blue 80 2.0
green 90 3
問題1:問題是我怎樣才能只修改單個特征,例如重量 2,以:
dataset=
color price weight
red 120 3.2
blue 80 4.0
green 90 5
我嘗試做類似的事情:
dataset = dataset.apply(lambda x: x['weight'] 2)
但錯誤是:“TypeError:'FilterDataset'物件不可下標”
檔案中的示例https://www.tensorflow.org/api_docs/python/tf/data/Dataset#apply沒有顯示它。
問題 2:如何洗掉單個功能?有沒有等效于 pandas drop column 的東西?
uj5u.com熱心網友回復:
您可以通過僅過濾您想要的功能來洗掉功能。這就是您只能修改一個功能的方式:
import tensorflow as tf
import pandas as pd
df = pd.DataFrame(data={'color': ['red', 'blue','green'], 'price': [120, 80, 90], 'weight': [3.2, 4.0, 5]})
df.to_csv('data.csv', index=False)
dataset = tf.data.experimental.make_csv_dataset('/content/data.csv', batch_size=1, num_epochs = 1, shuffle=False)
dataset = dataset.map(lambda x: (x['color'], x['price'], x['weight'] 2))
for x in dataset:
print(x[0], x[1], x[2])
tf.Tensor([b'red'], shape=(1,), dtype=string) tf.Tensor([120], shape=(1,), dtype=int32) tf.Tensor([5.2], shape=(1,), dtype=float32)
tf.Tensor([b'blue'], shape=(1,), dtype=string) tf.Tensor([80], shape=(1,), dtype=int32) tf.Tensor([6.], shape=(1,), dtype=float32)
tf.Tensor([b'green'], shape=(1,), dtype=string) tf.Tensor([90], shape=(1,), dtype=int32) tf.Tensor([7.], shape=(1,), dtype=float32)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/426829.html
