"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"]