"LogisticRegression" (機械学習メソッド)
- Classifyのためのメソッドである.
- 特徴の線形結合のロジスティック関数でクラス確率をモデル化する.
詳細とサブオプション
- "LogisticRegression"は,数値的特徴
,
の線形結合で各クラスの対数確率をモデル化する.ただし,
はクラス k のパラメータに対応する.パラメータ行列の推定
は,損失関数
を最小化することで行われる. - 次は使用可能なオプションである.
-
"L1Regularization" 0 損失関数中の
の値"L2Regularization" Automatic 損失関数中の
の値"OptimizationMethod" Automatic 使用するメソッド - "OptimizationMethod"の可能な設定には次がある.
-
"LBFGS" 有限メモリのBroyden–Fletcher–Goldfarb–Shannoアルゴリズム "StochasticGradientDescent" 確率勾配メソッド "Newton" ニュートンメソッド
例題
すべて開く すべて閉じる例 (2)
c = Classify[{1, 2, 3, 4} -> {1, 1, 2, 2}, Method -> "LogisticRegression"]Information[c]c[1.3]sampledata[center_] := RandomVariate[MultinormalDistribution[center, IdentityMatrix[2]], 200];clusters = sampledata /@ {{1.5, 1}, {-1.5, 1}, {0, -3}};ListPlot[clusters, PlotStyle -> Darker@{Yellow, Blue, Green}]c = Classify[<|Yellow -> clusters[[1]], Blue -> clusters[[2]], Green -> clusters[[3]]|>, Method -> "LogisticRegression"]訓練集合と各クラスの確率分布を特徴の関数としてプロットする:
Show[
Plot3D[{
c[{x, y}, "Probability" -> Yellow],
c[{x, y}, "Probability" -> Blue],
c[{x, y}, "Probability" -> Green]},
{x, -4, 4}, {y, -5, 4},
Exclusions -> None], ListPointPlot3D[Map[Append[#, 1]&, clusters, {2}], PlotStyle -> {Yellow, Blue, Green}]]オプション (6)
"L1Regularization" (2)
"L1Regularization"オプションを使って分類器を訓練する:
c = Classify[<|1 -> {1, 23, 1.3, 4}, 2 -> {-4, -3.2, -4, -5}|>, Method -> {"LogisticRegression", "L1Regularization" -> 3}]colors = {RGBColor[1, 0, 0], RGBColor[0, 0, 1], RGBColor[0, 1, 0], RGBColor[1., 0.77, 0.]};
clusters = Table[RandomVariate[BinormalDistribution[
RandomReal[{-3, 3}, 2],
RandomReal[{0.5, 2}, 2],
RandomReal[{0.2, 0.8}]], RandomInteger[{30, 40}]], {4}];
plot = ListPlot[clusters, PlotStyle -> Darker[colors, 0.1], ImageSize -> 200, PlotRange -> {{-5, 5}, {-5, 5}}, Frame -> True, AspectRatio -> 1, PlotLabel -> "data"]line = Range[-5, 5, 0.25];
points = Tuples[line, 2];makecolormap[probs_] := Transpose @ Partition[
Map[Blend[Keys[#], Values[#]]&, probs],
Length[line]];"L1Regularization"の値を変えていくつかの分類器を訓練し,結果を比較する:
data = AssociationThread[colors, clusters];
Table[
ArrayPlot[
makecolormap @ Classify[data, points, "Probabilities", Method -> {"LogisticRegression", "L1Regularization" -> λ}],
PlotLabel -> ("L1Regularization" -> λ), DataReversed -> True, ImageSize -> 150],
{λ, {0, 3, 5, 10}}]~Multicolumn~2 ~Legended~plot"L2Regularization" (2)
"L2Regularization"オプションを使って分類器を訓練する:
c = Classify[<|1 -> {1, 23, 1.3, 4}, 2 -> {-4, -3.2, -4, -5}|>, Method -> {"LogisticRegression", "L2Regularization" -> 3}]colors = {RGBColor[1, 0, 0], RGBColor[0, 0, 1], RGBColor[0, 1, 0], RGBColor[1., 0.77, 0.]};
clusters = Table[RandomVariate[BinormalDistribution[
RandomReal[{-4, 3}, 2],
RandomReal[{0.5, 2}, 2],
RandomReal[{0.2, 0.8}]], RandomInteger[{30, 40}]], {4}];
plot = ListPlot[clusters, PlotStyle -> Darker[colors, 0.1], ImageSize -> 200, PlotRange -> {{-5, 5}, {-5, 5}}, Frame -> True, AspectRatio -> 1, PlotLabel -> "data"]line = Range[-5, 5, 0.25];
points = Tuples[line, 2];"L2Regularization"の値を変えていくつかの分類器を訓練し,結果を比較する:
makecolormap[probs_] := Transpose @ Partition[
Map[Blend[Keys[#], Values[#]]&, probs],
Length[line]];data = AssociationThread[colors, clusters];
Table[
ArrayPlot[
makecolormap @ Classify[data, points, "Probabilities", Method -> {"LogisticRegression", "L2Regularization" -> λ}],
PlotLabel -> ("L2Regularization" -> λ), DataReversed -> True, ImageSize -> 150],
{λ, {0, 3, 5, 10}}]~Multicolumn~2 ~Legended~plot"OptimizationMethod" (2)
特定の"OptimizationMethod"を使って分類器を訓練する:
c = Classify[<|1 -> {1, 23, 1.3, 4}, 2 -> {-4, -3.2, -4, -5}|>, Method -> {"LogisticRegression", "OptimizationMethod" -> "StochasticGradientDescent"}]trainingset = {[image] -> 2, [image] -> 5, [image] -> 8, [image] -> 0, [image] -> 2, [image] -> 7, [image] -> 5, [image] -> 1, [image] -> 3, [image] -> 0, [image] -> 3, [image] -> 9, [image] -> 6, [image] -> 2, [image] -> 8, [image] -> 2, [image] -> 0, [image] -> 6, [image] -> 6, [image] -> 1, [image] -> 1, [image] -> 7, [image] -> 8, [image] -> 5, [image] -> 0, [image] -> 4, [image] -> 7, [image] -> 6, [image] -> 0, [image] -> 2, [image] -> 5, [image] -> 3, [image] -> 1, [image] -> 5, [image] -> 6, [image] -> 7, [image] -> 5, [image] -> 4, [image] -> 1, [image] -> 9, [image] -> 3, [image] -> 6, [image] -> 8, [image] -> 0, [image] -> 9, [image] -> 3, [image] -> 0, [image] -> 3, [image] -> 7, [image] -> 4, [image] -> 4, [image] -> 3, [image] -> 8, [image] -> 0, [image] -> 4, [image] -> 1, [image] -> 3, [image] -> 7, [image] -> 6, [image] -> 4, [image] -> 7, [image] -> 2, [image] -> 7, [image] -> 2, [image] -> 5, [image] -> 2, [image] -> 0, [image] -> 9, [image] -> 8, [image] -> 9, [image] -> 8, [image] -> 1, [image] -> 6, [image] -> 4, [image] -> 8, [image] -> 5, [image] -> 8, [image] -> 0, [image] -> 6, [image] -> 7, [image] -> 4, [image] -> 5, [image] -> 8, [image] -> 4, [image] -> 3, [image] -> 1, [image] -> 5, [image] -> 1, [image] -> 9, [image] -> 9, [image] -> 9, [image] -> 2, [image] -> 4, [image] -> 7, [image] -> 3, [image] -> 1, [image] -> 9, [image] -> 2, [image] -> 9, [image] -> 6};logistic = Classify[trainingset, Method -> {"LogisticRegression", "OptimizationMethod" -> "Newton"}]"StochasticGradientDescent"メソッドを使って分類器を訓練する:
logistic2 = Classify[trainingset, Method -> {"LogisticRegression", "OptimizationMethod" -> "StochasticGradientDescent"}]Information[logistic, "TrainingTime"]
Information[logistic2, "TrainingTime"]