我有三个载体如下:
y_test=['ApplyLipstick', 'BalanceBeam', 'Archery', 'Archery', 'BalanceBeam',
'ApplyEyeMakeup', 'BasketballDunk', 'BalanceBeam', 'ApplyLipstick',
'BaseballPitch', 'BandMarching', 'BaseballPitch', 'Basketball',
'BabyCrawling', 'BabyCrawling', 'BandMarching', 'BenchPress',
'BandMarching', 'BalanceBeam', 'BasketballDunk', 'ApplyLipstick',
'BenchPress', 'Basketball', 'Basketball', 'Basketball',
'BabyCrawling', 'BenchPress', 'Archery', 'Basketball',
'BabyCrawling', 'BabyCrawling', 'BalanceBeam', 'BandMarching',
'BandMarching', 'BabyCrawling', 'ApplyLipstick', 'BasketballDunk',
'BaseballPitch', 'BalanceBeam', 'ApplyLipstick', 'BenchPress',
'ApplyLipstick', 'BenchPress', 'BasketballDunk', 'Basketball',
'BaseballPitch', 'BasketballDunk', 'Archery', 'Basketball',
'BenchPress', 'BaseballPitch', 'ApplyLipstick', 'BaseballPitch',
'BandMarching', 'BalanceBeam', 'ApplyEyeMakeup', 'Archery',
'BasketballDunk', 'Archery', 'BasketballDunk', 'Archery',
'Basketball', 'BabyCrawling', 'BandMarching', 'BandMarching',
'BenchPress', 'BasketballDunk', 'ApplyEyeMakeup', 'BandMarching',
'ApplyLipstick', 'BalanceBeam', 'BenchPress', 'BaseballPitch',
'BabyCrawling', 'Basketball', 'ApplyLipstick', 'BalanceBeam',
'ApplyLipstick', 'BaseballPitch', 'ApplyEyeMakeup', 'BandMarching',
'BandMarching', 'BalanceBeam', 'BenchPress', 'BabyCrawling',
'Archery', 'BabyCrawling', 'BabyCrawling', 'Basketball',
'BenchPress', 'BaseballPitch', 'BalanceBeam', 'BalanceBeam',
'ApplyEyeMakeup', 'BaseballPitch', 'ApplyEyeMakeup',
'BaseballPitch', 'BandMarching', 'BandMarching', 'BasketballDunk']
和
y_train=['BabyCrawling', 'Archery', 'BasketballDunk', 'BabyCrawling',
'Basketball', 'ApplyLipstick', 'BasketballDunk', 'ApplyEyeMakeup',
'BasketballDunk', 'BabyCrawling', 'ApplyLipstick', 'ApplyEyeMakeup',
'BaseballPitch', 'Archery', 'BalanceBeam', 'ApplyEyeMakeup',
'ApplyLipstick', 'BalanceBeam', 'BaseballPitch', 'BaseballPitch',
'ApplyEyeMakeup', 'Archery', 'BalanceBeam', 'BasketballDunk',
'BalanceBeam', 'ApplyLipstick', 'Basketball', 'BandMarching',
'BenchPress', 'ApplyEyeMakeup', 'ApplyLipstick', 'Archery',
'BaseballPitch', 'BalanceBeam', 'Basketball', 'BaseballPitch',
'BasketballDunk', 'ApplyEyeMakeup', 'BenchPress', 'BandMarching',
'BabyCrawling', 'BasketballDunk', 'BandMarching', 'BabyCrawling',
'BalanceBeam', 'BasketballDunk', 'BaseballPitch', 'Basketball',
'Archery', 'ApplyLipstick', 'ApplyLipstick', 'BasketballDunk',
'ApplyEyeMakeup', 'ApplyEyeMakeup', 'Archery', 'BasketballDunk',
'BabyCrawling', 'BenchPress', 'Basketball', 'BabyCrawling',
'BenchPress', 'BabyCrawling', 'BenchPress', 'Basketball',
'BenchPress', 'Archery', 'ApplyEyeMakeup', 'Archery', 'BenchPress',
'ApplyEyeMakeup', 'BaseballPitch', 'Archery', 'BaseballPitch',
'ApplyEyeMakeup', 'Basketball', 'BandMarching', 'ApplyLipstick',
'ApplyEyeMakeup', 'Basketball', 'BenchPress', 'Archery',
'Basketball', 'ApplyLipstick', 'Basketball', 'BasketballDunk',
'Archery', 'ApplyEyeMakeup', 'BalanceBeam', 'BandMarching',
'BasketballDunk', 'BandMarching', 'ApplyEyeMakeup', 'BandMarching',
'BabyCrawling', 'BenchPress', 'ApplyLipstick', 'BenchPress',
'BaseballPitch', 'Archery', 'BandMarching']
和班级:
classes=['BenchPress', 'ApplyLipstick', 'BabyCrawling', 'BandMarching', 'Archery', 'Basketball', 'ApplyEyeMakeup', 'BalanceBeam', 'BaseballPitch', 'BasketballDunk']
我会得到相同的情节, y_test 和 y_train (两种不同的颜色)。我尝试了以下内容,但它仅适用于1个向量。如何扩展它以使y_test和y_train在同一个图中
from collections import Counter
import numpy as np
import matplotlib.pyplot as plt
y_train=np.asarray(y_train)
y_test=np.asarray(y_test)
labels, values = zip(*Counter(y_train).items())
labels2, values2 = zip(*Counter(y_test).items())
print("labels ", labels)
print("values", values)
print('train',len(values))
print("labels ", labels2)
print("values", values2)
print('train',len(values2))
#pylab.rcParams['figure.figsize'] = (30,10)
indexes = np.arange(len(labels))
width = 0.5
plt.bar(indexes, values, width)
plt.xlabel('Classes')
plt.ylabel('Number of example per class')
plt.title('Training set distribution 50% ')
plt.xticks(indexes + width * 0.25, labels)
#plt.figure(figsize=(300,300))
plt.show()
以下是1个向量的结果,例如y_train:
labels ('Basketball', 'BabyCrawling', 'BaseballPitch', 'ApplyLipstick', 'ApplyEyeMakeup', 'BalanceBeam', 'Archery', 'BenchPress', 'BasketballDunk', 'BandMarching')
lvalues (10, 9, 9, 10, 14, 7, 12, 10, 11, 8)
train 10
labels ('Basketball', 'BabyCrawling', 'BaseballPitch', 'ApplyLipstick', 'ApplyEyeMakeup', 'BalanceBeam', 'Archery', 'BenchPress', 'BasketballDunk', 'BandMarching')
lvalues (10, 11, 11, 10, 6, 12, 8, 10, 9, 13)
train 10
和相应的情节:
答案 0 :(得分:0)
在您的示例中,labels
和labels2
需要按字母顺序排序。此外,在x轴上标记标签时,您可能希望旋转文本,使它们不会相互重叠。例如,可以使用来自Matplotlib website的示例来制作比较条形图。以下代码显示了代码所需的修改。
y_train=np.asarray(y_train)
y_test=np.asarray(y_test)
c1 = Counter(y_train)
c2 = Counter(y_test)
labels, values = zip(*sorted(c1.items())) # sort the labels
labels2, values2 = zip(*sorted(c2.items())) # sort the labels
print("labels ", labels)
print("values", values)
print('train',len(values))
print("labels ", labels2)
print("values", values2)
print('train',len(values2))
indexes = np.arange(len(labels))
width = 0.35 # adjust the width of the bars
plt.bar(indexes, values, width, label='y_train') #put legend labels
plt.bar(indexes + width, values2, width, label='y_test') # put legend labels
plt.xlabel('Classes')
plt.ylabel('Number of example per class')
plt.title('Training set distribution 50% ')
plt.xticks(indexes + 0.5*width, labels, rotation=70) # adjust axis label location and rotation
plt.tight_layout() # Tight layout
plt.legend()
plt.show()