【问题标题】:why am i getting same value as prediction for all?为什么我得到与所有人的预测相同的价值?
【发布时间】:2017-09-08 14:40:34
【问题描述】:

我正在尝试使用 sci-kit 构建决策树。但我得到的值与对所有值的预测相同。

le = preprocessing.LabelEncoder()
    def labelEncoder(df, col_name):
       df[[col_name]] = le.fit_transform(df[[col_name]])
    labelEncoder(dfr, "Gender")
    labelEncoder(dfr, "Subscription Tenure Type")
    labelEncoder(dfr, "Located Region")
    labelEncoder(dfr, "Attrition")
    labelEncoder(dfr, "Type of subscription")
    labelEncoder(dfr, "Genre")
    # # Splitiing the data to test and train
    feature = dfr[["Gender", "Age", "Subscription year", "Subscription Tenure Type", "Type of subscription",
          "Located Region", "Average Hours of watching(Weekly)", "Attrition",
          "Web channle utilization", "Mobile Channel Utilization"]]
    labels = dfr[["Genre"]]
clf_gini = DecisionTreeClassifier(criterion="entropy", random_state=100,
                                     max_depth=3, min_samples_leaf=9 ,min_samples_split=2, splitter='random')

clf_gini.fit(feature_train, labels_train)
y_pred = clf_gini.predict(feature_test)

print(list((y_pred)))

以下是样本数据。

User Id Genre   Rating  Gender  Age Subscription year   Subscription Tenure Type    Type of subscription    Located Region  Average Hours of watching(Weekly)   Attrition   Web channle utilization Mobile Channel Utilization
1   Romance 4   Female  51  2000    Annual  Individual  R3  7   Yes 89  11
2   Action  4.769230769 Female  42  2004    6 Months    Individual  R6  13  No  88  12
2   Adventure   4.909090909 Female  42  2004    6 Months    Individual  R6  13  No  88  12
2   Comedy  4.2 Female  42  2004    6 Months    Individual  R6  13  No  88  12
2   Crime   5   Female  42  2004    6 Months    Individual  R6  13  No  88  12
2   Drama   4.2 Female  42  2004    6 Months    Individual  R6  13  No  88  12

【问题讨论】:

    标签: python scikit-learn recommendation-engine


    【解决方案1】:

    您提供的代码 sn-p 存在一些问题。

    • 您正在使用svm 而不是clf_gini 进行预测;
    • 缺少用于将数据集实际拆分为训练和测试的代码;
    • 您是否对训练集和测试集应用了相同的转换?

    【讨论】:

    • 我错误地复制了注释代码。在我的实际代码中,我一直在使用 clf_gini.predict()
    • 是的!我确实申请了。我不太关心数据。如果您查看示例数据,您可以看到单个用户的条目不止一个。这意味着如果用户评价了一种以上的流派。然后在训练数据集中该用户不止一行。这是正确的做法吗?
    【解决方案2】:

    你打电话给svm,而不是clf_gini。如果这不能回答您的问题,您能否提供更多详细信息?

    以下示例代码有效:

    import pandas as pd
    
    arr = [[1  , 'Romance', 4,   'Female',  51,  2000,    'Annual' , 'Individual' , 'R3',  7,   'Yes', 89,  11],
    [2  , 'Action' , 4.7, 'Female',  42,  2004,    '6 Months' ,   'Individual',  'R6',  13,  'No',  88,  12],
    [2  , 'Adventure',   4.9, 'Female',  42,  2004,    '6 Months',    'Individual',  'R6',  13,  'No',  88,  12],
    [2  , 'Comedy' , 4.2, 'Female',  42 , 2004,    '6 Months' ,   'Individual',  'R6'  ,13,  'No',  88,  12],
    [2  , 'Crime'  , 5  , 'Female',  42 , 2004,    '6 Months' ,   'Individual',  'R6' , 13,  'No',  88,  12],
    [2  , 'Drama'  , 4.2, 'Female',  42,  2004,    '6 Months' ,   'Individual',  'R6',  13,  'No',  88,  12]]
    
    headers = ['User Id', 'Genre',   'Rating',  'Gender',  'Age', 'Subscription year',   'Subscription Tenure Type', 'Type of subscription',  'Located Region',  'Average Hours of watching(Weekly)',   'Attrition',   'Web channle utilization', 'Mobile Channel Utilization']
    
    dfr = pd.DataFrame(arr, columns = headers )
    
    import sklearn
    le = sklearn.preprocessing.LabelEncoder()
    def labelEncoder(df, col_name):
        df[[col_name]] = le.fit_transform(df[[col_name]])
    labelEncoder(dfr, "Gender")
    labelEncoder(dfr, "Subscription Tenure Type")
    labelEncoder(dfr, "Located Region")
    labelEncoder(dfr, "Attrition")
    labelEncoder(dfr, "Type of subscription")
    labelEncoder(dfr, "Genre")
    
    # # Splitiing the data to test and train
    
    feature = dfr[["Gender", "Age", "Subscription year", "Subscription Tenure Type", "Type of subscription",
      "Located Region", "Average Hours of watching(Weekly)", "Attrition",
      "Web channle utilization", "Mobile Channel Utilization"]]
    
    clf_gini = DecisionTreeClassifier(criterion="entropy", random_state=100,
                                     max_depth=3, min_samples_leaf=9 ,min_samples_split=2, splitter='random')
    
    # create test / train split
    dfr_train = dfr.iloc[:-1]
    dfr_test = dfr.iloc[-1]
    y_train = dfr_train['Genre']
    y_test = dfr_test['Genre']
    
    del dfr_train['Genre']
    del dfr_test['Genre']
    
    
    clf_gini.fit(dfr_train, y_train)
    y_pred = clf_gini.predict(dfr_test)
    
    print(list((y_pred)))
    

    【讨论】:

    • 与 Ryan 的回答相比,这并没有增加新的东西。
    • 谢谢,瓦伦。我不太关心数据。如果您查看示例数据,您可以看到单个用户的条目不止一个。这意味着如果用户评价了一种以上的流派。然后在训练数据集中该用户不止一行。这是正确的做法吗?
    猜你喜欢
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多