5.4.1. Keras로 분석한 선형 회귀

Last updated
Was this helpful?

Last updated
Was this helpful?
Was this helpful?
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import RMSprop
from mpl_toolkits.mplot3d import Axes3D
raw_data = np.genfromtxt('x09.txt', skip_header=36)
xs = np.array(raw_data[:,2], dtype=np.float32)
ys = np.array(raw_data[:,3], dtype=np.float32)
zs = np.array(raw_data[:,4], dtype=np.float32)
#==== 입력 데이터를 보기위한 테스트 코드 =====
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xs, ys, zs)
ax.set_xlabel('Weight')
ax.set_ylabel('Age')
ax.set_zlabel('Blood fat')
ax.view_init(15, 15)
plt.show() import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import RMSprop
from mpl_toolkits.mplot3d import Axes3D
raw_data = np.genfromtxt('x09.txt', skip_header=36)
xs = np.array(raw_data[:,2], dtype=np.float32)
ys = np.array(raw_data[:,3], dtype=np.float32)
zs = np.array(raw_data[:,4], dtype=np.float32)
x_data = np.array(raw_data[:,2:4], dtype=np.float32)
y_data = np.array(raw_data[:,4], dtype=np.float32)
y_data = y_data.reshape((25,1))
rmsprop = RMSprop(lr=0.01)
model = Sequential()
model.add(Dense(1,input_shape=(2,)))
model.compile(loss='mse',optimizer=rmsprop)
model.summary()
hist = model.fit(x_data, y_data, epochs=1000)
print(hist.history.keys())
print("100Kg 40세 혈중지방함량치=",model.predict(np.array([100,40]).reshape(1,2)))
print("60Kg 25세 혈중지방함량치=",model.predict(np.array([60,25]).reshape(1,2)))
W_, b_ = model.get_weights()
x = np.linspace(20, 100, 50).reshape(50,1)
y = np.linspace(10, 70, 50).reshape(50,1)
X = np.concatenate((x,y), axis=1)
Z = np.matmul(X, W_) + b_
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, Z)
ax.scatter(xs, ys, zs)
ax.set_xlabel('Weight')
ax.set_ylabel('Age')
ax.set_zlabel('Blood fat')
ax.view_init(15, 15)
plt.show()_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 1) 3
=================================================================
Total params: 3
Trainable params: 3
Non-trainable params: 0
100Kg 40세 혈중지방함량치= [[350.53384]]
60Kg 25세 혈중지방함량치= [[219.5949]]