{"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":"# 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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-25T23:46:53.919456Z","iopub.execute_input":"2022-03-25T23:46:53.920236Z","iopub.status.idle":"2022-03-25T23:46:53.947945Z","shell.execute_reply.started":"2022-03-25T23:46:53.920107Z","shell.execute_reply":"2022-03-25T23:46:53.947227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pylab as plt\nimport os\nfrom os import listdir  #ファイルの一覧が見れる。\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2022-03-25T23:46:53.951220Z","iopub.execute_input":"2022-03-25T23:46:53.951742Z","iopub.status.idle":"2022-03-25T23:46:54.322442Z","shell.execute_reply.started":"2022-03-25T23:46:53.951691Z","shell.execute_reply":"2022-03-25T23:46:54.321758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_dir = \"../input/h-and-m-personalized-fashion-recommendations/images\"\ncat_images = [f for f in listdir(images_dir)]","metadata":{"execution":{"iopub.status.busy":"2022-03-25T23:46:54.323810Z","iopub.execute_input":"2022-03-25T23:46:54.324141Z","iopub.status.idle":"2022-03-25T23:46:54.347622Z","shell.execute_reply.started":"2022-03-25T23:46:54.324099Z","shell.execute_reply":"2022-03-25T23:46:54.346855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_dir","metadata":{"execution":{"iopub.status.busy":"2022-03-25T23:46:54.349250Z","iopub.execute_input":"2022-03-25T23:46:54.349682Z","iopub.status.idle":"2022-03-25T23:46:54.357043Z","shell.execute_reply.started":"2022-03-25T23:46:54.349647Z","shell.execute_reply":"2022-03-25T23:46:54.356448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(listdir(images_dir))\nprint(\"==================================================\")\nprint(cat_images) #それぞれのファイルを順番に取り出すようにしている","metadata":{"execution":{"iopub.status.busy":"2022-03-25T23:46:54.358101Z","iopub.execute_input":"2022-03-25T23:46:54.358680Z","iopub.status.idle":"2022-03-25T23:46:54.370435Z","shell.execute_reply.started":"2022-03-25T23:46:54.358646Z","shell.execute_reply":"2022-03-25T23:46:54.369783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getImagePaths(path):\n    image_names = []\n    for dirname, _, filenames in os.walk(path): #　path⇒それぞれの画像ファイル walk⇒ファイル内のディレクトリ名とファイル名を取り出している。\n        for filename in filenames:\n            fullpath = os.path.join(dirname, filename) # 一つ一つのファイル名とディレクトリ名を一つのパスにしている。\n            image_names.append(fullpath)\n    return image_names #パスとしての表示としている\n\ndef display_multiple_img(images_paths, rows, cols):\n    figure, ax = plt.subplots(nrows=rows,ncols=cols,figsize=(16,8) ) #与えたデータ分、サブプロットにしている。\n    for ind,image_path in enumerate(images_paths):  #インデックス番号と画像のパスを同時に出している。\n        image=cv2.imread(image_path) #cvで画像の読み込み\n        \n        try:\n            ax.ravel()[ind].imshow(image)\n            ax.ravel()[ind].set_axis_off() #Axes.set_axis_off()[source]X 軸と Y 軸をオフにします。\n        except:\n            continue;\n    plt.tight_layout() #tight_layout() メソッドはサブプロット間の正しい間隔を自動的に維持します。\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-25T23:46:54.371781Z","iopub.execute_input":"2022-03-25T23:46:54.372246Z","iopub.status.idle":"2022-03-25T23:46:54.382331Z","shell.execute_reply.started":"2022-03-25T23:46:54.372203Z","shell.execute_reply":"2022-03-25T23:46:54.381539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path = getImagePaths(images_dir)\ndisplay_multiple_img(images_path[0:30], 6, 5) #30個までのデータを6行5列で表している","metadata":{"execution":{"iopub.status.busy":"2022-03-25T23:46:54.383856Z","iopub.execute_input":"2022-03-25T23:46:54.384297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## トレインデータを見てみる","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#transaction_train.csv-各日付の各顧客の購入と追加情報で構成されるトレーニングデータ。重複する行は、同じアイテムの複数の購入に対応します。あなたの仕事はarticle_id、トレーニングデータ期間の直後の7日間に各顧客が購入するものを予測することです。\n\n#transactions_train.csv - the training data, consisting of the purchases each customer for each date, as well as additional information. Duplicate rows correspond to multiple purchases of the same item. Your task is to predict the article_ids each customer will purchase during the 7-day period immediately after the training data period.","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import pandas as pd","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pylab as plt","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"price\"].plot(kind='hist', bins=100, figsize=(15, 6)) #日本円ではないことがわかる","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['sales_channel_id'].unique() #二種類あることがわかる","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#円グラフで表してみる","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"sales_channel_id\"].value_counts().sort_values(ascending=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"sales_channel_id\"].value_counts().sort_values(ascending=True).index","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 8))\nplt.rcParams[\"font.size\"] = 18\nplt.pie(df_train[\"sales_channel_id\"].value_counts().sort_values(ascending=True),labels = df_train[\"sales_channel_id\"].value_counts().sort_values(ascending=True).index,startangle = 90,autopct=\"%1.1f%%\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#startangle:各要素の出力を開始する角度。 (デフォルト値: None)\n#autopct\t構成割合をパーセンテージで表示。 (デフォルト値: None)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## costomerが2年の間に何回買い物しているかヒストグラムで見てみよう。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.groupby('customer_id').size() ##customer_idが出てくる要素数を数えている。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df1) # df1.values.tolist()でこのdf1の値をリストにすることができる","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=df_train.groupby('customer_id').size()\nplt.figure(figsize=(15, 8))\nplt.hist(df1.values.tolist(),bins=100,range=(0,100)) #ユーザーはおよそ２０着以下を購入している。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#売れ筋グラフを見てみる。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_train[[\"article_id\"]].head() ##売れたアイテム\ndf_train[\"article_id\"].value_counts().sort_values(ascending=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 20))\nplt.rcParams[\"font.size\"] = 18 \nplt.xticks(rotation=90)\nplt.bar(df_train[\"article_id\"].astype(str).value_counts().sort_values(ascending=True)[-10:].index,df_train[\"article_id\"].value_counts().sort_values(ascending=True)[-10:],tick_label = df_train[\"article_id\"].astype(str).value_counts().sort_values(ascending=True)[-10:].index)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"../input/h-and-m-personalized-fashion-recommendations/images/070/0706016001.jpg\"\nim = plt.imread(path)\nplt.figure(figsize=(15, 6))\nplt.imshow(im)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"../input/h-and-m-personalized-fashion-recommendations/images/070/0706016002.jpg\"\nim = plt.imread(path)\nplt.figure(figsize=(15, 6))\nplt.imshow(im)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#article.csvを見てみる","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_article = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(df_article.head())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_article.info()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_article)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_article['product_type_name'].unique())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_article['product_group_name'].unique())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#131もの製品データがあり、１９個のグループに分けられている。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#気になるデータを可視化してみる。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_article[\"product_group_name\"].value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nplt.rcParams[\"font.size\"]=18\nplt.xticks(rotation=90)\nplt.bar(df_article[\"product_group_name\"].value_counts().sort_values(ascending=True).index,\n        df_article[\"product_group_name\"].value_counts().sort_values(ascending=True),\n        tick_label = df_article[\"product_group_name\"].value_counts().sort_values(ascending=True).index)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Garment Upper bodyがダントツだということが分かる","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# \"product_type_name\"のデータを可視化してみる。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nplt.rcParams[\"font.size\"]=18\nplt.xticks(rotation=90)\nplt.bar(df_article[\"product_type_name\"].value_counts().sort_values(ascending=True)[-20:].index,\n        df_article[\"product_type_name\"].value_counts().sort_values(ascending=True)[-20:],\n        tick_label = df_article[\"product_type_name\"].value_counts().sort_values(ascending=True).index)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nplt.rcParams[\"font.size\"] = 18\nplt.barh(df_article[\"product_type_name\"].value_counts().sort_values(ascending=True)[-20:].index\n         ,df_article[\"product_type_name\"].value_counts().sort_values(ascending=True)[-20:]\n         ,tick_label = df_article[\"product_type_name\"].value_counts().sort_values(ascending=True)[-20:].index)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ズボン、ドレス、セーター、T-shirtsが上位を争っている。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 20))\nplt.rcParams[\"font.size\"] = 18\nplt.barh(df_article[\"colour_group_name\"].value_counts().sort_values(ascending=True).index\n         ,df_article[\"colour_group_name\"].value_counts().sort_values(ascending=True)\n         ,tick_label = df_article[\"colour_group_name\"].value_counts().sort_values(ascending=True).index)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 20))\nplt.rcParams[\"font.size\"] = 18\nplt.barh(df_article[\"colour_group_name\"].value_counts().sort_values(ascending=True).index,df_article[\"colour_group_name\"].value_counts().sort_values(ascending=True),tick_label = df_article[\"colour_group_name\"].value_counts().sort_values(ascending=True).index)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 20))\nplt.rcParams[\"font.size\"] = 18\nplt.barh(df_article[\"department_name\"].value_counts().sort_values(ascending=True)[-40:].index,df_article[\"department_name\"].value_counts().sort_values(ascending=True)[-40:],tick_label = df_article[\"department_name\"].value_counts().sort_values(ascending=True)[-40:].index)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nplt.rcParams[\"font.size\"] = 18\nplt.xticks(rotation=90)\nplt.bar(df_article[\"index_name\"].value_counts().sort_values(ascending=True).index\n        ,df_article[\"index_name\"].value_counts().sort_values(ascending=True)\n        ,tick_label = df_article[\"index_name\"].value_counts().sort_values(ascending=True).index)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## articleデータのまとめ\n#黒のアイテムが多い\n#アイテムグループの中だとGarment Upper bodyがダントツに多い。\n# ズボン、ドレス、セーター、T-shirtsが上位を争っている。\n# 女性服に人気があるが、二番目はDividedというタイプが人気である。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# customer_idを確認してみる。","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_costomers = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ndf_costomers.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 25))\nplt.rcParams[\"font.size\"] = 15\nplt.barh(df_costomers[\"age\"].value_counts().sort_values(ascending=True).index,df_costomers[\"age\"].value_counts().sort_values(ascending=True),tick_label = df_costomers[\"age\"].value_counts().sort_values(ascending=True).index)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}