我是 seaborn
的新手,我有这个数据集,想创建一个这样的图表,但要使用 seaborn
。
这是我的数据:
max_depth = [ 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 10, 10,
10, 10, 10, 12, 12, 12, 12, 12]
min_samples_split = [2, 5, 15, 20, 25, 2, 5, 15, 20, 25, 2, 5,
15, 20, 25, 2, 5, 15, 20, 25, 2, 5, 15, 20, 25]
test_score = [0.85089537, 0.85089537, 0.85089537, 0.85348114, 0.85354819, 0.87357118, 0.87328475, 0.87147859, 0.87425471, 0.87402261,
0.86355856, 0.86120602, 0.87259394, 0.87582926, 0.87943536, 0.80913078, 0.82786446, 0.86109688, 0.86773115, 0.87619951,
0.79090683, 0.8038633 , 0.84915534, 0.86083209, 0.87192132]
results_DT = pd.DataFrame({'max_depth': max_depth, 'min_samples_split': min_samples_split, 'test_score': test_score})
这是我在 seaborn
中的尝试:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.lineplot(x = 'max_depth', y = 'test_score', hue = 'min_samples_split', marker = 'o', data = results_DT) # need to work out how to fix this
plt.legend(loc='lower left')
plt.xlabel("Max depth")
plt.ylabel("Mean CV score")
但是如您所见,类别不正确:
当我尝试将其转换为字符串时,出现错误。
#convert
results_DT2 = results_DT
results_DT2['min_samples_split'] = results_DT2['min_samples_split'].astype(str)
sns.lineplot(x = 'max_depth', y = 'test_score', hue = 'min_samples_split', marker = 'o', data = results_DT2) # need to work out how to fix this
plt.legend(loc='lower left')
plt.xlabel("Max depth")
plt.ylabel("Mean CV score")
AttributeError: 'str' object has no attribute 'view'
我该如何解决这个问题?
最佳答案
您可以将 min_samples_split 列转换为分类:
results_DT.min_samples_split = pd.Categorical(results_DT.min_samples_split)
sns.lineplot(x = 'max_depth', y = 'test_score', hue = 'min_samples_split', marker = 'o', data = results_DT)
关于python - 数字作为字符串的 seaborn 色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61747669/