【机器学习入门笔记】第五篇-KNN算法

以facebook案例来学习KNN算法

1.读取数据,有29118021 rows × 6 columns

import pandas as pd
data = pd.read_csv("train.csv")
data

运行结果:

【机器学习入门笔记】第五篇-KNN算法_第1张图片

 2.数据太多对数据筛选一下

# 1)缩小数据范围
# 2 2 & y<1.5 & y>1.0")


# 2)处理时间特征
time_value = pd.to_datetime(data["time"],unit="s")
date = pd.DatetimeIndex(time_value)


data["day"] = date.day
data["weekday"] = date.weekday
data["hour"] = date.hour
#以上三步会出现警告不过没关系

# 3)过滤掉签到次数少的
place_count = data.groupby("place_id").count()["row_id"]
place_count = place_count[place_count > 3]
data_final = data[data["place_id"].isin(place_count.index.values)]

x = data_final[["x","y","accuracy","day","weekday","hour"]]
y = data_final[["place_id"]]

3.划分数据集

#数据集划分
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y)

4.实例化一个转换器

from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import  KNeighborsClassifier
from sklearn.model_selection import GridSearchCV

transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
estimator = KNeighborsClassifier()
param_dict = {"n_neighbors":[3,5,7,9]}
estimator = GridSearchCV(estimator,param_grid=param_dict,cv=3)
estimator.fit(x_train, y_train)

5.结果


    # 5.模型评估
    # 方法1:直接比对真实值和预测值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n", y_predict)
    print(y_test)
    print("直接比对真实值和预测值:\n",y_test["place_id"] == y_predict)
    # 方法2:计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率:\n", score);
    #最佳参数
    print("最佳参数:\n",estimator.best_params_)
    print("最佳结果:\n",estimator.best_score_)
    print("最佳估计器:\n",estimator.best_estimator_)
    print("交叉验证结构:\n",estimator.cv_results_)

运行结果:

【机器学习入门笔记】第五篇-KNN算法_第2张图片

 

你可能感兴趣的:(大数据,机器学习,人工智能,sklearn,算法)