【问题标题】:How do I turn the X-ticks and Y-ticks on a matplotlib plot into a tuppled list in python?如何将 matplotlib 图上的 X-ticks 和 Y-ticks 转换为 python 中的元组列表?
【发布时间】:2022-11-11 05:45:31
【问题描述】:

我想将 xticks(特征名称)和 yticks(特征值)转换为 python 中的元组列表,以便我最终可以将这些对导出到 csv。我该怎么做?这是下图的代码。提前致谢。

from sklearn import svm
import matplotlib.pyplot as plt
def feature_plot(classifier, feature_names, top_features=25):
 coef = classifier.coef_.ravel()
 top_positive_coefficients = np.argsort(coef)[-top_features:]
 #top_negative_coefficients = np.argsort(coef)[:top_features]
 #top_coefficients = np.hstack([top_negative_coefficients, top_positive_coefficients])
 plt.figure(figsize=(18, 7))
 colors = ['green' if c < 0 else 'blue' for c in coef[top_positive_coefficients]]
 plt.bar(np.arange(top_features), coef[top_positive_coefficients], color=colors)
 feature_names = np.array(feature_names)
 plt.xticks(np.arange(top_features), feature_names[top_positive_coefficients], rotation=45, ha='right')
 plt.show()

#print(pandasdfx.drop(columns=['target_label'], axis = 1).columns.values)

trainedsvm = svm.LinearSVC(C=0.001, max_iter=10000, dual=False).fit(Xx_train2, yx_train)
feature_plot(trainedsvm, pandasdfx.drop(columns=['target_label'], axis = 1).columns.values)

【问题讨论】:

  • x=np.arange(top_features)y=coef[top_positive_coefficients]names=feature_names[top_positive_coefficients] 还不够吗?您希望 xticks 和 yticks 提供哪些额外信息?举例说明您拥有和期望的最终结果。
  • 这会填充图表上的刻度 - 我正在尝试获取配对的 x 刻度和 y 刻度的列表,以便我可以将其导出到 CSV

标签: python matplotlib svm


【解决方案1】:
 classifier = svm.LinearSVC(C=0.01, max_iter=10000, dual=False).fit(Xx_train2, yx_train)
feature_names=pandasdfx.drop(columns=['target_label', axis = 1).columns.values
def feature_plot(classifier, feature_names, top_features=25):
 coef = classifier.coef_.ravel()
 top_positive_coefficients = np.argsort(coef)[-top_features:]
 return np.array((feature_names[top_positive_coefficients], coef[top_positive_coefficients])).T

var=feature_plot(classifier, feature_names, top_features=25)
print(var)

【讨论】:

    猜你喜欢
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多