- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我在理解这个 KNN 分类器的标准化过程时遇到了一些困难。基本上我需要知道标准化过程中发生了什么。如果有人可以提供帮助,我们将不胜感激。我知道“训练示例”中存在平均值和标准差的变量,但之后实际发生的情况正是我遇到的困难。
classdef myknn
methods(Static)
%the function m calls the train examples, train labels
%and the no. of nearest neighbours.
function m = fit(train_examples, train_labels, k)
% start of standardisation process
m.mean = mean(train_examples{:,:}); %mean variable
m.std = std(train_examples{:,:}); %standard deviation variable
for i=1:size(train_examples,1)
train_examples{i,:} = train_examples{i,:} - m.mean;
train_examples{i,:} = train_examples{i,:} ./ m.std;
end
% end of standardisation process
m.train_examples = train_examples;
m.train_labels = train_labels;
m.k = k;
end
function predictions = predict(m, test_examples)
predictions = categorical;
for i=1:size(test_examples,1)
fprintf('classifying example example %i/%i\n', i, size(test_examples,1));
this_test_example = test_examples{i,:};
% start of standardisation process
this_test_example = this_test_example - m.mean;
this_test_example = this_test_example ./ m.std;
% end of standardisation process
this_prediction = myknn.predict_one(m, this_test_example);
predictions(end+1) = this_prediction;
end
end
function prediction = predict_one(m, this_test_example)
distances = myknn.calculate_distances(m, this_test_example);
neighbour_indices = myknn.find_nn_indices(m, distances);
prediction = myknn.make_prediction(m, neighbour_indices);
end
function distances = calculate_distances(m, this_test_example)
distances = [];
for i=1:size(m.train_examples,1)
this_training_example = m.train_examples{i,:};
this_distance = myknn.calculate_distance(this_training_example, this_test_example);
distances(end+1) = this_distance;
end
end
function distance = calculate_distance(p, q)
differences = q - p;
squares = differences .^ 2;
total = sum(squares);
distance = sqrt(total);
end
function neighbour_indices = find_nn_indices(m, distances)
[sorted, indices] = sort(distances);
neighbour_indices = indices(1:m.k);
end
function prediction = make_prediction(m, neighbour_indices)
neighbour_labels = m.train_labels(neighbour_indices);
prediction = mode(neighbour_labels);
end
end
结束
最佳答案
标准化是对训练示例中的每个特征进行标准化的过程,以便每个特征的均值为零,标准差为一。执行此操作的过程是找到每个特征的平均值和每个特征的标准差。之后,我们获取每个特征并减去其相应的平均值,然后除以其相应的标准差。
通过这段代码可以清楚地看出:
m.mean = mean(train_examples{:,:}); %mean variable
m.std = std(train_examples{:,:}); %standard deviation variable
for i=1:size(train_examples,1)
train_examples{i,:} = train_examples{i,:} - m.mean;
train_examples{i,:} = train_examples{i,:} ./ m.std;
end
m.mean
记住每个特征的平均值,而 m.std
记住每个特征的标准差。请注意,当您想要在测试时执行分类时,您必须记住这两点。这可以通过您拥有的预测
方法看出,该方法获取测试特征并从训练示例中减去每个特征的平均值和标准差。
function predictions = predict(m, test_examples)
predictions = categorical;
for i=1:size(test_examples,1)
fprintf('classifying example example %i/%i\n', i, size(test_examples,1));
this_test_example = test_examples{i,:};
% start of standardisation process
this_test_example = this_test_example - m.mean;
this_test_example = this_test_example ./ m.std;
% end of standardisation process
this_prediction = myknn.predict_one(m, this_test_example);
predictions(end+1) = this_prediction;
end
请注意,我们在测试示例中使用 m.mean
和 m.std
,这些数量来自训练示例。
我关于标准化的帖子应该提供更多背景信息。此外,它实现了与您提供的代码相同的效果,但以更加矢量化的方式:How does this code for standardizing data work?
关于matlab - 了解KNN的标准化过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54151580/
library(ISLR) standardized.X=scale(Caravan [,-86]) test =1:1000 train.X=standardized.X[-test ,] test
这可能是一个愚蠢的问题,但我只是想知道在 scikit.ml 中实现的 ML-KNN 与 scikit-learn 的 KNeighborsClassifier 之间的区别是什么。根据sklearn'
我担心我的预测与测试的准确性,这完全有意义。 X_train , X_test, y_train ,y_test =train_test_split(iris_dataset['data'], iri
我已经开始在 python tensorflow 库上使用 K-Nearest-Neighbors 方法开发一个机器学习项目。我没有使用tensorflow工具的经验,所以我在github上找到了一些
大侠幸会,在下全网同名「算法金」 0 基础转 AI 上岸,多个算法赛 Top 「日更万日,让更多人享受智能乐趣」 KNN算法的工作原理简单直观,易于理解和实现,这使得它在各种应用场景
我试图在 R 中使用 knn(使用了几个包( knnflex , class ))来预测基于 8 个变量的违约概率。数据集大约有 100k 行 8 列,但我的机器似乎很难处理 10k 行的样本。在数据
我有一个 60.000 obs/40 变量数据集,我在其中使用了 Clara,主要是由于内存限制。 library(cluster) library(dplyr) mutate(kddne
我尝试运行这条线: knn(mydades.training[,-7],mydades.test[,-7],mydades.training[,7],k=5) 但我总是收到这个错误: Error in
我正在尝试使用 python 实现 k-近邻算法。我最终得到了以下代码。但是,我正在努力寻找最近邻居项目的索引。以下函数将返回距离矩阵。但是,我需要在features_train(算法的输入矩阵)中获
我正在使用Kobe Bryant Dataset 。我希望用 KnnRegressor 预测 shot_made_flag。 我使用game_date来提取year和month特征: # covert
在 kNN classifier 的文档中,有一个方法kneighbors ,返回 k 个最近邻居。我感兴趣的是如何优雅地返回此类分类器中的 k 个最远邻居? 最佳答案 不,没有这样的能力。 您需要记
我想优化 KNN。关于SVM、RF和XGboost的内容有很多;但对于 KNN 来说很少。 据我所知,邻居的数量是一个需要调整的参数。 但是还有哪些参数需要测试呢?有什么好的文章吗? 谢谢 最佳答案
我正在尝试使用具有两列的数据集进行词袋问题 - 摘要和解决方案。我正在使用 KNN。训练数据集有 91 列,测试数据集有 15 列。 为了生成向量,我使用以下代码。 vectorizer = Coun
我了解 k 最近邻 (KNN) 的工作原理,但我不熟悉“软投票”一词。与 KNN 相关的软投票是什么?它与标准 KNN 投票相比如何工作? 比较两种投票方案的简单示例会很有用,并且指向 Matlab
我正在尝试将 KNN 应用到 tips 数据集 并将对象映射如下: f.Male=df.Gender.map({'Female':0,'Male':1}) df.Smokes = df.Smoker.
在下面的代码(最后一行)中,根据文档使用了 X_test 和 y_test: Returns the mean accuracy on the given test data and label 问题
我有约 65 个特征、450k 个观察值和不平衡的分类响应变量 Y(约 5% 真实,2 个状态)的 df。这已通过 train_test_split 分为 {Xtrain, ytrain} (10%)
为什么在 KNN 中需要规范化?我知道这个过程标准化了所有特征对结果的影响,但是在标准化之前到特定点 V 的“K”最近点将与到该特定点的“K”最近点完全相同归一化后的 V。那么归一化对欧氏距离有何影响
我目前正在为我的人工智能考试做一个机器学习项目。目标是使用 WEKA 正确选择两种分类算法进行比较,请记住,这两种算法必须有足够的不同才能进行比较。此外,算法必须同时处理标称数据和数字数据(我想这是进
进行数据挖掘时,什么时候应该选择其中一种算法而不是另一种?有具体原因吗?另外,其中哪一个是最有效的? 我将给出一个表格作为示例。 最佳答案 一种选择方法是尝试所有这些并选择最好的。 如果我要尝试构建数
我是一名优秀的程序员,十分优秀!