Pythonã³ã¼ãã®ã¢ãããã¼ããããã®ã§ãã©ã³ãã³ã°ãã¼ã¿ã®éè¨ã¨åææ¹æ³ã«ã¤ãã¦ã¾ã¨ãã¦ããã¾ãã
é·ããèµ°ã£ã¦ããªãã£ãã¡ã¿ãã®ã¢ã©ãã£ãããããã®ã©ã³ãã³ã°è¨é²ã§ããèµ°ãå§ãã¦79æ¥ãèµ°ã£ãåæ°ã¯32åã«ãªãã¾ããã
æ©ãã¦ããã®ãèµ°ã£ã¦ããã®ããå¾®å¦ãªãã¼ã¿ã並ã¶ãã¨ããäºæ¿ãã ããã
- ãã¼ã¿åéããåæã¾ã§
- Jupyter Labï¼Pythonï¼ã§ã°ã©ãã¨è¡¨ãä½æ
- ç¾ç¶
- ã¾ã¨ã
ãã¼ã¿åéããåæã¾ã§
Apple Watchã§åéããã©ã³ãã³ã°ãã¼ã¿ããiPhoneããCSVã¨ã¯ã¹ãã¼ããã¦ãPCã®Pythonã§éè¨ãã¦ãã¾ãã
flowchart TB node_1["èµ°ã"] node_2["iPhoneãã£ãããã¹ã¢ããª"] node_3["Runmeter"] node_4["OneDrive"] node_5["Jupter Lab"] node_6["ã°ã©ãä½æ"] node_7["Googleã«ã¬ã³ãã¼"] node_1 =="Apple Watchã§è¨æ¸¬"==> node_2 node_2 =="ã¤ã³ãã¼ã"==> node_3 node_3 =="çµ±è¨æ å ±ãã¨ã¯ã¹ãã¼ã"==> node_4 node_4 ==> node_5 node_5 =="CSVã«çµ±åã»ã°ã©ãä½æ"==> node_6 node_3 =="ã«ã¬ã³ãã¼åæ"==> node_7
é±/æãã¨ã®ä¸è¨ã®ãã¼ã¿ãç¶ç¶çã«è¿½ã£ã¦ãã¾ãã
- åè¨è·é¢
- å¹³åè·é¢
- ã©ã³ãã³ã°æé
é¢é£æ§ãè¦ãããã®äºè»¸æ§ã®ã°ã©ãã¯iPhoneã®ã¢ããªã§ã¯è¡¨ç¤ºã§ããªãã®ã§ãPythonã§ã°ã©ããä½æãã¦ãã¾ãã
Jupyter Labï¼Pythonï¼ã§ã°ã©ãã¨è¡¨ãä½æ
Pythonã®ã³ã¼ãä½æã¯ChatGPTã使ç¨ãã¦ãã¾ãã使ããªããå°ãã¥ã¤æ¹å¤ãã¦ãã¾ããä»åã¯ä¸è¨ã®å¤æ´ãè¡ãã¾ããã
- ä½æããã°ã©ãã®PNGæ¸ãåºã
- é±éã»æéã®åæãåæã«å®æ½
- ãã¼ã¿ãMarkdownã®è¡¨å½¢å¼ã§ããã¹ããã¡ã¤ã«ã«æ¸ãåºã
ã©ã³ãã³ã°ã®ãã¼ã¿ãæãã¨ã»é±ãã¨ã«ã°ã©ãåã»ãã¼ãã«åããPythonã³ã¼ã
import pandas as pd import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties import os from datetime import datetime from tabulate import tabulate # CSVãã¡ã¤ã«ã®ãã¹ file_path = 'your_data_path/run2024.csv' # æ¥æ¬èªãã©ã³ãã®ãã¹ãè¨å® font_path = 'your_font_path/ipaexg.ttf' japanese_font = FontProperties(fname=font_path) # ãã¼ã¿ã®èªã¿è¾¼ã¿ running_data = pd.read_csv(file_path) # éè¤è¡ãåé¤ããåã®è¡æ° initial_row_count = len(running_data) # éè¤è¡ãåé¤ãã running_data.drop_duplicates(inplace=True) # éè¤è¡ãåé¤ããå¾ã®è¡æ° final_row_count = len(running_data) # éè¤è¡ãåé¤ããããç¢ºèª if final_row_count < initial_row_count: # ç¾å¨ã®ã¿ã¤ã ã¹ã¿ã³ããåå¾ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") # ããã¯ã¢ãããã¡ã¤ã«ã®ãã¹ãä½æ backup_file_path = f"{file_path.rsplit('.', 1)[0]}_{timestamp}.csv" # å ã®CSVãã¡ã¤ã«ãããã¯ã¢ãããã¡ã¤ã«ã«ã³ãã¼ os.rename(file_path, backup_file_path) # æ°ããCSVãã¡ã¤ã«ã¨ãã¦ä¿å running_data.to_csv(file_path, index=False) print(f"éè¤è¡ãåé¤ããæ°ãããã¡ã¤ã«ãä½æãã¾ãããå ã®ãã¡ã¤ã«ã¯ {backup_file_path} ã«ããã¯ã¢ããããã¾ããã") else: print("éè¤è¡ã¯è¦ã¤ããã¾ããã§ãããããã¯ã¢ãããã¡ã¤ã«ã¯ä½æããã¾ããã") # æéåãdatetimeå½¢å¼ã«å¤æãã¦ã¤ã³ããã¯ã¹ã«è¨å® running_data['æé'] = pd.to_datetime(running_data['æé']) running_data.set_index('æé', inplace=True) # ã©ã³ã¿ã¤ã ãç§ããåã«å¤æãã1kmãããã®ãã¼ã¹ãè¨ç® running_data['ã©ã³ã¿ã¤ã (å)'] = running_data['ã©ã³ã¿ã¤ã (ç§)'] / 60 running_data['ãã¼ã¹ (å/km)'] = running_data['ã©ã³ã¿ã¤ã (å)'] / running_data['è·é¢ (km)'] # é±åä½ã®åæ running_data['ISOé±çªå·'] = running_data.index.isocalendar().week weekly_data = running_data.groupby('ISOé±çªå·').agg({ 'ãã¼ã¹ (å/km)': 'mean', 'è·é¢ (km)': ['sum', 'count'] }) weekly_data.columns = ['é±éãã¼ã¹ (å/km)', 'é±éç·èµ°è¡è·é¢', 'é±éã¯ã¼ã¯ã¢ã¦ãåæ°'] weekly_data['é±éå¹³åèµ°è¡è·é¢'] = weekly_data['é±éç·èµ°è¡è·é¢'] / weekly_data['é±éã¯ã¼ã¯ã¢ã¦ãåæ°'] # æåä½ã®åæ running_data['å¹´æ'] = running_data.index.to_period('M') monthly_data = running_data.groupby('å¹´æ').agg({ 'ãã¼ã¹ (å/km)': 'mean', 'è·é¢ (km)': ['sum', 'count'] }) monthly_data.columns = ['æéãã¼ã¹ (å/km)', 'æéç·èµ°è¡è·é¢', 'æéã¯ã¼ã¯ã¢ã¦ãåæ°'] monthly_data['æéå¹³åèµ°è¡è·é¢'] = monthly_data['æéç·èµ°è¡è·é¢'] / monthly_data['æéã¯ã¼ã¯ã¢ã¦ãåæ°'] # æ¥æ¬èªãã©ã³ãã®è¨å® plt.rcParams['font.family'] = japanese_font.get_name() # é±åä½ã®ã°ã©ãä½æ fig, ax1 = plt.subplots(figsize=(16, 9)) ax1.set_xlabel('ISOé±çªå·', fontproperties=japanese_font) ax1.set_ylabel('èµ°è¡è·é¢ (km)', fontproperties=japanese_font) ax1.bar(weekly_data.index, weekly_data['é±éç·èµ°è¡è·é¢'], color='lightblue', width=0.4, label='é±éç·èµ°è¡è·é¢', align='center', zorder=1) ax1.bar(weekly_data.index + 0.4, weekly_data['é±éå¹³åèµ°è¡è·é¢'], color='green', width=0.4, label='é±éå¹³åèµ°è¡è·é¢', align='center', zorder=2) ax1.tick_params(axis='x', labelsize=8) ax1.tick_params(axis='y') ax2 = ax1.twinx() ax2.set_ylabel('é±éãã¼ã¹ (å/km)', fontproperties=japanese_font) ax2.plot(weekly_data.index, weekly_data['é±éãã¼ã¹ (å/km)'], color='red', label='é±éãã¼ã¹ (å/km)', zorder=3) ax2.tick_params(axis='y', labelcolor='red') fig.legend(loc='upper left', bbox_to_anchor=(0.1, 0.9), prop=japanese_font) plt.title('ISOé±çªå·å¥ ã©ã³ãã³ã°ããã©ã¼ãã³ã¹', fontproperties=japanese_font) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") weekly_output_path = f"WeeklyAnalysis-{timestamp}.png" plt.savefig(weekly_output_path) plt.show() # æåä½ã®ã°ã©ãä½æ fig, ax1 = plt.subplots(figsize=(16, 9)) bar_width = 0.35 months = range(len(monthly_data)) bars1 = ax1.bar(months, monthly_data['æéç·èµ°è¡è·é¢'], width=bar_width, color='lightblue', label='æéç·èµ°è¡è·é¢', align='center') bars2 = ax1.bar([x + bar_width for x in months], monthly_data['æéå¹³åèµ°è¡è·é¢'], width=bar_width, color='green', label='æéå¹³åèµ°è¡è·é¢', align='center') for bar in bars1: yval = bar.get_height() ax1.text(bar.get_x() + bar.get_width()/2, yval + 0.5, round(yval, 1), va='bottom', ha='center', fontsize=12, color='black') for bar in bars2: yval = bar.get_height() ax1.text(bar.get_x() + bar.get_width()/2, yval + 0.5, round(yval, 1), va='bottom', ha='center', fontsize=12, color='black') month_labels = [f'{m+1}æ' for m in monthly_data.index.month - 1] ax1.set_xticks([x + bar_width / 2 for x in months]) ax1.set_xticklabels(month_labels) ax1.set_xlabel('æ', fontproperties=japanese_font) ax1.set_ylabel('èµ°è¡è·é¢ (km)', fontproperties=japanese_font) ax1.tick_params(axis='x', labelsize=8) ax1.tick_params(axis='y') ax2 = ax1.twinx() ax2.set_ylabel('æéãã¼ã¹ (å/km)', fontproperties=japanese_font) ax2.plot(months, monthly_data['æéãã¼ã¹ (å/km)'], color='red', label='æéãã¼ã¹ (å/km)') ax2.tick_params(axis='y', labelcolor='red') fig.legend(loc='upper left', bbox_to_anchor=(0.1, 0.9), prop=japanese_font) plt.title('å¹´æå¥ ã©ã³ãã³ã°ããã©ã¼ãã³ã¹', fontproperties=japanese_font) monthly_output_path = f"MonthlyAnalysis-{timestamp}.png" plt.savefig(monthly_output_path) plt.show() # Markdownã®ã³ã¼ãå½¢å¼ã§è¡¨ãããã¹ããã¡ã¤ã«ã«æ¸ãåºã weekly_markdown = tabulate(weekly_data, headers='keys', tablefmt='pipe', showindex=True) monthly_markdown = tabulate(monthly_data, headers='keys', tablefmt='pipe', showindex=True) markdown_output_path = f"Markdown-table-{timestamp}.txt" with open(markdown_output_path, 'w', encoding='utf-8') as f: f.write("### é±ãã¨ã®ã©ã³ãã³ã°ãã¼ã¿\n") f.write(weekly_markdown) f.write("\n\n### æãã¨ã®ã©ã³ãã³ã°ãã¼ã¿\n") f.write(monthly_markdown) print("\n### é±ãã¨ã®ã©ã³ãã³ã°ãã¼ã¿\n") print(weekly_markdown) print("\n### æãã¨ã®ã©ã³ãã³ã°ãã¼ã¿\n") print(monthly_markdown) print(f"é±åä½ã®ã°ã©ã㯠{weekly_output_path} ã«ä¿åããã¾ããã") print(f"æåä½ã®ã°ã©ã㯠{monthly_output_path} ã«ä¿åããã¾ããã") print(f"Markdownå½¢å¼ã®è¡¨ã¯ {markdown_output_path} ã«ä¿åããã¾ããã")
ä½æããã°ã©ã
ãã¼ã¿ãèç©ããã¦ãPythonã®ãã¡ã¤ã«ãå®è¡ããã°èªåçã«ä¸è¨ã®ã°ã©ããä½æããã¾ãã
Markdownã§è¡¨ãä½æ
ãã¡ããPythonã³ã¼ããå®è¡ããã°ãèªåçã«ããã¹ããã¡ã¤ã«ã«æ¸ãåºãããã«å¤æ´ãã¾ããã
é±ãã¨ã®ã©ã³ãã³ã°ãã¼ã¿
ISOé±çªå· | é±éãã¼ã¹ (å/km) | é±éç·èµ°è¡è·é¢ | é±éã¯ã¼ã¯ã¢ã¦ãåæ° | é±éå¹³åèµ°è¡è·é¢ |
---|---|---|---|---|
9 | 8.24618 | 1.09 | 1 | 1.09 |
11 | 10.5731 | 1.55 | 1 | 1.55 |
12 | 8.54601 | 1.92 | 1 | 1.92 |
13 | 8.91069 | 2.09 | 1 | 2.09 |
14 | 9.27627 | 1.84 | 1 | 1.84 |
15 | 9.65168 | 4.5 | 3 | 1.5 |
16 | 8.66516 | 9.15 | 5 | 1.83 |
17 | 8.37914 | 11.94 | 6 | 1.99 |
18 | 8.43324 | 14.97 | 6 | 2.495 |
19 | 8.61574 | 12.35 | 5 | 2.47 |
20 | 8.20327 | 5.24 | 2 | 2.62 |
æãã¨ã®ã©ã³ãã³ã°ãã¼ã¿
å¹´æ | æéãã¼ã¹ (å/km) | æéç·èµ°è¡è·é¢ | æéã¯ã¼ã¯ã¢ã¦ãåæ° | æéå¹³åèµ°è¡è·é¢ |
---|---|---|---|---|
2024-02 | 8.24618 | 1.09 | 1 | 1.09 |
2024-03 | 9.34327 | 5.56 | 3 | 1.85333 |
2024-04 | 8.7799 | 32.26 | 17 | 1.89765 |
2024-05 | 8.42348 | 27.73 | 11 | 2.52091 |
ç¾ç¶
å¿ææ°ãã³ã³ããã¼ã«ããå¿ è¦ããªããªã£ã
3ï½4é±åã¯èµ°ãè·é¢ã伸ã°ãããã«å¿ææ°ãä¸ããããªãããã«èµ°ã£ã¦ãã¾ãããæ¯ãä¸ãã£ã¦ããã©ãã£ãããã§ããããã®2é±éã¯æ¯ãä¸ãããããããµããã¯ãã®å¼µãã®æç¡ãã©ã³ãã³ã°ã®è·é¢ã¨ãã¼ã¹ãå·¦å³ããããã«ãªã£ã¦ããã
ä¸è¨ã®ã°ã©ãã§ã¯æãç·ã°ã©ãã®ã©ã³ãã³ã°ãã¼ã¹ãæ©ããªã£ã¦ãããã®ã®ãå¿ææ°ï¼æ大平åï¼ãä¸ãã£ã¦ããã客観çã«ãå¿èºæ©è½ã¸ã®è² æ ãæ¸ã£ã¦ããããçèã®å¼µãã®ãããªã©ã³ãã³ã°ãã©ã¼ã ç±æ¥ã®åé¡ã大ãããªã£ã¦ããã
èµ°ãæ¹ãèããã
ä»å¾ã¯è² æ ãå°ãªãã©ã³ãã³ã°ãã©ã¼ã ãèãã¦ããããã¨æãã¾ãã
ã¾ã¨ã
ã©ã³ãã³ã°ã®ãã°ãå¯è¦åããããã®Pythonã³ã¼ããã¢ãããã¼ããã¾ããã1ã³ã¼ã1æ©è½ã§éç¨ãã¦ãã¾ããããæ©è½ãåºã¾ã£ã¦ããã®ã§è¤æ°ã®æ©è½ãçµ±åããã³ã¼ãã«å¤æ´ãã¾ããã
ã¡ããã©ChatGPTãGPT-4oã«ãã¼ã¸ã§ã³ã¢ããããã¨ããã§ãé·ãã³ã¼ããéåãããã¨ãªãä½ã£ã¦ãããããã«ãªãã¾ããã
é ã ã¨ãã¦ãããã®ã®ã©ã³ãã³ã°è½åã¯æ¹åãã¦ã¦ãã¾ãããã ä½éãã»ã¨ãã©å¤ããã¾ãããããã¯10å¹´åã®ãã©ã½ã³ã®æãããã§ããã
ããã¾ã§ã¯é£ãããåã®ãããã¨æã£ã¦ãã¾ããããæ¨æ¥ç·æ§ãã«ã¢ã³ï¼ãã¹ãã¹ããã³ï¼ã®åæ³éã人ãããå°ãªãå¯è½æ§ãåºã¦ãã¾ãããç·æ§ãã«ã¢ã³ãæ¸ãã¨å¤ªãããããªãããã§ãã
ç·æ§ãã«ã¢ã³èµ°ããã¨ã«å ãã¦ãçãã¬ãçµã¿åããã¦ããããã§ãããããã°ã«ã©ã³ãã³ã°ããã¬ã¼ãã³ã°ã¾ã§è¿½å ããã¨æéãåé¡ã«ãªãã¾ããã