環境
Winodws10 64bit
Python 3.7
anaconda3
前提・実現したいこと
上記のような点の集合があります。
y軸が1のときは省きまして、それ以外の点について2つの山があると仮定してその中央のxを求めたいです。
最も効率のいい方法としてpythonの2次曲線もしくは3次曲線の回帰を行おうと考えています。
該当のソースコード
Python
1import numpy as np 2import matplotlib.pyplot as plt 3from sklearn.linear_model import LinearRegression 4from sklearn.preprocessing import PolynomialFeatures 5from sklearn.pipeline import Pipeline 6from math import sin 7 8# create samples 9sample_size = 100 10err_sigma = 0.1 11 12#x = 12 * np.random.rand(sample_size, 1) 13#x_math = [[], []] 14#y_math = [[], []] 15x = [[1],[1],[2],[2],[2],[2],[4],[6],[6],[6]] 16err = [[4],[5],[5],[3],[3],[6],[3],[6],[2],[2]] 17 18 19#err = err_sigma*np.random.randn(sample_size, 1) 20func = np.vectorize(sin) 21y = func(x) + err 22 23# plot train data 24plt.plot(x, y, 'o') 25 26# train linear regression models with different polynomial basis 27deg = [1, 3, 6] 28for d in deg: 29 regr = Pipeline([ 30 ('poly', PolynomialFeatures(degree=d)), 31 ('linear', LinearRegression()) 32 ]) 33 regr.fit(x, y) 34 35 # make predictions 36 xt = np.linspace(0.0, 12.0, num=1000).reshape((1000, 1)) 37 yt = regr.predict(xt) 38 39 # plot regression result 40 plt.plot(xt, yt, label='polynomial of degree %d' % (d)) 41 42plt.legend() 43plt.show() 44
このあと微分をして範囲内の最小値を求めていけば中央値がでると思いました。
強引に曲線をあてはめてその式を求めるにはscikit-learnとpythonによる手法で行いたいです。
x座標が多ければ多いほどより精度のある曲線が描けると思うのですが上記のように非常に少ないです。
2元3次方程式をx,y代入していき近似式を求める方法として何かアドバイス頂けないでしょうか。
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/18 00:04