[R] KNN

Ben Tupper btupper at bigelow.org
Thu Feb 25 16:32:14 CET 2016


Hi,

Do the knn() or knn1() functions in the 'class' package serve your purpose?

https://cran.r-project.org/web/packages/class/index.html

Ben
 
> On Feb 25, 2016, at 9:49 AM, David L Carlson <dcarlson at tamu.edu> wrote:
> 
> Perhaps Alnazer is trying to implement "majority vote" kNN:
> 
>> From Wikipedia
> (https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm):
> 
> In k-NN classification, the output is a class membership. An object is classified by a majority vote of its neighbors, with the object being assigned to the class most common among its k nearest neighbors (k is a positive integer, typically small). If k = 1, then the object is simply assigned to the class of that single nearest neighbor.
> 
> But as Jim said, your function does not do this. It does not even run kNN. 
> 
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
> 
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Lemon
> Sent: Thursday, February 25, 2016 4:15 AM
> To: Alnazer Elbedairy
> Cc: r-help mailing list
> Subject: Re: [R] KNN
> 
> Hi Alnazer,
> I'm not surprised that it didn't do what you expected. Even if I clean
> up the code so that it will actually run:
> 
> majorityGuessing<-function(trainingData,categories) {
> GuessMPG<-sample(1:length(categories),nrow(trainingData),replace=TRUE)
> return(GuessMPG)
> }
> 
> and call it like this (assuming that you are trying to do something
> like guessing MPG from the number of cylinders):
> 
> auto<-read.csv("auto.csv")
> majorityGuessing(auto$MPG,unique(auto$CYLINDERS))
> 
> the result is just a sample of 398 integers ranging from 1 to 5, which
> is not even a guess. Unfortunately, I can't work out what metric you
> want to select "nearest neighbors", but perhaps someone else can.
> 
> Jim
> 
> On Thu, Feb 25, 2016 at 5:30 PM, Alnazer Elbedairy
> <alnazer.elbedairy at gmail.com> wrote:
>> Dear Jim
>> thanks you for your kind help.
>> KNN - is K- Nearest Neighbor, is a technique used in Machine Learning.
>> attached you will find a CSV file dataset, my question is :
>> use the attached Dataset, Use majority guessing technique to evaluate KNN ?
>> this is the solution I came up with, but I didn't work :-
>> majorityGuessing <- function(trainingData,categories)
>> {GuessMPG <- sample(1:length (categories-1, nrow(testingData),replace=T)
>> return(GuessMPG)
>> 
>> 
>> 
>> On Wed, Feb 24, 2016 at 8:18 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
>>> 
>>> Hi Alnazar,
>>> I looked at your question yesterday and was unable to find what a
>>> "majority guessing" function is. I think it may be related to the
>>> "Pandemonium" model of decision making, but that doesn't get me very
>>> far. Could you give us a hint as to what this function is?
>>> 
>>> 
>> 
>> On Wed, Feb 24, 2016 at 8:18 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
>>> 
>>> Hi Alnazar,
>>> I looked at your question yesterday and was unable to find what a
>>> "majority guessing" function is. I think it may be related to the
>>> "Pandemonium" model of decision making, but that doesn't get me very
>>> far. Could you give us a hint as to what this function is?
>>> 
>>> Jim
>>> 
>>> 
>>> On Wed, Feb 24, 2016 at 7:02 AM, Alnazer <alnazer.elbedairy at gmail.com>
>>> wrote:
>>>> How I can use majority guessing function to evaluate KNN, if I have data
>>>> saved in CSV file
>>>> 
>>>> Alnazer Elbedairy
>>>> 
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>> 
>> 
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org



More information about the R-help mailing list