ユーザ用ツール

サイト用ツール


プログラム:python

差分

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

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

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
プログラム:python [2024/08/15 09:13]
kawaue
プログラム:python [2025/08/14 09:23] (現在)
kawaue
行 67: 行 67:
 ===スコープ=== ===スコープ===
 indentの位置でスコープが決まる(?)。\\ indentの位置でスコープが決まる(?)。\\
-コメントであってもindentの位置を変えることは許されない。+コメントであってもindentの位置を変えることは許されない。(),[],{}内は改行が許されている
  
 ===宣言=== ===宣言===
 変数の宣言時に型の指定は不要(?)。 変数の宣言時に型の指定は不要(?)。
 函数の引数にも不要(?)。 函数の引数にも不要(?)。
 +
 +注:組み込み関数を上書きしようとしても何もエラーを吐かず、そのまま上書きする(?)。\\
 +例:print = 0などとしてしまうと、何もエラーを吐かずprintが関数でなくintになり、print()がエラーを吐くようになる。\\
 +対処法:del printでリセット可能。
  
 ===文字列=== ===文字列===
-<nowiki>''</nowiki>と""を区別しない(!?)+<nowiki>''</nowiki>と""どちらも可(!?) 
 + 
 +変数を文字列にして出力 
 +<code> 
 +print("Process %s type %d : progress %.3f %%"%(hoge,huga,piyo)) 
 +</code> 
 +%s : 文字, %d : 整数, %.3f : float3桁まで表示。 %% : %のエスケープ
  
 ===ライブラリの読み込み方=== ===ライブラリの読み込み方===
行 92: 行 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 : ラムダ式。ここでだけ使う関数を宣言できる。
  
 ===クラス=== ===クラス===
行 127: 行 152:
 ''range(start,stop,step)''は$\mathrm{start}\leqq i<\mathrm{stop}$の間で''step''ずつカウンターを増やす。 ''range(start,stop,step)''は$\mathrm{start}\leqq i<\mathrm{stop}$の間で''step''ずつカウンターを増やす。
  
-===関数など===+===組み込み関数など===
 type() : 型を表示する。 type() : 型を表示する。
  
行 156: 行 181:
  
 a=value of each bin, b=bin 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>
 +
 ====機械学習==== ====機械学習====
 ===tensorflow=== ===tensorflow===
プログラム/python.1723713195.txt.gz · 最終更新: 2024/08/15 09:13 by kawaue