【问题标题】:"Could not evaluate the 'x' mapping: 'Cluster' error in plotnine“无法评估 'x' 映射:plotnine 中的 'Cluster' 错误
【发布时间】:2021-11-19 05:49:48
【问题描述】:

我正在运行此代码来确定我需要使用 K 原型集群的集群数量,但我收到了此错误

PlotnineError: "Could not evaluate the 'x' mapping: 'Cluster' (original error: name 'Cluster' is not defined)"

# Choose optimal K using Elbow method
cost = []
for cluster in range(1, 10):
    try:
        kprototype = KPrototypes(n_jobs = -1, n_clusters = cluster, init = 'Huang', random_state = 0)
        kprototype.fit_predict(dfMatrix, categorical = catColumnsPos)
        cost.append(kprototype.cost_)
        print('Cluster initiation: {}'.format(cluster))
    except:
        break
# Converting the results into a dataframe and plotting them

a = {'Cluster':range(1, 6), 'Cost':cost}
df_cost = pd.DataFrame.from_dict(a, orient='index')
df_cost.transpose()

# Data viz
plotnine.options.figure_size = (8, 4.8)
(
    ggplot(data = df_cost)+
    geom_line(aes(x = 'Cluster',
                  y = 'Cost'))+
    geom_point(aes(x = 'Cluster',
                   y = 'Cost'))+
    geom_label(aes(x = 'Cluster',
                   y = 'Cost',
                   label = 'Cluster'),
               size = 10,
               nudge_y = 1000) +
    labs(title = 'Optimal number of cluster with Elbow Method')+
    xlab('Number of Clusters k')+
    ylab('Cost')+
    theme_minimal()
)
有人知道吗?

【问题讨论】:

    标签: python cluster-analysis prototype plotnine


    【解决方案1】:

    您的数据转换代码存在疏忽。

    这一行

    df_cost.transpose()
    

    应该是

    df_cost = df_cost.transpose()
    

    【讨论】:

    • 非常感谢!!!我什至没有注意到它!非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    相关资源
    最近更新 更多