簡體   English   中英

Sci-Kit學習:調查分類錯誤的數據

[英]Sci-Kit Learn: Investigating Incorrectly Classified Data

我想使用sci-kit Learn分析已被模型錯誤分類的數據,以便改善特征生成。 我有一種方法可以做到這一點,但是我既是新手,也是ci-kit學習和熊貓的新手,所以我想知道是否有更有效/直接的方法來完成此任務。 似乎是標准工作流程的一部分,但是在我所做的研究中,我沒有發現任何直接解決從模型分類到特征矩陣再到原始數據的反向映射的方法。

這是我正在使用的上下文/工作流程以及我設計的解決方案。 下面是示例代碼。

語境。 我的工作流程如下:

  1. 從一堆JSON Blob(原始數據)開始。 這是pandas DataFrame。
  2. 提取用於建模的相關部分,將其稱為數據。 這是一個熊貓數據框。
  3. 此外,我們擁有所有數據的真實數據,因此我們將其稱為真實或y。
  4. 在sci-kit學習中創建一個特征矩陣,稱為X。這是一個大的稀疏矩陣。
  5. 創建一個隨機森林對象,稱為該森林。
  6. 使用sci-kit learning split_train_test()函數為訓練和測試創建特征矩陣的隨機子集。
  7. 根據上面的訓練數據X_train訓練森林,X_train是一個大的稀疏矩陣。
  8. 獲取假陽性和假陰性結果的索引。 這些是X_test(稀疏矩陣)的索引。
  9. 從一個錯誤的肯定索引進入X_test返回原始數據
  10. 如有必要,請從數據轉到原始數據。

解。

  • 將索引數組傳遞到split_test_train()函數,該函數將在索引數組上應用相同的隨機化器,並將其作為訓練和測試數據的索引返回(idx_test)
  • 收集誤報和誤報的索引,它們是nd.arrays
  • 使用它們來查找索引數組中的原始位置,例如,對於false_neg數組中的false_example,使用index = idx_test [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

您的工作流程是:

原始數據->功能->拆分->訓練->預測->標簽上的錯誤分析

預測與特征矩陣之間存在逐行對應關系,因此,如果您要對特征進行誤差分析,應該沒有問題。 如果要查看哪些原始數據與錯誤相關聯,則必須對原始數據進行拆分,或者跟蹤將哪些數據行映射到哪些測試行(您當前的方法)。

第一個選項如下所示:

在原始數據上安裝轉換器->拆分原始數據->分別轉換訓練/測試->訓練/測試-> ...

也就是說,它在拆分前使用fit在拆分后進行transform ,從而使原始數據的划分方式與標簽相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM