我在weka上使用k最近邻分类器(http://weka.sourceforge.net/doc.dev/weka/classifiers/lazy/IBk.html).
我想欧几里德距离是默认的距离函数.我如何更改该功能并使用与我自己的距离函数相同的类?
谢谢,
马尔科
我正在尝试使用knn创建一个简单的推荐系统.
可以说我有一张桌子:
User | Book1 | Book2 | Book3 | Book4 | Book5 | Book6 | Book7 |
1 | 5 | ? | 3 | ? | 4 | 3 | 2 |
2 | 3 | 4 | ? | 2 | 3 | 4 | 2 |
3 | 4 | 2 | 1 | ? | ? | 3 | 3 |
4 | 2 | 5 | 3 | ? | 4 | 1 | …Run Code Online (Sandbox Code Playgroud) 我有关于在matlab中计算分类器的精度和召回率的问题.我使用fisherIris数据(由150个数据点,50个setosa,50个versicolor,50个virginica组成).我使用kNN算法进行了分类.这是我的困惑矩阵:
50 0 0
0 48 2
0 4 46
Run Code Online (Sandbox Code Playgroud)
正确的分类率是96%(144/150),但如何用matlab计算精度和召回率,有什么功能吗?我知道该精度的公式= tp /(tp + fp),并且召回= tp /(tp + fn),但我在识别组件时丢失了.例如,我可以说矩阵的真阳性是144吗?假阳性和假阴性怎么样?请帮忙!!!我真的很感激!谢谢!
我有7个类需要分类,我有10个功能.在这种情况下我是否需要使用k的最佳值,或者我必须运行KNN以获得介于1和10(大约10)之间的k值,并在算法本身的帮助下确定最佳值?
我正在使用 KNeighborsRegressor,但我想将它与自定义距离函数一起使用。我的训练集是 Pandas DataFrame,它看起来像:
week_day hour minute temp humidity
0 1 9 0 1
1 1 9 0 1
2 1 9 0 1
3 1 9 0 1
4 1 9 1 1
...
def customDistance(a, b):
print a, b
return np.sum((a-b)**2)
dt = DistanceMetric.get_metric("pyfunc", func=customDistance)
knn_regression = KNeighborsRegressor(n_neighbors=15, metric='pyfunc', metric_params={"func": customDistance})
knn_regression.fit(trainSetFeatures, trainSetResults)
Run Code Online (Sandbox Code Playgroud)
我还尝试直接从 KNeighborsRegressor 构造函数调用 customDistance ,例如:
knn_regression = KNeighborsRegressor(n_neighbors=15, metric=customDistance)
Run Code Online (Sandbox Code Playgroud)
函数执行的两种方式,但结果有点奇怪。首先,我希望从我的 DataFrame 中看到函数输入 A 和 B 行,但我得到的是:
[0.87716989 11.46944914 1.00018801 1.10616031 1.] [ 1. 9. 0. …Run Code Online (Sandbox Code Playgroud) I have a np array, X that is size 1000 x 1000 where each element is a real number. I want to find the 5 closest points for every point in each row of this np array. Here the distance metric can just be abs(x-y). I have tried to do
for i in range(X.shape[0]):
knn = NearestNeighbors(n_neighbors=5)
knn.fit(X[i])
for j in range(X.shape[1])
d = knn.kneighbors(X[i,j], return_distance=False)
Run Code Online (Sandbox Code Playgroud)
However, this does not work for me and I am not sure how efficient …
我正在使用 Scikit learn 进行 K 最近邻分类:
from sklearn.neighbors import KNeighborsClassifier
model=KNeighborsClassifier()
model.fit(train_input,train_labels)
Run Code Online (Sandbox Code Playgroud)
如果我打印我的数据:
print("train_input:")
print(train_input.iloc[0])
print("\n")
print("train_labels:")
print(train_labels.iloc[0])
Run Code Online (Sandbox Code Playgroud)
我明白了:
train_input:
PassengerId 1
Pclass 3
Name Braund, Mr. Owen Harris
Sex male
Age 22
SibSp 1
Parch 0
Ticket A/5 21171
Fare 7.25
Cabin NaN
Embarked S
Name: 0, dtype: object
train_labels:
0
Run Code Online (Sandbox Code Playgroud)
代码失败并出现以下错误:
ValueError Traceback (most recent call last)
<ipython-input-21-1f18eec1e602> in <module>()
63
64 model=KNeighborsClassifier()
---> 65 model.fit(train_input,train_labels)
ValueError: could not convert string to float: 'Q'
Run Code Online (Sandbox Code Playgroud)
那么,KNN 算法不适用于String值吗? …
输入:
\n\n\xe2\x80\xa2 N 个点{P1,\xe2\x80\xa6。, Pn} - 每个点都来自同一维度 t:
\n\n\xe2\x80\xa2 距离函数 \xe2\x80\x93 dist(Pi, Pj) - 返回一个数字,即点之间的距离。(该函数是自定义函数 \xe2\x80\x93 不是标准 Minkowski 距离)。
\n\n问题:
\n\n\xe2\x80\xa2 主要问题:
\n\n\xe2\x80\xa2 第二个问题:
\n\n\xe2\x80\xa2 很高兴拥有:
\n\n相关数据结构:
\n\n\xe2\x80\xa2 KD-树
\n\nspatial spatial-query multidimensional-array knn spatial-index
给定一个数据点,我需要生成 K 个最近的邻居。我阅读了sklearn 的 sklearn.neighbors 模块,但它在两组数据之间生成了邻居。我想要的可能是最接近传递的数据点的 100 个数据点的列表。
无论如何,任何 KNN 算法都应该在幕后找到这些 K 个数据点。有什么办法可以将这些 K 点作为输出返回?
这是我的示例笔记本。
我正在构建一个 KNN 模型来预测房价。我将检查我的数据和我的模型,然后是我的问题。
数据 -
# A tibble: 81,334 x 4
latitude longitude close_date close_price
<dbl> <dbl> <dttm> <dbl>
1 36.4 -98.7 2014-08-05 06:34:00 147504.
2 36.6 -97.9 2014-08-12 23:48:00 137401.
3 36.6 -97.9 2014-08-09 04:00:40 239105.
Run Code Online (Sandbox Code Playgroud)
模型 -
library(caret)
training.samples <- data$close_price %>%
createDataPartition(p = 0.8, list = FALSE)
train.data <- data[training.samples, ]
test.data <- data[-training.samples, ]
model <- train(
close_price~ ., data = train.data, method = "knn",
trControl = trainControl("cv", number = 10),
preProcess = c("center", "scale"),
tuneLength …Run Code Online (Sandbox Code Playgroud)