楼主: oliyiyi
1998 8

KNN(示例代码 python 和 R) [推广有奖]

版主

泰斗

0%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
271951 个
通用积分
31269.3519
学术水平
1435 点
热心指数
1554 点
信用等级
1345 点
经验
383775 点
帖子
9598
精华
66
在线时间
5468 小时
注册时间
2007-5-21
最后登录
2024-4-18

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

KNN (K- Nearest Neighbors)

It can be used for both classification and regression problems. However, it is more widely used in classification problems in the industry. K nearest neighbors is a simple algorithm that stores all available cases and classifies new cases by a majority vote of its k neighbors. The case being assigned to the class is most common amongst its K nearest neighbors measured by a distance function.

These distance functions can be Euclidean, Manhattan, Minkowski and Hamming distance. First three functions are used for continuous function and fourth one (Hamming) for categorical variables. If K = 1, then the case is simply assigned to the class of its nearest neighbor. At times, choosing K turns out to be a challenge while performing KNN modeling.

More: Introduction to k-nearest neighbors : Simplified.

KNN can easily be mapped to our real lives. If you want to learn about a person, of whom you have no information, you might like to find out about his close friends and the circles he moves in and gain access to his/her information!

Things to consider before selecting KNN:

  • KNN is computationally expensive
  • Variables should be normalized else higher range variables can bias it
  • Works on pre-processing stage more before going for KNN like outlier, noise removal
Python Code
  1. #Import Library
  2. from sklearn.neighbors import KNeighborsClassifier
  3. #Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
  4. # Create KNeighbors classifier object model
  5. KNeighborsClassifier(n_neighbors=6) # default value for n_neighbors is 5
  6. # Train the model using the training sets and check score
  7. model.fit(X, y)
  8. #Predict Output
  9. predicted= model.predict(x_test)
复制代码

R Codelibrary(knn)

  1. x <- cbind(x_train,y_train)
  2. # Fitting model
  3. fit <-knn(y_train ~ ., data = x,k=5)
  4. summary(fit)
  5. #Predict Output
  6. predicted= predict(fit,x_test)
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python knn introduction information Computation

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html
沙发
hyq2003 发表于 2017-9-12 16:07:01 |只看作者 |坛友微信交流群

使用道具

藤椅
ztzxx 学生认证  发表于 2017-9-12 16:08:03 |只看作者 |坛友微信交流群
学习学习

使用道具

板凳
钱学森64 发表于 2017-9-12 16:45:45 |只看作者 |坛友微信交流群
谢谢分享

使用道具

报纸
MouJack007 发表于 2017-9-12 19:29:58 |只看作者 |坛友微信交流群
谢谢楼主分享!

使用道具

地板
MouJack007 发表于 2017-9-12 19:31:08 |只看作者 |坛友微信交流群

使用道具

7
ekscheng 发表于 2017-9-12 23:31:19 |只看作者 |坛友微信交流群

使用道具

8
minixi 发表于 2017-9-13 10:54:59 |只看作者 |坛友微信交流群
谢谢分享

使用道具

9
piiroja 发表于 2020-9-20 11:30:19 |只看作者 |坛友微信交流群
thx for sharing~

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-20 08:54