机器学习-6.K近邻

EduCoder:机器学习—K近邻

第1关:KNN原理

在这里插入图片描述

第2关:K近邻再识

在这里插入图片描述
在这里插入图片描述

第3关:K近邻小试

编程要求:
请仔细阅读右侧代码,结合相关知识,在 Begin-End 区域内进行代码补充,计算并输出数字的类型以及数字各属于两类的概率。其中 X 为样本点,y 为其类别(二分类问题),参数 k 的值设置为 3。

代码如下:

#引入KNeighborsClassifier模块
from sklearn.neighbors import KNeighborsClassifier

X = [[0], [1], [2], [3]]
n = int(input())
if n == 0:
   y = [1,1,0,0]
else:
   y = [0,0,1,1]

#使用KNeighborsClassifier函数以及fit函数填空
# ********** Begin ********** #
neigh=KNeighborsClassifier(n_neighbors=3)
neigh.fit(X,y)

# ********** End ********** #

#输出数字1.1的类型以及数字0.9各属于两类的概率
print(neigh.predict([[1.1]]))
print(neigh.predict_proba([[0.9]]))
    

第4关:K近邻实战

编程要求:
请仔细阅读右侧代码,结合相关知识,在 Begin-End 区域内进行代码补充,实现面向鸢尾花数据集的 k 近邻分类方法。
除补充完整代码之外,还应逐行理解右边示例代码,学习各函数及参数的用法,并举一反三,运用到其他的分类问题之中。

代码如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import neighbors, svm, tree, ensemble 
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
import warnings
warnings.filterwarnings("ignore")   #忽略警告
with warnings.catch_warnings():
    warnings.filterwarnings("ignore",category=DeprecationWarning)
    from numpy.core.umath_tests import inner1d

# set the number of neighbors
n_neighbors = 15
# ********** Begin ********** #
# import the iris dataset
iris = datasets.load_iris()
# only take the first two features
X = iris.data[:, :2]
y = iris.target
# ********** End ********** #

h = .02  # step size in the mesh
# Create color maps
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])

#基于K近邻分类结果描绘分类边界
# ********** Begin ********** #
for weights in ['uniform', 'distance']:
  # create an instance of KNN Classifier and fit the data.
    clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights)
    clf.fit(X, y)
    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, x_max]x[y_min, y_max].
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
# ********** End ********** #

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    plt.figure()
    plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
    # Plot also the training points
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
                edgecolor='k', s=20)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.title("3-Class classification (k = %i, weights = '%s')"
              % (n_neighbors, weights))
plt.savefig("step3/结果/result.png")
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=1000, n_features=4,
                           n_informative=2, n_redundant=0,
                           random_state=0, shuffle=False)
clf_rf = RandomForestClassifier(n_estimators=100, max_depth=2,
                             random_state=0)
clf_rf.fit(X, y)

#输出clf_rf的各特征权重以及预测[0,0,0,0]的类别
# ********** Begin ********** #
print(clf_rf.feature_importances_)
print(clf_rf.predict([[0, 0, 0, 0]]))
# ********** End ********** #

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pretend ^^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值