


[{"content":" First-Principles Calculations # First-principles calculations are computational methods for investigating the electronic structure of materials based on quantum theory. In our research group, we conduct research using first-principles software packages such as VASP and Quantum ESPRESSO.\n","externalUrl":null,"permalink":"/en/research/first-principles/","section":"Research","summary":"","title":"First-Principles Materials Research","type":"research"},{"content":"","externalUrl":null,"permalink":"/notes/python/matplotlib/","section":"授業・研究ノート","summary":"Maplotlibの基本","title":"Matplotlib","type":"notes"},{"content":" NumPyとは # NumPyはベクトルや行列の演算を高速に行うことが可能な多次元配列のためのライブラリ． 中身はC言語等で実装されているので高速．\nここでは，機械学習の基礎を学ぶ際に使用するものを中心に， NumPyの基礎を記しておく． jupyterでの使用を想定していて，最後に変数だけ書いた行はその変数情報を表示するために書いてある．\nimport # import numpy as np NumPyの配列（ndarray） # 1次元はベクトルで，2次元は行列． 1次元配列に縦ベクトル，横ベクトルの区別はない（転置しても変化無し）． リストをndarrayに変換 # x = [1, 2] x = np.array(x) x # jupyterで変数の情報を表示 出力\narray([1, 2]) np.array()で変換 2次元配列 # A = np.array([[1, 2], [3, 4]]) A # jupyterで出力用 出力\narray([[1, 2], [3, 4]]) 転置 # ndarray.Tで2次元以上の配列は転置がとれる．\n1次元配列 # x.T 出力\narray([1, 2]) 1次元配列は変わらない．縦ベクトル，横ベクトルの様な区別はない． 2次元配列 # A.T 出力\narray([[1, 3], [2, 4]]) 配列の形状 # C = np.array([[1, 2, 3], [4, 5, 6]]) C # jupyterで出力用 出力\narray([[1, 2, 3], [4, 5, 6]]) ndarray.shapeで形状を確認できる．\nC.shape 出力\n(2, 3) 配列の形状の変更 # ndarray.reshape()で配列の形状を変更できる．\n上記の2行3列の \\( C \\) を3行2列に変更\nC.reshape(3, 2) 出力\narray([[1, 2], [3, 4], [5, 6]]) reshapeでは引数に-1を指定すれば自動でうまく形状変更してくれる．\nC.reshape(3, -1) # 3行だけ指定 出力\narray([[1, 2], [3, 4], [5, 6]]) ","externalUrl":null,"permalink":"/notes/python/numpy/basics/","section":"授業・研究ノート","summary":"NumPy入門","title":"NumPyの基本","type":"notes"},{"content":"","externalUrl":null,"permalink":"/notes/python/","section":"授業・研究ノート","summary":"MaplotlibやNumPyの使い方","title":"Python","type":"notes"},{"content":" グラフを描く時によく使う設定 # 好みもあるので，各自調整するとよい\n2025/12/31 更新\nimport matplotlib.pyplot as plt # ---------- rcParams rcParams_dict = { # ---------- figure \u0026#39;figure.figsize\u0026#39;: (8, 6), \u0026#39;figure.dpi\u0026#39;: 120, \u0026#39;figure.facecolor\u0026#39;: \u0026#39;white\u0026#39;, # ---------- axes \u0026#39;axes.grid\u0026#39;: True, \u0026#39;axes.linewidth\u0026#39;: 1.5, \u0026#39;axes.labelsize\u0026#39;: 20, # ---------- ticks \u0026#39;xtick.direction\u0026#39;: \u0026#39;in\u0026#39;, \u0026#39;ytick.direction\u0026#39;: \u0026#39;in\u0026#39;, \u0026#39;xtick.major.width\u0026#39;: 1.0, \u0026#39;ytick.major.width\u0026#39;: 1.0, \u0026#39;xtick.major.size\u0026#39;: 8.0, \u0026#39;ytick.major.size\u0026#39;: 8.0, \u0026#39;xtick.labelsize\u0026#39;: 16, \u0026#39;ytick.labelsize\u0026#39;: 16, # ---------- lines \u0026#39;lines.linewidth\u0026#39;: 2.0, \u0026#39;lines.markersize\u0026#39;: 10, # ---------- grid \u0026#39;grid.linestyle\u0026#39;: \u0026#39;:\u0026#39;, # ---------- legend \u0026#39;legend.fontsize\u0026#39;: 20, # ---------- other fonts \u0026#39;font.size\u0026#39;: 20, \u0026#39;font.family\u0026#39;: \u0026#39;sans-serif\u0026#39;, \u0026#39;font.sans-serif\u0026#39;: [\u0026#39;Helvetica Neue\u0026#39;, \u0026#39;Arial\u0026#39;, \u0026#39;Liberation Sans\u0026#39;, \u0026#39;DejaVu Sans\u0026#39;, \u0026#39;sans\u0026#39;], \u0026#39;mathtext.fontset\u0026#39;: \u0026#39;cm\u0026#39;, # use together with svg.fonttype=\u0026#39;path\u0026#39; #\u0026#39;mathtext.fontset\u0026#39;: \u0026#39;stix\u0026#39;, \u0026#39;svg.fonttype\u0026#39;: \u0026#39;path\u0026#39;, # Embed characters as paths #\u0026#39;svg.fonttype\u0026#39;: \u0026#39;none\u0026#39;, # Assume fonts are installed on the machine \u0026#39;pdf.fonttype\u0026#39;: 42, # embed fonts in PDF using type42 (True type) # ---------- save \u0026#39;savefig.bbox\u0026#39;: \u0026#39;tight\u0026#39;, \u0026#39;savefig.pad_inches\u0026#39;: 0.05, } plt.rcParams.update(rcParams_dict) \u0026lsquo;Helvetica Neue\u0026rsquo;, \u0026lsquo;Arial\u0026rsquo;, \u0026lsquo;Liberation Sans\u0026rsquo;, \u0026lsquo;DejaVu Sans\u0026rsquo;, \u0026lsquo;sans\u0026rsquo; ←左から優先．sans serif系にしている． 数式：cmフォント．（少し古い）latexの数式で使われていて見た目は綺麗．しかしモダンなフォントではないのでsvgと相性が悪い．'svg.fonttype': 'path'とすることで，文字ではなくpathで保存している．文字として保存したければ，'mathtext.fontset': 'stix'と'svg.fonttype': 'none'を組み合わせると良いが，stixは一部フォントがイマイチ． プロット例 # ここでは，plt.plot() のような書き方ではなく fig, ax = plt.subplots(1, 1) のようなオブジェクト指向インターフェースを使用．こちらの書き方に慣れた方が細かい設定ができて便利．\n図のサイズを変えたければ fig, ax = plt.subplots(1, 1, figsize=(8, 8)) のようにしてサイズ指定することもできる．\nfig, ax = plt.subplots(1, 1) ax.plot([1, 2 ,3, 4], label=\u0026#39;$y = x$\u0026#39;) ax.set_xlabel(\u0026#39;test $x$\u0026#39;) ax.set_ylabel(\u0026#39;test $y$\u0026#39;) ax.legend() 保存 # # ---------- save figure fig.savefig(\u0026#39;fig.svg\u0026#39;) # SVG #fig.savefig(\u0026#39;fig.pdf\u0026#39;) # PDF #fig.savefig(\u0026#39;fig.png\u0026#39;) # PNG #fig.savefig(\u0026#39;fig.png\u0026#39;, dpi=300) # high dpi PNG 最初の設定でplt.rcParams['figure.facecolor'] = 'white' としているので背景は白． これがない場合はグラフ領域外（軸とか軸ラベルの部分）は無色透明になる． svgでは'svg.fonttype': 'path'にしてpathにして形状だけを保存するようにしているので，フォントが入ってないPCでも表示が崩れない． pdfで保存するときも上記で'pdf.fonttype': 42のように設定しているので，フォントが埋め込まれて見た目が崩れない．ただしsvgより多少ファイルサイズが大きくなり，再編集には不向き． ","externalUrl":null,"permalink":"/notes/python/matplotlib/setting_save/","section":"授業・研究ノート","summary":"Matplotlibの使い方","title":"よく使う設定と保存","type":"notes"},{"content":" 機械学習のための線形代数学 # 機械学習のための線形代数学\n最小二乗法 # 最小二乗法\n","externalUrl":null,"permalink":"/notes/linear_regression/basics/","section":"授業・研究ノート","summary":"線形代数の基礎と最小二乗法の理論","title":"機械学習のための線形代数の基礎と最小二乗法","type":"notes"},{"content":"このページでは省略しているが， よく使う設定と保存 に書かれている設定とnumpyのインポートを用いている．\nax.plot()の構文 # ax.plot() は多機能なので色々な引数があるが，よく使用するものは下記.\nax.plot( x, y, color=\u0026#39;red\u0026#39;, marker=\u0026#39;o\u0026#39;, linestyle=\u0026#39;-\u0026#39;, linewidth=2, markersize=10, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;label\u0026#39;, ) x, y は座標．list や ndarray などの ArrayLike color, marker, linestyleはよく省略されて\nax.plot(x, y, \u0026#39;ro-\u0026#39;) のように書くことも多い．これは下記の意味．\nax.plot(x, y, color=\u0026#39;red\u0026#39;, marker=\u0026#39;o\u0026#39;, linestyle=\u0026#39;-\u0026#39;) linestyle # カスタマイズすればより詳細な調整も可能ではあるが，基本的には以下の4種類．\n値 線種 '-' 実線 '--' 破線 ':' 点線 '-.' 一点鎖線 また，linestyle=''やlinestyle='none'のように書けば線なしにできる．\n# ---------- x, y x = np.array([0, 1, 2, 3]) y = np.array([1, 2, 3, 4]) # ---------- plot fig, ax = plt.subplots(1, 1) ax.plot(x, y, linestyle=\u0026#39;-\u0026#39;, marker=\u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;solid\u0026#39;) ax.plot(x, y + 1, linestyle=\u0026#39;--\u0026#39;, marker=\u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;dashed\u0026#39;) ax.plot(x, y + 2, linestyle=\u0026#39;:\u0026#39;, marker=\u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;dotted\u0026#39;) ax.plot(x, y + 3, linestyle=\u0026#39;-.\u0026#39;, marker=\u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;dashdot\u0026#39;) ax.set_xlabel(\u0026#39;test $x$\u0026#39;) ax.set_ylabel(\u0026#39;test $y$\u0026#39;) ax.legend() marker # Maplotlib公式ドキュメント に使用できるマーカーが一覧がある． よく使用されるものを取り上げたのが下の表．\n値 マーカー 'o' 円 's' 四角 '^' 上向き三角 'D' ひし形 マーカーの種類の他にも関連オプションがある．'none'にすると塗りつぶしや枠線を無しにできる．'none'は小文字で特殊なので注意．color で設定すると，線やマーカーなどまとめて色が設定されるが，markerfacecolor や markeredgecolor がマーカーでは優先されるので個別に設定できる．\nオプション 説明 markersize マーカーの大きさ markerfacecolor マーカー内部の色 markeredgecolor マーカーの枠線の色 markeredgewidth マーカーの枠線の太さ alpha 透明度 次の図はマーカーの一例 カラーサイクル # デフォルトのカラーサイクルは10色． 何も色を指定しなければ順番に色が変わって10色で一周する．\n# ---------- x, y x = np.array([0, 1, 2, 3]) y = np.array([1, 2, 3, 4]) # ---------- plot fig, ax = plt.subplots(1, 1) for i in range(12): ax.plot(x, y + i) ax.set_xlabel(\u0026#39;test $x$\u0026#39;) ax.set_ylabel(\u0026#39;test $y$\u0026#39;) カラーサイクルの n 番目の色を使いたい場合，例えば0番目なら'C0'で使用できる．\n# ---------- x, y x = np.array([0, 1, 2, 3]) y = np.array([1, 2, 3, 4]) # ---------- plot fig, ax = plt.subplots(1, 1) for i in range(12): ax.plot(x, y + i, color=\u0026#39;C0\u0026#39;) ax.set_xlabel(\u0026#39;test $x$\u0026#39;) ax.set_ylabel(\u0026#39;test $y$\u0026#39;) ","externalUrl":null,"permalink":"/notes/python/matplotlib/lines_markers/","section":"授業・研究ノート","summary":"ax.plot()の基本的な使い方","title":"ax.plot()の基本","type":"notes"},{"content":" Materials Informatics # Materials informatics is a research field that applies information-science techniques, such as artificial intelligence and machine learning, to materials development. It aims to discover new principles governing material properties and to utilize them for the design of novel materials.\nIn our research group, these approaches are applied to crystal structure prediction using Bayesian optimization and to battery materials design using machine-learning potentials.\n","externalUrl":null,"permalink":"/en/research/mi/","section":"Research","summary":"","title":"Materials Informatics","type":"research"},{"content":"","externalUrl":null,"permalink":"/notes/python/numpy/","section":"授業・研究ノート","summary":"NumPyの基本","title":"NumPy","type":"notes"},{"content":" np.arange() # step（刻み幅）を指定して等間隔な値の配列を生成する．整数列や決まった幅を持つ配列が欲しい時によく使用される．\nnp.arange([start,] stop[, step,], dtype=None)\nstartを省略した場合のデフォルト値は0 stepを省略した場合のデフォルト値は1 np.arange(10) # 0から10まで１刻み（10は含まない） 出力\narray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) np.linspace() # 要素数を指定して等間隔な値の配列を生成する．グラフを描くときや数値計算でよく用いられる．\nnp.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0,)\nデフォルトではstopの値も含まれる（endpoint=True） np.linspace(0, 10, 4) # 0から10まで，要素数4 出力\narray([ 0. , 3.33333333, 6.66666667, 10. ]) np.logspace # 対数スケールで等間隔な値の配列を生成する．ハイパーパラメータ探索などでよく用いられる．\nnp.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)\nデフォルトではbase=10.0（対数の底） デフォルトではstopの値も含まれる（endpoint=True） np.logspace(0, 3, 4) # 10^0 から 10^3 まで，要素数4 出力\narray([ 1., 10., 100., 1000.]) ","externalUrl":null,"permalink":"/notes/python/numpy/range/","section":"授業・研究ノート","summary":"arange()とlinspace()","title":"指定した範囲の離散化","type":"notes"},{"content":" はじめに # この演習では，パターン認識と機械学習 上 (PRML 上)の第1章に出てくる多項式回帰を再現することで，線形回帰の基礎を学習する． データはsin関数にガウスノイズを乗せたものを10点用意し，0次，1次，3次および9次の多項式で回帰する． 正規方程式を NumPy のライブラリを使って解く． ある程度コードの骨格は載せているが，??????の部分などを自分で考えて書くこと．\nNumPy とグラフの描画に Matplotlib を用いるので先に下記のページを見てそれぞれの基本を学んでおくこと．\nPython \u0026gt; Matplotlib Python \u0026gt; NumPy Jupyter Notebookでの演習を想定．下記のようにライブラリのインポートも必要． ここに出てくるグラフ出力例は Matplotlib \u0026gt; よく使う設定と保存 の設定も用いている．\nimport numpy as np import matplotlib.pyplot as plt 演習1：sin関数のプロット # この演習での目的関数は $$ y = \\sin(2\\pi x) $$ ここで， \\(x\\) の範囲は \\(0 \\le x \u003c 1 \\) を考える．\nsin関数のプロットは NumPy \u0026gt; 数学関数 のページに書いてあるので，そのまま用いる．ただし， 変数x，yは後で別のもので使いたいので，それぞれ x_denseおよびy_sinに変更してある．\n# ---------- x, y x_dense = np.linspace(0, 1, 100) y_sin = np.sin(2 * np.pi * x_dense) # ---------- plot fig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend() 自分でプロットして確認すること．\n演習2：訓練データ # 訓練データとして，sin関数にガウスノイズを加えた10個のデータを作成する． 下記の手順にしたがって訓練データを生成し，sin関数とともにプロットすること． 一様乱数やガウス分布に従う乱数生成については NumPy \u0026gt; 乱数 を参考．\n\\(0 \\le x \u003c 1 \\) の範囲で，ランダムに10点とった1次元配列（ndarray）x_trainを作る． x_trainに対して，\\(y_\\mathrm{train} = \\sin(2\\pi x_\\mathrm{train})\\) を計算し，同サイズの1次元配列y_trainを作る． 平均0，標準偏差0.1のガウス分布に従う乱数を10点生成して，同サイズの1次元配列noiseを作る． y_trainにnoiseを加えて更新して，sin関数とともにプロットする 乱数の再現性が欲しい場合はrng = np.random.default_rng(123)のように何か適当な数値を seed として入れておくとよい rng = np.random.default_rng() x_train = ?????? x_train # jupyterで出力用 y_train = ?????? # sin y_train # jupyterで出力用 noise = ?????? # ガウスノイズ noise # jupyterで出力用 y_train = y_train + noise # ガウスノイズを加えて更新 # ---------- plot fig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend() 演習3：計画行列 # 最終的には以下の4つのモデルで，演習2で生成したデータ10点の線形回帰を行いたい．\n0次：\\( y = w_0 \\) 1次：\\( y = w_0 + w_1 x \\) 3次：\\( y = w_0 + w_1 x + w_2 x^2 + w_3 x^3 \\) 9次：\\( y = \\sum_{j=0}^9 w_j x^j \\) そのためにここではまず計画行列 \\(\\Phi\\) を計算する関数design_matrix(x, M)を作る． ここで，\\(M\\) はモデルの次数． ちなみに定数項を入れると項の数は \\( (M+1) \\) になる．\n線形回帰モデルは $$ \\mathbf{y} = \\Phi \\mathbf{w} $$ と表され，作成した10点の訓練データをこのモデルに当てはめると $$ \\begin{pmatrix} y_0 \\\\ y_1 \\\\ \\vdots \\\\ y_9 \\end{pmatrix} = \\begin{pmatrix} x_0^0 \u0026 x_0^1 \u0026 \\cdots \u0026 x_0^{M} \\\\ x_1^0 \u0026 x_1^1 \u0026 \\cdots \u0026 x_1^{M} \\\\ \\vdots \u0026 \\vdots \u0026 \\ddots \u0026 \\vdots \\\\ x_9^0 \u0026 x_9^1 \u0026 \\cdots \u0026 x_9^{M} \\\\ \\end{pmatrix} \\begin{pmatrix} w_0 \\\\ \\vdots \\\\ w_{M} \\end{pmatrix} $$ となる．ここで $$ \\Phi = \\begin{pmatrix} x_0^0 \u0026 x_0^1 \u0026 \\cdots \u0026 x_0^{M} \\\\ x_1^0 \u0026 x_1^1 \u0026 \\cdots \u0026 x_1^{M} \\\\ \\vdots \u0026 \\vdots \u0026 \\ddots \u0026 \\vdots \\\\ x_9^0 \u0026 x_9^1 \u0026 \\cdots \u0026 x_9^{M} \\end{pmatrix} $$ が計画行列である．この計画行列を返す関数は色々な書き方があるが，例えば以下のようになる．\ndef design_matrix(x, M): x_mat = x.reshape(-1, 1) # 10行1列の2次元配列に変換 p = np.arange(M + 1) # 0, 1, 2, ..., M dmat = np.power(x_mat, p) return dmat np.arange()やnp.power()については下記ページを参考にすること．\nNumPy \u0026gt; 指定した範囲の離散化 NumPy \u0026gt; べき乗 チェックのために，\\(M=3\\) の場合の計画行列を画面出力してプログラムが合っているかどうか確認すること．\nM = 3 phi = design_matrix(x_train, M) phi # jupyterで出力用 例えば下記は一例．\narray([[1.00000000e+00, 5.35370744e-01, 2.86621833e-01, 1.53448944e-01], [1.00000000e+00, 2.51244518e-01, 6.31238078e-02, 1.58595107e-02], [1.00000000e+00, 3.59168825e-01, 1.29002245e-01, 4.63335848e-02], [1.00000000e+00, 5.44489549e-01, 2.96468869e-01, 1.61424200e-01], [1.00000000e+00, 1.68456471e-01, 2.83775827e-02, 4.78038745e-03], [1.00000000e+00, 4.44173207e-02, 1.97289838e-03, 8.76308599e-05], [1.00000000e+00, 7.67506529e-01, 5.89066272e-01, 4.52112210e-01], [1.00000000e+00, 3.48437878e-01, 1.21408955e-01, 4.23034785e-02], [1.00000000e+00, 8.79593377e-01, 7.73684508e-01, 6.80527769e-01], [1.00000000e+00, 6.41433853e-01, 4.11437388e-01, 2.63909869e-01]]) 演習4：フィッティング # ここでは，\\(M=3\\) で試しにフィッティングしてみる． 計画行列が計算できれば，正規方程式よりが計算できる． 正規方程式の導出などは 最小二乗法 のPDFを見ること． PDFにも書いてあるが，ランク落ちや数値計算上不安定になる場合に備えて， この演習では，\\( \\Phi \\mathbf{w} = \\mathbf{y} \\) の形の最小二乗解を計算できるnp.linalg.lstsq(phi, y)を用いて \\( \\mathbf{w} \\) を求める．\nnp.linalg.lstsq(phi, y)の返り値はタプルで (w, Sums of squared residuals, rank of phi, singular values of phi)のようになっているので，はじめのインデックスを取り出せばそれが \\( \\mathbf{w} \\) になる．\nw, residuals, rank, s = np.linalg.lstsq(phi, y_train) w # jupyterで出力用 例えば \\( \\mathbf{w} \\) は以下のように出力される．\narray([ -0.09749882, 10.87685823, -31.30137186, 20.41339222]) \\( \\mathbf{w} \\) が求まったので，\\(x\\) を指定すればその多項式のモデル $$ y = w_0 + w_1 x + w_2 x^2 + w_3 x^3 $$ が計算できる．sin関数を描くときに使ったx_denseを用いて，それに対応するy_dense_predictを計算すれば，連続的なモデルを描画できる．\n\\( \\mathbf{y} = \\Phi \\mathbf{w} \\) であったことを思い出すと，10点の訓練データx_trainの代わりに，x_denseの計画行列phi_denseを計算して，\\( \\mathbf{w} \\) との積を取ればy_dense_predictは簡単に計算できる．\nphi_dense = design_matrix(x_dense, M) y_dense_predict = ?????? y_dense_predictが計算できたら，sin関数と訓練データとともにプロットすること． 例えば下図のようなものが得られる．\nfig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.plot(x_dense, y_dense_predict, label=\u0026#39;Prediction\u0026#39;) ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend() 演習5：0次，1次，3次，9次の多項式回帰 # ここまでで，3次多項式による回帰ができたはずなので，0次，1次，3次，9次で同様に回帰を行ない， 全て同時にプロットすることで比較する．\nM の値を変化させるためのリスト用意する．\nmlist = [0, 1, 3, 9] あとはMをfor文で回しながら，3次の回帰の時と同様に計算すれば良い．\nfig, ax = plt.subplots(4, 1, figsize=(8, 12)) for i, M in enumerate(mlist): phi = ?????? w, residuals, rank, s = ?????? phi_dense = ?????? y_dense_predict = ?????? ax[i].plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax[i].plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax[i].plot(x_dense, y_dense_predict, label=\u0026#39;Prediction\u0026#39;) ax[i].set_ylim(-2, 2) # yの描画範囲を設定 ax[i].set_ylabel(r\u0026#39;$y$\u0026#39;) ax[i].text( # M = 0などのテキストの表示 0.05, 0.90, rf\u0026#39;$M = {M}$\u0026#39;, transform=ax[i].transAxes, va=\u0026#39;top\u0026#39;, bbox=dict(facecolor=\u0026#39;white\u0026#39;), fontsize=14 ) ax[-1].set_xlabel(r\u0026#39;$x$\u0026#39;) # x軸ラベルは一番下のグラフだけに表示 ax[0].legend(fontsize=12) # 一番上のグラフだけに凡例を表示．少しフォントを小さくしている fig, ax = plt.subplots(4, 1, figsize=(8, 12))のようにして４つのグラフを縦に並べる． figsizeは自由に調節して良い． ax.plot()ではなく，ax[0].plot(), ax[1].plot()\u0026hellip; のように使う enumerate()の使い方は各自で調べること． 下記のような結果が得られればうまく回帰できているので確認すること．\n","externalUrl":null,"permalink":"/notes/linear_regression/polynomial_regression/","section":"授業・研究ノート","summary":"正規方程式を NumPy のライブラリを使って解く演習","title":"演習：多項式回帰","type":"notes"},{"content":"","externalUrl":null,"permalink":"/notes/linear_regression/","section":"授業・研究ノート","summary":"最小二乗法，Ridge，Lasso回帰などの演習","title":"線形回帰","type":"notes"},{"content":" Crystal Structure Prediction Tool CrySPY # Crystal structure prediction (CSP) is a computational approach for predicting stable crystal structures from a given chemical composition. It is a key technology for the discovery and design of new materials and is expected to become increasingly important as computational resources continue to advance.\nCrySPY is an open-source Python software package developed primarily by our research group for crystal structure prediction. For installation instructions and detailed usage information, please see the link below.\nLink # CrySPY Documentation GitHub ","externalUrl":null,"permalink":"/en/research/csp/","section":"Research","summary":"","title":"Crystal Structure Prediction Methods","type":"research"},{"content":" NumPyの乱数 # 昔は（今でも使えるが）np.random.rand()が使われていたが，現在はnp.random.default_rng()というGeneratorを使うのが主流なのでこちらを使用すると良い．\n昔のnp.random.rand()はグローバルな乱数状態を共有するので大規模コードでは不向き．np.random.default_rng()はGeneratorなので乱数生成器を独立に持てる．\nインスタンス生成とseed # Generatorクラスのインスタンスを生成する．変数名はrngでもrng1でも好きなものでいい．\nrng = np.random.default_rng() 再現性が欲しいときは引数にseedを指定する．この場合はなんでも良いが0．\nrng = np.random.default_rng(0) 以下はこのように生成したrngを用いる．\n一様乱数 # 区間 \\( [0, 1) \\)の一様乱数 # rng.random(size)\nrng.random(size=5)のように書いてもいいが，省略してrng.random(5)でも良い スカラー # rng.random() 出力\n0.39122819049566204 1次元配列 # rng.random(5) # 要素数5 出力\narray([0.08564917, 0.23681051, 0.80127447, 0.58216204, 0.09412864]) 2次元配列 # rng.random((2, 2)) # 2行2列 出力\narray([[0.43312694, 0.4790513 ], [0.15973891, 0.73457715]]) 指定した範囲の整数の一様乱数 # rng.integers(low, high, size)\nrng.integers(low=0, high=10, size=5) 出力\narray([9, 7, 2, 3, 6]) low（この場合は0）は範囲に含まれる high（この場合は10）は範囲に含まれない 正規分布に従う乱数 # rng.normal(loc=0.0, scale=1.0, size=None)\nloc: 平均 scale: 標準偏差 size: ndarrayのサイズ 1次元配列 # rng.normal(loc=0.0, scale=1.0, size=5) 出力\narray([ 1.49673717, -2.03950384, -0.34031662, -0.60861062, 0.5327216 ]) 分布をヒストグラムにした例 # 平均0，標準偏差1の標準正規分布はrng.standard_normal()も使える． rng.normal(loc=0.0, scale=1.0)と同じ．\n# ---------- sample rng = np.random.default_rng() x = rng.standard_normal(size=10000) # ---------- theory xx = np.linspace(-4, 4, 1000) yy = np.exp(-xx**2/2) / np.sqrt(2*np.pi) # ---------- plot fig, ax = plt.subplots() ax.hist( x, bins=50, density=True, edgecolor=\u0026#39;black\u0026#39;, linewidth=0.5, label=\u0026#39;Sample\u0026#39;, ) ax.plot(xx, yy, label=\u0026#39;Theory\u0026#39;) ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(\u0026#39;Probability density\u0026#39;) ax.legend() ","externalUrl":null,"permalink":"/notes/python/numpy/random/","section":"授業・研究ノート","summary":"一様分布や正規分布の乱数","title":"乱数","type":"notes"},{"content":" はじめに # 演習：多項式回帰 では，NumPy を用いてある程度自分でプログラムを作成したが，scikit-learn という便利な機械学習ライブラリを使えば簡単に線形回帰を行うことができるので， 使い方を覚える目的でここでも同じ多項式回帰を行う．\nJupyter Notebookでの演習を想定．下記のようにライブラリのインポートも必要． ここに出てくるグラフ出力例は Matplotlib \u0026gt; よく使う設定と保存 の設定も用いている．\nimport numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression # LinearRegressionのインポート from sklearn.preprocessing import PolynomialFeatures # PolynomialFeaturesのインポート 参考\nLinearRegression PolynomialFeatures 演習1：データの準備 # 演習：多項式回帰 と同様にsin関数にガウスノイズを加えたデータを10点用意してプロットすること．ただの復習．\n# ---------- data x_dense = np.linspace(0, 1, 100) y_sin = np.sin(2 * np.pi * x_dense) rng = np.random.default_rng() x_train = ?????? y_train = ?????? noise = ?????? y_train = y_train + noise # ---------- plot fig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend() 演習2：計画行列 # 復習と比較のために，演習：多項式回帰 と同様に計画行列を計算する関数を準備しておく．\ndef design_matrix(x, M): x_mat = x.reshape(-1, 1) # 10行1列の2次元配列に変換 p = np.arange(M + 1) # 0, 1, 2, ..., M dmat = np.power(x_mat, p) return dmat scikit-learn の PolynomialFeatures を使っても全く同じことができるので，練習がてら利用して，scikit-learnの使い方に慣れる．\nPolynomialFeatures の簡単な例が下記．\ntest_x = np.array([1, 2, 3]) # 1次元配列 test_x = test_x.reshape(-1, 1) # 3行1列の2次元配列にreshape test_x # juypterで出力用 出力\narray([[1], [2], [3]]) これを入力に用いると\nM = 3 test_pf = PolynomialFeatures(degree=M) # インスタンスの作成 test_phi = test_pf.fit_transform(test_x) # fitしてtransform．要するにここで多項式の特徴量を作成 test_phi # juypterで出力用 出力\narray([[ 1., 1., 1., 1.], [ 1., 2., 4., 8.], [ 1., 3., 9., 27.]]) このようにそれぞれの0乗，1乗，2乗，3乗のような多項式を特徴量とした行列が得られる．\n上記の例を参考に，PolynomialFeatures を利用して計画行列を計算する関数を作成する．\ndef polymat(x, M): pf = ?????? dmat = ?????? return dmat 2通りの計画行列を計算する関数ができたら比較する．\nM = 3 phi = design_matrix(x_train, M) # NumPyで作った関数 phi # jupyterで出力用 phi = polymat(x_train, M) # sklearnで作った関数 phi # jupyterで出力用 どちらの出力も同じ結果になることを確認する．出力の一例．\narray([[1.00000000e+00, 6.38195334e-01, 4.07293284e-01, 2.59932673e-01], [1.00000000e+00, 9.91459775e-01, 9.82992486e-01, 9.74597510e-01], [1.00000000e+00, 9.67294554e-02, 9.35658755e-03, 9.05057619e-04], [1.00000000e+00, 5.06704342e-01, 2.56749290e-01, 1.30095980e-01], [1.00000000e+00, 3.96438951e-01, 1.57163842e-01, 6.23058688e-02], [1.00000000e+00, 2.06016236e-01, 4.24426895e-02, 8.74388314e-03], [1.00000000e+00, 8.83368823e-01, 7.80340478e-01, 6.89328450e-01], [1.00000000e+00, 5.40482880e-01, 2.92121743e-01, 1.57886801e-01], [1.00000000e+00, 6.12204461e-01, 3.74794302e-01, 2.29450743e-01], [1.00000000e+00, 2.85361588e-01, 8.14312357e-02, 2.32373467e-02]]) 演習3：LinearRegressionを用いた回帰 # LinearRegression を用いて線形回帰を行う． LinearRegression にはfit_interceptというキーワード引数があり，切片（定数項）を入れるか入れないかを指定できる．今回は \\(x^0\\) の特徴量が定数項に当たるため，fit_intercept=Falseを用いる．\n使い方は PolynomialFeatures のときと同様で，まずインスタンスを生成し，そのあとfit()メソッドでフィッティングするときに，計画行列phiと訓練データy_trainを引数に入れるだけ．\nlr = LinearRegression(fit_intercept=False) # インスタンスの生成 lr.fit(phi, y_train) # フィッティング これで係数 \\(\\mathbf{w}\\) が計算されてcoef_属性に格納されている．出力して確認すること．\nlr.coef_ # jupyterで出力用 例えば，出力は以下．\narray([ -0.03188931, 10.38906877, -31.40759752, 20.92979435]) また，今回はfit_intercept=Falseにしたので使わなかったが，切片（定数項）の値はintercept_属性に格納されている（今回は値はゼロ）．\n上記の係数 \\(\\mathbf{w}\\) があれば予測モデルが計算できるので，演習：多項式回帰 と同様に \\(\\mathbf{y} = \\Phi \\mathbf{w}\\) を利用して，自分で行列とベクトルの積を計算しても良いが，LinearRegression にはそれと同じ計算を行うpredict()メソッドがあるのでそれを使う． 連続的なx_denseの計画行列phi_denseをpredict()に入れて予測モデルを計算し，プロットする．\nphi_dense = polymat(x_dense, M) y_dense_predict = lr.predict(phi_dense) fig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.plot(x_dense, y_dense_predict, label=\u0026#39;Prediction\u0026#39;) ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend() 演習4：0次，1次，3次，9次の多項式回帰 # LinearRegression を用いて3次の回帰ができたら，0次，1次，3次，9次で同様に回帰を行い， まとめてグラフにプロットする．\n# ---------- M = 0, 1, 3, 9 mlist = [0, 1, 3, 9] # ---------- plot fig, ax = plt.subplots(4, 1, figsize=(8, 12)) for i, M in enumerate(mlist): phi = ?????? lr = ?????? # インスタンスの生成 ?????? # フィッティング phi_dense = ?????? y_dense_predict = ?????? ax[i].plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax[i].plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax[i].plot(x_dense, y_dense_predict, label=\u0026#39;Prediction\u0026#39;) ax[i].set_ylim(-2, 2) # yの描画範囲を設定 ax[i].set_ylabel(r\u0026#39;$y$\u0026#39;) ax[i].text( # M = 0などのテキストの表示 0.05, 0.90, rf\u0026#39;$M = {M}$\u0026#39;, transform=ax[i].transAxes, va=\u0026#39;top\u0026#39;, bbox=dict(facecolor=\u0026#39;white\u0026#39;), fontsize=14 ) ax[-1].set_xlabel(r\u0026#39;$x$\u0026#39;) # x軸ラベルは一番下のグラフだけに表示 ax[0].legend(fontsize=12) # 一番上のグラフだけに凡例を表示．少しフォントを小さくしている ","externalUrl":null,"permalink":"/notes/linear_regression/sklearn_lr/","section":"授業・研究ノート","summary":"scikit-learnを用いて多項式回帰を行うことで，scikit-learnの使い方を学ぶ","title":"演習：scikit-learnを用いた多項式回帰","type":"notes"},{"content":" Lithium-Ion Batteries # We conduct collaborative research with experimental groups and industry partners on solid electrolyte materials for lithium-ion batteries, using crystal structure prediction methods and molecular dynamics simulations based on machine-learning potentials.\n","externalUrl":null,"permalink":"/en/research/battery/","section":"Research","summary":"","title":"Lithium-Ion Batteries","type":"research"},{"content":" はじめに # この演習では scikt-learn を用いて Ridge 回帰を行う．演習：多項式回帰 および 演習：scikit-learnを用いた多項式回帰 では， 9次多項式の回帰で過学習が起きていた．Ridge 回帰を用いることで過学習が抑制されることを確認する．\nここでは，ハイパーパラメータalphaの値は固定して正則化について学ぶ．ハイパーパラメータチューニングは次の 演習：Cross-validation とハイパーパラメータチューニング で行う予定である．\nJupyter Notebookでの演習を想定．下記のようにライブラリのインポートも必要． ここに出てくるグラフ出力例は Matplotlib \u0026gt; よく使う設定と保存 の設定も用いている．\nimport numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import PolynomialFeatures, StandardScaler from sklearn.linear_model import Ridge from sklearn.metrics import root_mean_squared_error 参考\nStandardScaler Ridge mean_squared_error root_mean_squared_error 演習1：標準化の練習 # ここではデータの標準化の練習を行う．下記の行列（2次元配列）を NumPy の機能を用いて標準化する． 標準化を習ってない学生は自分で調べること． 各特徴量ごと（縦方向）に標準化を行う． $$ X= \\begin{pmatrix} 1 \u0026 4 \u0026 2 \\\\ 3 \u0026 7 \u0026 2 \\\\ 8 \u0026 5 \u0026 9 \\end{pmatrix} $$x = np.array([[1, 4, 2], [3, 7, 2], [8, 5, 9]]) x # jupyterで出力用 x = ?????? # 標準化 x # jupyterで出力用 おそらく以下のような出力になるはず．\narray([[-1.01904933, -1.06904497, -0.70710678], [-0.33968311, 1.33630621, -0.70710678], [ 1.35873244, -0.26726124, 1.41421356]]) 標準化したデータzの平均と分散を確認する．\nz.mean(axis=0) # jupyterで出力用 z.std(axis=0) # jupyterで出力用 それぞれarray([0., 0., 0.])およびarray([1., 1., 1.])とほぼ同じ値になっているはず（数値計算上の微々たる誤差は出る）．\nscikit-learn にデータを標準化できる StandardScaler があるのでそれを使って同じ結果になるか確認する．使い方は下記を参考にする．データ行列を入れるだけ．\nss = StandardScaler() # インスタンスの作成 z = ss.fit_transform(x) # fitしてtransform z # jupyterで出力用 自分で計算したzと同じ値になっているか確認する．\nまた，次のような計画行列を標準化しようとするとどうなるか考えること（1列目に注目）． $$ X= \\begin{pmatrix} 1 \u0026 4 \u0026 2 \\\\ 1 \u0026 7 \u0026 2 \\\\ 1 \u0026 5 \u0026 9 \\end{pmatrix} $$ 演習2：データの準備 # 演習：多項式回帰 および 演習：scikit-learnを用いた多項式回帰 と同様にsin関数にガウスノイズを加えたデータを10点用意する． また，今回はテストデータとしてさらに4点同様に準備して，訓練データ等と一緒にプロットする．\n# ---------- training data x_dense = np.linspace(0, 1, 100) y_sin = np.sin(2 * np.pi * x_dense) rng = np.random.default_rng() x_train = ?????? y_train = ?????? noise = ?????? y_train = y_train + noise # ----------test data x_test = ?????? y_test = ?????? noise_test = ?????? y_test = y_test + noise_test # ---------- plot fig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.plot(x_test, y_test, \u0026#39;s\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Test data\u0026#39;) ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend() 演習3：計画行列の標準化 # Ridge 回帰ではデータを標準化する必要があるが，多項式回帰の \\(x^0\\) の特徴量のような定数項があると標準化できないので，定数項を除外する． 演習：scikit-learnを用いた多項式回帰 で用いた PolynomialFeatures ではinclude_bias=Falseのようにすると定数項を除外することができるので，ここではこれを用いる．\ndef design_matrix(x, M): pf = PolynomialFeatures(degree=M, include_bias=False) dmat = ?????? return dmat 最小二乗法では過学習が起きていた \\(M=9\\) で,計画行列の shape を確認する．10行9列の行列になるはず．10行はデータ数，9列は定数項を除いた多項式の項数に対応する．\nM = 9 phi = design_matrix(x_train, M) # 計画行列 phi.shape # jupyterで出力用 定数項が除外されて，1列分少ない10行9列になっているか確認すること．\n(10, 9) 後で使用するので，テストデータx_testの計画行列phi_testも同様に計算しておく．\nphi_test = design_matrix(x_test, M) # テストデータの計画行列 phi_test.shape # jupyterで出力用 計画行列phiを標準化する．\nss = StandardScaler() phi_std = ss.fit_transform(phi) 同様にテストデータに対する計画行列phi_testも標準化する．ただし，テストデータは訓練データと同じスケーリングパラメーターを使う必要があるので fit_transform() はせずに，transform() だけ行うこと．上記のssにはすでに訓練データで fit した時のパラメータが格納されているので，下記のようにそれを用いて transform() だけ行う． テストデータに対して fit してはいけない phi_test_std = ss.transform(phi_test) # transformのみ また，連続的な予測モデルを計算する際に，x_denseに対する標準化された計画行列phi_dense_stdが必要になるので，これもついでにここで計算しておく． テストデータの時と同様に，fit せずにtransform()だけ行い，訓練データと同じスケーリングパラメータを用いる．\nphi_dense = design_matrix(x_dense, M) phi_dense_std = ss.transform(phi_dense) # transformのみ 演習4：Ridge 回帰 # 標準化された計画行列phi_stdに対して， scikit-learnの Ridge クラス を用いて回帰を行う． ただし，ハイパーパラメータalphaに関して，ここでは0.0と0.1で固定して2通りの回帰を行う． 計画行列から外した定数項の部分はfit_intercept=Trueにしてここで考慮する（デフォルトでTrueなので省略可能）． 使い方は 演習：scikit-learnを用いた多項式回帰 で使った LinearRegression とほぼ同じ．\nalpha=0.0 # まずはalpha=0.0（つまり最小二乗法）から始める． ここではインスタンス生成時にridge0という変数名を用いることにする．\nridge0 = Ridge(alpha=0.0, fit_intercept=True) # インスタンスの作成 ridge0.fit(phi_std, y_train) # フィッティング これで回帰ができているので， LinearRegression のときと同様に，係数および切片（定数項）はそれぞれcoef_およびintercept_属性に格納されている． また，訓練データに対する決定係数も下記のようにscore()メソッドを使うことで確認できる． 標準化された訓練データの計画行列phi_stdと訓練データの値y_trainを引数に入れればよい．\nridge0.score(phi_std, y_train) # 決定係数．jupyterで出力用 うまく回帰できていれば0.9999といった1に近い値になる．\nまた，これまで同様連続的な予測モデルもプロットしたいので，事前に準備しておいたphi_dense_stdをpredict()メソッドに入れることで連続的な予測モデルy0_dense_predictを計算する．\ny0_dense_predict = ridge0.predict(phi_dense_std) 予測モデルが計算できたので，sin関数やデータとともにプロットする． 今はalpha=0.0で最小二乗法なので，当然過学習している結果が得られる．\nfig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.plot(x_test, y_test, \u0026#39;s\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Test data\u0026#39;) ax.plot(x_dense, y0_dense_predict, label=\u0026#39;Prediction\u0026#39;) ax.set_ylim(-1.5, 1.5) # 過学習するのでyの描画範囲を設定しておいた方がいい ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend(loc=\u0026#39;upper right\u0026#39;) alpha=0.1 # 次にalpha=0.1にして，正則化がどう働くかを学習する．alpha=0.0のときと同じように計算して，結果をプロットすること． ここではridge1などのxxx1といった変数名を用いることにする．\n# ---------- Ridge 回帰 ridge1 = ?????? # インスタンスの作成 ?????? # フィッティング y1_dense_predict = ?????? # ---------- plot fig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.plot(x_test, y_test, \u0026#39;s\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Test data\u0026#39;) ax.plot(??????, ??????, label=\u0026#39;Prediction\u0026#39;) ax.set_ylim(-1.5, 1.5) ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend(loc=\u0026#39;upper right\u0026#39;) そこまで回帰結果はよくないものの，過学習が抑制されていることはわかるはず．\n演習5：誤差の評価 # Parity plot # 予測値が真の値とどれくらい合っているか可視化するために，parity plot を行う． 横軸を真の値，縦軸を予測値にしてプロットすると，予測が完全に一致していれば \\(y=x\\) のグラフになるので，どのくらい予測が合っているかが可視化できる．\nparity plot のために訓練データ10点とテストデータ4点に対する予測値を計算する． 訓練データに対してはphi_stdを，テストデータに対しては事前に計算しておいたphi_test_stdを predict()に入れればそのデータに対する予測値を計算できる．\nalpha=0.0\ny0_predict = ridge0.predict(phi_std) # 訓練データに対する予測値 y0_test_predict = ridge0.predict(phi_test_std) # テストデータに対する予測値 alpha=0.1\ny1_predict = ridge1.predict(phi_std) # 訓練データに対する予測値 y1_test_predict = ridge1.predict(phi_test_std) # テストデータに対する予測値 横軸を真値，縦軸を予測値にして parity plot する． alpha=0.0の場合は過学習していてテストデータは大きく外れるので描画範囲は広めにしておくとよい．\nalpha=0.0\n# ---------- setting for parity plot # 過学習していてテストデータは大きく外れているので，描画範囲を広くしておく x_min = y_min = -50 x_max = y_max = 50 # ---------- plot fig, ax = plt.subplots(1, 1, figsize=(6, 6)) # parity plotでは縦横比を1:1にしておく ax.plot([x_min, x_max], [y_min, y_max]) # y = xの基準線 ax.plot(y_train, y0_predict, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.plot(y_test, y0_test_predict, \u0026#39;s\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Test data\u0026#39;) ax.set_xlim(x_min, x_max) ax.set_ylim(y_min, y_max) ax.set_aspect(\u0026#39;equal\u0026#39;) # 縦横のスケールを同じにする ax.set_xlabel(\u0026#39;True\u0026#39;) ax.set_ylabel(\u0026#39;Predicted\u0026#39;) ax.legend() alpha=0.1\n# ---------- setting for parity plot x_min = y_min = -1.0 x_max = y_max = 1.0 # ---------- plot fig, ax = plt.subplots(1, 1, figsize=(6, 6)) # parity plotでは縦横比を1:1にしておく ax.plot([x_min, x_max], [y_min, y_max]) # y = xの基準線 ax.plot(y_train, y1_predict, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.plot(y_test, y1_test_predict, \u0026#39;s\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Test data\u0026#39;) ax.set_xlim(x_min, x_max) ax.set_ylim(y_min, y_max) ax.set_aspect(\u0026#39;equal\u0026#39;) # 縦横のスケールを同じにする ax.set_xlabel(\u0026#39;True\u0026#39;) ax.set_ylabel(\u0026#39;Predicted\u0026#39;) ax.legend() RMSE # 平均二乗平方根誤差（root-mean-square error: RMSE）を用いて誤差を定量化する． 誤差を定量化する方法は RMSE の他にも MSE や MAE がある．\nscikit-learnには RMSE を計算できる root_mean_squared_error が用意されている． すでに import してあると思うので， root_mean_squared_error(y_true, ypred)のように使えば計算できる．\nalpha=0.0\nprint(f\u0026#39;Training: RMSE = {root_mean_squared_error(??????, ??????)}\u0026#39;) print(f\u0026#39;Test: RMSE = {root_mean_squared_error(??????, ??????)}\u0026#39;) 出力例\nTraining: RMSE = 1.785522733440865e-08 Test: RMSE = 792.2491322956186 alpha=0.1\nprint(f\u0026#39;Training: RMSE = {root_mean_squared_error(??????, ??????)}\u0026#39;) print(f\u0026#39;Test: RMSE = {root_mean_squared_error(??????, ??????)}\u0026#39;) 出力例\nTraining: RMSE = 0.19572243793782718 Test: RMSE = 0.32356534169959145 ","externalUrl":null,"permalink":"/notes/linear_regression/ridge/","section":"授業・研究ノート","summary":"Ridge 回帰を行い，過学習や正則化を理解する","title":"演習：Ridge 回帰","type":"notes"},{"content":" 要素ごとの演算 # NumPyの四則演算（\\( +, -, *, / \\)）は基本的には要素ごとの演算を行う．\n加算なら\nx = np.array([1, 2, 3]) y = np.array([4, 5, 6]) x + y # jupyterで出力用 出力\narray([5, 7, 9]) 乗算なら\nx * y # jupyterで出力用 出力\narray([4, 10, 18]) ブロードキャスト # NumPy では，形状が異なる配列同士でも，条件を満たせば自動的に形状をそろえて演算できる．この仕組みをブロードキャストという．\n普通の四則演算や np.sin()，np.power() などもブロードキャストが適用される．\nNumPy は配列の shape を後ろの軸から比較する．次元数が異なる場合は，次元数が少ない配列の先頭にサイズ 1 の軸を補う．その後，各軸のサイズが等しいか，どちらかが 1 であれば演算可能であり，サイズ 1 の軸は自動的に拡張されて揃えられる．\n（例）\nA.shape: (2, 3) x.shape: (3,) まず，次元が少ない方の \\(x\\) の shape は $$ (3,) → (1, 3) $$ のように先頭にサイズ 1 の軸が補われる．次に $$ (1, 3) → (2, 3) $$ のように \\(A\\) の shapeに揃えられて，その後に要素ごとに演算される．\nスカラーと配列 # x = np.array([1, 2, 3]) x + 2 # jupyterで出力用 出力\narray([3, 4, 5]) スカラーの 2 は配列 \\( x \\) の形状に合わせてブロードキャストされ，各要素に加算される． 概念的には array([2, 2, 2])が足されているものと思って良い（実際にそのような配列を内部で作っているわけではない） 異なる形状の配列同士 # x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.array([10, 20, 30]) x + y # jupyterで出力用 出力\narray([[11, 22, 33], [14, 25, 36]]) \\(x\\) の形状は \\((2, 3)\\)，\\(y\\) の形状は \\((3,)\\) \\(y\\) は \\(x\\) の形状に合わせてブロードキャストされる 概念的には次のような配列が足されていると考えてよい array([[10, 20, 30], [10, 20, 30]]) ベクトルや行列の積 # NumPy では @ 演算子または np.matmul() によって線形代数における積を計算できる． np.dot()も積の計算に使用できるが3次元以上の配列では@と挙動が異なるので注意．現在は@の利用が推奨されている．\nこれらの演算は四則演算等のブロードキャストとは別物なので注意．\nベクトルの内積 # x = np.array([0, 1, 2]) y = np.array([3, 4, 5]) x @ y # 0*3 + 1*4 + 2*5 出力\n14 行列とベクトルの積 # $$ A = \\begin{pmatrix} 0 \u0026 1 \\\\ 2 \u0026 3 \\\\ \\end{pmatrix}, \\quad \\mathbf{x} = \\begin{pmatrix} 0 \\\\ 1 \\\\ \\end{pmatrix} $$$$ A \\mathbf{x} = \\begin{pmatrix} 0 \u0026 1 \\\\ 2 \u0026 3 \\\\ \\end{pmatrix} \\begin{pmatrix} 0 \\\\ 1 \\\\ \\end{pmatrix} = \\begin{pmatrix} 1 \\\\ 3 \\\\ \\end{pmatrix} $$x = np.array([0, 1]) # ベクトル A = np.array([[0, 1], [2, 3]]) # 行列 A @ x 出力\narray([1, 3]) ここで x @ A のように順番を変えると上記とは異なる計算になるので注意． @ はブロードキャストとは違なり特別な演算になる． 下記の例ではベクトルは横ベクトルと解釈される．\n$$ \\begin{pmatrix} 0 \u0026 1 \\\\ \\end{pmatrix} \\begin{pmatrix} 0 \u0026 1 \\\\ 2 \u0026 3 \\\\ \\end{pmatrix} = \\begin{pmatrix} 2 \u0026 3 \\\\ \\end{pmatrix} $$x @ A 出力\narray([2, 3]) ただし，最終的に得られるのは1次元配列なので，横ベクトルではなくなる．1次元配列は縦横の区別はない．\n行列同士の積 # $$ \\begin{pmatrix} 0 \u0026 1 \\\\ 2 \u0026 3 \\\\ \\end{pmatrix} \\begin{pmatrix} 3 \u0026 2 \\\\ 1 \u0026 0 \\\\ \\end{pmatrix} = \\begin{pmatrix} 1 \u0026 0 \\\\ 9 \u0026 4 \\\\ \\end{pmatrix} $$A = np.array([[0, 1],[2, 3]]) B = np.array([[3, 2], [1, 0]]) A @ B 出力\narray([[1, 0], [9, 4]]) ","externalUrl":null,"permalink":"/notes/python/numpy/array_operations/","section":"授業・研究ノート","summary":"ベクトルや行列の計算","title":"配列演算","type":"notes"},{"content":" Semiconductors # We perform first-principles electronic structure calculations on defects in semiconductors. Our research focuses on compound semiconductors and other materials being investigated experimentally through collaborative research projects.\n","externalUrl":null,"permalink":"/en/research/semiconductor/","section":"Research","summary":"","title":"Semiconductors","type":"research"},{"content":" \\(x\\) を1次元配列で準備 # 参考：指定した範囲の離散化 \u0026gt; np.linspace()\nx = np.linspace(0, 1, 100) # 0から1まで100個の要素を持つ1次元配列 x # jupyterで表示用 出力\narray([0. , 0.01010101, 0.02020202, 0.03030303, 0.04040404, 0.05050505, 0.06060606, 0.07070707, 0.08080808, 0.09090909, 0.1010101 , 0.11111111, 0.12121212, 0.13131313, 0.14141414, 0.15151515, 0.16161616, 0.17171717, 0.18181818, 0.19191919, 0.2020202 , 0.21212121, 0.22222222, 0.23232323, 0.24242424, 0.25252525, 0.26262626, 0.27272727, 0.28282828, 0.29292929, 0.3030303 , 0.31313131, 0.32323232, 0.33333333, 0.34343434, 0.35353535, 0.36363636, 0.37373737, 0.38383838, 0.39393939, 0.4040404 , 0.41414141, 0.42424242, 0.43434343, 0.44444444, 0.45454545, 0.46464646, 0.47474747, 0.48484848, 0.49494949, 0.50505051, 0.51515152, 0.52525253, 0.53535354, 0.54545455, 0.55555556, 0.56565657, 0.57575758, 0.58585859, 0.5959596 , 0.60606061, 0.61616162, 0.62626263, 0.63636364, 0.64646465, 0.65656566, 0.66666667, 0.67676768, 0.68686869, 0.6969697 , 0.70707071, 0.71717172, 0.72727273, 0.73737374, 0.74747475, 0.75757576, 0.76767677, 0.77777778, 0.78787879, 0.7979798 , 0.80808081, 0.81818182, 0.82828283, 0.83838384, 0.84848485, 0.85858586, 0.86868687, 0.87878788, 0.88888889, 0.8989899 , 0.90909091, 0.91919192, 0.92929293, 0.93939394, 0.94949495, 0.95959596, 0.96969697, 0.97979798, 0.98989899, 1. ]) sin関数 # $$ y = \\sin(2\\pi x) $$y = np.sin(2 * np.pi * x) # 要素数100の1次元配列 sin関数は np.sin() 円周率は np.pi \\(x\\) が1次元配列なので \\(y\\) も1次元配列になる プロット # ここに出てくる出力例は よく使う設定と保存 の設定も用いている．\nfig, ax = plt.subplots(1, 1) ax.plot(x, y, label=r\u0026#39;$y = \\sin(2\\pi x)$\u0026#39;) ax.set_xlabel(r\u0026#39;$x \\ \\mathrm{in} \\ \\sin(2\\pi x)$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend() ","externalUrl":null,"permalink":"/notes/python/numpy/math_func/","section":"授業・研究ノート","summary":"sin関数などの数学関数","title":"数学関数","type":"notes"},{"content":" はじめに # 演習：Ridge 回帰 では ハイパーパラメータalphaの値は0.0と0.1の2通りで固定して Ridge 回帰を行った． この演習では，交差検証（cross-validation: CV）によりハイパーパラメータチューニングを行う．\nここでは，KFoldを用いて CV を行い，alphaを変化させながら最適なalphaを探索する． 実は scikit-learn にはその処理を自動化した RidgeCV というクラスがあるが， まずは自分である程度コードを書いて流れを理解した方が良い．\nJupyter Notebookでの演習を想定．下記のようにライブラリのインポートも必要． ここに出てくるグラフ出力例は Matplotlib \u0026gt; よく使う設定と保存 の設定も用いている．\nimport numpy as np import matplotlib.pyplot as plt from sklearn.preprocessing import PolynomialFeatures, StandardScaler from sklearn.linear_model import Ridge from sklearn.metrics import root_mean_squared_error from sklearn.model_selection import train_test_split from sklearn.model_selection import KFold 演習1：k-fold の練習 # 演習：Ridge 回帰 では訓練データとテストデータを分けて生成して準備したが，普通はデータセットが一つあって，それを訓練データとテストデータに分割して回帰等を行う． データの分け方は色々あるが，ここでは以下の図のようなデータ分割を考える． テストデータは最後まで取っておき，訓練データをk分割する．\nここでは，下記サイトのExample（3.1.2.1.1. K-fold）を試して scikit-learn の KFoldの使う練習から始める．\nhttps://scikit-learn.org/stable/modules/cross_validation.html#k-fold\nインデックスが返ってくると思うので，どのようにインデックスを利用すれば良いか理解すること． 下記のコードも試して，動作を理解しておくこと．\nshuffle=Trueにしておくと，テストのindexの選び方が順番通りではなくシャッフルされる． random_stateは seed．何か適当な数値を入れおけば再現性がとれる． hogex = np.array([\u0026#34;a\u0026#34;, \u0026#34;b\u0026#34;, \u0026#34;c\u0026#34;, \u0026#34;d\u0026#34;, \u0026#34;e\u0026#34;, \u0026#34;f\u0026#34;, \u0026#34;g\u0026#34;, \u0026#34;h\u0026#34;]) hogey = np.array([1, 2, 3, 4, 5, 6, 7, 8]) kf = KFold(n_splits=4, shuffle=True, random_state=None) for i, (train_idx, valid_idx) in enumerate(kf.split(hogex), start=1): print(f\u0026#39;loop: {i}\u0026#39;) print(\u0026#39;train index :\u0026#39;, train_idx) print(\u0026#39;validation index :\u0026#39;, valid_idx) print(\u0026#39;train data :\u0026#39;, hogex[train_idx], hogey[train_idx]) print(\u0026#39;validation data :\u0026#39;, hogex[valid_idx], hogey[valid_idx]) print() 出力\nloop: 1 train index : [2 3 4 5 6 7] validation index : [0 1] train data : [\u0026#39;c\u0026#39; \u0026#39;d\u0026#39; \u0026#39;e\u0026#39; \u0026#39;f\u0026#39; \u0026#39;g\u0026#39; \u0026#39;h\u0026#39;] [3 4 5 6 7 8] validation data : [\u0026#39;a\u0026#39; \u0026#39;b\u0026#39;] [1 2] loop: 2 train index : [0 1 2 4 5 6] validation index : [3 7] train data : [\u0026#39;a\u0026#39; \u0026#39;b\u0026#39; \u0026#39;c\u0026#39; \u0026#39;e\u0026#39; \u0026#39;f\u0026#39; \u0026#39;g\u0026#39;] [1 2 3 5 6 7] validation data : [\u0026#39;d\u0026#39; \u0026#39;h\u0026#39;] [4 8] loop: 3 train index : [0 1 3 5 6 7] validation index : [2 4] train data : [\u0026#39;a\u0026#39; \u0026#39;b\u0026#39; \u0026#39;d\u0026#39; \u0026#39;f\u0026#39; \u0026#39;g\u0026#39; \u0026#39;h\u0026#39;] [1 2 4 6 7 8] validation data : [\u0026#39;c\u0026#39; \u0026#39;e\u0026#39;] [3 5] loop: 4 train index : [0 1 2 3 4 7] validation index : [5 6] train data : [\u0026#39;a\u0026#39; \u0026#39;b\u0026#39; \u0026#39;c\u0026#39; \u0026#39;d\u0026#39; \u0026#39;e\u0026#39; \u0026#39;h\u0026#39;] [1 2 3 4 5 8] validation data : [\u0026#39;f\u0026#39; \u0026#39;g\u0026#39;] [6 7] 演習2：データの準備と分割 # まずこれまでと同様にsin関数に関連するデータをいくつか準備する． データ数はなんでも良いが，すぐに変えられるようにここではndataという変数を用いる． データ数は自分で何度か変更してみて色々試してみること．\nデータが少ないと検証誤差のブレが大きくうまくいかないことが多いので， データを40点生成し，訓練データとテストデータに分ける． ここで分けるのは k-fold ではなく，最後まで取っておくためのテストデータ．\nまずいつも通りデータを40点生成する．\nndata = 40 x_dense = np.linspace(0, 1, 100) y_sin = np.sin(2 * np.pi * x_dense) rng = np.random.default_rng() # 何か適当なseed値を入れておくと良い x_all = ?????? # ndataを使ってデータ生成 y_all = ?????? noise = ?????? # ndataを使ってデータ生成 y_all = y_all + noise scikit-leaarn には train_test_split というデータをランダムに分割することができる関数があるので，それを利用する．train_test_split はデフォルトでshuffle=Trueになっている．\nx_train, x_test, y_train, y_test = train_test_split( x_all, y_all, test_size=0.2, random_state=None, ) random_stateに何か適当な seed 値を入れておくと再現できる test_size=0.2のように40データのうちの2割がテストになるように分割される． いつも通りsin関数とともにプロットして，きちんとデータが分割されているか確認する．\nfig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.plot(x_test, y_test, \u0026#39;s\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Test data\u0026#39;) ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend(loc=\u0026#39;upper right\u0026#39;) 演習3：CV によるハイパーパラメータチューニング # これまで通り，計画行列を計算する関数を準備しておく．\ndef design_matrix(x, M): pf = PolynomialFeatures(degree=M, include_bias=False) dmat = ?????? return dmat alphaはnp.logspace（参考：np.logspace）を使って対数スケールで変化させる． 下記のn_alphasやalphasの範囲などは各自調整すること．\nn_alphas = 100 alphas = np.logspace(-10, 3, n_alphas) 上記alphasを for 文で回しながら，CV で RMSE を k 回計算して，その平均を求める． この例での分割数は \\(k = 4\\) にするが，各自で調整してよい．\n# ---------- settings M = 9 # 多項式の次数 nsp = 4 # k分割の数 mean_rmse_train = [] # 各alphaの値における訓練データに対するRMSEの平均 mean_rmse_valid = [] # 各alphaの値における検証データに対するRMSEの平均 # ---------- loop for alpha for alpha in alphas: kf = KFold(n_splits=nsp, shuffle=True, random_state=None) # random_stateに何か適当なseed値を入れて確認するとよい fold_rmse_train = [] # alphaごとに，CV中の訓練データに対するRMSEを格納するためのリスト fold_rmse_valid = [] # alphaごとに，CV中の検証データに対するRMSEを格納するためのリスト # ---------- k-loop for train_idx, valid_idx in kf.split(x_train): # ------ k分割の訓練データと検証データ x_fold_train = x_train[train_idx] # x_trainを分割 x_fold_valid = x_train[valid_idx] # x_trainを分割 y_fold_train = y_train[train_idx] # y_trainを分割 y_fold_valid = y_train[valid_idx] # y_trainを分割 # ------ 計画行列の作成と標準化 phi = design_matrix(??????, M) # 計画行列作成 ss = StandardScaler() phi_std = ?????? # 標準化 # ------ 検証データの計画行列と標準化 phi_valid = design_matrix(??????, M) # 検証データの計画行列作成 phi_valid_std = ?????? # 検証データはfitはしない。transformだけ # ------ Ridge回帰 ridge = Ridge(alpha=alpha, fit_intercept=True) ridge.fit(??????, ??????) # fit y_fold_predict = ridge.predict(??????) # 訓練データに対する予測値 y_fold_valid_predict = ridge.predict(??????) # 検証データに対する予測値 # ------ RMSEの計算 fold_rmse_train.append(root_mean_squared_error(??????, ??????)) # 訓練 fold_rmse_valid.append(root_mean_squared_error(??????, ??????)) # 検証 # ---------- alphaごとのRMSEの平均と標準偏差を格納 fold_rmse_train = np.array(fold_rmse_train) fold_rmse_valid = np.array(fold_rmse_valid) mean_rmse_train.append(fold_rmse_train.mean()) # CVの平均値を格納 mean_rmse_valid.append(fold_rmse_valid.mean()) 訓練データ（x_fold_trainなどのこと）と検証データに対する誤差を 横軸alpha，縦軸 RMSE のグラフでプロットする． 40データでも少ないくらいなので，検証誤差のブレが大きく，うまくいかない場合はデータ数などを調整すること．\nfig, ax = plt.subplots(1, 1) ax.plot(alphas, mean_rmse_train, label=\u0026#39;Training\u0026#39;) ax.plot(alphas, mean_rmse_valid, label=\u0026#39;Validation\u0026#39;) ax.set_xscale(\u0026#39;log\u0026#39;) # 横軸を対数スケールにする ax.set_ylim(0, 1) # 縦軸の範囲を指定 ax.set_xlabel(r\u0026#39;$\\alpha$\u0026#39;) ax.set_ylabel(\u0026#39;RMSE\u0026#39;) ax.legend() 検証誤差が最小になるalphaをalpha_optとして求めておく． np.argmin（最小となるインデックスを返すメソッド．わからない人は調べること）を用いる．\nalpha_opt = alphas[np.argmin(mean_rmse_valid)] alpha_opt # jupyterで出力用 この例では0.003053855508833419が出力された．\n演習4：最適化したハイパーパラメータを用いた Ridge 回帰 # ハイパーパラメータチューニングができたので，上で求めたalpha_optと全ての訓練データを用いて改めて Ridge 回帰を行う． 比較のために，ここでもalpha=0.0の最小二乗法の計算も行う．\nあとは 演習：Ridge 回帰 のときと同じことを行えばよい．\n計画行列 # # ---------- 計画行列の計算 phi = design_matrix(??????, M) # 計画行列作成 phi_test = design_matrix(??????, M) # テストデータの計画行列作成 phi_dense = design_matrix(??????, M) # x_denseの計画行列作成 # ---------- 標準化 ss = StandardScaler() phi_std = ss.?????? # 標準化 phi_test_std = ss.?????? # テストデータの標準化．fitはしない phi_dense_std = ss.?????? # x_denseの標準化．fitはしない alpha=0.0 # ridge0 = Ridge(alpha=0, fit_intercept=True) ridge0.fit(??????, ??????) # fit y0_dense_predict = ridge0.predict(??????) # x_denseに対する予測値 fig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.plot(x_test, y_test, \u0026#39;s\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Test data\u0026#39;) ax.plot(x_dense, y0_dense_predict, label=\u0026#39;Prediction\u0026#39;) ax.set_ylim(-1.5, 1.5) # 過学習するのでyの描画範囲を設定しておいた方がいい ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend(loc=\u0026#39;upper right\u0026#39;) データ数が増えても，訓練データがない端の方では大きく外れているのがわかる．\nalpha_opt # ridge1 = Ridge(alpha=alpha_opt, fit_intercept=True) ridge1.fit(??????, ??????) # fit y1_dense_predict = ridge1.predict(??????) # x_denseに対する予測値 fig, ax = plt.subplots(1, 1) ax.plot(x_dense, y_sin, label=r\u0026#39;$\\sin(2\\pi x)$\u0026#39;) ax.plot(x_train, y_train, \u0026#39;o\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Training data\u0026#39;) ax.plot(x_test, y_test, \u0026#39;s\u0026#39;, markeredgecolor=\u0026#39;black\u0026#39;, label=\u0026#39;Test data\u0026#39;) ax.plot(x_dense, y1_dense_predict, label=\u0026#39;Prediction\u0026#39;) ax.set_ylim(-1.5, 1.5) # 過学習するのでyの描画範囲を設定しておいた方がいい ax.set_xlabel(r\u0026#39;$x$\u0026#39;) ax.set_ylabel(r\u0026#39;$y$\u0026#39;) ax.legend(loc=\u0026#39;upper right\u0026#39;) 最小二乗法よりうまく回帰できている．\n誤差の評価 # 演習：Ridge 回帰 のときと全く同様に評価できるので，ここでは結果の例だけ載せておく．各自でやってみること．\nParity plot # alpha=0.0 alpha_opt RMSE # alpha=0.0\nTraining: RMSE = 0.08028158915814174 Test: RMSE = 0.6539752273916258 alpha_opt\nTraining: RMSE = 0.08888866230477017 Test: RMSE = 0.08870881375600688 ","externalUrl":null,"permalink":"/notes/linear_regression/cv/","section":"授業・研究ノート","summary":"Cross-validation を用いたハイパーパラメータチューニングを学ぶ","title":"演習：Cross-validation とハイパーパラメータチューニング","type":"notes"},{"content":" Transition Metal Oxides # We investigate the structural stability and electronic properties of transition metal oxides, with a particular focus on materials for spintronics and thermal management devices.\n","externalUrl":null,"permalink":"/en/research/tmo/","section":"Research","summary":"","title":"Transition Metal Oxides","type":"research"},{"content":" power() # np.power(x, y)は \\(x\\) と \\(y\\)をブロードキャストして，要素ごとのべき乗を計算する．\nnp.power(2, 3) # 2の3乗 出力\n8 x = np.array([1, 2, 3]) np.power(x, 2) 出力\narray([1, 4, 9]) スカラーの2は \\(x\\)に合わせてブロードキャストされて，要素ごとに2乗される x = np.array([1, 2, 3]) np.power(x, x) # 1**1, 2**2, 3**3 出力\narray([ 1, 4, 27]) x = np.array([1, 2, 3]) np.power(2, x) # 2**1, 2**2, 2**3 出力\narray([2, 4, 8]) 第1引数のスカラーがブロードキャストされる np.power(2, 3) # 2の3乗 出力\n8 A = np.array([[1], [2], [3]]) # 3行1列の2次元配列 p = np.array([1, 2, 3]) # 1次元配列 np.power(A, p) 出力\narray([[ 1, 1, 1], [ 2, 4, 8], [ 3, 9, 27]]) \\(A\\) のshapeは(3, 1)，\\(p\\) のshapeは(3, )なのでブロードキャストによって\nA.shpae: \\( (3, 1) \\to (3, 1) \\to (3, 3)\\) p.shape: \\( (3,) \\to (1, 3) \\to (3, 3) \\) のように形状が揃えられる．\n$$ A= \\begin{pmatrix} 1\\\\ 2\\\\ 3 \\end{pmatrix} \\rightarrow $$$$ A= \\begin{pmatrix} 1\\\\ 2\\\\ 3 \\end{pmatrix} \\rightarrow \\begin{pmatrix} 1 \u0026 1 \u0026 1\\\\ 2 \u0026 2 \u0026 2\\\\ 3 \u0026 3 \u0026 3 \\end{pmatrix} $$$$ p= \\begin{pmatrix} 1 \u0026 2 \u0026 3 \\end{pmatrix} \\rightarrow \\begin{pmatrix} 1 \u0026 2 \u0026 3\\\\ 1 \u0026 2 \u0026 3\\\\ 1 \u0026 2 \u0026 3 \\end{pmatrix} $$出力結果は，\\(3 \\times 3\\) 行列の各要素ごとのべき乗になっている．\n","externalUrl":null,"permalink":"/notes/python/numpy/power/","section":"授業・研究ノート","summary":"多項式回帰などで利用","title":"べき乗","type":"notes"},{"content":"ウェブサイトを一新しました．Hugo の Blowfish というテーマに変更しました．\nまた，授業・研究ノート の文章や画像などもある程度新しくしています．\n","date":"2026年6月19日","externalUrl":null,"permalink":"/posts/2026_06_19_renewal/","section":"お知らせ","summary":"ウェブサイトを一新しました","title":"ウェブサイトリニューアル","type":"posts"},{"content":"","date":"2026年6月19日","externalUrl":null,"permalink":"/tags/%E3%82%A6%E3%82%A7%E3%83%96%E3%82%B5%E3%82%A4%E3%83%88%E6%9B%B4%E6%96%B0/","section":"Tags","summary":"","title":"ウェブサイト更新","type":"tags"},{"content":"","date":"2026年6月19日","externalUrl":null,"permalink":"/categories/%E3%81%8A%E7%9F%A5%E3%82%89%E3%81%9B/","section":"Categories","summary":"","title":"お知らせ","type":"categories"},{"content":"","date":"2026年6月19日","externalUrl":null,"permalink":"/posts/","section":"お知らせ","summary":"","title":"お知らせ","type":"posts"},{"content":"Computational Materials Science Laboratory (Yamashita Laboratory) is established in November 2019 at Nagaoka University of Technology, Department of Electrical, Electronics and Information Engineering, Electronic Devices and Light Wave Control Engineering Group. We have been working on condensed matter physics and materials design using first-principles calculations and materials informatics. We have also developed a crystal structure prediction tool, CrySPY, which is available as an open source software.\nOverview # Members Research Photos Access ","date":"10 April 2026","externalUrl":null,"permalink":"/en/","section":"Computational Materials Science Laboratory","summary":"","title":"Computational Materials Science Laboratory","type":"page"},{"content":" Fuculty and Staff # Associate Professor # Tomoki Yamashita : yamashita06 vos.nagaokaut.ac.jp (replace \u0026ldquo;at\u0026rdquo;) : Tomoki Yamashita Students # M2 # Miyu Urasaki Yuta Watanabe M1 # Ryunosuke Suzuki Tanaka Rinku Takayoshi Matsumoto B4 # Koutarou Ikegai Mirai Ookawa Nagi Kato Shohei Sasaki Alumni # 2026 March # Yu Takatsuka Reo Morii Kaito Kosugi Suparerk Kongkitimanon 2025 March # Keita Kato Shin Yamai LAM DAO KHANG 2024 March # Takuma Otani Haruya Watanabe Keishiro Maeichi 2023 March # Takumi Sato Hirotaka Sekine Aoi Nishimura 2022 March # Taishi Igari 2021 March # Momoka Noguchi ","date":"10 April 2026","externalUrl":null,"permalink":"/en/members/","section":"Computational Materials Science Laboratory","summary":"","title":"Member","type":"page"},{"content":"","date":"2024年11月15日","externalUrl":null,"permalink":"/tags/%E3%83%A1%E3%83%B3%E3%83%90%E3%83%BC/","section":"Tags","summary":"","title":"メンバー","type":"tags"},{"content":"3年生が4名，研究室に配属され，現在\nM2: 2名 M1: 3名 B4: 3名 B3: 4名 の計12名の学生が所属しています．\nメンバーページ，トップページ等いくつかウェブサイトを更新しました．\n","date":"2024年11月15日","externalUrl":null,"permalink":"/posts/2024_11_15_b3/","section":"お知らせ","summary":"メンバーページを更新しました。","title":"メンバー更新","type":"posts"},{"content":"本研究室が中心となって開発しているCrySPYが 2024年度HPCIソフトウェア賞開発部門賞奨励賞を受賞しました！\n賞杯のクオリティが高い．\nhttps://www.hpci-c.jp/hrdevelop/award.html\n","date":"2024年6月3日","externalUrl":null,"permalink":"/posts/2024_06_03_hpci/","section":"お知らせ","summary":"賞を受賞しました","title":"HPCIソフトウェア賞開発部門賞奨励賞","type":"posts"},{"content":"","date":"2024年6月3日","externalUrl":null,"permalink":"/tags/%E5%8F%97%E8%B3%9E/","section":"Tags","summary":"","title":"受賞","type":"tags"},{"content":"3月に修士1名，4年生1名が卒業しました． 4月から1名修士で入ってきて，現在\nM2: 2名 M1: 3名 B4: 3名 の計8名の学生が所属しています．\nメンバーページ，トップページ等いくつかウェブサイトを更新しました．\n","date":"2024年4月1日","externalUrl":null,"permalink":"/posts/2024_04_01_member/","section":"お知らせ","summary":"メンバーページを更新しました．","title":"メンバー更新","type":"posts"},{"content":"B3の研究室配属が行われて，今年度も新たに3名が配属されました． メンバーページを更新しました．\n","date":"2023年11月2日","externalUrl":null,"permalink":"/posts/2023_11_02_b3/","section":"お知らせ","summary":"B3学生が研究室配属されました．","title":"2023年度B3配属","type":"posts"},{"content":"","date":"2023年3月31日","externalUrl":null,"permalink":"/tags/%E7%A0%94%E7%A9%B6%E5%AE%A4/","section":"Tags","summary":"","title":"研究室","type":"tags"},{"content":"電気3号棟の改修が終わり，当研究室は3号棟432へ引越しました． 広い部屋の奥半分が学生のデスク，手前半分がミーティングをしたり 人が集まるような場所になっています．\n広くて綺麗でかなり快適に過ごせる環境になりました．\n","date":"2023年3月31日","externalUrl":null,"permalink":"/posts/2023_03_31_move/","section":"お知らせ","summary":"研究室の引越しを行いました．","title":"研究室引越し","type":"posts"},{"content":"B3の研究室配属が行われて，うちの研究室には新たに3名が配属されました．\nM2が2名\nM1が1名\nB4が4名\nB3が3名\nの計10名体制になります．\nメンバーページを更新しました．\n","date":"2022年11月1日","externalUrl":null,"permalink":"/posts/2022_11_01_new_member/","section":"お知らせ","summary":"B3学生が配属されました．","title":"2022年度B3配属","type":"posts"},{"content":"","date":"2022年4月14日","externalUrl":null,"permalink":"/tags/%E7%B5%90%E6%99%B6%E6%A7%8B%E9%80%A0%E6%8E%A2%E7%B4%A2/","section":"Tags","summary":"","title":"結晶構造探索","type":"tags"},{"content":"結晶構造探索の探索アルゴリズムに関する論文がSTAM-Methodsに2本公開されました．\n1本目：進化的アルゴリズムとベイズ最適化のハイブリッドアルゴリズムの探索効率 https://doi.org/10.1080/27660400.2022.2055987\n2本目：改良したLAQAの探索効率 https://www.tandfonline.com/doi/full/10.1080/27660400.2022.2059335\n","date":"2022年4月14日","externalUrl":null,"permalink":"/posts/2022_04_14_two_paper/","section":"お知らせ","summary":"結晶構造探索の探索アルゴリズムに関する論文がSTAM-Methodsに2本公開されました．","title":"結晶構造探索に関する論文が2本公開されました","type":"posts"},{"content":"","date":"2022年4月14日","externalUrl":null,"permalink":"/tags/%E8%AB%96%E6%96%87/","section":"Tags","summary":"","title":"論文","type":"tags"},{"content":"B3の研究室配属が行われて，うちの研究室には新たに3名が配属されました．\nM1が2名\nB4が3名\nB3が3名\nの体制になります．\nメンバーページを更新しました．\n","date":"2021年11月9日","externalUrl":null,"permalink":"/posts/2021_11_09_new_member/","section":"お知らせ","summary":"B3学生が配属されました．","title":"B3配属","type":"posts"},{"content":"結晶構造探索ツールCrySPYに関する我々の論文が STAM-Methodsに公開されました．\nhttps://www.tandfonline.com/doi/full/10.1080/27660400.2021.1943171\n","date":"2021年7月14日","externalUrl":null,"permalink":"/posts/2021_07_14_paper_cryspy/","section":"お知らせ","summary":"結晶構造探索ツールCrySPYに関する我々の論文がSTAM-Methodsに公開されました．","title":"CrySPYの論文公開","type":"posts"},{"content":"","date":"2021年1月15日","externalUrl":null,"permalink":"/tags/%E6%9B%B8%E7%B1%8D/","section":"Tags","summary":"","title":"書籍","type":"tags"},{"content":"一部を執筆した書籍が発刊されます． 結晶構造探索について書きました．\nマテリアルズ・インフォマティクス開発事例最前線\nhttp://www.nts-book.co.jp/item/detail/summary/kobunsi/20210100_174.html\n","date":"2021年1月15日","externalUrl":null,"permalink":"/posts/2021_01_15_book/","section":"お知らせ","summary":"一部を執筆した書籍が発刊されます．","title":"書籍発刊","type":"posts"},{"content":"","date":"2021年1月4日","externalUrl":null,"permalink":"/tags/cmd/","section":"Tags","summary":"","title":"CMD","type":"tags"},{"content":"2021年2月22日（月）〜2月26日（金）に行われる第38回CMDワークショップに講師として参加します． マテリアルズインフォマティクスコース担当で，CrySPYの講義・演習を行う予定です．\n今回も前回と同じくオンラインで行われます．\n第38回 CMDワークショップ\nhttps://cmdworkshop.sakura.ne.jp/index.html\n","date":"2021年1月4日","externalUrl":null,"permalink":"/posts/2021_01_04_cmd38/","section":"お知らせ","summary":"第38回CMDワークショップに講師として参加します．","title":"第38回 CMDワークショップ","type":"posts"},{"content":"研究室のPCクラスターの計算ノードを7台増設しました．\n","date":"2020年12月7日","externalUrl":null,"permalink":"/posts/2020_12_07_cluster/","section":"お知らせ","summary":"研究室のPCクラスターの計算ノードを7台増設しました．","title":"計算ノード増設","type":"posts"},{"content":"B3の研究室配属が行われて，うちの研究室には新たに3名が配属されました．\nB4が3名\nB3が3名\nの体制になります．徐々に人が増えてきましたね．\nメンバーページを更新しました．\n","date":"2020年11月20日","externalUrl":null,"permalink":"/posts/2020_11_20_new_menber/","section":"お知らせ","summary":"B3学生が配属されました．","title":"B3配属","type":"posts"},{"content":"B4の野口さんに素晴らしい研究室のロゴを作ってもらいました． Computational Materials Science Laboratory の頭文字のCMSがロゴになっています．\n","date":"2020年10月7日","externalUrl":null,"permalink":"/posts/2020_10_07_logo/","section":"お知らせ","summary":"学生に研究室のロゴを作ってもらいました．","title":"研究室ロゴ","type":"posts"},{"content":"2020年8月31日（月）〜9月4日（金）に大阪大学で行われる第37回CMDワークショップに講師として参加します． マテリアルズ・インフォマティクス・コース担当で，CrySPYの講義・演習を行う予定です．\n今回はオンラインで行われます．\n第37回 CMDワークショップ\nhttp://phoenix.mp.es.osaka-u.ac.jp/CMD/CMD37/index.html\n","date":"2020年6月4日","externalUrl":null,"permalink":"/posts/2020_06_04_cmd/","section":"お知らせ","summary":"第37回CMDワークショップに講師として参加します．","title":"第37回 CMDワークショップ","type":"posts"},{"content":"2019年11月〜12月のミーティングルーム兼学生部屋の立ち上げの様子．\n","date":"2020年4月20日","externalUrl":null,"permalink":"/posts/2020_04_20_lab_room/","section":"お知らせ","summary":"ミーティングルーム兼学生部屋の立ち上げの様子","title":"研究室の部屋づくり","type":"posts"},{"content":"研究室のウェブサイトを公開しました．\n","date":"2020年4月20日","externalUrl":null,"permalink":"/posts/2020_04_20_start_web/","section":"お知らせ","summary":"研究室のウェブサイトを公開しました．","title":"研究室のウェブサイト","type":"posts"},{"content":" E-mail # yamashita06 vos.nagaokaut.ac.jp (replace \u0026ldquo;at\u0026rdquo;)\nAddress # Room 432, Third building of Electrical Engineering Department,\nNagaoka University of Technology,\n1603-1 Kamitomioka-machi, Nagaoka, Niigata, 940-2188, JAPAN\nAccess # Nagaoka University of Technology \u0026gt; Access\nMap # ","externalUrl":null,"permalink":"/en/access/","section":"Computational Materials Science Laboratory","summary":"","title":"Access","type":"page"},{"content":"","externalUrl":null,"permalink":"/en/authors/","section":"Authors","summary":"","title":"Authors","type":"authors"},{"content":"","externalUrl":null,"permalink":"/en/categories/","section":"Categories","summary":"","title":"Categories","type":"categories"},{"content":"","externalUrl":null,"permalink":"/en/tags/cryspy/","section":"Tags","summary":"","title":"CrySPY","type":"tags"},{"content":"","externalUrl":null,"permalink":"/en/tags/csp/","section":"Tags","summary":"","title":"CSP","type":"tags"},{"content":"","externalUrl":null,"permalink":"/en/tags/dft/","section":"Tags","summary":"","title":"DFT","type":"tags"},{"content":"","externalUrl":null,"permalink":"/en/tags/li-ion-batteries/","section":"Tags","summary":"","title":"Li-Ion Batteries","type":"tags"},{"content":"","externalUrl":null,"permalink":"/tags/li%E3%82%A4%E3%82%AA%E3%83%B3%E9%9B%BB%E6%B1%A0/","section":"Tags","summary":"","title":"Liイオン電池","type":"tags"},{"content":"","externalUrl":null,"permalink":"/series/matplotlib/","section":"Series","summary":"","title":"Matplotlib","type":"series"},{"content":"","externalUrl":null,"permalink":"/tags/matplotlib/","section":"Tags","summary":"","title":"Matplotlib","type":"tags"},{"content":"","externalUrl":null,"permalink":"/en/tags/mi/","section":"Tags","summary":"","title":"MI","type":"tags"},{"content":"","externalUrl":null,"permalink":"/series/numpy/","section":"Series","summary":"","title":"NumPy","type":"series"},{"content":"","externalUrl":null,"permalink":"/tags/numpy/","section":"Tags","summary":"","title":"NumPy","type":"tags"},{"content":" ","externalUrl":null,"permalink":"/en/photos/","section":"Computational Materials Science Laboratory","summary":"","title":"Photos","type":"page"},{"content":"","externalUrl":null,"permalink":"/tags/python/","section":"Tags","summary":"","title":"Python","type":"tags"},{"content":"","externalUrl":null,"permalink":"/en/categories/research/","section":"Categories","summary":"","title":"Research","type":"categories"},{"content":"","externalUrl":null,"permalink":"/en/research/","section":"Research","summary":"","title":"Research","type":"research"},{"content":"","externalUrl":null,"permalink":"/en/tags/semiconductors/","section":"Tags","summary":"","title":"Semiconductors","type":"tags"},{"content":"","externalUrl":null,"permalink":"/en/series/","section":"Series","summary":"","title":"Series","type":"series"},{"content":"","externalUrl":null,"permalink":"/en/tags/","section":"Tags","summary":"","title":"Tags","type":"tags"},{"content":"","externalUrl":null,"permalink":"/en/tags/transition-metal-oxides/","section":"Tags","summary":"","title":"Transition Metal Oxides","type":"tags"},{"content":"","externalUrl":null,"permalink":"/tags/%E3%83%9E%E3%83%86%E3%83%AA%E3%82%A2%E3%83%AB%E3%82%BA%E3%82%A4%E3%83%B3%E3%83%95%E3%82%A9%E3%83%9E%E3%83%86%E3%82%A3%E3%82%AF%E3%82%B9/","section":"Tags","summary":"","title":"マテリアルズインフォマティクス","type":"tags"},{"content":"","externalUrl":null,"permalink":"/tags/%E5%8D%8A%E5%B0%8E%E4%BD%93/","section":"Tags","summary":"","title":"半導体","type":"tags"},{"content":"","externalUrl":null,"permalink":"/categories/%E6%8E%88%E6%A5%AD%E7%A0%94%E7%A9%B6%E3%83%8E%E3%83%BC%E3%83%88/","section":"Categories","summary":"","title":"授業・研究ノート","type":"categories"},{"content":"","externalUrl":null,"permalink":"/notes/","section":"授業・研究ノート","summary":"","title":"授業・研究ノート","type":"notes"},{"content":"","externalUrl":null,"permalink":"/categories/%E7%A0%94%E7%A9%B6/","section":"Categories","summary":"","title":"研究","type":"categories"},{"content":"","externalUrl":null,"permalink":"/tags/%E7%AC%AC%E4%B8%80%E5%8E%9F%E7%90%86%E8%A8%88%E7%AE%97/","section":"Tags","summary":"","title":"第一原理計算","type":"tags"},{"content":"","externalUrl":null,"permalink":"/series/%E7%B7%9A%E5%BD%A2%E5%9B%9E%E5%B8%B0/","section":"Series","summary":"","title":"線形回帰","type":"series"},{"content":"","externalUrl":null,"permalink":"/tags/%E7%B7%9A%E5%BD%A2%E5%9B%9E%E5%B8%B0/","section":"Tags","summary":"","title":"線形回帰","type":"tags"},{"content":"","externalUrl":null,"permalink":"/tags/%E9%81%B7%E7%A7%BB%E9%87%91%E5%B1%9E%E9%85%B8%E5%8C%96%E7%89%A9/","section":"Tags","summary":"","title":"遷移金属酸化物","type":"tags"}]