[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
近朱者赤,近墨者黑。
(学习笔记)
KNN:
一个样本在特征空间中,总有k个与之最相似(即特征空间中最邻近)的样本。其中,大多数属于某一类别,则该样本也属于这个类别。
计算步骤:
1.算距离:算出测试样本到训练集中每个样本的距离。(例如:欧氏距离)
2.找邻居:找出距离最近的k个训练对象。(k值的选取:交叉验证)
3.做分类:将这k个对象的主要类别作为测试数类别。(少数服从多数/根据距离的远近,距离越近权重越大,权重为距离平方的倒数)
算法流程
1.计算已知类别数据集中的点与当前点之间的距离。
2.按照距离递增次序排序。
3.选取与当前点距离最小的k个点。
4.确定前k个点所在类别对应的出现频率。
5.返回前k个点出现频率最高的类别作为当前点的预测分类。
优点:
1.简单,易于理解,易于实现,无需估计参数,无需训练;
2.适合对稀有事件进行分类;
3.适合于多分类问题。
缺点:
1.计算量大,内存开销大,评分慢;
2.可解释性较差。
行业应用:
* 客户流失预测
* 欺诈侦测(更适合于稀有事件的分类问题)
knn算法解决鸢尾花分类问题
from sklearn.datasets import load_iris from sklearn.model_selection import
train_test_splitfrom sklearn.neighbors import KNeighborsClassifier iris =
load_iris() # 鸢尾花数据 data_tr, data_te, label_tr, label_te = train_test_split(iris
.data, iris.target, test_size=0.2) # 拆分专家样本集 model = KNeighborsClassifier(
n_neighbors=5) # 构建模型 model.fit(data_tr, label_tr) # 模型训练 pre = model.predict(
data_te) # 模型预测 acc = model.score(data_te, label_te) # 模型在测试集上的精度 acc