机器学习01案例-预测facebook签到位置

整体流程图

3.2.4 案例:预测facebook签到位置

流程分析:
  1)获取数据
  2)数据处理
  目的:
  特征值 x
  目标值 y
   a.缩小数据范围
     2 < x < 2.5
     1.0 < y < 1.5
   b.time -> 年月日时分秒
   c.过滤签到次数少的地点
   数据集划分
   3)特征工程:标准化
   4)KNN算法预估流程
   5)模型选择与调优
   6)模型评估

1.获取数据

import pandas as pd
import numpy as np
#1.获取数据
data = pd.read_csv("../input/train.csv")
print(data.head())
print(data.shape)
   row_id       x       y  accuracy    time    place_id
0       0  0.7941  9.0809        54  470702  8523065625
1       1  5.9567  4.7968        13  186555  1757726713
2       2  8.3078  7.0407        74  322648  1137537235
3       3  7.3665  2.5165        65  704587  6567393236
4       4  4.0961  1.1307        31  472130  7440663949
(29118021, 6)

2.基本的数据处理

# 1) 缩小数据范围
data = data.query("x<2.5 & x>2 & y<1.5 & y> 1.0")
# 2)时间戳转换为时间
time_value = pd.to_datetime(data['time'],unit='s')
time_value.head()
112    1970-01-08 05:06:14
180    1970-01-08 01:29:55
367    1970-01-07 17:01:07
874    1970-01-02 15:52:46
1022   1970-01-03 09:46:33
Name: time, dtype: datetime64[ns]
# 3) 将时间从Series的格式转换到DatetimeIndex的格式
time = pd.DatetimeIndex(time_value)
time.weekday  # 星期几
time.month    # 几月
time.year     # 年份
time.hour     # 小时
Int64Index([ 5,  1, 17, 15,  9, 19, 13, 22, 14, 16,
            ...
             9, 10, 11,  3, 22,  8, 12, 20, 18, 22],
           dtype='int64', name='time', length=83197)
data["day"] = time.day
data["weekday"] = time.weekday
data["hour"] = time.weekday
data.head()
row_id x y accuracy time place_id day weekday hour
112 112 2.2360 1.3655 66 623174 7663031065 8 3 3
180 180 2.2003 1.2541 65 610195 2358558474 8 3 3
367 367 2.4108 1.3213 74 579667 6644108708 7 2 2
874 874 2.0822 1.1973 320 143566 3229876087 2 4 4
1022 1022 2.0160 1.1659 65 207993 3244363975 3 5 5
# 4) 过滤掉签到次数少的地点   分组+聚合函数    
# 因为都是重复的没必要全都要,我们拿一个“row_id”
place_count = data.groupby("place_id").count()["row_id"]
# 挑选正确的地点,签到次数小于3次以下的都去除掉
place_count[place_count > 3].head()
place_id
1014605271    28
1015645743     4
1017236154    31
1024951487     5
1028119817     4
Name: row_id, dtype: int64
# 在data中通过地点过滤数据   
boo_ = data["place_id"].isin(place_count[place_count > 3].index.values)   # !!! 后面加index和values
data_final = data[boo_]
# 筛选特征值和目标值
x = data_final[['x','y','accuracy','day','weekday','hour']]
y = data_final['place_id']
x.head()
x y accuracy day weekday hour
112 2.2360 1.3655 66 8 3 3
367 2.4108 1.3213 74 7 2 2
874 2.0822 1.1973 320 2 4 4
1022 2.0160 1.1659 65 3 5 5
1045 2.3859 1.1660 498 6 1 1
y.head()
112     7663031065
367     6644108708
874     3229876087
1022    3244363975
1045    6438240873
Name: place_id, dtype: int64
# 数据集的划分
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y)

3.特征工程 标准化

from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
# 1) 特征工程标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)

4.KNN算法预估流程

# 1) KNN算法预估器
estimator = KNeighborsClassifier()

# 2)加入网格搜索与交叉验证
# 参数准备
param_dict = {"n_neighbors":[1,3,5,7,9,11,13]}
estimator = GridSearchCV(estimator,param_grid=param_dict,cv=3)
estimator.fit(x_train,y_train)

5.模型评估

# 计算准确率
score = estimator.score(x_test,y_test)
print("测试集的准确率为:",score)
# 最佳参数:best_params
print("最佳参数为:",estimator.best_params_)
print("最佳验证集结果为:",estimator.best_score_)
print("最佳估计器为:",estimator.best_estimator_)
准确率为: 0.43726517698240064
最佳参数为: {'n_neighbors': 9}
最佳结果为: 0.42577700141722424
最佳估计器为: KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
                     metric_params=None, n_jobs=None, n_neighbors=9, p=2,
                     weights='uniform')

7.使用全部数据来,训练模型,然后来预测test的数据

未完待续。。。

发布了31 篇原创文章 · 获赞 13 · 访问量 9899

猜你喜欢

转载自blog.csdn.net/qq_43497702/article/details/99546311