# Questions tagged [k-nn]

K-Nearest Neighbor (K-NN) is a classification algorithm that determines the label of some data point based on the most common label of the closest k other points.

153 questions
Filter by
Sorted by
Tagged with
131 views

### Mapping of categorical features into binary indicator features

I am currently reading an introductory machine learning book by Daumé (ch. 03, p. 30) and when discussing the mapping of categorical features with "n" possible values into "n" binary indicator ...
28 views

### Does it make sense to use KNN to target ticket buyers for arts events?

I work for a medium-sized non-profit. We have a database with approximately 40,000 profiles of folks (mostly patrons). For each show a patron attends, we add an attribute that indicated that they're ...
7k views

### How to save a knn model?

I need to save the results of a fit of the SKlearn NearestNeighbors model: knn = NearestNeighbors(10) knn.fit(my_data) How do you save to disk the traied ...
1k views

### Coordinate System's influence on $L$ distances (Manhattan and Euclidean)

I don't understand this picture, which says if we change the coordinate system, we would have the same result for $L_2$ distance, whereas, our result would differ for $L_1$ distance. What does it ...
337 views

### K-Nearest Neighbours algorithm explanation needed

I need some explanation for K-Nearest Neighbors algorithm. Why is the training process needed in KNN algorithm? In regression models the training process means to find optimum parameters for a ...
89 views

### Normalization(minmax) gives me worse results than before in KNN, follow up actions?

Hello I'm studying a classification problem with KNN right now. I have many numeric features that I normalized with MinMaxScaler, I also got some OHE categorical features that not seem to cause the ...
122 views

### Can k=1 be a good choice for K neighbors classification?

Running sklearn.KNeighborsClassifier() on Kaggle's Leaf Classification sample (set of 99 species, 10 specimen each), with defaults kNN parameters and a grid search ...
792 views

### K Nearest Neighbour with different distance matrix to each datapoint

I'm wondering if there is library support in python (such as sklearn) for doing KNN on a data set that has a custom distance matrix (positive definite) for each data point (x is a query point, $x_i$ ...
116 views

### Is there an algorithm that imputes missing values based on n nearest columns? (KNN hybrid)

I have a dataset of 70 columns that have missing values. Each column has a few columns (3-5) that it is significantly more correlated than the others but each column's correlated columns are very ...
503 views

### Choosing k value in KNN classifier?

I'm working on classification problem and decided to use KNN classifier for the problem. so if k=131 gave me auc of 0.689 and k=71 gave me auc of 0.682 what should be my ideal k? Does choosing ...
58 views

### Date transformation for KNN to get distance [duplicate]

I have data set with date features like 01/01/2019 and I would like to use KNN. However, I cannot find a good transformation for dates that has a meaningful distance result for the last feature. For ...
146 views

### Date transformation for KNN

I have data set with date features like 01/01/2019 and I would like to use KNN. However, I cannot find a good transformation for dates that has a meaningful ...
927 views

### How to decide how many n_neighbors to consider while implementing LocalOutlierFactor?

I have a data set with rows: 134000 and columns: 200. I am trying to identify the outliers in data set using LocalOutlierFactor from scikit-learn. Although I ...
477 views

### Training anomaly detection on text string [closed]

I have text string stored in a column, resulting after pre-processing of web traffic data. Now I want to apply anomaly detection on it and for that purpose I applied ...
2k views

### Scaling label encoded values for Linear Algorithms

I have encoded categorical variables to numerical values. As we know that for feeding values to Linear Algorithms like SVM or KNN, we scale the values for columns having large variations. I have ...
179 views

### A metric between trees

I have certain tree structures. I am not an expert in machine learning. As I would with take KNN, I would calculate distances via metric function and a new data point and the points from the training ...
902 views

### Why do I not get 100% Accuracy with KNN with $K=1$

I am playing with KNN on the Iris Dataset I expected to get 100% accuracy with $K=1$ since every point should predict itself based on the Voronoi volume around it created by the KNN algorithm. ...
3k views

### Regression for discrete values?

Im a noob in ml / statistical algorithm, but I do have worked with simple classifiers and regression I like some opinions if I am going the right way, given my limited knowledge My problem is ...
366 views

### ValueError: operands could not be broadcast together with shapes (140,) (10230,)

I wrote this code for emotion recognition using EEG data. I am performing feature extraction by finding mean and standard deviation and performing classification using Knn algorithm. I am getting this ...
995 views

### How to handle date data for Knn?

I'm working on a project about predicting kickstarter project success(classification) and my dataset has many columns that could be used as features such as : state_changed_at, launched_at, ...
8k views

### How Does Weighted KNN Work?

I am reading notes on using weights for KNN and I came across an example that I don't really understand. Suppose we have K = 7 and we obtain the following: Decision set = {A, A, A, A, B, B, B} If ...
10k views

### Features selection in KNN

I have a naive question about using the K Nearest Neighbor algorithm: is feature selection more important in KNN than in other algorithms? If a particular feature is not predictive in a neural ...
704 views

### How can I implement tangent distance for k-nearest neighbor in python/scikit-learn?

My ultimate aim is to have a function which I can feed into scikit-learn's NearestNeighbor class as a custom metric parameter. ...
1k views

### How does KNN work if there are duplicates?

I am currently debating with my friend about how KNN handles duplicates. Suppose K = 2, and we have a 1-dimensional set of data points to illustrate my dilemma I = {1, 2, 2, 2, 2, 2, 6} Thus is it ...
295 views

### Is there any way of ordering/sorting vectors?

I am working on KD-Tree for nearest neighbor algorithm, where at each level of the tree we arbitrarily choose a dimension to cut upon and sort the points based on that chosen dimension value, after ...
34 views

### Best way to find dissimilarity in a 6x2 DataFrame?

I'm new to data science and am currently learning different techniques that I can do with Python. Currently, I'm trying it out with Spotify's API for my own playlists. The goal is to find the most ...
91 views

### Is there an upper bound for k in nearest neighbors-based methods?

When applying a nearest neighbors-based method to a data of, for instance, 2000 points, what is the largest number of neighbors that can be considered ? I am using a nearest neighbors method in an ...
76 views

### Creating a single number from a numpy array - Python [closed]

I am working on a gender classification project. I am extracting the pixels of an image using a Numpy array in Python, similar to the one below: ...
885 views

### Problem with calculating error rate for KNN

I am trying to validate the accuracy of my KNN algorithm for the movie rating prediction. I have $2$ vectors: $Y$ - with the real ratings, $Y'$ - with predicted ones. When I calculate Standard ...
83 views

### Why isn't local averaging (including KNN) used often for regression?

My professor said that the "holy grail of regression" is the function E(Y|X=x) i.e. the conditional expectation of Y on X. In practice, you'd take a small window of X and take the average value of Y ...
70 views

### How to decide the shape of input features, when each data file is of different length?

To help me understand the benefits and shortcomings of decision trees, KNN, Neural Networks, ...
179 views

### Taking Neural Network's false positives as the recommendation system result?

I am creating a recommendation system and considering two parallel ways of formalizing the problem. One classical, using proximity (recommend the product to the customer if a majority vote of 2k+1 ...
66 views

### How to determine the k in kNN [duplicate]

I'm looking to use the k-Nearest Neighbors (kNN) algorithm. What are the possible methods for determining the best K? From what I have read, looking at many different values(say 10-100) should work, ...
241 views

### Is K-NN applicable for binary variables?

I need help because I'm just new to machine learning and I do not know if k-nearest neighbors algorithm can be used to identify the appropriate program(s) for Student 11 in the table below. The ...
369 views

### scalable tools to build kNN graph over sparse data

I'm looking for scalable tools to build kNN graph over sparse data points. The dimension and number of data points can be both up to millions. What I have tried already: sklearn.neighbors....
133 views

### Python-SQL database [closed]

There are two SQL databases in which one database contains user data and other database contains service data. Firstly, I am connecting the databases and fetching it on python.Based on the user data, ...
8k views

### Sklearn: unsupervised knn vs k-means

Sklearn has an unsupervised version of knn and also it provides an implementation of k-means. If I am right, kmeans is done exactly by identifying "neighbors" (at ...
4k views

### sklearn.neighbors.NearestNeighbors - knn for unsupervised learning?

From basic theory I know that knn is a supervised algorithm while for example k-means is an unsupervised algorithm. However, at Sklearn there are is an implementation of KNN for unsupervised learning ...
124 views

### advice on distance metric for knn w/image recognition

I'm getting my feet wet with machine learning and am implementing a knn algorithm on a dataset that i've created. I've created a set of images of circles and squares and want the knn algorithm to ...
870 views

### How can I apply PCA to KNN?

I want to know the do not want to how to use library I will denote a $n\times p$ data matrix by $X$, where $n<p$. That is, each row of $X$ is one sample data with $p$ feature variables. By using ...
12k views

### How to calculate the silhouette coefficient?

Calculate the silhouette coefficient of point Pi from the above image. To apply the given formula, how to know which is a(i) and b(i)?
2k views

### Knn and euclidean distance

I'm studying the knn classification algorithm. Why can the euclidean distance be considered a nice measure of affinity between examples ? In one dimension (1 attribute) this seems correct, but if I ...
47 views

### How to weigh feature array

I have a feature array of around 4000 elements, extracted from one source. On this array I've extracted 7 more feature from other source and now I basically have a 4007 feature array from each data ...
63 views

### How to get nearest 5 points mean with Nearest Neighbours?

In a dataset of longitude, latitude and price (of houses) I'm using sklearn's KNearestRegressor to get the 5 nearest neighbors mean price for each point. The problem is I want to do this for the whole ...
76 views

### Question about Knn and split validation

I have a big database with 40k recors and 2 classification classes. In this big database the 76% of records belong to the first class. I've used a 70-30 split partition with stratified sampling, and ...
495 views

### k-Nearest Neighbours with time series data - how to obtain whole-time-period estimators

I have a large dataset for the activities performed by multiple staff in a factory over a long period of time - 01/01/2017 - present. The activities performed by the different staff are recorded at ...
5k views

### Evaluation metrics for Decision Tree regressor and KNN regressor

I have started working on the Decision Tree Regressor and KNN Regressor. I have built the model and not sure what are the metrics needs to be considered for evaluation. As of now I have considered ...
8k views

### Why is cross-validation score so low?

I am using Scikit-Learn for this classification problem. The dataset has 3 features and 600 data points with labels. First I used Nearest Neighbor classifier. ...