我正在尝试绘制我的精度/召回曲线的阈值。我只是使用 MNSIT 数据,以及《使用 scikit-learn、keras 和 TensorFlow 进行机器学习实践》一书中的示例。尝试训练模型来检测 5 的图像。我不知道你需要看多少代码。我已经为训练集制作了混淆矩阵,并计算了精度和召回值以及阈值。我已经绘制了预/记录曲线,书中的示例表示添加轴标签、壁架、网格并突出显示阈值,但代码在书中被截断,我在下面放置了星号。我能够弄清楚除了如何让阈值显示在绘图上之外的所有问题。我附上了一张图片,展示了书中的图表与我所拥有的图表的对比。这就是这本书所展示的:
我无法显示带有两个阈值点的红色点线。有谁知道我会怎么做?下面是我的代码:
from sklearn.metrics import precision_recall_curve
precisions, recalls, thresholds = precision_recall_curve(y_train_5, y_scores)
def plot_precision_recall_vs_thresholds(precisions, recalls, thresholds):
plt.plot(thresholds, precisions[:-1], "b--", label="Precision")
plt.plot(thresholds, recalls[:-1], "g--", label="Recall")
plt.xlabel("Threshold")
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
plt.grid(b=True, which="both", axis="both", color='gray', linestyle='-', linewidth=1)
plot_precision_recall_vs_thresholds(precisions, recalls, thresholds)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我知道这里有很多关于 sklearn 的问题,但似乎没有一个问题涉及如何显示红线。我将非常感谢您的帮助!
我正在审查 matpoltlib 和 seaborn 包。我知道这个问题有点低于堆栈级别,但没有人可以就这个错误给我一个可靠的答案。我正在使用 displot 制作直方图,并且笔记试图显示它显示的数量与密度之间的差异。使用带有 seaborn 的“iris”数据集,第一个示例是:
[IN]: sns.displot(iris["sepal_length"], kde=False)
[OUT]: histogram, no curve, count on the y-axis
Run Code Online (Sandbox Code Playgroud)
下一个示例使用“norm_hist”并且应该将计数更改为密度,但我收到了一个我不明白的错误?
[IN]: sns.displot(iris["sepal_length"], norm_hist=True, kde=False)
[OUT]: Traceback (most recent call last):
File "C:\Users\cyrra\OneDrive\Documents\HDS 802 - Programming in Healthcare (Python & R)\Module 7 Python\M7P - MINE.py", line 79, in <module>
sns.displot(iris["sepal_length"], norm_hist=True, kde=False)
File "C:\Users\cyrra\anaconda3\lib\site-packages\seaborn\distributions.py", line 2227, in displot
p.plot_univariate_histogram(**hist_kws)
File "C:\Users\cyrra\anaconda3\lib\site-packages\seaborn\distributions.py", line 555, in plot_univariate_histogram
artists = plot_func(
File "C:\Users\cyrra\anaconda3\lib\site-packages\matplotlib\__init__.py", line 1438, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File …Run Code Online (Sandbox Code Playgroud) 我正在研究二元逻辑回归(具有完全分类数据),我对其进行了 OneHotEncoded 并尝试运行二元逻辑回归。我在下面收到此错误,但我不知道如何处理错误。我知道它在最后一行为您提供了一些信息,但我不知道这里可能有 str 值吗?
[输入]:train_set,test_set = train_test_split(allyrs,test_size = 0.2,random_state = 42)
[输入] X = train_set.iloc[:, 31 : 175]
# Set up binary y value
[IN]: y=train_set.iloc[:, 29]
# Set up multi y value
[IN]: ym=train_set.iloc[:, 30]
# first attempt to feed through is says :
[IN]:
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LogisticRegressionCV
[IN]:BiLog_cv = LogisticRegressionCV(cv=3, random_state=0).fit(X, y)
Run Code Online (Sandbox Code Playgroud)
AttributeError Traceback (most recent call last)
<ipython-input-19-2468362218dc> in <module>
1 # Binary Logistic Regresiion with Cross Validation - …Run Code Online (Sandbox Code Playgroud)