-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_code_xgb
142 lines (109 loc) · 4.2 KB
/
test_code_xgb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from keras.models import Sequential,Model
from keras.layers import Dense, Dropout,Flatten,Conv1D,MaxPool1D,BatchNormalization
import os
from tqdm import tqdm
import numpy as np
import pandas as pd
import xgboost as xgb
#os.chdir(r'E:\planetdata\model')
fname=r'F:\行星比赛\planetdata-kaggle\model\model2_14-0.02.hdf5'
#重新搭建模型
model=Sequential()
model.add(Conv1D(filters=64,kernel_size=3,strides=1,activation='sigmoid',padding="same",input_shape=(2600,1)))
model.add(BatchNormalization())
model.add(Conv1D(filters=64,kernel_size=3,strides=1,activation='sigmoid',padding="same"))
model.add(MaxPool1D(pool_size=4))
model.add(Dropout(0.25))
model.add(Conv1D(filters=32,kernel_size=3,strides=1,activation='sigmoid',padding="same"))
model.add(BatchNormalization())
model.add(Conv1D(filters=32,kernel_size=3,strides=1,activation='relu',padding="same"))
model.add(BatchNormalization())
model.add(MaxPool1D(pool_size=4))
model.add(Conv1D(filters=32,kernel_size=3,strides=1,activation='relu',padding="same"))
model.add(BatchNormalization())
model.add(MaxPool1D(pool_size=2,strides=2))
model.add(Conv1D(filters=32,kernel_size=3,strides=1,activation='relu',padding="same"))
model.add(BatchNormalization())
model.add(Conv1D(filters=32,kernel_size=3,strides=1,activation='relu',padding="same"))
model.add(BatchNormalization())
model.add(MaxPool1D(pool_size=2,strides=2))
model.add(Conv1D(filters=32,kernel_size=3,strides=1,activation='relu',padding="same"))
model.add(Dropout(0.25))
model.add(BatchNormalization())
model.add(Conv1D(filters=32,kernel_size=3,strides=1,activation='relu',padding="same"))
model.add(Dropout(0.25))
model.add(BatchNormalization())
model.add(Conv1D(filters=32,kernel_size=3,strides=1,activation='relu',padding="same"))
model.add(Dropout(0.25))
model.add(BatchNormalization())
model.add(MaxPool1D(pool_size=2,strides=1))
model.add(Flatten())
model.add(Dense(128, activation='sigmoid'))
model.add(Dense(32, activation='sigmoid'))
model.add(Dropout(0.25))
# 输出层
model.add(Dense(3, activation='softmax'))
model.load_weights(fname)
model_layer_xgb = Model(inputs=model.input,outputs=model.get_layer('dense_2').output)
xgb_model=xgb.Booster(model_file=r'F:\行星比赛\planetdata-kaggle\model\xgb.model')
#逐条进行predict
#os.chdir(r'E:\planetdata\vaildation')
filepath=r'E:\planetdata\vaildation'
file=os.listdir(filepath)
file.sort()
file.sort(key=lambda x:int(x[:-5]))
val_data=[]
print('进行CNN前端特征提取')
for index in tqdm(range(len(file))):
data = np.fromfile(file[index],dtype=np.float64)
data=np.array(data) #只有先变成list形式,然后转为np.array才能reshape成功
data=data.reshape((data.shape[0],data.shape[1],1))
Y_pred = model_layer_xgb.predict(data)
val_data.append(Y_pred)
val_data=np.array(val_data)
print('测试集数据大小:',val_data.shape)
dtest=xgboost.DMatrix(val_data)
outcome=xgb_model.predict(dtest)
#标准验证集label读取
val_file=r'F:\行星数据\vaildation\val_labels_v1.csv'
val= pd.read_csv(val_file,encoding='utf-8')
v_label=val['label']
label_ture=[]
ID=val['id']
ID=ID.to_frame()
print('数据标签读取')
for i in tqdm(range(len(v_label))):
if v_label[i]=="star":
label_ture.append(0)
elif v_label[i]=="qso":
label_ture.append(1)
elif v_label[i]=="galaxy":
label_ture.append(2)
#生成submission格式
class_pred=[]
print("生成submission格式")
pre_result=[]
for i in range(len(outcome)):
if outcome[i][0]==0:
class_pred.append("star")
pre_result.append(0)
elif outcome[i][0]==1:
class_pred.append("qso")
pre_result.append(1)
elif outcome[i][0]==2:
class_pred.append("galaxy")
pre_result.append(2)
#pre_result=np.array(pre_result)
pre_result1=np.array(pre_result)
pre_result1.tofile(r"E:\planetdata"+"\\"+"result"+"\\"+"pre_result2.bin")
print("pre_result已保存")
#评分计算
from sklearn.metrics import f1_score
f1 = f1_score(label_ture,pre_result, average='macro' )
print('本模型测试结果:',f1)
from pandas.core.frame import DataFrame
c={"label":class_pred}
data=DataFrame(c)
df = pd.concat([ID,data], axis=1)
df.to_csv(r"E:\planetdata\result\predict_label3.csv",index=False)
print("保存为上传文件类型")