Python Sci工具包学习:调查错误分类的数据
我想使用sci工具包学习分析模型错误分类的数据,以便改进特征生成。我有一个这样做的方法,但我对sci工具包和熊猫都是新手,所以我想知道是否有更有效/直接的方法来实现这一点。这似乎是标准工作流程的一部分,但在我所做的研究中,我没有发现任何东西可以直接解决从模型分类到特征矩阵到原始数据的向后映射问题 这是我使用的上下文/工作流,以及我设计的解决方案。下面是示例代码 上下文。我的工作流程如下所示:Python Sci工具包学习:调查错误分类的数据,python,machine-learning,scikit-learn,Python,Machine Learning,Scikit Learn,我想使用sci工具包学习分析模型错误分类的数据,以便改进特征生成。我有一个这样做的方法,但我对sci工具包和熊猫都是新手,所以我想知道是否有更有效/直接的方法来实现这一点。这似乎是标准工作流程的一部分,但在我所做的研究中,我没有发现任何东西可以直接解决从模型分类到特征矩阵到原始数据的向后映射问题 这是我使用的上下文/工作流,以及我设计的解决方案。下面是示例代码 上下文。我的工作流程如下所示: 从一堆JSON blob(原始数据)开始。这是熊猫数据帧 提取建模的相关部分,称之为数据。这是一个数据帧
- 将索引数组传递到split_test_train()函数中,该函数将在索引数组上应用相同的随机化器,并将其作为训练和测试数据的索引返回(idx_test)
- 收集假阳性和假阴性的索引,这些是nd数组
- 使用这些来查找索引数组中的原始位置,例如,对于false_neg数组中的false_示例,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]
以及检查预测功能:
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
,这样您就可以使用与标签相同的方式对原始数据进行分区。谢谢。所以,我想你不认为我目前的方法一定是坏的,所以这是好的\n我曾考虑过在转换之前拆分数据,但对于正确的方法没有足够的清晰性。在上面的示例中,对所有数据使用拟合方法,拆分数据,然后使用变换方法,而不是在矢量器上使用拟合变换方法。谢谢我也要试试。谢谢你的帮助。