Fit
詳細とオプション
- Fitは,線形回帰あるいは最小二乗フィットとしても知られている.正則化では,LASSO回帰およびリッジ回帰としても知られている.
- Fitは,一般に,多項式や指数関数を含む関数の組合せをデータにフィットする際に使用される.これは,データからモデルを得る最も簡単な方法の一つを提供する.
- 最適フィットは,平方の和
を最小にする. - data は次の形式でよい.
-
{v1,…,vn} {{1,v1},…,{n,vn}}と同等 {{x1,v1},…,{xn,vn}} 座標 xiにおける値 viを持った一変量データ {{x1,y1,v1},…} 値 vi と座標{xi,yi}を持った二変量データ {{x1,y1,…,v1},…} 座標{xi,yi,…}における値 viを持った多変量データ - 計画行列 m は座標位置で関数を評価して得られる要素
を持つ.行列表現では,最適フィットはノルム
を最小化する.ただし,
かつ
である. - 関数 fi が依存するのは変数{x,y,…}だけでなければならない.
- 可能なフィット特性"prop"には次がある.
-
"BasisFunctions" funs 基底関数 "BestFit" 
基底関数の最適フィット線形結合 "BestFitParameters" 
最適フィットを与えるベクトル 
"Coordinates" {{x1,y1,…},…} data 中の vars の座標 "Data" data データ "DesignMatrix" m 
"FitResiduals" 
座標におけるモデルとフィットの差 "Function" Function[{x,y,…},a1 f1+…+an fn] 最適フィット純関数 "PredictedResponse" 
data 座標についてフィットされた値 "Response" 
入力 data からの応答ベクトル 
{"prop1","prop2",…} いくつかのフィット特性 - Fitは次のオプションを取る.
-
NormFunction Norm 最小化する偏差の測定値 FitRegularization None フィットパラメータ
についての正規化WorkingPrecision Automatic 使用精度 - NormFunction->normf かつFitRegularization->rfun のとき,Fitは normf[{a.f(x1,y1,…)-v1,…,a.f(xk,yk,…)-vk}] + rfun[a]を最小化する係数 a ベクトルを求める.
- NormFunctionの設定は次の形式で与えることができる.
-
normf 偏差に適用される関数 normf {"Penalty", pf} 偏差の各成分に適用されたペナルティ関数 pf の和 {"HuberPenalty",α} 各成分についてのHuberペナルティ関数の和 {"DeadzoneLinearPenalty",α} 各成分についてのデッドゾーン線形ペナルティ関数の和 - FitRegularizationの設定は次の形式で与えることができる.
-
None 正規化は行わない rfun rfun[a]による正規化 {"Tikhonov",λ}
による正規化{"LASSO",λ}
による正規化{"Variation",λ}
による正規化{"TotalVariation",λ}
による正規化{"Curvature",λ}
による正規化{r1,r2,…} r1,…の項目の和による正規化 - WorkingPrecision->Automaticのとき,Fitへの入力として与えられた厳密数は機械精度の近似数に変換される.
例題
すべて開く すべて閉じる例 (2)
data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};line = Fit[data, {1, x}, x]DesignMatrix[data, {1, x}, x]parabola = Fit[data, {1, x, x ^ 2}, x]Show[ListPlot[data, PlotStyle -> Red], Plot[{line, parabola}, {x, 0, 5}]]計画行列と応答ベクトルが与えられたときの最適フィットパラメータを求める:
m = N[HilbertMatrix[4]];
v = Range[4];
a = Fit[{m, v}]スコープ (2)
data = {{-Pi, 4}, {-Pi / 2, 0}, {0, 1}, {Pi / 2, -1}, {Pi, -4}};Fit[data, {Sin[x / 2], Sin[x], Sin[2 x]}, x]Fit[N[data, 24], {Sin[x / 2], Sin[x], Sin[2 x]}, x]Show[ListPlot[data], Plot[%, {x, -Pi, Pi}], PlotRange -> All]data = {{0, 0, 0}, {1, 0, 1}, {0, 1, 2}, {1, 1, 0}, {1 / 2, 1 / 2, 1}};plane = Fit[data, {1, x, y}, {x, y}]Show[Plot3D[plane, {x, 0, 1}, {y, 0, 1}, PlotStyle -> Opacity[.5], PlotRange -> {0, 2}], Graphics3D[{Red, PointSize[0.05], Map[Point, data]}]]quad = Fit[data, {1, x, y, x ^ 2, x y, y ^ 2}, {x, y}]Show[Plot3D[quad, {x, 0, 1}, {y, 0, 1}, PlotStyle -> Opacity[.5], PlotRange -> {0, 2}], Graphics3D[{Red, PointSize[0.05], Map[Point, data]}]]一般化と拡張 (1)
v = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5};二次関数にフィットする.座標は,与えられていなければ,値は1, 2, ...のペアとみなされる:
quad = Fit[v, {1, x, x ^ 2}, x]quart = Fit[v, {1, x, x ^ 2, x ^ 3, x ^ 4}, x]Show[ListPlot[v, PlotStyle -> Red], Plot[{quad, quart}, {x, 1, Length[v]}]]オプション (6)
FitRegularization (2)
Tikhonov正規化は最適フィットパラメータの大きさを制御する:
data = {{0., 0.}, {0.001, 1}, {0.01, 1}};Fit[data, {1, x, x ^ 2}, x, FitRegularization -> {"Tikhonov", 1}]Fit[data, {1, x, x ^ 2}, x]これは,"L2"または"RidgeRegression"で参照することができる:
Fit[data, {1, x, x ^ 2}, x, FitRegularization -> {"L2", 1}]Fit[data, {1, x, x ^ 2}, x, FitRegularization -> {"RidgeRegression", 1}]LASSO (least absolute shrinkage and selection operator) 正規化は,最も重要な基底関数を選択し,最適合パラメータの大きさをある程度制御する:
data = {{0., 0.}, {0.001, 1}, {0.01, 1}, {.1, 0}, {1, 1}};Fit[data, {1, x, x ^ 2, x ^ 3, x ^ 4}, x, FitRegularization -> {"LASSO", 1}]Fit[data, {1, x, x ^ 2, x ^ 3, x ^ 4}, x]Fit[data, {1, x, x ^ 2, x ^ 3, x ^ 4}, x, FitRegularization -> {"L1", 1}]NormFunction (3)
data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};l1 = Fit[data, {1, x}, x, NormFunction -> Function[Norm[#, 1]]]Show[ListPlot[data, PlotStyle -> PointSize[0.025]], Plot[l1, {x, 0, 5}], PlotRange -> All]最小二乗フィット(金)および最大ノルムフィット(緑)と比較する:
l2 = Fit[data, {1, x}, x];
l∞ = Fit[data, {1, x}, x, NormFunction -> Function[Norm[#, ∞]]];
Show[ListPlot[data, PlotStyle -> PointSize[0.025]], Plot[{l1, l2, l∞}, {x, 0, 5}], PlotRange -> All]Manipulate[Plot[Evaluate[With[{q = 1 / pr}, Fit[data, {1, x}, x, NormFunction -> Function[Norm[#, q]]]]], {x, 0, 5}, PlotRange -> {-1, 4}, Epilog -> {PointSize[.025], Point[data], Text["p = " <> ToString[1 / pr], {1, 3}]}], {{pr, 1 / 2, "1/p"}, 0.0001, .9999}, SaveDefinitions -> True]Huberのペナルティ関数を使って外れ値の影響と最小二乗のバランスを取る:
data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};hfit = Fit[data, {1, x}, x, NormFunction -> {"HuberPenalty", .1}]Show[ListPlot[data, PlotStyle -> PointSize[0.025]], Plot[hfit, {x, 0, 5}], PlotRange -> All]Manipulate[Plot[Evaluate[Fit[data, {1, x}, x, NormFunction -> {"HuberPenalty", α}]], {x, 0, 5}, PlotRange -> {-1, 4}, Epilog -> {PointSize[.025], Point[data], Text["α = " <> ToString[α], {1, 3}]}], {{α, .1}, 0.0001, 2}, SaveDefinitions -> True]data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};
fit = Fit[data, {1, x}, x, NormFunction -> Function[Norm[# Range[4]]]];
Show[ListPlot[data, PlotStyle -> PointSize[0.025]], Plot[fit, {x, 0, 5}], PlotRange -> All]アプリケーション (6)
チェビシェフ(Chebyshev)基底を使ってデータへの高次多項式フィットを計算する:
coord = RandomReal[{-1, 1}, 100];
data = Transpose[{coord, Sinc[10 coord]}];degree = 47;
basis = Table[ChebyshevT[k, x], {k, 0, degree}];
fit = Fit[data, basis, x];Plot[fit, {x, -1, 1}, Epilog -> Point[data]]正規化を使って
が摂動する場合の
の
に対する数値解を安定させる:
a = N[HilbertMatrix[47]];
xexact = Table[t * (1 - t), {t, 0., 1., 1 / 46}];
b = a.xexact + 10^ - 8 RandomReal[{-1, 1}, 47];LinearSolveによって求まった解には非常に大きい項がある:
ListPlot[LinearSolve[a, b]]
xreg = Table[Fit[{a, b}, FitRegularization -> {"Tikhonov", 10 ^ -ll}], {ll, 0, 3, 2}];ListPlot[Prepend[xreg, xexact], PlotStyle -> PointSize[0.025]]n = 201;
m = 201;
times = Range[m];m1 = Round[m / 5]; m2 = Round[m / 4]; m3 = Round[m / 4];output = ConstantArray[0, m];
output[[m1 + Range[m2]]] = 1;
output[[m1 + m2 + Range[m3]]] = -1;
op = ListLinePlot[output]h = (1/9)(1 - .4Cos[2 times]) * .9 ^ times;hmat = ToeplitzMatrix[h, Prepend[ConstantArray[0, n - 1], h[[1]]]];正規化なしでは,予想された応答は信号に非常に近いが,計算された入力には大きな振動がある:
{input, predicted} = Fit[{hmat, output}, {"BestFitParameters", "PredictedResponse"}];
Row[{ListLinePlot[input], ListLinePlot[{predicted, output}]}]{input, predicted} = Fit[{hmat, output}, {"BestFitParameters", "PredictedResponse"}, FitRegularization -> {"Variation", .3}];
Row[{ListLinePlot[input], ListLinePlot[{predicted, output}]}]{input, predicted} = Fit[{hmat, output}, {"BestFitParameters", "PredictedResponse"}, FitRegularization -> {{"Variation", .3}, {"Tikhonov", .01}}];
Row[{ListLinePlot[input], ListLinePlot[{predicted, output}]}]n = 4000;
times = N[Range[0, n - 1]];
original = .5 Sin[2 Pi times / n] * Sin[.01 times];
corrupted = original + RandomReal[{-.05, .05}, n];
ListLinePlot[corrupted]残差のノルムと変動のノルムの間のトレードオフをプロットする:
id = IdentityMatrix[n, SparseArray];tradeoff = Table[{smoothed, residual} = Fit[{id, corrupted}, {"BestFitParameters", "FitResiduals"}, FitRegularization -> {"Variation", 2 ^ logλ}];Tooltip[{Norm[Differences[smoothed]], Norm[residual]}, 2. ^ logλ], {logλ, -10, 20}];ListPlot[tradeoff, Joined -> True, Mesh -> All]smoothed = Fit[{id, corrupted}, "BestFitParameters", FitRegularization -> {"Variation", 100}];ListLinePlot[{corrupted, smoothed}]全変動の正規化を使ってジャンプがある破損信号を滑らかにする:
n = 2000;
id = IdentityMatrix[n, SparseArray];
times = Range[0, n - 1];
temp = ConstantArray[1, n / 4];
original = Join[temp, -temp, temp, -temp] + .5 Sin[2 Pi times / n];
corrupted = original + RandomReal[{-.1, .1}, n];
ListLinePlot[{corrupted, original}]smoothed = Fit[{id, corrupted}, "BestFitParameters", FitRegularization -> {"TotalVariation", 2.}];ListLinePlot[{smoothed, original}]
の値を小さくするとあまり滑らかではなくなるが,残差ノルムは小さくなる:
lesssmoothed = Fit[{id, corrupted}, "BestFitParameters", FitRegularization -> {"TotalVariation", .1}];ListLinePlot[{lesssmoothed, original}]{Norm[original - smoothed], Norm[original - lesssmoothed]}LASSO (L1)正規化を使って疎なフィット(基底追跡)を求める:
σ = 0.05;
nτ = 500;
times = Subdivide[0., 1., nτ];
nω = 30;signal = (1 + .5 Sin[11 times])Sin[30 Sin[5 times]];
sdata = Transpose[{times, signal}];
ListLinePlot[sdata]目標は,何千とあるGabor基底関数のいくつかを使って信号を近似することである:
Length[basis = Flatten[
Table[Exp[-(((t - τ)/σ))^2]
{1, Table[{Cos[ω t], Sin[ω t]}, {ω, Drop[Subdivide[0., 150., nω], 1]}]},
{τ, Subdivide[0., 1., nτ]}]]]
のときのフィットは基底要素のたった41個を使って求めることができる:
AbsoluteTiming[sparseFit = Fit[sdata, basis, t, "BestFitParameters", FitRegularization -> {"LASSO", 1.}]]fitt = basis.sparseFit /. t -> times;
ListLinePlot[Transpose[{times, signal - fitt}]]基底の重要な要素が見付かったら,これらの要素の最小二乗フィットを求めることで誤差を小さくすることができる:
bpos = Flatten[sparseFit["NonzeroPositions"]];
sbasis = basis[[bpos]];
sbfit = Fit[sdata, sbasis, t];
ListLinePlot[Transpose[{times, signal - (sbfit /. t -> times)}], PlotRange -> All]より小さな値の
でのフィットもほぼ同じ要素を使って求めることができる:
AbsoluteTiming[sparseFit = Fit[sdata, basis, t, "BestFitParameters", FitRegularization -> {"LASSO", 0.1}]]fitt = basis.sparseFit /. t -> times;
ListLinePlot[Transpose[{times, signal - fitt}]]特性と関係 (5)
Fitは最適フィットの関数を与える:
Fit[Range[10], {1, x ^ 2}, x]LinearModelFitはフィットに関する追加的な情報の抽出を許す:
lm = LinearModelFit[Range[10], x ^ 2, x]lm["BestFit"]lm[{"RSquared", "FitResiduals", "ANOVATable"}]data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};ss[a_, b_] = Block[{x = data[[All, 1]], y = data[[All, 2]], res}, res = y - (a + b x);res.res]Minimize[ss[a, b], {a, b}]N[a + b x /. %[[2]]]以下はFitで与えられる係数である:
Fit[data, {1, x}, x]厳密係数はWorkingPrecision->Infinityを使うと求まるかもしれない:
Fit[data, {1, x}, x, "BestFitParameters", WorkingPrecision -> Infinity]これは,二次方程式a+b x+c x^2の誤差の平方和である:
ss[a_, b_, c_] = Block[{x = data[[All, 1]], y = data[[All, 2]], res}, res = y - (a + b x + c x ^ 2);res.res]Minimize[ss[a, b, c], {a, b, c}]N[a + b x + c x ^ 2 /. %[[2]]]これらはFitで与えられる係数である:
Fit[data, {1, x, x ^ 2}, x, "BestFitParameters", WorkingPrecision -> Infinity]多項式のフィットが十分高い次数で行われるとき,Fitは補間多項式を返す:
data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};
Fit[data, {1, x, x ^ 2, x ^ 3}, x]結果はInterpolatingPolynomialで返されるものと矛盾しない:
InterpolatingPolynomial[data, x]N[Expand[%]]Fit は,TimeSeriesのタイムスタンプを変数として使う:
ts1 = TemporalData[TimeSeries, {{{1.102448341846048, 6.331833897009389, 7.5843632999043455,
44.59969789743589, 98.76273258351092, 150.00729051003992, 644.7367359379259,
1724.3968817536402, 5903.697723461762, 19468.09340765025}}, {{0, 9, 1}}, 1, {"Continuous", 1},
{"Discrete", 1}, 1, {ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}}}, False,
10.1];ts1["Times"]base = {1, Exp[x], Exp[2 x], Exp[x ^ 2]};Fit[ts1, base, x]ts2 = TimeSeriesRescale[ts1, {.1, 2}]ts2["Times"]Fit[ts2, base, x]Fit[ts1["Values"], base, x]Fitは,複数の経路があるTemporalDataについては,経路ごとに作用する:
Fit[TemporalData[Automatic, {{{1.102448341846048, 6.331833897009389, 7.5843632999043455,
44.59969789743589, 98.76273258351092, 150.00729051003992, 644.7367359379259,
1724.3968817536402, 5903.697723461762, 19468.09340765025},
{1.1024483418 ... 7359379259, 1724.3968817536402, 5903.697723461762,
19468.09340765025}}, {{0, 9, 1}, {0.1, 2., 0.2111111111111111}}, 2, {"Continuous", 2},
{"Discrete", 2}, 1, {ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}}}, False,
10.1], {1, Exp[x], Exp[2 x]}, x]考えられる問題 (2)
data = Block[{r = RandomReal[100, 100]}, Transpose[{r, Exp[-(r - 50) ^ 2 / 10] + RandomReal[.1, 100]}]];
lp = ListPlot[data, PlotRange -> All]nbasis[n_Integer][x_] := Table[x ^ k, {k, 0, n}]nbasis[10][x]Table[
fit = Fit[data, nbasis[n][x], x];
Show[Plot[fit, {x, 0, 100}, PlotRange -> All], lp, PlotRange -> {-.25, 1}, PlotLabel -> "n = " <> ToString[n]],
{n, 10, 40, 10}]Fit[data, nbasis[10][x], x]基底をスケールされシフトされた値で与えると問題の助けになる:
Table[
fit = Fit[data, nbasis[n][(x - 50) / 50], x];
Show[Plot[fit, {x, 0, 100}, PlotRange -> All], lp, PlotRange -> {-.25, 1}, PlotLabel -> "n = " <> ToString[n]],
{n, 10, 40, 10}]k = 7;
n = 1000;
signal = Normal[SparseArray[RandomInteger[{1, n}, k] -> RandomReal[{-1, 1}, k], {n}]];
ListPlot[signal, Filling -> Axis]長さ
のデータだけが送信されるように,信号に
の
行列を掛けてセンシングデータを圧縮する.
が十分に大きければ,独立かつ一様に分布した正規項を持つランダム行列は,制約された等尺性を満足し,もとの信号を非常に高い確率で回復することができる:
m = Ceiling[6 k Log[n / k]]a = RandomVariate[NormalDistribution[0, 1 / m], {m, n}];samples = a.signal;再構築は,
の可能なすべての解について,
を最小にすることで行われる:
AbsoluteTiming[reconstructed = NArgMin[{Norm[x, 1], a.x == samples}, x];]Max[Abs[reconstructed - signal]]最小化が線形最適化で解かれたとしても,すべての制約条件が等式制約なので,その過程は比較的遅い.基底追跡(L1型正則化 )を使うと,はるかに速く解を求めることができる:
AbsoluteTiming[bpsol = Fit[{a, samples}, FitRegularization -> {"L1", 0.0001}]]次は基底要素を与える.最適解を求めたければ,これらの成分に対応する線形方程式を解くとよい:
basisPositions = bpsol["NonzeroPositions"];
values = LinearSolve[a[[All, Flatten[basisPositions]]], samples];
bpReconstructed = SparseArray[basisPositions -> values, {n}]Max[Abs[bpReconstructed - signal]]テクニカルノート
関連するガイド
-
▪
- 曲線のフィットと近似の関数 ▪
- データの変換と平滑化 ▪
- 行列ベースの最小化 ▪
- 数値データ
履歴
1988 で導入 (1.0) | 2019 で更新 (12.0)
テキスト
Wolfram Research (1988), Fit, Wolfram言語関数, https://reference.wolfram.com/language/ref/Fit.html (2019年に更新).
CMS
Wolfram Language. 1988. "Fit." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/Fit.html.
APA
Wolfram Language. (1988). Fit. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Fit.html
BibTeX
@misc{reference.wolfram_2026_fit, author="Wolfram Research", title="{Fit}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/Fit.html}", note=[Accessed: 11-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_fit, organization={Wolfram Research}, title={Fit}, year={2019}, url={https://reference.wolfram.com/language/ref/Fit.html}, note=[Accessed: 11-November-2026]}