質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

12183閲覧

python 'Polyfit may be poorly conditioned' 原因について

sshhoo

総合スコア15

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/01/12 05:51

jupyter notebook上でpolyfitを使って近似曲線を描いていたら、以下のようなエラーが出てきてうまくいきません。

C:\Users\a3215\Anaconda3\lib\site-packages\ipykernel_launcher.py:12: RankWarning: Polyfit may be poorly conditioned
if sys.path[0] == '':

なお、同じグラフ内に複数個の近似曲線を描きたいのですが、最初の一個目の曲線はエラーなく描けるのですが、二つ目以降も同じようなコードで描こうとすると上記のメッセージが出ます。

何が問題なのかわからないため、質問させて頂きました。

以下が実行したコードです。

fig = plt.figure()
ax = fig.add_subplot(111)
plt.yscale('log')
plt.scatter(temp0, resi0, )
plt.scatter(temp02, resi02, )
plt.scatter(temp05, resi05, label='moise0.5')
plt.scatter(temp10, resi10, label='moise1.0')

func0 = np.poly1d(np.polyfit(temp0, resi0, 7))
plt.plot(temp0, func0(temp0), label='moise0')

func02 = np.poly1d(np.polyfit(temp02, resi02,7))
plt.plot(temp02, func02(temp02), label='moise02')

plt.legend(loc='best')

なお、未だにpythonは初心者なので、お手数ですがわかりやすく教えて頂けたらと思います。

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

numpy.polyfit
では、最小二乗法による多項式近似を行っているわけですが、与えられたデータではうまく近似できないよ と言われています。
その理由や回避策については

polyfit issues a RankWarning when the least-squares fit is badly conditioned. This implies that the best fit is not well-defined due to numerical error. The results may be improved by lowering the polynomial degree or by replacing x by x - x.mean(). The rcond parameter can also be set to a value smaller than its default, but the resulting fit may be spurious: including contributions from the small singular values can add numerical noise to the result.

Note that fitting polynomial coefficients is inherently badly conditioned when the degree of the polynomial is large or the interval of sample points is badly centered. The quality of the fit should always be checked in these cases. When polynomial fits are not satisfactory, splines may be a good alternative.

と記載されてますが、私には理解、説明できません。

ただ提示URLには警告が発生する例も載っており、コードを書いてみると直感的にだめだろうな~と理解できます。
なお、full=Trueを指定することで警告を抑制することはできます。

Python

1import numpy as np 2 3print( np.polyfit([1,2,3], [2,4,6], 1)) # OK 4print( np.polyfit([1,2,3], [2,4,6], 2)) # OK 5 6print( np.polyfit([1,1,3], [2,2,6], 2)) # Warning 2点が同じなので2次近似できない 7print( np.polyfit([1,2,3], [2,4,6], 3)) # Warning たった3点では3次近似できない 8print( np.polyfit([1,2,3], [2,4,6], 3, full=True)) # OK 統計情報も返すので妥当性は自分で判断してね

投稿2018/01/12 07:05

can110

総合スコア38233

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sshhoo

2018/01/12 08:11

そもそも与えたデータが悪かったのですね。 ご回答頂き助かりました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問