{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"data = pd.read_csv(\"../input/train.csv\")\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 缩小数据范围\ndata = data.query(\"x>1.0 & x<1.25 & y>2.5 & y<2.75\")\nprint(data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 将时间戳转换为年月日时分秒\ntime = pd.to_datetime(data['time'], unit='s')\n# 把日期转换为字典数据\ntime = pd.DatetimeIndex(time)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 增加特征\ndata['day'] = time.day\ndata['hour'] = time.hour\ndata['weekday'] = time.weekday","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 删除特征\ndata = data.drop(['time'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 查看数据\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 将签到位置比较少的数据删掉\n# 统计次数\nresult = data.groupby('place_id').count()\n# 去掉次数少于3的数据\nresult = result[result.row_id > 3]\n# place_id变为一列\nresult = result.reset_index()\n# 对原始数据进行删选\ndata = data[data['place_id'].isin(result.place_id)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 取出数据中的特征值和目标值\ny = data['place_id']\nx = data.drop(['place_id', 'row_id'], axis=1)\nx.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n# 进行数据的分割，训练集和测试集\nx_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\n# 数据标准化, 将数据特征聚集在均值为0，方差为1的范围内\nstd = StandardScaler()\nx_train = std.fit_transform(x_train)\nx_test = std.fit_transform(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n\n# knn\nknn = KNeighborsClassifier(n_neighbors=5)\n# 输入数据\nknn.fit(x_train, y_train)\n\n# 预测值\ny_predict = knn.predict(x_test)\n#print(\"预测的目标签到位置为：\", y_predict)\n\n# 使用测试数据集，计算准确率\naccuracy = knn.score(x_test, y_test)\nprint(\"预测的准确率:\", accuracy)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}