代码之家  ›  专栏  ›  技术社区  ›  Peanut14

用多uniques张量流神经网络处理分类特征

  •  0
  • Peanut14  · 技术社区  · 6 年前

    我试图预测某个用户将预订哪家酒店。我有12个输入列,它们都是分类的 INT 格式。我试着训练一个神经网络 Tensorflow Keras 预测特征列 hotel_cluster 它有6个可能的唯一值。

    数据的分布在 集群酒店 标签:

    Histogram of Distribution

    问题1:一个热编码

    我认为只有当字符串的分类特征出现时,才需要一个热编码。但后来我读到它是在分类特征在排序时没有任何意义时使用的(阿根廷=1,智利=4并不意味着阿根廷<智利)。

    因此,我想成为我的专栏,但大多数重要的专栏都有很多独特的价值观(而且可能有更多在培训中看不到的价值观):

    Col Name                   | Number of Unique Values
    ---------------------------------------------
    site_name :                | 42
    user_location_country :    | 218
    user_location_region :     | 873
    user_location_city :       | 20262
    srch_adults_cnt :          | 9
    srch_children_cnt :        | 10
    srch_rm_cnt :              | 8
    srch_destination_id :      | 12713
    srch_destination_type_id : | 8
    is_booking :               | 2
    hotel_continent :          | 7
    hotel_country :            | 176
    
    1. 在 DNN?
    2. 如何处理在训练分类价值观时没有看到的问题?
    3. 有哪些方法来准备分类数值数据 在Tensorflow中使用?
    4. 如果分类数字数据的顺序合理,我应该 它是“原样”?

    背景及相关规范

    这是来自Kaggle的Expedia问题,但是减少到6个酒店集群和510k个样本,作为一个类中的预测任务提供给我。任务已经过了截止日期,但是我想了解如何制作这个模型。

    import tensorflow as tf
    tf.enable_eager_execution()
    import pandas as pd
    import numpy as np
    from sklearn.preprocessing import LabelBinarizer
    from sklearn.model_selection import train_test_split
    
    
    # DATA
    to_predict = pd.read_csv('test.csv')
    df = pd.read_csv('train.csv')
    train, test = train_test_split(df, test_size=.2, train_size=.80)
    train, val = train_test_split(train, test_size=.2)
    
    # Features and target:
    features = ['site_name', 'user_location_country', 'user_location_region',  'user_location_city', 'srch_adults_cnt', 'srch_children_cnt', 'srch_rm_cnt', 'srch_destination_id', 'srch_destination_type_id', 'is_booking', 'hotel_continent', 'hotel_country']
    
    target = 'hotel_cluster'
    
    # Pre Keras processing
    X_train = train[features].values
    X_test = test[features].values
    X_val = val[features].values
    
    label_bin = LabelBinarizer()
    y_train = label_bin.fit_transform(train['hotel_cluster'].values)
    y_test = label_bin.transform(test['hotel_cluster'].values)
    y_val = label_bin.transform(val['hotel_cluster'].values)
    
    X_to_pred = dft[features].values
    
    # Model and fitting
    model = tf.keras.Sequential([
      layers.Dense(128, activation='relu', input_shape=(12,)),
      layers.Dense(64, activation='relu'),
      layers.Dense(len(label_bin.classes_), activation='softmax')
    ])
    
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'], run_eagerly=True)
    
    H = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=4, batch_size=32)
    
    # Predictions
    predictions = model.predict(X_to_pred)
    

    预测如下:

    array([[0., 1., 0., 0., 0., 0.],
           [0., 1., 0., 0., 0., 0.],
           [0., 1., 0., 0., 0., 0.],
           ...,
           [0., 1., 0., 0., 0., 0.],
           [0., 1., 0., 0., 0., 0.],
           [0., 1., 0., 0., 0., 0.]], dtype=float32)
    

    预期行为

    我希望预测是一个充满数字的矩阵,每行代表标签成为其每个级别的概率。目前,这个模型看起来不起作用,我认为这是因为错误的数据预处理。

    我不知道怎么让你们知道“train.csv”。。。

    0 回复  |  直到 6 年前