3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Python】Excelと同じ切片0の近似式の算出方法【scikit-learn】メモ

Posted at

概要

  • Excelには、切片固定で近似式を算出する方法がある
  • numpy.polyfitでは、切片固定ができない。
  • scikit-learnのLinerRegression,make_pipeline,PolyFeaturesで実現する

Excelの場合

  • 赤線部分の設定を行えば切片を固定できる(青プロットの近似式)
  • 今回は固定値を0とした。
    qiita_20200518.JPG

Pythonの場合

pandas+scikit-learn
from sklearn.linear_model import Ridge,LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline

# Excelと同じデータを作成
x=np.array([0,3,12.5,18.7,25,20,16,12.8,10.24,8.192])
y=np.array([0,15,46.6,60.3,74.3,59.44,47.552,46,36.8,29.44])

# DEGREE(次数)
degree = 3

# LinearRegression
# make_pipelineでPolynomialFeaturesとLineaRegressionをがっちゃんこ
model = make_pipeline(PolynomialFeatures(degree,include_bias=False),LinearRegression(fit_intercept=False))
model.fit(x.reshape(-1,1),y)
y_model=model.predict(x.reshape(-1,1))

#データフレームの確認
df = pd.DataFrame({'y_model.predict':y_model,},index=x)
df.sort_index().plot(kind ='line',figsize=(10.,5.))

# 係数
model.steps[1][1].coef_
  • よく似たグラフになりました。
    qiita_20200518-2.png

  • 係数一致(ほぼ)

array([ 5.06817229e+00, -1.71343566e-01,  3.49200227e-03])

まとめ

  • numpyのpolyfitではできなかったけど、scikit-learnでできてよかった。
  • Excelの代替手段としてPython使ってるときに困ったのでメモ
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?