# K近邻算法的python实现

## 1. K近邻算法的实现

```            ```

def

knn

(

x

,

dataSet

,

labels

,

k

)

:

distanceMemories

=

{

}

# 利用字典来记录距离

for

i

in

range

(

len

(

dataSet

)

)

:

distance

=

euDis

(

x

,

dataSet

[

i

]

)

distanceMemories

[

i

]

=

distance
sortResult

=

sorted

(

distanceMemories

.

items

(

)

,

key

=

lambda

x

:

x

[

1

]

)

distance_min_k

=

sortResult

[

:

k

]

classCount

=

{

}

# 用来记录前k个中各个类出现的次数

for

i

in

range

(

len

(

distance_min_k

)

)

:

if

labels

[

distance_min_k

[

i

]

[

0

]

]

not

in

classCount

:

classCount

[

labels

[

distance_min_k

[

i

]

[

0

]

]

]

=

0

classCount

[

labels

[

distance_min_k

[

i

]

[

0

]

]

]

+=

1

result

=

sorted

(

classCount

.

items

(

)

,

key

=

lambda

x

:

x

[

1

]

,

reverse

=

True

)

# 对统计结果，按照字典中的各个值，进行降序排序

return

result

[

0

]

[

0

]

def

euDis

(

x

,

y

)

:

# 欧式距离的计算

dim

=

len

(

x

)

temp

=

0

for

i

in

range

(

dim

)

:

temp

+=

(

x

[

i

]

-

y

[

i

]

)

**

2

return

temp

**

0.5

dataSet

=

[

[

3

,

104

]

,

[

2

,

100

]

,

[

1

,

81

]

,

[

101

,

10

]

,

[

99

,

5

]

,

[

98

,

2

]

]

# 这是机器学习实战一书上的小例子

labels

=

[

"爱情片"

,

"爱情片"

,

"爱情片"

,

"动作片"

,

"动作片"

,

"动作片"

]

print

(

knn

(

[

18

,

90

]

,

dataSet

,

labels

,

3

)

)

# 输出结果：爱情片

```
```

QQ号联系： 360901061

【本文对您有帮助就好】