python - 箱线图(来自seaborn)不会按预期绘制

标签 python arrays python-2.7 scikit-learn seaborn

箱线图不会按预期绘制。 这就是它实际绘制的: enter image description here

这就是它应该绘制的内容: enter image description here

这是代码和数据:

 from sklearn.ensemble import RandomForestClassifier
    from sklearn.cross_validation import cross_val_score
    scores = []
    for ne in range(1,41): ## ne is the number of trees
        clf = RandomForestClassifier(n_estimators = ne)
        score_list = cross_val_score(clf, X, Y, cv=10)
        scores.append(score_list)
        sns.boxplot(scores) # scores are list of arrays
        plt.xlabel('Number of trees')
        plt.ylabel('Classification score')
        plt.title('Classification score as a function of the number of trees')
        plt.show()

scores =

[array([ 0.8757764 ,  0.86335404,  0.75625   ,  0.85      ,  0.86875   ,
         0.81875   ,  0.79375   ,  0.79245283,  0.8490566 ,  0.85534591]),
 array([ 0.89440994,  0.8447205 ,  0.79375   ,  0.85      ,  0.8625    ,
         0.85625   ,  0.86875   ,  0.88050314,  0.86792453,  0.8427673 ]),
 array([ 0.91304348,  0.9068323 ,  0.83125   ,  0.84375   ,  0.8875    ,
         0.875     ,  0.825     ,  0.83647799,  0.83647799,  0.87421384]),
 array([ 0.86956522,  0.86956522,  0.85      ,  0.875     ,  0.88125   ,
         0.86875   ,  0.8625    ,  0.8490566 ,  0.86792453,  0.89308176]),

...]

最佳答案

我首先会根据分数创建pandas DF:

import pandas as pd

In [15]: scores
Out[15]:
[array([ 0.8757764 ,  0.86335404,  0.75625   ,  0.85      ,  0.86875   ,  0.81875   ,  0.79375   ,  0.79245283,  0.8490566 ,  0.85534591]),
 array([ 0.89440994,  0.8447205 ,  0.79375   ,  0.85      ,  0.8625    ,  0.85625   ,  0.86875   ,  0.88050314,  0.86792453,  0.8427673 ]),
 array([ 0.91304348,  0.9068323 ,  0.83125   ,  0.84375   ,  0.8875    ,  0.875     ,  0.825     ,  0.83647799,  0.83647799,  0.87421384]),
 array([ 0.86956522,  0.86956522,  0.85      ,  0.875     ,  0.88125   ,  0.86875   ,  0.8625    ,  0.8490566 ,  0.86792453,  0.89308176])]

In [16]: df = pd.DataFrame(scores)

In [17]: df
Out[17]:
          0         1        2        3        4        5        6         7         8         9
0  0.875776  0.863354  0.75625  0.85000  0.86875  0.81875  0.79375  0.792453  0.849057  0.855346
1  0.894410  0.844720  0.79375  0.85000  0.86250  0.85625  0.86875  0.880503  0.867925  0.842767
2  0.913043  0.906832  0.83125  0.84375  0.88750  0.87500  0.82500  0.836478  0.836478  0.874214
3  0.869565  0.869565  0.85000  0.87500  0.88125  0.86875  0.86250  0.849057  0.867925  0.893082

现在我们可以轻松绘制箱线图:

In [18]: sns.boxplot(data=df)
Out[18]: <matplotlib.axes._subplots.AxesSubplot at 0xd121128>

enter image description here

关于python - 箱线图(来自seaborn)不会按预期绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39070135/

相关文章:

python - 双重迭代的替代方案

arrays - 如何在fortran中传递数组中的函数?

arrays - Nodejs中通过for循环构造数组

python - Python 中的实时输出

python - 识别最近的网格点

python - 使用 matplotlib 从纪元开始绘制 time() 的日期

java - 在 Java 中将参数传递给 Python 脚本

python - Python中将Json Dict对象转换为DataFrame

Java-从数组方法返回一个数组到Main函数

python - 通过添加来自其他列的值在 Panda 数据框中创建新列