sklearn silhouette_samples是否依赖于数据的顺序?

时间:2016-06-08 17:55:16

标签: python pandas scikit-learn

我一直在尝试确定数据集中每个样本的轮廓分数,其中包含两个不同的类。但是,分布和样本值会根据我提前对数据进行排序的方式而改变。例如,如果我在调用silhouette_samples()之前按升序与降序按类标签(0& 1)对数据帧进行排序,则轮廓分数会发生变化。

有人可以帮我弄清楚发生了什么吗?我想知道

  1. 我的代码中是否有我不知道的错误?
  2. 我是sklearn silhouette_samples函数的正常行为吗? 不知道?
  3. 或者这是sklearn silhouette_samples中的错误吗?
  4. 使用以下代码生成效果:

    import pandas as pd
    from sklearn.metrics import silhouette_samples
    from sklearn.preprocessing import StandardScaler
    import matplotlib.pyplot as plt
    
    
    df_myfeatures #data frame containing features and class labels
    '''data frame sorted by output labels in ascending order'''
    df1 = df_myfeatures.copy().sort_values(['output_label'], ascending = True) 
    '''data frame sorted by output labels in descending order'''
    df2 = df_myfeatures.copy().sort_values(['output_label'], ascending = False)
    
    '''
    standardize the features ahead of time since they’re on different scales
    the X matrix has 26k rows (observations) and 9 columns (features)
    '''
    
    standard_scaler = StandardScaler() 
    X1 = standard_scaler.fit_transform(df1[cols]) #cols is just a list of columns for fitting
    X2 = standard_scaler.fit_transform(df2[cols])
    y1 = df1['output_label']
    y2 = df2['output_label']
    
    '''find the silhouette scores'''
    ss1 = silhouette_samples(X1,y1) 
    ss2 = silhouette_samples(X2,y2)
    
    '''plot the distribution'''
    plt.hist(ss1, bins =  np.linspace(-1,1,21), alpha = 0.3, label = 'sorted ascending')
    plt.hist(ss2, bins =  np.linspace(-1,1,21), alpha = 0.3, label = 'sorted descending')
    plt.legend()
    plt.title('distribution of silhouette scores')
    

    生成以下分数分布: histogram of silhouette scores

    如您所见,分数的分布根据数据的顺序而变化。我已经验证了标准缩放器没有问题,根据顺序产生不同的数据结果,并且pandas排序数据并且以某种方式搞乱跨列的不同行的对齐没有问题。我完全难过,据我所知,在计算轮廓分数时不应该有任何顺序效果。

    请帮我理解这个行为!谢谢!

    注意:我在Windows 10计算机上运行,​​使用Anaconda 4.0(64位),Python v3.5.1,sklearn v0.17.1和Pandas v0.18.0

0 个答案:

没有答案