Artificial Intelligence

[keras] 시각화

inee0727 2022. 6. 22. 16:32

전체코드

from tensorflow.keras.models import Sequential 
from tensorflow.keras.layers import Dense
import numpy as np
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,3,8,12,13,8,14,15,9,6,17,23,21,20])
x_train, x_test, y_train, y_test = train_test_split(x,y, train_size=0.7, shuffle=True, random_state=66)

#2. 모델구성
model=Sequential()
model.add(Dense(5, input_dim=1))
model.add(Dense(4))
model.add(Dense(4))
model.add(Dense(4))
model.add(Dense(1))
           
#3. 컴파일, 훈련
model.compile(loss='mse', optimizer='adam')
model.fit(x_train, y_train, epochs=1, batch_size=1)

#4. 평가, 예측
loss = model.evaluate(x_test, y_test)
print('loss : ',loss)

y_predict = model.predict(x)

import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.plot(x, y_predict, color='red')
plt.show()

코드풀이

y_predict = model.predict(x)

x데이터 전체의 데이터 예측값을 y_predict 변수에 저장한다. 

 

import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.plot(x, y_predict, color='red')
plt.show()

x,y 를 점으로 표현한다.

x,y 결과값을 (예측값) 빨간색 그래프로 표현한다.

위 두개의 값을 나타내라.

 

 

https://github.com/junginee/study/blob/main/keras/keras06_scatter.py

 

GitHub - junginee/study

Contribute to junginee/study development by creating an account on GitHub.

github.com