在 pandas.DataFeame 和 tensorFlow 中使用列表作为值

using a list as a value in pandas.DataFeame and tensorFlow

我想在 pandas.DataFrame 中使用列表作为值 但是当我尝试在 Normalization 层上使用具有 NumPy 数组

的 adapt 函数时出现异常

这是错误: ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).

这是代码:

import pandas as pd
import numpy as np

# Make NumPy printouts easier to read.
np.set_printoptions(precision=3, suppress=True)

    import tensorflow as tf
    from tensorflow.keras import layers
    
    data = [[45.975, 45.81, 45.715, 45.52, 45.62, 45.65, 4],
            [55.67, 55.975, 55.97, 56.27, 56.23, 56.275, 5],
            [86.87, 86.925, 86.85, 85.78, 86.165, 86.165, 3],
            [64.3, 64.27, 64.285, 64.29, 64.325, 64.245, 6],
            [35.655, 35.735, 35.66, 35.69, 35.665, 35.63, 5]
            ]
    lables = [0, 1, 0, 1, 1]
    
    
    def do():
        d_1 = None
        for l, d in zip(lables, data):
            if d_1 is None:
                d_1 = pd.DataFrame({'lable': l, 'close_price': [d]})
            else:
                d_1 = d_1.append({'lable': l, 'close_price': d}, ignore_index=True)
        dataset = d_1.copy()
    
        print(dataset.isna().sum())
        dataset = dataset.dropna()
        print(dataset.keys())
    
        train_dataset = dataset.sample(frac=0.8, random_state=0)
        test_dataset = dataset.drop(train_dataset.index)
        print(train_dataset.describe().transpose())
    
        train_features = train_dataset.copy()
        test_features = test_dataset.copy()
    
        train_labels = train_features.pop('lable')
        test_labels = test_features.pop('lable')
    
        print(train_dataset.describe().transpose()[['mean', 'std']])
    
        normalizer = tf.keras.layers.Normalization(axis=-1)
        ar = np.array(train_features)
        normalizer.adapt(ar)
        print(normalizer.mean.numpy())
    
        first = np.array(train_features[:1])
    
        with np.printoptions(precision=2, suppress=True):
            print('First example:', first)
            print()
            print('Normalized:', normalizer(first).numpy())
    
        diraction = np.array(train_features)
    
        diraction_normalizer = layers.Normalization(input_shape=[1, ], axis=None)
        diraction_normalizer.adapt(diraction)
    
        diraction_model = tf.keras.Sequential([
            diraction_normalizer,
            layers.Dense(units=1)
        ])
    
        print(diraction_model.summary())
    
        print(diraction_model.predict(diraction[:10]))
    
        diraction_model.compile(
            optimizer=tf.optimizers.Adam(learning_rate=0.1),
            loss='mean_absolute_error')
        print(train_features['close_price'])
        history = diraction_model.fit(
            train_features['close_price'],
            train_labels,
            epochs=100,
            # Suppress logging.
            verbose=0,
            # Calculate validation results on 20% of the training data.
            validation_split=0.2)
    
        hist = pd.DataFrame(history.history)
        hist['epoch'] = history.epoch
        print(hist.tail())
    
        test_results = {}
        test_results['diraction_model'] = diraction_model.evaluate(
            test_features,
            test_labels, verbose=0)
    
        x = tf.linspace(0.0, 250, 251)
        y = diraction_model.predict(x)
    
        print("end")
    
    
    def main():
        do()
    
    
    if __name__ == "__main__":
        main()

我认为将特征缩小到一列中不是通常的做法。

Quick-fix 是你可以放下面一行

train_features = np.array(train_features['close_price'].to_list())

之前

normalizer = tf.keras.layers.Normalization(axis=-1)

来消除错误,但是现在因为你的train_features已经从DataFrame变成了np.array,你的后续代码可能会受到影响,所以你需要小心那也是。

但是,如果我是你,我会这样构建 DataFrame

df = pd.DataFrame(data)
df['label'] = lables

请考虑。