分类 - 如何正确绘制重要性?

问题描述

我有以下(a 的一部分)数据框:

({'ID':  ['AA','BB','CC','DD','EE','FF','GG','HH','II','JJ',],'Satisfaction':  ['10','9','10','6','Boarding':  ['8','8','0','7','5','Food':  ['7','3','WiFi':  ['10','4','2','1','Luggage':  ['7','Snacks':  ['10','Entertainment':  ['10',})

所有值都是 int32

我有

  • 清理数据
  • Ran 分类算法和建模(X_train、y_train)
  • 聚类
  • 缩放数据
  • 应用的model.labels_
  • 创建集群
({'ID':  ['AA','Cluster':  ['0',})

然后我尝试使用以下代码将条形图聚类为:

  1. 主图显示了作为推荐人和非推荐人的满意度列的计数(其中 10 和 9 是推荐人,任何其他值都是非推荐人)

  2. 显示三个改进领域的子图(按重要性排列的前三列)

def classification_model(cluster_name,nps_cluster):

    #run the model
    X_train = nps_cluster.drop('Satisfaction',axis = 1)
    y_train = nps_cluster['Satisfaction']
    X_train = pd.get_dummies(X_train)

    model = RandomForestClassifier(random_state = 1)
    model.fit(X_train,y_train)
    cv_score = cross_val_score(model,X_train,y_train,scoring='accuracy',cv=5).mean()
    
    #about this group
    print()
    print("Overview - ",cluster_name)
    fig = plt.figure(figsize=[8,1])
    sns.countplot(y="Category",data=nps_cluster)
    plt.show()
    print()
    
    #create the importances
    importances = model.feature_importances_

       
    cluster_data = pd.DataFrame()
    cluster_data["Titles"] = nps_cluster.columns

    cluster_data["Importances"] = importances
    cluster_data = cluster_data.sort_values('Importances',ascending=False)
    
    #visualise the results
    # remove columns that we cannot visualise
    print("3 Most Important Areas to Improve/Maintain")
    show_data = cluster_data.copy()
    show_data.drop(cluster_data[cluster_data["Titles"] == "ID"].index,inplace=True)

    show_data = show_data.head(3)

    fig = plt.figure(figsize=[10,5])

    plt.subplot(1,1,1)
    g1 = sns.barplot(x="Importances",y="Titles",data=show_data) 


    plt.show()
    
    print("*Accuracy of Results (using CV Mean score) - ",round(cv_score*100,4))
    
    return cluster_data

但是,当我调用集群时,我收到错误消息 ValueError: Length of values does not match length of index

我可以通过在 # 前面添加 importances = model.feature_importances_ 来消除错误消息,这会将其从代码删除 - 但是,这会为每个集群提供完全相同的改进区域,并具有完全相同的重要性.虽然我认为每个集群有可能需要改进的领域是相同的,但我并不相信,尤其是因为我不得不从代码删除重要性。

我不知道我哪里出错了。

提前致谢

LT

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)