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

时间:2019-08-13 11:36:41

标签: python jupyter-notebook

set_trace()可以在Jupyter笔记本单元中调试我们自己的代码。

code_snippet_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))

这是“ Kris on Iris Datset”源代码的一部分。

link是一整块,可以在网上100%复制。

问题是

是否可以在Jupyter笔记本单元中调试第三方python文件,例如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

1 个答案:

答案 0 :(得分:0)

事实上,这是不可能的。但是有一个窍门。您可以有意地将错误的参数传递给predict函数,以使其失败,并且可以调用%debug以便逐行执行步骤。请参见下面的示例。

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

这将尝试执行predict方法,并会失败,因为您输入的是随机列表而不是数组。您可以从此处调用%debug魔术命令来执行执行