tensorflow / Keras
use tensorboard in keras
1 | from keras.callbacks import TensorBoard |
run in cmd
1 | tensorboard --logdir=./tmp/cnnsae |
obtain the output of an intermediate layerfrom keras
1 | from keras import backend as K |
plot_model
1 | from keras.utils import plot_model |
kfold
Split 5 folds for stacking1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
trainx=np.array(trainx)
testx=np.array(testx)
trainy=np.array(trainy)
ypred=copy.copy(trainy)
record=copy.copy(ypred)
out=np.zeros(29376)
for train_index, test_index in skf.split(trainx, trainy):
X_train, X_test = trainx[train_index], trainx[test_index]
y_train, y_test = trainy[train_index], trainy[test_index]
train_data = lgb.Dataset(X_train, label=y_train)
test_data = lgb.Dataset(X_test, label=y_test)
bst = lgb.train(params, train_data, num_round, valid_sets=test_data, early_stopping_rounds=50)
ypred[test_index] = bst.predict(X_test, num_iteration=bst.best_iteration)
out=out+bst.predict(testx, num_iteration=bst.best_iteration)
record=copy.copy(ypred)
out=out*0.2
out1=copy.copy(out)
Pandas
.iloc
選擇元素
Pandas 透過使用中括號 [] 與 .iloc 可以很靈活地從 data frame 中選擇想要的元素。要注意的是 Python 在指定 0:1 時不包含 1,在指定 0:2 時不包含 2,這一點是跟 R 語言有很大的不同之處。1
2Y = pd.read_csv('ind.csv', header=None)
trainy=Y.iloc[:52518,1]
numpy
argsort 排列
1 | x = np.array([3,2,1,0,4]) |
csv save & laod
1 |
|
print shape
1 | def shape(X): |
np.c_ / np.r_
用 [] not ()1
2
3
4
5
6
7
8
9
10a = np.array([1,2,3])
b = np.array([4,5,6])
np.c_[a,b]
>>>array([[1, 4],
[2, 5],
[3, 6]])
np.r_[a,b]
>>>array([1, 2, 3, 4, 5, 6])
random.seed()
1 | np.random.seed() |
兩點取最大距離
1 | dMax = max([np.linalg.norm(c1 - c2) for c1 in self.centers for c2 in self.centers]) |
np.array([]) 的shape
1 |
|
array 合併
1 | np.hstack |
Arg max/min index
1 | b = np.arange(6) |
norm
1 | np.linalg.norm(x-c)) |
解決 nam
1 | A = np.nan_to_num(A) |
矩陣 乘:
矩陣相乘 matirx multiplication1
2
3
4#A : (3,2) , B : (2,3)
c = np.dot(A,B) or A@B
>>>C.shape : (3,3)
元素相乘 element wise
1 | D = A*b.T |
matplotlib
import matplotlib
import matplotlib.pyplot as plt
畫矩陣
1 | plt.matshow(b) |
畫圖 plot / fig size
1 | fig.set_size_inches(50, 500) |
subplot
1 | http://blog.topspeedsnail.com/archives/760 |
others
回上一層路徑
1 | import os |
or1
2
3
4
5
6
7
8
拿到目前工作的目錄,然後將字串拆開,捨棄最後一段
在將其組合~~
import os
FullPath = os.getcwd()
FullPath = FullPath.split("\\")[:-1]
FullPath = "\\".join(FullPath)
print FullPath
requirements
生成requirements.txt文件
pip freeze > requirements.txt
安装requirements.txt依赖
pip install -r requirements.txt