Issue
I would like to turn the xticks (feature names) and yticks (feature values) into a tuppled list in python so I can eventually export the pairs to a csv. How would I do this? here is the code for the plot below. Thanks in advance.
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)
Solution
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)
Answered By - jw32022
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.