-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeras07_R2_1.py
44 lines (29 loc) · 941 Bytes
/
keras07_R2_1.py
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
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split
#1.데이터
x = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
y = np.array([1,2,4,3,5,7,9,8,9,3,10,11,12,13,23,15,20,17,18,16])
print(len(y))
x_train, x_test, y_train, y_test = train_test_split(x, y,
train_size=0.7, random_state=66)
#2.모델구성
model = Sequential()
model.add(Dense(5, input_dim=1))
model.add(Dense(5))
model.add(Dense(50))
model.add(Dense(50))
model.add(Dense(50))
model.add(Dense(50))
model.add(Dense(1))
#3. 컴파일, 훈련
model.compile(loss='mse', optimizer='adam' )
model.fit(x_train, y_train, epochs = 500, batch_size=10)
#4. 평가, 예측
loss = model.evaluate(x_test, y_test)
print('loss', loss)
y_predict = model.predict(x)
from sklearn.metrics import r2_score
r2 = r2_score(y, y_predict)
print('r2', r2)