ユーザ用ツール

サイト用ツール


プログラム:python

差分

このページの2つのバージョン間の差分を表示します。

この比較画面にリンクする

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
プログラム:python [2023/06/13 08:43]
kawaue [install (MacOS)]
プログラム:python [2025/08/14 09:23] (現在)
kawaue
行 41: 行 41:
  
 ===Jupyter Notebook=== ===Jupyter Notebook===
-仮想環境をアクティベートした状態で+仮想環境をアクティベートした状態で以下のコマンドで起動。
 <file> <file>
 $ conda install jupyter $ conda install jupyter
行 47: 行 47:
 </file> </file>
  
-モジュールのインストール\\+Notebook上でのモジュールのインストールの仕方。\\
 <file> <file>
- !pip install //module//+ !pip install module
 </file> </file>
-importできない時\\ +importできない時pathの確認
-pathの確認\\+
 <file> <file>
- !pip show //module//+ !pip show module
 </file> </file>
-追加\\+をして、追加\\
 <file> <file>
  import sys  import sys
- sys.path.append('//path//')+ sys.path.append('path')
 </file> </file>
 +
 +便利なコマンド\\
 +cellをdeactivate : esc + r\\
 +cellをactivate : esc + y
 ====文法==== ====文法====
 ===スコープ=== ===スコープ===
-indentの位置でスコープが決まる(?)。+indentの位置でスコープが決まる(?)。\\ 
 +コメントであってもindentの位置を変えることは許されない。(),[],{}内は改行が許されている
  
 ===宣言=== ===宣言===
 変数の宣言時に型の指定は不要(?)。 変数の宣言時に型の指定は不要(?)。
 函数の引数にも不要(?)。 函数の引数にも不要(?)。
 +
 +注:組み込み関数を上書きしようとしても何もエラーを吐かず、そのまま上書きする(?)。\\
 +例:print = 0などとしてしまうと、何もエラーを吐かずprintが関数でなくintになり、print()がエラーを吐くようになる。\\
 +対処法:del printでリセット可能。
 +
 +===文字列===
 +<nowiki>''</nowiki>と""どちらも可(!?)
 +
 +変数を文字列にして出力
 +<code>
 +print("Process %s type %d : progress %.3f %%"%(hoge,huga,piyo))
 +</code>
 +%s : 文字, %d : 整数, %.3f : float3桁まで表示。 %% : %のエスケープ
  
 ===ライブラリの読み込み方=== ===ライブラリの読み込み方===
行 85: 行 102:
 from hoge import Hoge from hoge import Hoge
 </file> </file>
 +
 +===関数===
 +以下のように宣言する。返り値を複数設定可能で、配列で渡される。
 +<file>
 +def hoge(huga,piyo):
 +    foo = huga + piyo
 +    bar = huga - piyo
 +    return foo, piyo
 +</file>
 +配列の各要素に関数を作用させたいとき\\
 +例:shapeが(1000,2)のnumpy.array num_arrayの二つ目の軸の要素の和と差のnumpu.arrayを作りたい
 +<file>
 +new_array = np.apply_along_axis(lambda element: hoge(*element), 1, num_array)
 +</file>
 +lambda : ラムダ式。ここでだけ使う関数を宣言できる。
  
 ===クラス=== ===クラス===
行 119: 行 151:
 </file> </file>
 ''range(start,stop,step)''は$\mathrm{start}\leqq i<\mathrm{stop}$の間で''step''ずつカウンターを増やす。 ''range(start,stop,step)''は$\mathrm{start}\leqq i<\mathrm{stop}$の間で''step''ずつカウンターを増やす。
 +
 +===組み込み関数など===
 +type() : 型を表示する。
  
 ====デバッグ==== ====デバッグ====
行 141: 行 176:
 |.concatenate() | 既存の次元に対して結合| |.concatenate() | 既存の次元に対して結合|
 |.stack() | 新たな次元を作って結合| |.stack() | 新たな次元を作って結合|
 +
 +===hist===
 +a,b,c=hist
 +
 +a=value of each bin, b=bin
 +
 +====2軸plot====
 +<code>
 +run_index = 0
 +fig, ax1 = plt.subplots()
 +ax2 = ax1.twinx()
 +ax2.plot(histories2[run_index].history['lr'],label='learning rate',linestyle='--',color='green')
 +ax1.plot(histories2[run_index].history['loss'],label='training loss')
 +ax1.plot(histories2[run_index].history['val_loss'],label='validation loss')
 +plt.title('1st iteration, step 1, batch : 8192 lr : 1e-6, 1st trials')
 +ax1.set_xlabel('epoch')
 +ax1.set_ylabel('Loss')
 +ax2.set_ylabel('Learning Rate')
 +handler1, label1 = ax1.get_legend_handles_labels()
 +handler2, label2 = ax2.get_legend_handles_labels()
 +ax1.legend(handler1 + handler2, label1 + label2)
 +#plt.legend()
 +plt.figure()
 +</code>
  
 ====機械学習==== ====機械学習====
行 174: 行 233:
 </file> </file>
  
 +====Condaの使い方====
 +環境の複製
 +<code>
 +conda create --name new_environment_name --clone existing_environment_name
 +</code>
 +環境の削除
 +<code>
 +conda env remove --name myenv
 +</code>
プログラム/python.1686645795.txt.gz · 最終更新: 2023/06/13 08:43 by kawaue