{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84795,"databundleVersionId":10462807,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"2024年12月12日公開 著者: Prata, Marília (mpwolke)","metadata":{"_uuid":"3947a852-316e-4bbe-bcc1-dd72803938c1","_cell_guid":"c744669f-5cce-4438-a5b3-8ace5281bba7","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"# 必要なライブラリのインポート\nimport numpy as np # 線形代数用\nimport pandas as pd # データ処理、CSV入出力用\n\nimport matplotlib.pyplot as plt \nimport seaborn as sns\n\n# Plotlyグラフ表示に必要な2行\nimport plotly.io as pio\npio.renderers.default = 'iframe'\n\nimport plotly.graph_objs as go\nimport plotly.offline as py\nimport plotly.express as px\n\n# 警告を無視\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# ファイルの確認\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"9fca78ef-c850-4473-b59f-7e6d969c0418","_cell_guid":"8c10b739-7227-49ae-a4bc-9c391f745bba","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-06T00:57:56.542965Z","iopub.execute_input":"2025-01-06T00:57:56.543481Z","iopub.status.idle":"2025-01-06T00:57:59.664919Z","shell.execute_reply.started":"2025-01-06T00:57:56.543434Z","shell.execute_reply":"2025-01-06T00:57:59.663795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"SWE-benchは、GitHubの実際のソフトウェア課題に対する大規模言語モデルの評価ベンチマークです。\nコードベースと課題が与えられた時に、言語モデルは問題を解決するパッチを生成することが求められます。","metadata":{"_uuid":"350c20b3-7659-4d77-b297-ceca090ebb32","_cell_guid":"2a083ae0-b53d-4d56-9496-a9c647541efb","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"# ZIPファイルの展開\n!unzip -n ../input/konwinski-prize/data.a_zip\n\n# データの読み込み\nimport zipfile\nkonwinski = zipfile.ZipFile('../input/konwinski-prize/data.a_zip')\nkonwinski.extractall()\n\ntrain_data = pd.read_parquet(\"data/data.parquet\")","metadata":{"_uuid":"8f337229-6adc-4785-8cb5-1a00fd3078cd","_cell_guid":"0c6872fa-482a-4ea9-9800-fc17e4bd13b5","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-06T00:57:59.666323Z","iopub.execute_input":"2025-01-06T00:57:59.66705Z","iopub.status.idle":"2025-01-06T00:58:06.544042Z","shell.execute_reply.started":"2025-01-06T00:57:59.667007Z","shell.execute_reply":"2025-01-06T00:58:06.542861Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"データの内容:\n- instance_id: インスタンス(GitHub issue)の一意の識別子\n- repo: 対象のGitHubリポジトリ \n- problem_statement: 課題の説明文\n- patch: トレーニングセットのみ。課題を解決するパッチ\n- test_patch: トレーニングセットのみ。課題を解決するパッチ\nなど","metadata":{"_uuid":"a74e262f-1745-4eda-8b9a-996d738edb1c","_cell_guid":"a3bf0cec-d831-4643-bd02-26cf9df1bd0e","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"# データの確認\ntrain_data.head()\ntrain_data.info()\n\n# 欠損値の確認\nprint(\"各カラムの欠損値数:\\n\", train_data.isnull().sum())","metadata":{"_uuid":"963739db-3101-41ce-827c-31c4d7e1f751","_cell_guid":"b4381fb6-247f-4d26-9946-10cdcd12142d","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-06T00:58:06.545273Z","iopub.execute_input":"2025-01-06T00:58:06.545665Z","iopub.status.idle":"2025-01-06T00:58:06.584268Z","shell.execute_reply.started":"2025-01-06T00:58:06.545636Z","shell.execute_reply":"2025-01-06T00:58:06.58299Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"GitHubリポジトリの分布\nAstropyが大多数を占めています","metadata":{"_uuid":"4be01afd-77d0-44a9-b7f4-0419e3c8ccd8","_cell_guid":"901a2024-a709-47fd-92d7-790dac0a88bb","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"# リポジトリの円グラフ表示\nrepo = train_data['repo'].value_counts()\nplt.pie(repo.values,\n        labels=repo.index,\n        autopct='%1.1f%%')\nplt.title('GitHubリポジトリの分布')\nplt.show()","metadata":{"_uuid":"4e5d89ab-b7dd-47fb-b834-b3b7b70cd66b","_cell_guid":"7ca755b4-aae5-48e3-b02a-c11dbf6295f2","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-06T00:58:06.58567Z","iopub.execute_input":"2025-01-06T00:58:06.586166Z","iopub.status.idle":"2025-01-06T00:58:06.772425Z","shell.execute_reply.started":"2025-01-06T00:58:06.586128Z","shell.execute_reply":"2025-01-06T00:58:06.770293Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"問題文の分析\nproblem_statement - 課題を説明するテキスト。評価APIでも提供される。","metadata":{"_uuid":"b292cca2-86ee-4a35-a344-97b1695118a0","_cell_guid":"77acb189-33bc-4fe6-b057-8336f5932e05","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"# ワードクラウドの作成\nfrom wordcloud import WordCloud, STOPWORDS\n\ndef plot_wordcloud(text, mask=None, max_words=200, max_font_size=100, figure_size=(24.0,16.0), \n                   title = None, title_size=40, image_color=False):\n    # ストップワードの設定\n    stopwords = set(STOPWORDS)\n    more_stopwords = {'one', 'br', 'Po', 'th', 'sayi', 'fo', 'Unknown'}\n    stopwords = stopwords.union(more_stopwords)\n\n    # ワードクラウドの生成\n    wordcloud = WordCloud(background_color='white',\n                    color_func=lambda *args, **kwargs: \"black\",      \n                    stopwords = stopwords,\n                    max_words = max_words,\n                    max_font_size = max_font_size, \n                    random_state = 42,\n                    width=800, \n                    height=400,\n                    mask = mask)\n    wordcloud.generate(str(text))\n    \n    # プロット設定\n    plt.figure(figsize=figure_size)\n    if image_color:\n        image_colors = ImageColorGenerator(mask)\n        plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation=\"bilinear\")\n        plt.title(title, fontdict={'size': title_size,  \n                                  'verticalalignment': 'bottom'})\n    else:\n        plt.imshow(wordcloud)\n        plt.title(title, fontdict={'size': title_size, 'color': 'black', \n                                  'verticalalignment': 'bottom'})\n    plt.axis('off')\n    plt.tight_layout()  \n    \n# 問題文のワードクラウドを表示\nplot_wordcloud(train_data[\"problem_statement\"], title=\"問題文に含まれる単語の分布\")","metadata":{"_uuid":"b11e0f71-8569-43ad-96ef-08061cf9736d","_cell_guid":"f257c867-4ab0-4218-b14b-3496b0fd2881","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-06T00:58:06.773452Z","iopub.execute_input":"2025-01-06T00:58:06.773915Z","iopub.status.idle":"2025-01-06T00:58:08.355657Z","shell.execute_reply.started":"2025-01-06T00:58:06.773866Z","shell.execute_reply":"2025-01-06T00:58:08.354377Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Fail to Passの分析\nテスト失敗から合格への過程で頻出する単語を可視化","metadata":{"_uuid":"68980335-6f76-4c55-a489-03cc99fb1815","_cell_guid":"702ec5b1-3b30-4415-8380-e828ef97c225","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"from plotly import tools\nfrom collections import defaultdict\n\n# N-gramを生成するカスタム関数\ndef generate_ngrams(text, n_gram=1):\n    text = str(text)\n    token = [token for token in text.lower().split(\" \") if token != \"\" if token not in STOPWORDS]\n    ngrams = zip(*[token[i:] for i in range(n_gram)])\n    return [\" \".join(ngram) for ngram in ngrams]\n\n# 水平棒グラフ作成用のカスタム関数\ndef horizontal_bar_chart(df, color):\n    trace = go.Bar(\n        y=df[\"word\"].values[::-1],\n        x=df[\"wordcount\"].values[::-1],\n        showlegend=False,\n        orientation = 'h',\n        marker=dict(\n            color=color,\n        ),\n    )\n    return trace\n\n# Fail to Passの単語頻度分析\nfreq_dict = defaultdict(int)\nfor sent in train_data[\"FAIL_TO_PASS\"]:\n    for word in generate_ngrams(sent):\n        freq_dict[word] += 1\nfd_sorted = pd.DataFrame(sorted(freq_dict.items(), key=lambda x: x[1])[::-1])\nfd_sorted.columns = [\"word\", \"wordcount\"]\ntrace1 = horizontal_bar_chart(fd_sorted.head(50), 'black')\n\n# サブプロットの作成\nfig = tools.make_subplots(rows=1, cols=2, vertical_spacing=0.04,\n                          subplot_titles=[\"Fail to Pass での頻出単語\"]) \nfig.append_trace(trace1, 1, 2)\nfig['layout'].update(height=1200, width=900, paper_bgcolor='rgb(233,233,233)', \n                    title=\"単語出現頻度の分析\")\npy.iplot(fig, filename='word-plots')","metadata":{"_uuid":"e6bb6506-a20b-4a5d-99d2-3ab63f54adba","_cell_guid":"708ad0be-964a-469e-bd95-f710ec2b8c73","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-06T00:58:08.35765Z","iopub.execute_input":"2025-01-06T00:58:08.358142Z","iopub.status.idle":"2025-01-06T00:58:09.471701Z","shell.execute_reply.started":"2025-01-06T00:58:08.358115Z","shell.execute_reply":"2025-01-06T00:58:09.470287Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"SWE-benchへのアクセス方法","metadata":{"_uuid":"89eb6cd9-5018-4405-b8cc-23d9b76bfb37","_cell_guid":"9cea2c7b-e230-4f40-aa14-343845a73750","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"# SWE-benchデータセットの読み込み\nfrom datasets import load_dataset\nswebench = load_dataset('princeton-nlp/SWE-bench', split='test')","metadata":{"_uuid":"9f99073e-33e1-4284-84e7-947eb6fb3a70","_cell_guid":"c44f4694-e4de-47ea-9423-975aa29b33be","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-06T00:58:09.473058Z","iopub.execute_input":"2025-01-06T00:58:09.473389Z","iopub.status.idle":"2025-01-06T00:58:16.815242Z","shell.execute_reply.started":"2025-01-06T00:58:09.473325Z","shell.execute_reply":"2025-01-06T00:58:16.814176Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"謝辞：\nSRK氏のノートブックを参考にさせていただきました\nhttps://www.kaggle.com/code/sudalairajkumar/simple-exploration-notebook-qiqc","metadata":{"_uuid":"312e974c-6300-4914-918a-9fac54944239","_cell_guid":"4ae4cba1-29c9-48d2-a603-477825fe78db","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"markdown","source":"MPwolkeによる作業終了。作業時間：2時間09分","metadata":{"_uuid":"9b342c5b-90d2-4d39-8669-9ae3fc5de4fc","_cell_guid":"9c0f85c0-7280-443e-a73d-c3c6aaba17d2","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}}]}