np.fmodに絡んだnumpy記載メモ
x_train = x[np.fmod(range(16), 4) !=i]
という記述があり、意味がわからず、調べて理解したのでメモ
i には、
for i in range(0,4)
x は、np.array
x = np.array([15.42555012, 23.00811234, 5.00285937, 12.55831432, 8.66889727, 7.30846487, 9.65650528, 13.63901818, 14.91918686, 18.47041835, 15.47986286, 2.13048751, 10.11130624, 26.95293591, 5.68468983, 21.76168775])
np.fmodは、
In [3]: np.fmod(10,3) == 10%3 Out[3]: True
と同じ
In [32]: np.fmod(range(16), 4) Out[32]: array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int32)
iに0が入ったら、
In [24]: [np.fmod(range(16), 4) !=0] Out[24]: [array([False, True, True, True, False, True, True, True, False, True, True, True, False, True, True, True])]
Falseが取り除かれ、Trueが残るってことか、、、
In [47]: x[np.fmod(range(16), 4) !=0] Out[47]: array([23.00811234, 5.00285937, 12.55831432, 7.30846487, 9.65650528,13.63901818, 18.47041835, 15.47986286, 2.13048751, 26.95293591, 5.68468983, 21.76168775]) In [48]: x Out[48]: array([15.42555012, 23.00811234, 5.00285937, 12.55831432, 8.66889727, 7.30846487, 9.65650528, 13.63901818, 14.91918686, 18.47041835, 15.47986286, 2.13048751, 10.11130624, 26.95293591, 5.68468983, 21.76168775])
numpyならではの書き方ね。
f表記がPython3.6から使える
item1 = 'apple' item2 = 'bananas' item3 = 'grapes' print('At the grocery store, I bought some {0} and {1} and {2}.'.format(item1,item2,item3)) print('At the grocery store, I bought some {0} and {1} and {2}.'.format('apple',2,3))#intも入れられる。 print(f'At the grocery store, I bought some {item1} and {item2} and {item3}.')#3.6から f表記
f表記、3.6から
shelve したあと、中身みたいとき。
# 変数専用ファイルにデータを保管 pickle モジュールを使う import shelve shelve_file = shelve.open('friend') my_friend = ['A_san', 'B_san', 'C_san'] shelve_file['my_friend'] = my_friend print(shelve_file.keys) a =list(map(str,shelve_file)) print(a) shelve_file.close() print('file save')
ほんと、これしか無いのかな。。
jupyter_notebookでのモジュールを自動で読み込み直すやつ
%reload_ext autoreload
%autoreload 2
mapがわからなくなるのでメモ
a = list(range(1,100)) print(list(map(str, a)))
たまにsplitとjoinがわからなくなるのでメモ(ついでにmap)
a = '1 2 3 4 5 6 7 8' b = list(a.split()) print(b) ['1', '2', '3', '4', '5', '6', '7', '8']
c= ",".join(b) print(type(c)) <class 'str'>
d = list(map(int,b)) print(d) [1, 2, 3, 4, 5, 6, 7, 8]
pwdとcdをpython os モジュールで やってみる。
import os #カレントディレクトリ os.getcwd() #ディレクトリ移動(一つ上のディレクトリへ) os.chdir('../')