1. 多列one-hot列转变为一列label:
# 需要被转换的dataframe只有one-hot数据列,转换的结果会得到series
study_label = pd.read_csv('./train_study_level.csv', index_col='id')
study_labels = study_label.idxmax(axis=1).rename('study_label')
2. 一列label转变为多列one-hot:
# 忘了,待补充
3. 将一列结构数据值扩展为多列:
# 注意[dict(zip([...],[...])) for i in ...]的使用
from ast import literal_eval
data = []
for row in image_label.values:
data = data + [dict(zip(['id','boxes','study_id','class','confident','xmin','ymin','xmax','ymax'],[row[0],str(literal_eval(row[1])[i]) if type(row[1])==str else row[1],row[3]]+list(v))) for i,v in enumerate(np.array(row[2].split()).reshape(-1,6))]
df = pd.DataFrame(data)
df.head()
4. 数据总和占比显示(饼图):
import matplotlib.pyplot as plt
label_count = study_label.sum()
plt.figure(figsize=(8,8))
# explode跟据占比不一样,看着不错
plt.pie(label_count, labels=label_count.index, wedgeprops={'edgecolor':'black'}, autopct='%1.f%%', textprops={'fontsize':16},
explode=[.01]*4, shadow=True)
plt.title('Label Distribution', fontdict={'fontsize':22})