是否可以在Jupyter笔记本单元中调试第三方python文件?

2024-03-29 01:46:32 发布

您现在位置:Python中文网/ 问答频道 /正文

set_trace()允许在Jupyter笔记本单元中调试我们自己的代码。你知道吗

代码段\u 1

#import the KNeighborsClassifier class from sklearn
from sklearn.neighbors import KNeighborsClassifier
from IPython.core.debugger import set_trace
#import metrics model to check the accuracy 
from sklearn import metrics
#Try running from k=1 through 25 and record testing accuracy
k_range = range(1,26)
scores = {}
scores_list = []
for k in k_range:
    set_trace()
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train,y_train)
    y_pred=knn.predict(X_test)
    scores[k] = metrics.accuracy_score(y_test,y_pred)
    scores_list.append(metrics.accuracy_score(y_test,y_pred))

这是“KNN on Iris Datset”源代码的一部分。你知道吗

link是整件可以100%在线复制的作品。你知道吗

问题是

是否可以调试第三方python文件,例如Jupyter笔记本单元中的classification.py?你知道吗

特别是,是否可以调试Jupyter笔记本单元中的knn.predict()?你知道吗

位于

/usr/local/lib/python3.6/dist-packages/sklearn/neighbors/classification.py

这件

y_pred=knn.predict(["trap", X_test])

%debug

获取此错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-054b4ff1b356> in <module>()
----> 1 y_pred=knn.predict(["trap", X_test])
      2 
      3 get_ipython().magic('debug')

。。。你知道吗

只运行这一行

y_pred=knn.predict(["trap", X_test])

获取此错误(已删除长数组输出)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-054b4ff1b356> in <module>()
----> 1 y_pred=knn.predict(["trap", X_test])
      2 
      3 get_ipython().magic('debug')

1 frames
/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    519                     "Reshape your data either using array.reshape(-1, 1) if "
    520                     "your data has a single feature or array.reshape(1, -1) "
--> 521                     "if it contains a single sample.".format(array))
    522 
    523         # in the future np.flexible dtypes will be handled like object dtypes

在这个错误之后,我在一个新的单元格中运行了%debug,然后我得到了这个错误

> /usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py(521)check_array()
    519                     "Reshape your data either using array.reshape(-1, 1) if "
    520                     "your data has a single feature or array.reshape(1, -1) "
--> 521                     "if it contains a single sample.".format(array))
    522 
    523         # in the future np.flexible dtypes will be handled like object dtypes

和ipdb输入

enter image description here

我进入up,pdb切换到classification.py

设置断点

enter image description here

然后up,切换回来

enter image description here

断点不起作用

这是全部记录

> /usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py(521)check_array()
    519                     "Reshape your data either using array.reshape(-1, 1) if "
    520                     "your data has a single feature or array.reshape(1, -1) "
--> 521                     "if it contains a single sample.".format(array))
    522 
    523         # in the future np.flexible dtypes will be handled like object dtypes

ipdb> up
> /usr/local/lib/python3.6/dist-packages/sklearn/neighbors/classification.py(147)predict()
    145             Class labels for each data sample.
    146         """
--> 147         X = check_array(X, accept_sparse='csr')
    148 
1   149         neigh_dist, neigh_ind = self.kneighbors(X)

ipdb> b
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /usr/local/lib/python3.6/dist-packages/sklearn/neighbors/classification.py:149
2   breakpoint   keep yes   at /usr/local/lib/python3.6/dist-packages/sklearn/neighbors/classification.py:150
ipdb> up
> <ipython-input-22-be2dbe619b73>(2)<module>()
      1 X = ["trap", X_test]
----> 2 y_pred=knn.predict(X)

ipdb> X = X_test
ipdb> s

Tags: inpytestdatalibpackagesusrlocal
1条回答
网友
1楼 · 发布于 2024-03-29 01:46:32

事实上这是不可能的。但有一个诀窍。您可以故意将错误的参数传递给predict函数,这样它就会失败,您可以调用%debug,以便逐行执行步骤。请参见下面的示例。你知道吗

y_pred=knn.predict(["trap", X_test])

这将尝试执行predict方法,并将失败,因为您输入的是随机列表而不是数组。您可以从那里调用%debugmagic命令执行

相关问题 更多 >