You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KNN调参折线图绘制报错:lineplot()仅接受0-1个位置参数却传入2个

解决Seaborn lineplot的TypeError错误

问题原因

你遇到的TypeError: lineplot() takes from 0 to 1 positional arguments but 2 were given错误,是因为新版本Seaborn的lineplot()函数不再支持直接传递位置参数,必须显式指定x和y的关键字参数。旧版本Seaborn允许省略关键字直接传x、y数据,但新版本要求必须明确标注参数名。

修复代码

修改绘制折线图的两行代码,给x和y数据加上关键字参数:

plt.figure(figsize=(12,5))
p = sns.lineplot(x=range(1,15), y=train_scores, marker='*', label='Train Score')
p = sns.lineplot(x=range(1,15), y=test_scores, marker='o', label='Test Score')

额外注意事项

如果你的代码开头没有导入必要的库,还需要补充以下导入语句,否则plt和sns会未定义:

import matplotlib.pyplot as plt
import seaborn as sns

优化后的完整绘图代码片段

# 导入必要库(如果之前没添加)
import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(figsize=(12,5))
p = sns.lineplot(x=range(1,15), y=train_scores, marker='*', label='Train Score')
p = sns.lineplot(x=range(1,15), y=test_scores, marker='o', label='Test Score')
# 补充图表元素提升可读性
plt.title('KNN模型不同k值下的训练/测试分数')
plt.xlabel('k值')
plt.ylabel('准确率')
plt.show()

内容的提问来源于stack exchange,提问作者Nur Rokhaini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 15:27:28