Pandas DataFrame and Keras

前端 未结 3 1032
星月不相逢
星月不相逢 2020-12-14 09:46

I\'m trying to perform a sentiment analysis in Python using Keras. To do so, I need to do a word embedding of my texts. The problem appears when I try to fit the data to my

3条回答
  •  死守一世寂寞
    2020-12-14 10:36

    https://pypi.org/project/keras-pandas/

    Easiest way is having the keras_pandas package to fit a pandas dataframe to keras.The code shown below is an general example from the package docs.

    from keras import Model
    from keras.layers import Dense
    
    from keras_pandas.Automater import Automater
    from keras_pandas.lib import load_titanic
    
    observations = load_titanic()
    
    # Transform the data set, using keras_pandas
    categorical_vars = ['pclass', 'sex', 'survived']
    numerical_vars = ['age', 'siblings_spouses_aboard', 'parents_children_aboard', 'fare']
    text_vars = ['name']
    
    auto = Automater(categorical_vars=categorical_vars, numerical_vars=numerical_vars, text_vars=text_vars,
     response_var='survived')
    X, y = auto.fit_transform(observations)
    
    # Start model with provided input nub
    x = auto.input_nub
    
    # Fill in your own hidden layers
    x = Dense(32)(x)
    x = Dense(32, activation='relu')(x)
    x = Dense(32)(x)
    
    # End model with provided output nub
    x = auto.output_nub(x)
    
    model = Model(inputs=auto.input_layers, outputs=x)
    model.compile(optimizer='Adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    
    # Train model
    model.fit(X, y, epochs=4, validation_split=.2)
    

提交回复
热议问题