预测facebook签到位置

news/2024/7/9 8:46:07 标签: sklearn, python, 机器学习

1 项⽬描述

在这里插入图片描述
本次⽐赛的⽬的是预测⼀个⼈将要签到的地⽅。 为了本次⽐赛,Facebook创建了⼀个虚拟世界,其中包括10公⾥*10 公⾥共100平⽅公⾥的约10万个地⽅。 对于给定的坐标集,您的任务将根据⽤户的位置,准确性和时间戳等预测⽤户下 ⼀次的签到位置。 数据被制作成类似于来⾃移动设备的位置数据。 请注意:您只能使⽤提供的数据进⾏预测。

2 数据集介绍

在这里插入图片描述

⽂件说明 train.csv, test.csv
 row id:签⼊事件的id 
 x y:坐标 
 accuracy: 准确度,定位精度
  time: 时间戳 
  place_id: 签到的位置,这也是你需要预测的内容

3 步骤分析

在这里插入图片描述

python">import pandas as pd
#数据基本处理(训练集和测试集)
from sklearn.model_selection import train_test_split,GridSearchCV
#特征工程和特征预处理
from sklearn.preprocessing import StandardScaler
#机器学习
from sklearn.neighbors import KNeighborsClassifier

3.1 读取数据

python">data=pd.read_csv("./data/train.csv") 
data.head(5)

在这里插入图片描述

python">data.shape

3.2 基本数据处理

3.2.1 缩小数据范围

python">partial_data=data.query("x>2.0 & x<2.5 & y>2.0 &y<2.5")
partial_data.head()

在这里插入图片描述

python">partial_data.shape # 明显和原数据相比少了很多数据

在这里插入图片描述

3.2.2 选择时间特征

python">partial_data["time"].head()

在这里插入图片描述

python">time=pd.to_datetime(partial_data["time"],unit="s")
time.head()
#脱敏   数据脱敏是指对某些敏感信息通过脱敏规则进行数据的变形,实现敏感隐私数据的可靠保护

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

python">partial_data["hour"]=time.hour
partial_data["day"]=time.day
partial_data["weekday"]=time.weekday

在这里插入图片描述

3.2.3 去掉签到数较少的地方

python">place_count=partial_data.groupby("place_id").count()   # 聚合

在这里插入图片描述

python">place_count=place_count[place_count["row_id"]>3]
place_count.head()

在这里插入图片描述

python"># 获取row_id>3的数据
partial_data=partial_data[partial_data["place_id"].isin(place_count.index)]

在这里插入图片描述

3.2.4 确定特征值和目标值

python">x=partial_data[["x","y","accuracy","hour","day","weekday"]]
y=partial_data["place_id"]

在这里插入图片描述

3.2.5 分割数据集

python">x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=2,test_size=0.25)

在这里插入图片描述

3.3 特征工程—特征预处理

python">#实例化
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.fit_transform(x_test)

3.4 机器学习

3.4.1 实例化一个训练模型

python">estimator=KNeighborsClassifier()

3.4.2 交叉验证网格搜索实现

python">param_grid={"n_neighbors":[3,5,7,9]}
estimator=GridSearchCV(estimator=estimator,param_grid=param_grid,cv=10,n_jobs=4)

3.4.3 模型训练

python">estimator.fit(x_train,y_train)

在这里插入图片描述

3.5 模型评估

3.5.1 准确率输出

python">score_ret=estimator.score(x_test,y_test)
print("模型准确率为:\n",score_ret)

在这里插入图片描述

3.5.2 预测结果

python">y_pridict=estimator.predict(x_test)
print("预测值:\n",y_pridict)

在这里插入图片描述

3.5.3 其他结果输出

python">print("最好的模型是\n",estimator.best_estimator_)
print("最好的结果是\n",estimator.best_score_)
print("所有的结果\n",estimator.cv_results_)
最好的模型是
 KNeighborsClassifier()
最好的结果是
 0.36103403905372417
所有的结果
 {'mean_fit_time': array([0.23680475, 0.24250968, 0.27274141, 0.28223894]), 'std_fit_time': array([0.04789135, 0.05988373, 0.05860585, 0.04193857]), 'mean_score_time': array([0.54308534, 0.54496615, 0.72975917, 0.7652776 ]), 'std_score_time': array([0.02659151, 0.07117335, 0.08149376, 0.08075544]), 'param_n_neighbors': masked_array(data=[3, 5, 7, 9],
             mask=[False, False, False, False],
       fill_value='?',
            dtype=object), 'params': [{'n_neighbors': 3}, {'n_neighbors': 5}, {'n_neighbors': 7}, {'n_neighbors': 9}], 'split0_test_score': array([0.34725698, 0.36438884, 0.36612127, 0.3599615 ]), 'split1_test_score': array([0.34687199, 0.35938402, 0.35919153, 0.35688162]), 'split2_test_score': array([0.35052936, 0.35899904, 0.3574591 , 0.35784408]), 'split3_test_score': array([0.34898941, 0.36458133, 0.36246391, 0.35899904]), 'split4_test_score': array([0.35264678, 0.36381136, 0.36458133, 0.35803657]), 'split5_test_score': array([0.34513956, 0.3572666 , 0.35688162, 0.35514918]), 'split6_test_score': array([0.35437921, 0.36477382, 0.36554379, 0.36997113]), 'split7_test_score': array([0.35264678, 0.35880654, 0.3626564 , 0.36323388]), 'split8_test_score': array([0.34501348, 0.35618021, 0.35463997, 0.35309973]), 'split9_test_score': array([0.35021178, 0.36214863, 0.3567578 , 0.35309973]), 'mean_test_score': array([0.34936853, 0.36103404, 0.36062967, 0.35862765]), 'std_test_score': array([0.00310395, 0.00310417, 0.00392977, 0.00478577]), 'rank_test_score': array([4, 1, 2, 3])}

http://www.niftyadmin.cn/n/1711696.html

相关文章

梯度下降⽅法介绍

1 详解梯度下降算法 1.1梯度下降的相关概念复习 相关概念 步⻓(Learning rate)&#xff1a; 步⻓决定了在梯度下降迭代的过程中&#xff0c;每⼀步沿梯度负⽅向前进的⻓度。⽤前⾯下⼭的例⼦&#xff0c;步⻓就是在当前 这⼀步所在位置沿着最陡峭最易下⼭的位置⾛的那⼀步的…

线性回归api再介绍

1 线性回归api再介绍 2. 案例&#xff1a;波⼠顿房价预测 2.1 案例背景介绍 2.2. 案例分析 回归当中的数据⼤⼩不⼀致&#xff0c;是否会导致结果影响较⼤。所以需要做标准化处理。 数据分割与标准化处理回归预测线性回归的算法效果评估 2.3 回归性能评估 2.4 代码实现 正…

正则化线性模型

1 Ridge Regression (岭回归&#xff0c;⼜名 Tikhonov regularization) 2 Lasso Regression(Lasso 回归) 3 Elastic Net (弹性⽹络) 4 Early Stopping [了解] Early Stopping 也是正则化迭代学习的⽅法之⼀。 其做法为&#xff1a;在验证错误率达到最⼩值的时候停⽌训练 5.线…

时间序列模型--ARIMA模型

1. 数据平稳性与差分法 平稳性&#xff1a; 平稳性就是要求经由样本时间序列所得到的拟合曲线&#xff0c;在未来的一段时间内仍然能顺着现有的形态“惯性”的延续下去平稳性要求&#xff0c;序列的方差和均值不发生明显的变化 严平稳和弱平稳 严平稳&#xff1a;严平稳表示的…

【Tensorflow学习笔记】

目录 1 环境配置 2. 回归问题 2.1 连续值的预测 2.2 回归问题的例子&#xff1a; 2.3 分类问题--手写数字识别问题 3. tensorflow的基本操作 3.1 tensorflow的数据类型 3.2 索引与切片 3.3 维度变换 3.4 broadingcast: 3.5 数学运算 3.6 前向传播 3.7 张量的合并与…

基于webservice 使用HttpClient调用

1. webservice 的服务端 - 配置类 Configuration public class CxfConfig {AutowiredCatalogServiceImpl catalogService;Autowiredprivate Bus bus;Beanpublic Endpoint endpoint() {EndpointImpl endpoint new EndpointImpl(bus, catalogService);endpoint.publish("…

利用soapUI获取freemarker的ftl文件模板

目录1. 查找webservice的wsdl连接2. 打开soapui软件获取请求连接1. 查找webservice的wsdl连接 一般是 springboot里面&#xff0c;ip端口服务名称services 即可查找wsdl连接 点击蓝色链接&#xff0c;将wsdl链接复制 2. 打开soapui软件获取请求连接 得到freemarker的模板文件…

File的io流与base64

1.file转换为base64 字符串 Value("${file.store.path}\\")private String path;/*** 将文件进行base64编码* param file* return*/public String FileToBase64(File file){String base64 null;InputStream in null;byte[] data null;try {in new FileInputStrea…