one hot 预测

问题1:  我运行老师发的6.6 LR代码,我将买车的最后期望改成了两类1(good)和0(bad),运行就会出现这样的问题
Traceback (most recent call last):
  File "/Users/eden/Downloads/小象机器学习/6.Data/6.6.LR.py", line 46, in <module>
    fpr, tpr, _ = metrics.roc_curve(y_test_one_hot.ravel(), y_test_one_hot_hat.ravel())
  File "/Library/Python/2.7/site-packages/sklearn/metrics/ranking.py", line 505, in roc_curve
    y_true, y_score, pos_label=pos_label, sample_weight=sample_weight)
  File "/Library/Python/2.7/site-packages/sklearn/metrics/ranking.py", line 297, in _binary_clf_curve
    check_consistent_length(y_true, y_score)
  File "/Library/Python/2.7/site-packages/sklearn/utils/validation.py", line 163, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [519, 1038]
 
如果我随意将两行或三行最后的结果改成2(可以理解为不确定),然后就可以运行了,为什么呢?但是不确保每次都会运行正确,有时候会出现这样的问题:
Traceback (most recent call last):
  File "/Users/eden/Downloads/小象机器学习/6.Data/6.6.LR.py", line 49, in <module>
    auc = metrics.roc_auc_score(y_test_one_hot, y_test_one_hot_hat, average='macro')
  File "/Library/Python/2.7/site-packages/sklearn/metrics/ranking.py", line 260, in roc_auc_score
    sample_weight=sample_weight)
  File "/Library/Python/2.7/site-packages/sklearn/metrics/base.py", line 118, in _average_binary_score
    sample_weight=score_weight)
  File "/Library/Python/2.7/site-packages/sklearn/metrics/ranking.py", line 251, in _binary_roc_auc_score
    raise ValueError("Only one class present in y_true. ROC AUC score "
ValueError: Only one class present in y_true. ROC AUC score is not defined in that case.
问题2: Traceback (most recent call last):
  File "/Users/eden/Downloads/小象机器学习/8.Regression/8.4.Iris_LR.py", line 94, in <module>
    x_test = np.stack((x1.flat, x2.flat), axis=1)   # 测试点
AttributeError: 'module' object has no attribute 'stack'
 

邹博 - 计算机科学博士,深谙机器学习算法原理

赞同来自: fish Eden_sly

这是《机器学习升级版IV》中“数据清洗和特征提取”章节的问题。 依次回答如下: 1、对于二分类的one-hot编码其实是没必要分成两列的,因此,=13.5ptlabel_binarize对于这样的数据实际上只是给出1列,因此在后面调用auc时就出错了。可以按照下面的截图修改代码即可。 2、这个问题是由于Numpy在1.10才加入stack函数,因此,可以要么使用vstack/hstack代替,要么升级Numpy即可。
1490944107513.png
1490944225266.png
 

Eden_sly

赞同来自:

谢谢老师

要回复问题请先登录注册