You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Pandas DataFrame字符串列导入TensorFlow线性回归模型?

Hey there! Let's work through how to get that categorical Animal column ready for your linear regression model in TensorFlow—you’ve already looked at a couple of tools, so let’s clear up the confusion around each one.

Option 1: Using tf.one_hot directly (simple, no extra setup)

You’re right that tf.one_hot is useful here, but it needs integer inputs instead of strings. Here’s a complete, step-by-step workflow with your Pandas DataFrame:

  • Step 1: Map Animal strings to integer indices
    First, we’ll turn your known Animal values (like "Cat" and "Dog") into integers since tf.one_hot can’t work with raw strings.

    import pandas as pd
    import tensorflow as tf
    
    # Your sample DataFrame
    df = pd.DataFrame({
        'Animal': ['Cat', 'Dog', 'Cat', 'Dog', 'Cat'],
        'Name': ['Fluffy', 'Denton', 'Mittens', 'Max', 'Whiskers'],
        'Age': [2, 3, 1, 4, 2]
    })
    
    # Your known vocabulary list
    animal_vocab = ['Cat', 'Dog']
    # Map each Animal to an integer index
    df['Animal_idx'] = df['Animal'].map({animal: idx for idx, animal in enumerate(animal_vocab)})
    
  • Step 2: Convert to a TensorFlow Dataset and apply one-hot encoding
    We’ll wrap your DataFrame into a TensorFlow Dataset, then add the one-hot encoding in a simple preprocessing function:

    # Split features (Animal) and labels (Age, assuming this is your regression target)
    features = df[['Animal_idx']]
    labels = df['Age']
    
    # Create a Dataset from the DataFrame
    dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
    dataset = dataset.batch(2)  # Batch size for training
    
    # Add one-hot encoding
    def preprocess(features, label):
        # One-hot encode the integer index (depth = number of unique Animals)
        features['Animal_onehot'] = tf.one_hot(features['Animal_idx'], depth=len(animal_vocab))
        # Remove the integer index if you don't need it anymore
        del features['Animal_idx']
        return features, label
    
    dataset = dataset.map(preprocess)
    
  • Step 3: Build and train your linear regression model
    Now you can feed the encoded features directly into a Keras model:

    model = tf.keras.Sequential([
        # Input shape matches the number of one-hot columns (2 for Cat/Dog)
        tf.keras.layers.Dense(units=1, input_shape=(len(animal_vocab),))
    ])
    
    model.compile(optimizer='adam', loss='mean_squared_error')
    model.fit(dataset, epochs=10)
    
Option 2: Using tf.feature_column.categorical_column_with_vocabulary_list (no Protocol Buffers required!)

You mentioned thinking this tool needs make_parse_example_spec and Protocol Buffers—but that’s only if you’re loading data from TFRecord files. You can use feature columns directly with your Pandas DataFrame, no protobufs needed. Here’s how:

  • Step 1: Define your categorical feature column
    We’ll create a categorical column using your known vocabulary, then convert it to an indicator column (which gives one-hot encoding, perfect for linear regression):

    # Define the categorical column with your known Animal values
    animal_cat_col = tf.feature_column.categorical_column_with_vocabulary_list(
        key='Animal',  # Matches the column name in your DataFrame
        vocabulary_list=['Cat', 'Dog']
    )
    
    # Convert to an indicator column (one-hot encoding)
    animal_ind_col = tf.feature_column.indicator_column(animal_cat_col)
    
  • Step 2: Create an input function for the model
    This function will turn your DataFrame into a TensorFlow Dataset that works with feature columns:

    def input_fn(df, shuffle=True, batch_size=2):
        # Create Dataset from DataFrame (features = Animal column, labels = Age)
        dataset = tf.data.Dataset.from_tensor_slices((dict(df[['Animal']]), df['Age']))
        if shuffle:
            dataset = dataset.shuffle(buffer_size=len(df))
        dataset = dataset.batch(batch_size)
        return dataset
    
  • Step 3: Build and train the model
    Use DenseFeatures to wrap your feature column so Keras can understand it:

    model = tf.keras.Sequential([
        tf.keras.layers.DenseFeatures([animal_ind_col]),
        tf.keras.layers.Dense(units=1)
    ])
    
    model.compile(optimizer='adam', loss='mean_squared_error')
    model.fit(input_fn(df), epochs=10)
    
Quick Recommendations
  • Go with tf.one_hot if you want a simple, no-frills workflow with full control over the encoding process.
  • Use tf.feature_column if you think you might need more flexibility later (like adding embedding columns instead of one-hot, combining features, or scaling up to more complex pipelines).

内容的提问来源于stack exchange,提问作者Daniel Worthington-Bodart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:50:01