{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"## Import\nimport pandas as pd\nimport cudf\nimport gc\nfrom sklearn.cluster import KMeans\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-27T04:47:52.656226Z","iopub.execute_input":"2022-11-27T04:47:52.656904Z","iopub.status.idle":"2022-11-27T04:47:52.710930Z","shell.execute_reply.started":"2022-11-27T04:47:52.656870Z","shell.execute_reply":"2022-11-27T04:47:52.709983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hm_data = cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\nhm_data['article_id'] = hm_data.article_id.astype('int32')\nhm_data.t_dat = cudf.to_datetime(hm_data.t_dat)\nhm_data.loc[:, 'year'] = hm_data['t_dat'].dt.year\nhm_data = hm_data[hm_data.year == 2020]\nhm_data = hm_data[['customer_id','article_id', 'price']]","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:38:48.926845Z","iopub.execute_input":"2022-11-27T04:38:48.927164Z","iopub.status.idle":"2022-11-27T04:39:20.985268Z","shell.execute_reply.started":"2022-11-27T04:38:48.927136Z","shell.execute_reply":"2022-11-27T04:39:20.984337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_data = cudf.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv\")\narticles_data = articles_data[[\"article_id\", \"prod_name\", \"product_type_name\", \"product_group_name\",\n                     \"department_name\", \"index_name\", \"index_group_name\",\n                     \"section_name\", \"garment_group_name\"]]\nhm_data = hm_data.merge(articles_data, on = [\"article_id\"], how = \"inner\")","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:39:20.986642Z","iopub.execute_input":"2022-11-27T04:39:20.986995Z","iopub.status.idle":"2022-11-27T04:39:21.509804Z","shell.execute_reply.started":"2022-11-27T04:39:20.986961Z","shell.execute_reply":"2022-11-27T04:39:21.508832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_data = cudf.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ncustomers_data = customers_data[[\"customer_id\", \"age\"]]\nhm_data = hm_data.merge(customers_data, on = [\"customer_id\"], how = \"inner\")\nhm_data.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:39:21.512519Z","iopub.execute_input":"2022-11-27T04:39:21.512902Z","iopub.status.idle":"2022-11-27T04:39:24.232449Z","shell.execute_reply.started":"2022-11-27T04:39:21.512867Z","shell.execute_reply":"2022-11-27T04:39:24.231460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()\nhm_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:39:24.233785Z","iopub.execute_input":"2022-11-27T04:39:24.234147Z","iopub.status.idle":"2022-11-27T04:39:24.406039Z","shell.execute_reply.started":"2022-11-27T04:39:24.234108Z","shell.execute_reply":"2022-11-27T04:39:24.405168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hm_data = hm_data.to_pandas()\nproduct_group_count_matrix = pd.crosstab(index=hm_data.customer_id, columns=hm_data.product_group_name)\nproduct_group_count_matrix.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:39:24.407614Z","iopub.execute_input":"2022-11-27T04:39:24.407941Z","iopub.status.idle":"2022-11-27T04:39:52.372785Z","shell.execute_reply.started":"2022-11-27T04:39:24.407908Z","shell.execute_reply":"2022-11-27T04:39:52.371677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_prices = hm_data.groupby(\"customer_id\")[\"price\"].mean()\nmean_customer_age = hm_data.groupby(\"customer_id\")[\"age\"].mean()\ntrain = pd.merge(product_group_count_matrix, mean_prices, on = \"customer_id\", how = \"inner\")\ntrain = pd.merge(train, mean_customer_age, on = \"customer_id\", how = \"inner\")","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:39:52.374513Z","iopub.execute_input":"2022-11-27T04:39:52.374889Z","iopub.status.idle":"2022-11-27T04:40:01.350051Z","shell.execute_reply.started":"2022-11-27T04:39:52.374853Z","shell.execute_reply":"2022-11-27T04:40:01.348968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:40:01.351776Z","iopub.execute_input":"2022-11-27T04:40:01.352161Z","iopub.status.idle":"2022-11-27T04:40:01.372816Z","shell.execute_reply.started":"2022-11-27T04:40:01.352123Z","shell.execute_reply":"2022-11-27T04:40:01.371801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\n\nscaled = MinMaxScaler().fit_transform(train)\nX = pd.DataFrame(data=scaled, columns=train.columns, index=train.index)\nX","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:40:01.374179Z","iopub.execute_input":"2022-11-27T04:40:01.376494Z","iopub.status.idle":"2022-11-27T04:40:02.228599Z","shell.execute_reply.started":"2022-11-27T04:40:01.376457Z","shell.execute_reply":"2022-11-27T04:40:02.226461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = KMeans()\npreds = model.fit_predict(X)\n\nresults = KMeans(n_clusters=4).fit_predict(X)","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:40:02.231668Z","iopub.execute_input":"2022-11-27T04:40:02.232174Z","iopub.status.idle":"2022-11-27T04:40:35.938263Z","shell.execute_reply.started":"2022-11-27T04:40:02.232145Z","shell.execute_reply":"2022-11-27T04:40:35.937190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:40:35.939722Z","iopub.execute_input":"2022-11-27T04:40:35.940263Z","iopub.status.idle":"2022-11-27T04:40:35.947840Z","shell.execute_reply.started":"2022-11-27T04:40:35.940227Z","shell.execute_reply":"2022-11-27T04:40:35.946579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import MiniBatchSparsePCA\n\npca = MiniBatchSparsePCA(n_components=2, batch_size=100000, random_state=42)\n\nX_reduced = pca.fit_transform(X)\npca_df = pd.DataFrame(data=X_reduced, columns=[f\"PC{i}\" for i in range(X_reduced.shape[1])])\n\nresult_sse = KMeans(n_clusters=4).fit_predict(X)\npca_df['group'] = result_sse\n\nplt.figure(figsize=(8, 6))\nsns.scatterplot(data=pca_df, x=\"PC0\", y=\"PC1\", hue=\"group\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-27T04:47:58.308445Z","iopub.execute_input":"2022-11-27T04:47:58.308803Z","iopub.status.idle":"2022-11-27T04:48:47.631876Z","shell.execute_reply.started":"2022-11-27T04:47:58.308775Z","shell.execute_reply":"2022-11-27T04:48:47.630928Z"},"trusted":true},"execution_count":null,"outputs":[]}]}