Sci-Kit学习:调查错误分类的数据

时间:2015-12-31 18:54:59

标签: python machine-learning scikit-learn

我想使用sci-kit learn分析模型错误分类的数据,以便我可以改进我的功能生成。我有一个方法可以做到这一点,但我是sci-kit学习和熊猫的新手,所以我想知道是否有更有效/直接的方法来实现这一目标。这似乎是标准工作流程的一部分,但在我做的研究中,我没有找到任何直接解决从模型分类,通过特征矩阵到原始数据的向后映射的任何事情。

这里是我使用的上下文/工作流程,以及我设计的解决方案。下面是示例代码。

上下文。我的工作流程如下:

  1. 从一堆JSON blob开始,即原始数据。这是pandas DataFrame。
  2. 为建模提取相关部分,将其称为数据。这是一只大熊猫Dataframe。
  3. 此外,我们有所有数据的真实数据,因此我们称之为真相或y。
  4. 在sci-kit中创建一个特征矩阵学习,调用此X.这是一个大的稀疏矩阵。
  5. 创建一个随机的林对象,调用此林。
  6. 使用sci-kit learn split_train_test()函数创建特征矩阵的随机子集,以进行训练和测试。
  7. 在上面的训练数据上训练森林,X_train,这是一个大的稀疏矩阵。
  8. 获取误报和误报结果的索引。这些是X_test的索引,一个稀疏矩阵。
  9. 从假阳性指数转到X_test回到原始数据
  10. 如有必要,请从数据转到原始数据。
  11. 解。

    • 将一个索引数组传递给split_test_train()函数,该函数将在索引数组上应用相同的随机函数并将其作为列车和测试数据的索引返回(idx_test)
    • 收集误报和漏报的索引,这些是nd.arrays
    • 使用这些来查找索引数组中的原始位置,例如,index_idx_test [false_example]表示false_neg数组中的false_example
    • 使用该索引查找原始数据,data.iloc [index]是原始数据
    • 然后data.index [index]将索引值返回原始数据(如果需要)

    这里的代码与使用推文的示例相关联。再次,这是有效的,但是有更直接/更聪明的方法吗?

    # take a sample of our original data
    data=tweet_df[0:100]['texts']
    y=tweet_df[0:100]['truth']
    
    # create the feature vectors
    vec=TfidfVectorizer(analyzer="char",ngram_range=(1,2))
    X=vec.fit_transform(data) # this is now feature matrix
    
    # split the feature matrix into train/test subsets, keeping the indices back into the original X using the
    # array indices
    indices = np.arange(X.shape[0])
    X_train, X_test, y_train, y_test,idx_train,idx_test=train_test_split(X,y,indices,test_size=0.2,random_state=state)
    
    # fit and test a model
    forest=RandomForestClassifier()
    forest.fit(X_train,y_train)
    predictions=forest.predict(X_test)
    
    # get the indices for false_negatives and false_positives in the test set
    false_neg, false_pos=tweet_fns.check_predictions(predictions,y_test)
    
    # map the false negative indices in the test set (which is features) back to it's original data (text)
    print "False negatives: \n"
    pd.options.display.max_colwidth = 140
    for i in false_neg:
        original_index=idx_test[i]
        print data.iloc[original_index]
    

    和checkpredictions函数:

    def check_predictions(predictions,truth):
        # take a 1-dim array of predictions from a model, and a 1-dim truth vector and calculate similarity
        # returns the indices of the false negatives and false positives in the predictions. 
    
        truth=truth.astype(bool)
        predictions=predictions.astype(bool)
        print sum(predictions == truth), 'of ', len(truth), "or ", float(sum(predictions == truth))/float(len(truth))," match"
    
        # false positives
        print "false positives: ", sum(predictions & ~truth)
        # false negatives
        print "false negatives: ",sum( ~predictions & truth)
        false_neg=np.nonzero(~predictions & truth) # these are tuples of arrays
        false_pos=np.nonzero(predictions & ~truth)
        return false_neg[0], false_pos[0] # we just want the arrays to return
    

1 个答案:

答案 0 :(得分:0)

您的工作流程是:

原始数据 - >功能 - >拆分 - >火车 - >预测 - >标签上的错误分析

预测和特征矩阵之间存在逐行对应,因此如果要对功能进行错误分析,则应该没有问题。如果要查看哪些原始数据与错误相关联,则必须对原始数据执行拆分,或者跟踪映射到哪些测试行的数据行(当前方法)。

第一个选项如下:

在原始数据上拟合变换器 - >拆分原始数据 - >单独转换训练/测试 - >火车/测试 - > ...

也就是说,它在拆分之前使用fit,在拆分之后使用transform,使您以与标签相同的方式对原始数据进行分区。

相关问题