Seaborn 色调,用于数字字符串

wil*_*031 2 python matplotlib seaborn

我是新手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})
Run Code Online (Sandbox Code Playgroud)

这是我的尝试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")
Run Code Online (Sandbox Code Playgroud)

但正如您所看到的,类别是不正确的:

在此输入图像描述

当我尝试将其转换为字符串时,出现错误。

#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")
Run Code Online (Sandbox Code Playgroud)

AttributeError:“str”对象没有属性“view”

我该如何解决?

Stu*_*olf 5

您可以将 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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述