【发布时间】:2017-04-23 20:13:09
【问题描述】:
我有一个看起来像这样的pandas.DataFrame:
In [48]: df
Out[48]:
AMID A B C
0 AMID-1000 0.149176 0.768200 0.689369
1 AMID-1001 0.169934 0.607390 0.471788
2 AMID-1002 0.632052 0.806657 0.994664
3 AMID-1003 0.003798 0.382427 0.894856
4 AMID-1004 0.182947 0.712373 0.870068
5 AMID-1005 0.385039 0.691643 0.546960
6 AMID-1006 0.971885 0.169759 0.804370
7 AMID-1007 0.443199 0.686212 0.377556
8 AMID-1008 0.149402 0.981370 0.588750
9 AMID-1009 0.214107 0.264285 0.463403
'AMID' 列保存数据点 ID,其余列中的每一列都是每个数据点的特征。
我想将此数据集与需要缩放数据的算法一起使用,因此对于每一列我都有mean == 0 和std == 1。我为此使用sklearn.preprocessing.StandardScaler,但是,为了扩展,我需要删除非数字'AMID' 列的数据集。
In [61]: from sklearn import preprocessing
In [62]: data = df[[_ for _ in df.columns.values.tolist() if _ not in ['AMID']]]
In [64]: scaler = preprocessing.StandardScaler().fit(data)
In [65]: data_scaled = scaler.transform(data)
In [66]: data_scaled
Out[66]:
array([[ -6.60180258e-01, 6.63739262e-01, 9.55187160e-02],
[ -5.84458777e-01, 1.47534202e-03, -9.87448200e-01],
[ 1.10128130e+00, 8.22117198e-01, 1.61505880e+00],
[ -1.19049913e+00, -9.24989864e-01, 1.11828380e+00],
[ -5.36991596e-01, 4.33827828e-01, 9.94906952e-01],
[ 2.00212895e-01, 3.48454485e-01, -6.13293011e-01],
[ 2.34094244e+00, -1.80081691e+00, 6.67913149e-01],
[ 4.12372276e-01, 3.26087187e-01, -1.45646800e+00],
[ -6.59357873e-01, 1.54163661e+00, -4.05292050e-01],
[ -4.23321269e-01, -1.41153114e+00, -1.02918017e+00]])
In [67]: data_scaled.mean(axis=0)
Out[67]: array([ -8.32667268e-17, -4.44089210e-17, -2.88657986e-16])
In [68]: data_scaled.std(axis=0)
Out[68]: array([ 1., 1., 1.])
到目前为止,情况看起来不错!
现在我可以继续将此数据提供给我的模型,然后使用测试数据进行测试(也使用相同的缩放器和拟合进行缩放)。但是,我需要能够准确地看到分类器对每个AMID 给出的预测。所以,我想我应该将每个数据点的缩放数据映射回AMID,然后使用分类器的.predict()方法分别尝试每个数据点,或者我应该以某种方式将.predict()的结果映射回AMID的列表。
我的第一个想法是将新值分配给原始数据框,如下所示:
In [73]: df_copy['A'] = data_scaled[:,0:1]
In [74]: df_copy
Out[74]:
AMID A B C
0 AMID-1000 -0.660180 0.768200 0.689369
1 AMID-1001 -0.584459 0.607390 0.471788
2 AMID-1002 1.101281 0.806657 0.994664
3 AMID-1003 -1.190499 0.382427 0.894856
4 AMID-1004 -0.536992 0.712373 0.870068
5 AMID-1005 0.200213 0.691643 0.546960
6 AMID-1006 2.340942 0.169759 0.804370
7 AMID-1007 0.412372 0.686212 0.377556
8 AMID-1008 -0.659358 0.981370 0.588750
9 AMID-1009 -0.423321 0.264285 0.463403
但我不确定这是否会扭曲原始 'AMID' 和每列的缩放值之间的关联。
有没有更好的方法来做到这一点?
【问题讨论】:
-
However, what I need to do is map the scaled data back to the 'AMID', so that I can record the classifier's .predict() output for each AMID value separately不明白。你能重新制定一下吗? -
我有每个数据点的基本事实,所以我实际上可以很好地评估分类器的性能,即使没有映射到
AMID。但我还需要知道哪个特定的汽车 (AMID) 属于哪个类别。所以我在想这将需要遍历数据集并将每个数据点分别提供给分类器。简而言之,我想找出分类器对每个AMID的预测。谢谢! -
I have the ground truth for each one of the datapoints。如果你有课,那么你想得到什么? -->But I also need to know which specific automobile (AMID) belongs to which class?没看懂 -
@MMF 我正在尝试找到表示我的数据的最佳方式,因此我针对我的数据集的几种不同风格评估分类器的性能,并查看哪些数据集提供了最佳结果或允许分类器更有效地解决问题。对于这个二元问题,我有基本事实,因此我可以计算每个数据集风格的性能。
-
@MMF 问:
But I also need to know which specific automobile (AMID) belongs to which class。 A:我想说的是,我需要知道算法为每个数据点预测了什么。
标签: python python-3.x pandas machine-learning scikit-learn