2014-01-08 10 views
7

で2エラーparse_args()を呼び出す私は、Pythonを使用するために学び、scikit-学び、(Pythonの2.7を使用して)iPythonノートブックで(もともとhttp://scikit-learn.org/stable/auto_examples/document_classification_20newsgroups.html#example-document-classification-20newsgroups-pyからの)コードの次のブロックを実行しています:SystemExitを:iPythonノート

コードを実行すると
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. 

Iは命令に従い、%の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のために廃止されていることを理解していますが、ブロックごとにチュートリアルブロックを理解したいので、iPythonノートブック内でコードを実行してその動作を感じることができました。 someone else had this problem previouslyと思われますが、解決策は提案されていませんでした。

iPythonノートブックからチュートリアルコードを実行できるように、このエラーに対処する方法はありますか?

+0

の可能性のある重複した[SystemExitを:パース\ _argsを呼び出す2エラー()](http://stackoverflow.com/questions/42249982/systemexit-2-error-when-calling-parse-args ) – daphtdazz

+0

私の場合、私のコードは別のIDE(同じコンピュータ)と別のコンピュータのJupyterノートブックで動作します – user3226167

答えて

2

あなたは、以下のリンク文字列

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

変更されたコードのリストとして必要な引数を追加することができます。理解のためにいくつかの説明/冗長を加えました。

をもっと整理する必要がありますが、いくつか推奨されなくなったパッケージは警告になります。

https://github.com/aspiringguru/document-_classification.git

+0

関連するコードを*自分自身に*追加する必要があります。スタックオーバーフローはリンクリポジトリではありません。 –

関連する問題