【问题标题】:using a list as a value in pandas.DataFeame and tensorFlow在 pandas.DataFrame 和 tensorFlow 中使用列表作为值
【发布时间】:2022-01-23 14:00:42
【问题描述】:

我想在 pandas.DataFrame 中使用 list 作为值 但是当我尝试在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()

【问题讨论】:

    标签: python-3.x pandas tensorflow numpy-ndarray


    【解决方案1】:

    我认为将您的功能缩减为一列不是通常的做法。

    快速修复是您可以输入以下行

    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
    

    请考虑。

    【讨论】:

      猜你喜欢
      • 2015-04-26
      • 2018-01-29
      • 2020-04-27
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      相关资源
      最近更新 更多