2016-04-13 7 views
2

私はMLの教科書に従っています:機械をマスターするscikit-learnで学び、私のコードは私に正しい答えを与えますが、本の中身とマッチする。私matplotlibの中にこのチャートを与え私の結果が自分の教科書と一致するようにmatplotlibに行を表示します

import matplotlib.pyplot as plt 
X = [[6], [8], [10], [14], [18]] 
y = [[7], [9], [13], [17.5], [18]] 
plt.figure() 
plt.title('Pizza price plotted against diameter') 
plt.xlabel('Diameter in inches') 
plt.ylabel('Price in dollars') 
plt.plot(X, y, 'k.') 
plt.axis([0, 25, 0, 25]) 
plt.grid(True) 
plt.show() 

は、まずそれが私にこのコードを提供します

enter image description here

をそして、それは私の結果と一致してアップ。

from sklearn.linear_model import LinearRegression 
# Training data 
X = [[6], [8], [10], [14], [18]] 
y = [[7], [9], [13], [17.5], [18]] 
# Create and fit the model 
model = LinearRegression() 
model.fit(X, y) 
print 'A 12" pizza should cost: $%.2f' % model.predict([12])[0] 

そして、このチャート:

enter image description here

はフダンソウは、私のコードと一致しないこと、それはdoesnの

ただし、次のステップでそれが私にこのコードを示しますmatplotlib chart-maker関数があります。

from sklearn.linear_model import LinearRegression 
import numpy as np 
import matplotlib.pyplot as plt 


X = [[6], [8], [10], [14], [18]] 
y = [[7], [9], [13], [17.5], [18]] 


model = LinearRegression() 
model.fit(X, y) 

z = np.array([12]).reshape(-1,1) 

print ('A 12" pizza should cost: $%.2f' % model.predict(z)[0]) 
print ("\n" + "_" * 50 + "\n") 

plt.figure() 
plt.title('Pizza price plotted against diameter') 
plt.xlabel('Diameter in inches') 
plt.ylabel('Price in dollars') 
plt.plot(X, y, z, 'k.') 
plt.axis([0, 25, 0, 25]) 
plt.grid(True) 
plt.show() 

しかし、それはちょうど私にこの奇妙な青事与えた:私はガイドを読んで、自分自身を作ってみました、私はpythonで数学での作業に新たなんだ

enter image description here

を、そう誰であれば私にこれを解決する方法に関するより多くの情報を与えることができます、それは認められるでしょう。

+0

だったのですか? – adhg

+0

だから。たくさんの。エラー。 – Rich

+0

本当ですか?私はscikitでMLの本を探していた。あまりにも悪い – adhg

答えて

1

この「奇妙な青いもの」は、データが線分で結合されていることです。あなたのデータはplt.scatterを使用してプロットしなければなりません。

回帰直線のあなたの計算を固定する必要があるもの、正しいことは、データセット上でその行をプロットする方法である:あなたのデータを当てはめた後

、あなたは回帰直線を描画するために必要な値を抽出する必要があります;必要なデータは、x軸の各端点(ここではx=0およびx=25)の2点です。これら2つの値に対してmodel.predictと呼ぶと、対応する予測が得られます。それらの対応する予測と結合されたこれらのx値は、線をプロットするために使用する2つの点を形成します。

まず、予測値y0y25を抽出します。次に、点(0、y0)と(25、y25)を持つplt.plotを使用して、回帰直線を緑色で描画します。 「scikit学習とマスタリング機械学習」本は、それはあなたのために役立つ方法を

from sklearn.linear_model import LinearRegression 
import numpy as np 
import matplotlib.pyplot as plt 


X = [[6], [8], [10], [14], [18]] 
y = [[7], [9], [13], [17.5], [18]] 


model = LinearRegression() 
model.fit(X, y) 

z = np.array([12]).reshape(-1,1) 

print ('A 12" pizza should cost: $%.2f' % model.predict(z)[0]) 
print ("\n" + "_" * 50 + "\n") 

plt.figure() 
plt.title('Pizza price plotted against diameter') 
plt.xlabel('Diameter in inches') 
plt.ylabel('Price in dollars') 
plt.scatter(X, y, z, 'k') 

y0, y25 = model.predict(0)[0][0], model.predict(25)[0][0] 
plt.plot((0, 25), (y0, y25), 'g') 

plt.axis([0, 25, 0, 25]) 
plt.grid(True) 
plt.show() 

enter image description here

+0

あなたが線を持たないプロットを使用してサイズや色でマーカーを拡大しない限り、ax.plot(x、y、 'o')はパフォーマンスが少し向上します(点数が多い場合にのみ重要です) – tacaswell

+0

、 'predict'への呼び出しをベクトル化できませんか? – tacaswell

+1

さて、うまくいきました。ありがとう。 – Rich

関連する問題