[英]Is it possible to debug a 3rd-party python file inside a Jupyter notebook cell?
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”源代碼的一部分。
此鏈接是完整的片段,可以在線上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輸入
我進入up
,pdb切換到了classification.py
設定斷點
然后up
,切換回去,
斷點不起作用
這是整個日志
> /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
事實上,這是不可能的。 但是有一個竅門。 您可以有意地將錯誤的參數傳遞給predict
函數,以使其失敗,並且可以調用%debug
以便逐行執行步驟。 請參閱下面的示例。
y_pred=knn.predict(["trap", X_test])
這將嘗試執行predict
方法,但會失敗,因為您輸入的是隨機列表而不是數組。 您可以從那里調用%debug
magic命令來執行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.