Home > database >  using a list as a value in pandas.DataFeame and tensorFlow
using a list as a value in pandas.DataFeame and tensorFlow

Time:01-24

Im tring to use list as a value in pandas.DataFrame but Im getting Exception when trying to use use the adapt function in on the Normalization layer with the NumPy array

this is the error: ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).

and this is the code:

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()

CodePudding user response:

I think it is not the usual practice to shrink your features into one column.

Quick-fix is you may put the following line

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

before

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

to get rid of the error, but now because your train_features has changed from a DataFrame into a np.array, your subsequent code may suffer, so you need to take care of that too.

If I were you, however, I would have constructed the DataFrame this way

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

Please consider.

  •  Tags:  
  • Related