当前位置: 首页 > news >正文

机器学习_KNN(K近邻)算法_FaceBook_Location案例(附数据集下载链接)

Facebook_location_KNN

流程分析:

1.数据集获取(大型数据怎么获取? 放在电脑哪里? 算力怎么搞?)

2.基本数据处理(数据选取-确定特征值和目标值-分割数据集)

缩小数据范围

选择时间特征

去掉签到较少的地方

确定特征值和目标值

分割数据集

3.特征工程(特征预处理:标准化)

4.模型训练(KNN+CV)

5.模型评估

代码实现基本步骤

1.数据导入

1.1导入facebook_location_train_set(数据集大小:1.8G), 需要加载一段时间

import pandas as pd
locdata=pd.read_csv(r"C:\Users\鹰\Desktop\ML_Set\FaceBook_train.csv\FaceBook_train.csv")

1.2对数据的信息的简单分析

显示部分数据

locdata.head()

查看数据描述

locdata.describe()

查看数据行列数

locdata.shape

2.数据基本处理

2.1缺失值处理

print(“查看数据缺失值:”)
print(locdata.isna().sum())
locdata.dropna()
print(locdata.isna().sum())

2.2数据提取

缩小数据范围

locdata= locdata.query(“x>2.0 & x<2.5 & y>2.0 & y<2.5”)

选择时间特征, 对time进行转化

time=pd.to_datetime(locdata[“time”], unit=“s”)
time=pd.DatetimeIndex(time)
locdata[“day”]=time.day
locdata[“hour”]=time.hour
locdata[“weekday”]=time.weekday

去掉签到较少的地方, 在这里去掉签到次数小于三的地点

place_set= locdata.groupby(“place_id”).count()
place_set= place_set[place_set[“row_id”]>3]
locdata=locdata[locdata[“place_id”].isin(place_set.index)]

确定目标值和特征值, 用loc和iloc可以吗? 有什么区别吗?

x_all=locdata[[“x”,“y”,“accuracy”,“day”,“hour”,“weekday”]]
y_all=locdata[“place_id”]####

2.3数据集分割

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test=train_test_split(x_all, y_all,test_size=0.2)###
print(x_train)
print(x_test)
print(y_train)
print(y_test)

3.特征工程

3.1特征预处理-标准化

from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
x_train=scaler.fit_transform(x_train)
x_test=scaler.fit_transform(x_test)

4.模型训练-KNN+CV

4.1KNN调用

from sklearn.neighbors import KNeighborsClassifier
estimator=KNeighborsClassifier()

4.2模型优化

from sklearn.model_selection import GridSearchCV
params={“n_neighbors”:[1,3,5,7,9]}
estimator=GridSearchCV(estimator, param_grid=params, cv=5)

4.3模型训练

estimator.fit(x_train, y_train)

5.模型评估

5.1预测值

y_predict=estimator.predict(x_test)
print(“预测值为:”, y_predict)

5.2准确率

score=estimator.score(x_test,y_test)
print(“准确率为:”, score)

5.2最优模型参数:

print(“最优模型为:”, estimator.best_estimator_)

5.3最好评分

print(“最高分:”, estimator.best_score_)

数据集Facebook_Location下载地址:

链接:https://pan.baidu.com/s/1uoeo6pukkjSuLlKW9RwnCQ
提取码:7hlo

http://www.lryc.cn/news/464632.html

相关文章:

  • 【str_replace替换导致的绕过】
  • 如何用AI大模型提升挖洞速度
  • 两个数列问题
  • python中堆的用法
  • 轮班管理新策略,提高效率与降低员工抱怨
  • spring-cloud-alibaba-nacos-config2023.0.1.*启动打印配置文件内容
  • 数据结构:二叉树、堆
  • hi3798mv100 linux 移植
  • Docker-Harbor概述及构建
  • 部署项目最新教程
  • linux证明变量扩展在路径名扩展之前执行
  • CentOS 7.9安装MySQL
  • MacOS虚拟机安装Windows停滞在“让我们为你连接到网络”,如何解决?
  • 黑马程序员Java笔记整理(day03)
  • centos7更换阿里云镜像源操作步骤及命令
  • 冲刺大厂 | 一个线程调用两次start()方法会出现什么现象?
  • leaflet(一)初始化地图
  • Unity开发Hololens项目
  • 立志最细,FreeRtos的中断管理(Interrupt Management)函数,详解!!!
  • 作业2-线性回归的Matlab代码实现
  • 用jQuery在canvas上绘制绝对定位的元素
  • Android中 tools:text 和 android:text区别
  • Wordpress GutenKit 插件 远程文件写入致RCE漏洞复现(CVE-2024-9234)
  • Redis历史漏洞未授权RCE复现
  • Greenhills学习总结
  • 【深入学习Redis丨第八篇】详解Redis数据持久化机制
  • 【27续】c++项目练习
  • Lazarus Query转EXCEL功能
  • AnaTraf | 深入探讨DNS流量分析:保障网络稳定性的关键
  • P1017 [NOIP2000 提高组] 进制转换