Pythonでモジュールを実行するとエラーが出る!原因と解決方法は?

このQ&Aのポイント
  • Pythonの初心者が集合値プログラミングを始めたばかりですが、モジュールを実行するとエラーが表示されます。OSはXPで、バージョンはPython3.0を使用しています。
  • エラーの内容は、ピアソン相関係数を算出するモジュール(recommendations.py)を実行しようとした際にSyntaxErrorが発生しています。
  • 具体的には、recommendationsモジュールのsim_pearson関数を呼び出した際に、SyntaxErrorが発生しています。原因は正しい構文を使っていないことです。正しい構文を使うよう修正することで問題が解決する可能性があります。
回答を見る
  • ベストアンサー

pythonでモジュールを実行するとエラーが出るのですが原因がわかりません。

pythonの集合値プログラミングを始めたばかりの初心者です。 OS: XP バージョン: python3.0 ピアソン相関係数を算出するモジュール(recommendations.py)を実行したらエラー表示されます。 モジュール(recommendations.py)のソース↓ critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5, 'The Night Listener': 3.0}, 'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5, 'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0, 'You, Me and Dupree': 3.5}, 'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0, 'Superman Returns': 3.5, 'The Night Listener': 4.0}, 'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'The Night Listener': 4.5, 'Superman Returns': 4.0, 'You, Me and Dupree': 2.5}, 'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 'Just My Luck': 2.0, 'Superman Returns': 3.0,'The Night Listener': 3.0, 'You, Me and Dupree': 2.0}, 'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5}, 'Toby': {'Snakes on a Plane':4.5, 'You, Me and Dupree':1.0, 'Superman Return':4.0}} from math import sqrt #p1とp2のピアソン相関係数を返す def sim_pearson(prefs,p1,p2): #両者が互いに評価しているアイテムのリストを取得 si={} for item in prefs[p1]: if item in prefs[p2]: si[item]=1 #要素の数を調べる n=len(si) #共に評価しているアイテムがなければ0を返す if n==0: return 0 #すべての嗜好を合計する sum1=sum([prefs[p1][it] for it in si]) sum2=sum([prefs[p2][it] for it in si]) #平方を合計する sum1Sq=sum([pow(prefs[p1][it],2) for it in si]) sum2Sq=sum([pow(prefs[p2][it],2) for it in si]) #積を合計する pSum=sum([prefs[p1][it]*prefs[p2][it] for it in si]) #ピアソンによるスコアを計算する num=pSum-(sum1*sum2/n) den=sqrt((sum1Sq-pow(sum1,2)/n)*(sum2Sq-pow(sum2,2)/n)) if den==0: return 0 r=num/den return r 実行結果は >>>import recommendations >>>print recommendations.sim_pearson(recommendations.critics,'Lisa Rose','Gene Seymour') File "<studio>", line 1 print recommendations.sim_pearson(recommendations.critics,'Lisa Rose','Gene Seymour') ^ SyntaxError: invalid syntax となります。わかる方教えてください。

質問者が選んだベストアンサー

  • ベストアンサー
  • LOHA
  • ベストアンサー率52% (203/388)
回答No.1

python3からprintは関数になっています。 http://d.hatena.ne.jp/gom68/20090812/1250089816

syosinsyo1
質問者

お礼

すみません。わかりました!ありがとうございます。

syosinsyo1
質問者

補足

ほんとにPC自体を触ることが珍しいくらいの初心者なのでよくわからないのですが command line で print(recommendations.sim_pearson.critics,'Lisa Rose','Gene Seymour')) と入力すればいいということですか?

関連するQ&A

  • pythonのモジュールのエラーの原因

    python のモジュール、特にcvxopt,openopt,matplotlibを ダウンロードしようと思っています。 その際、Couldn't find a setup script in~~ というエラーが出て 困っています(以下はその概略です。) まず、easy_installは使える状態であり、 たとえばうまくいった例として コマンドプロンプト上でnumpyモジュールをダウンロードすべく、 C:\Users\****(←自分の名前)> easy_install numpyと 入力、実行したときはきちんとfinishedしたようで、 pythonコマンドライン上で >>> import numpy とやれば(当然かもしれませんが)うまく通っているようです。 easy_installへのパスも確認済みです。 ところが、たとえばうまくいっていないmatplotlibの場合、 C:\Users\****(←自分の名前)> easy_install matplotlib と入力、Enterでしばらく待つと error: Couldn't find a setup script in c:\users\****(名前)~1\appdata\local\temp\easy _install-unpmv8\download そしてcvxoptでも、 error: Couldn't find a setup script in c:\users\hashim~1\appdata\local\temp\easy _install-cflmgh\cvxopt-1.1.5.tar.gz なるエラーがやはり出ます。 自分が調べた限りでは、windowsでは、setup scriptはez_setup.pyを 実行したら一緒に含まれているようなのですが・・・ 自分はpython2.7を使っており、windows 7,64bitです。 長ったらしい文になり申し訳ないですが 大学の研究に使いたいのでぜひ入れたいと思っているので どうか宜しくお願いします!

  • 英文をチェックしてください

    私は映画館でフォーゴットンのCMをみたよ。日本では6月にやるよ。でも、私はアイルランドに行った時、飛行機の中でもう観たんだよ。 I watched CM of forgotten at cinema it will be opening on Jun in Japan,but I already watched it because when I went to Ireland it watched on the plane. こんな英文通じますか?なんとか通じたらいいなと思ってます。 お願いします

  • pythonのモジュールについて

    pythonのdatetimeモジュールと同じくらいの機能をもったモジュールをpythonでつくることはできるのでしょうか?できるとしたらどうするんですか?教えてください

  • YES or NO 再度質問です

    ごめんなさい、再度こちらに投稿してしまいました。 あるネイティブの先生に以下の英文を見せ、Is it impossible to omit "about"?(aboutを省略するのは不可能ですか?) という質問をしました。 Is there nothing you can recall about last night that struck you as suspicious? そしたら以下のような答えがきました。 No, it isn't. Not if you want to know what the listener remembers "about" (relating to a particular subject) the previous night. "About" is a key word in the question. No, it isn'tの後はimpossibleと考えると【不可能ではない→可能】となってaboutが省略可能ということになると思うのですが。 ただ最後に"About" is a key word in the question. と言っているのでネイティブの人はaboutは必要といているようにも聞こえるし・・一体どちらなのでしょうか?

  • pythonでの実行エラーを解消したい

    windows10です。 pythonの超初心者です。 添付資料のようにコードを作成し、実行させると python:can't open file 'C:¥¥Users¥¥知之¥¥Greeting.py': [Errno 2] No such file or directory と、エラーメッセージが出ます。 エラーの解消方法を教えてください。 宜しくお願いいたします。

  • ちょっと気になってます【9】

    下記の文章を英語で言うとどうなるのでしょう。 気が付いたところだけでいいので教えてください。 よろしくお願いします。 1. for the sake of God. (成句のようです。意味をよろしくお願いします。) 2. ~,off to one side,~ (off の意味がわかりません。) 3. Tell me of your companions. It means of noble birth. (of の用法を教えてください。) 4. I will attend him in good time. (「彼を長い時間待つことだろう。」でいいのでしょうか。) 5. We are to the Lady Claire. (Lady Claire は人名です。be to で、どのようなことを意味するのでしょうか。訳をお願いします。) 6. He is on every tongue. (意味を教えてください。) よろしくお願いします。

  • Pythonのモジュールやデータ型はどこにある?

    Pythonの本を読んでいて、 モジュールの内部構成の説明が無かったので質問です。 randomモジュールやdatetimeモジュール、date型などは Pythonのどこに格納されているのでしょうか? 実際にファイルの中身を見てどのような記述がされているのか確認したいです。 ちなみに環境は、 mac os X 10.6.8 にデフォルトで入っていたPythonを使っています。 よろしくお願いします。

  • pythonモジュールがインストール出来ない

    SSLやpyAMFなどを python setup.py install でインストールしようとしているのですが、 エラーが出てしまいます。 pythonは2.6だとできるようですがどうしても2.5xを使う必要があります。 python25-appleをデフォルトのpythonにしています。 エラーの詳細は次のようになります。 User-no-MacBook-2:PyAMF-0.6.1 user$ python setup.py install 略 no previously-included directories found matching 'doc/build' no previously-included directories found matching 'doc/_build' warning: no previously-included files matching '*.swf' found anywhere in distribution warning: no previously-included files matching '*.pyc' found anywhere in distribution 略 gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -O3 -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c cpyamf/amf0.c -o build/temp.macosx-10.3-i386-2.5/cpyamf/amf0.o cc1: error: unrecognized command line option "-Wno-long-double" cc1: error: unrecognized command line option "-Wno-long-double" lipo: can't figure out the architecture type of: /var/folders/dE/dEVcnZLtFw8U1ZFnQjT42E+++TI/-Tmp-//ccz4QQFi.out error: command 'gcc' failed with exit status 1 始めはMacOSX10.4u.sdkがないと言われ、インストールしてみると そのエラーは出なくなりましたが上記のような状態です。 これは使っているgccコンパイラに問題があるということなのでしょうか? どなたかアドバイスをお願いします。

  • 和訳をお願いします

    和訳をお願いします The worker called the doctor and then found Emily's owner. An airline company heard about this story in the news and gave Emily a free plane tickct back to the United States. Can you believe it? Emily flew home in business class! On the flight home, Emily was served a nice salmon dinner from the menu,but she wouldn't eat it. She chose French cat food instead.

  • 和訳をお願いします。

    In the two-dimensional case where the protoplanet and the planetesimal revolve in the sample plane around the protosun (i.e., i=0), the phase angle ω loses its meaning and Eq. (10) must be modified as <P(e, 0)>=∫(3/2) |b|(1/2π)p_col(e, i=0, b, τ)dτdb. ・・・・・(11) We consider that the planetesimal collides with the protoplanet if the separation distance becomes smaller than the sum of their radii; the protoplanet radius scaled by ha_0* is given by r_p=0.005(ρ/3gcm^-3)^-(1/3)(a_0*/1AU)^-1, ・・・・・(12) where ρ is the mean mass density of the protoplanet. よろしくお願いいたします。