白手起家学习数据科学 ——k-Nearest Neighbors之“例子篇”(九)

来源:互联网 发布:cvr100身份证阅读软件 编辑:程序博客网 时间:2024/05/17 21:43

例子:最喜欢的编程语言(Example: Favorite Languages)

DataSciencester网站用户调查结果出来了,我们发现在许多大城市里人们所喜欢的编程语言如下:

# each entry is ([longitude, latitude], favorite_language)cities = [([-122.3 , 47.53], "Python"),  # Seattle          ([ -96.85, 32.85], "Java"),    # Austin          ([ -89.33, 43.13], "R"),       # Madison         ]

公司副总裁想要知道,在没有参加调查的地方,是否我们能使用这些结果预测最喜欢的编程语言。

像往常一样,第一个步骤是把数据画出来:

# key is language, value is pair (longitudes, latitudes)plots = { "Java" : ([], []), "Python" : ([], []), "R" : ([], []) }# we want each language to have a different marker and colormarkers = { "Java" : "o", "Python" : "s", "R" : "^" }colors  = { "Java" : "r", "Python" : "b", "R" : "g" }for (longitude, latitude), language in cities:    plots[language][0].append(longitude)    plots[language][1].append(latitude)# create a scatter series for each languagefor language, (x, y) in plots.iteritems():    plt.scatter(x, y, color=colors[language],                 marker=markers[language],                 label=language, zorder=10)plot_state_borders(plt)      # pretend we have a function that does thisplt.legend(loc=0)            # let matplotlib choose the locationplt.axis([-130,-60,20,55])   # set the axesplt.title("Favorite Programming Languages")plt.show()

这里写图片描述

由于相近的地方趋向同一种编程语言,KNN似乎是一种合理的预测语言模型。

如果我们试着使用相邻城市而不是本身来预测每个城市所喜爱的语言,会发生什么呢:

# try several different values for kfor k in [1, 3, 5, 7]:    num_correct = 0    for city in cities:        location, actual_language = city        other_cities = [other_city                        for other_city in cities                        if other_city != city]        predicted_language = knn_classify(k, other_cities, location)        if predicted_language == actual_language:            num_correct += 1    print k, "neighbor[s]:", num_correct, "correct out of", len(cities)

看起来3NN执行的效果最好,大约59%的正确率:

这里写图片描述

现在我们能看出在最近邻方案中什么区域被分类成哪种语言,我们能画图如下:

plots = { "Java" : ([], []), "Python" : ([], []), "R" : ([], []) }k = 1 # or 3, or 5, or ...for longitude in range(-130, -60):    for latitude in range(20, 55):        predicted_language = knn_classify(k, cities, [longitude, latitude])        plots[predicted_language][0].append(longitude)        plots[predicted_language][1].append(latitude)

在下图,展示的是k=1情况:
这里写图片描述

当k增加到5时,边界变得更加光滑:
这里写图片描述

这里是我们粗略的进行比较,如果它们有单位,你可能想要先进行尺度变换操作。接下来我们将要介绍不同维度距离的变化。

0 0
原创粉丝点击