gpt4 book ai didi

python - 系统退出 : 2 error when calling parse_args() in iPython Notebook

转载 作者:太空狗 更新时间:2023-10-29 21:57:10 26 4
gpt4 key购买 nike

我正在学习使用 Python 和 scikit-learn,并在 iPython 笔记本(使用 Python 2.7)中执行了以下代码块(最初来自 http://scikit-learn.org/stable/auto_examples/document_classification_20newsgroups.html#example-document-classification-20newsgroups-py):

from __future__ import print_function
from optparse import OptionParser

# parse commandline arguments
op = OptionParser()
op.add_option("--report",
action="store_true", dest="print_report",
help="Print a detailed classification report.")
op.add_option("--chi2_select",
action="store", type="int", dest="select_chi2",
help="Select some number of features using a chi-squared test")
op.add_option("--confusion_matrix",
action="store_true", dest="print_cm",
help="Print the confusion matrix.")
op.add_option("--top10",
action="store_true", dest="print_top10",
help="Print ten most discriminative terms per class"
" for every classifier.")
op.add_option("--all_categories",
action="store_true", dest="all_categories",
help="Whether to use all categories or not.")
op.add_option("--use_hashing",
action="store_true",
help="Use a hashing vectorizer.")
op.add_option("--n_features",
action="store", type=int, default=2 ** 16,
help="n_features when using the hashing vectorizer.")
op.add_option("--filtered",
action="store_true",
help="Remove newsgroup information that is easily overfit: "
"headers, signatures, and quoting.")

(opts, args) = op.parse_args()
if len(args) > 0:
op.error("this script takes no arguments.")
sys.exit(1)

在运行代码时,我遇到了以下错误:

An exception has occurred, use %tb to see the full traceback.

SystemExit: 2


Usage: -c [options]

-c: error: no such option: -f
To exit: use 'exit', 'quit', or Ctrl-D.

我按照说明执行了%tb,出现了如下内容:

---------------------------------------------------------------------------
SystemExit Traceback (most recent call last)
<ipython-input-1-d44fadf3a28b> in <module>()
37 "headers, signatures, and quoting.")
38
---> 39 (opts, args) = op.parse_args()
40 if len(args) > 0:
41 op.error("this script takes no arguments.")

C:\Anaconda\lib\optparse.pyc in parse_args(self, args, values)
1399 stop = self._process_args(largs, rargs, values)
1400 except (BadOptionError, OptionValueError), err:
-> 1401 self.error(str(err))
1402
1403 args = largs + rargs

C:\Anaconda\lib\optparse.pyc in error(self, msg)
1581 """
1582 self.print_usage(sys.stderr)
-> 1583 self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
1584
1585 def get_usage(self):

C:\Anaconda\lib\optparse.pyc in exit(self, status, msg)
1571 if msg:
1572 sys.stderr.write(msg)
-> 1573 sys.exit(status)
1574
1575 def error(self, msg):

SystemExit: 2

我知道 optparse 已被弃用,取而代之的是 argparse,但由于我想逐 block 理解教程,我希望我可以在 iPython notebook 中运行代码以了解它的工作原理。似乎someone else had this problem previously但没有提出解决方案。

有没有办法解决这个错误,以便我可以从 iPython notebook 中运行教程代码?

最佳答案

您可以添加您需要的参数作为字符串列表

(opts, args) = op.parse_args(["--report"])

关于python - 系统退出 : 2 error when calling parse_args() in iPython Notebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21000428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com