"NearestNeighbors" (機械学習メソッド)
詳細とサブオプション
例題
すべて開く すべて閉じる例 (2)
c = Classify[{1, 2, 3, 4} -> {1, 1, 2, 2}, Method -> "NearestNeighbors"]Information[c]c[1.3]data = Table[x -> x + RandomVariate[NormalDistribution[0, 2]], {x, RandomReal[{-10, 10}, 40]}];
ListPlot[List@@@data]p = Predict[data, Method -> "NearestNeighbors"]Show[Plot[{p[x],
p[x] + StandardDeviation[p[x, "Distribution"]], p[x] - StandardDeviation[p[x, "Distribution"]]},
{x, -2, 6},
PlotStyle -> {Blue, Gray, Gray},
Filling -> {2 -> {3}},
Exclusions -> False,
PerformanceGoal -> "Speed", PlotLegends -> {"Prediction", "Confidence Interval"}], ListPlot[List@@@data, PlotStyle -> Red, PlotLegends -> {"Data"}]]オプション (6)
"DistributionSmoothing" (2)
"DistributionSmoothing"サブオプションを使って分類器を訓練する:
Classify[{1.98, 3.83, 1.69, 0.04, 2.48, 1.66} -> {"a", "a", "b", "b", "a", "b"}, Method -> {"NearestNeighbors", "DistributionSmoothing" -> 2}]"DistributionSmoothing"の値を変えた不均衡なデータ集合で2つの分類器を訓練する:
data = {1 -> True, 2 -> True, 3 -> True, 4 -> True, 5 -> False, 6 -> True};c1 = Classify[data, Method -> {"NearestNeighbors", "DistributionSmoothing" -> .1}]c2 = Classify[data, Method -> {"NearestNeighbors", "DistributionSmoothing" -> 10}]c1[5, "Probabilities"]c2[5, "Probabilities"]"NearestMethod" (2)
特定の"NearestMethod"を使って分類器を訓練する:
Classify[{1.98, 3.83, 1.69, 0.04, 2.48, 1.66} -> {"a", "a", "b", "b", "a", "b"}, Method -> {"NearestNeighbors", "NearestMethod" -> "Scan"}]gaussian[μ_, σ_, n_] := RandomVariate[MultinormalDistribution[μ, {{σ, 0}, {0, σ}}], n];
positions = {{4, 2}, {-2, 2}, {0, -3}, {3, 0}};
sizes = {2, 1, 5, 0.5};
colors = {RGBColor[1, 0, 0], RGBColor[0, 0, 1], RGBColor[0, 1, 0], RGBColor[1., 0.77, 0.]};
nums = {10000, 10000, 50000, 20000};clusters = MapThread[gaussian, {positions, sizes, nums}];
trainigset = AssociationThread[colors, clusters];
plot = ListPlot[clusters, PlotStyle -> Darker[colors, 0.1], ImageSize -> 200, PlotRange -> {{-5, 5}, {-5, 5}}, Frame -> True, AspectRatio -> 1, PlotLabel -> "data"]異なるメソッドを使っていくつかの分類器を訓練し,訓練時間を比較する:
classifiers = Classify[trainigset, Method -> {"NearestNeighbors", "NearestMethod" -> #}]& /@ {"Octree", "KDtree", "Scan"};Information[#, "TrainingTime"]& /@ classifiers"NeighborsNumber" (2)
特定の"NeighborsNumber"を使って予測器関数を訓練する:
Predict[{1.98, 3.83, 1.69, 0.04, 2.48, 1.66} -> {-1.41, -0.71, -0.701, -0.4, -1.91, -1.6}, Method -> {"NearestNeighbors", "NeighborsNumber" -> 2}]trainingset = Table[x -> Sin[4x] + RandomReal[.4], {x, RandomReal[{0, 6}, 30]}];
ListPlot[List@@@trainingset]小さい"NeighborsNumber"を使って予測器を訓練する:
p2 = Predict[trainingset, Method -> {"NearestNeighbors", "NeighborsNumber" -> 2}];大きい"NeighborsNumber"を使って予測器を訓練する:
p10 = Predict[trainingset, Method -> {"NearestNeighbors", "NeighborsNumber" -> 10}];Plot[{p2[x], p10[x]}, {x, 0, 6}]