import pandas as pd
import tensorflow as tf
---------------------------------------------------------
레모네이드 = pd.read_csv(path)
독립 = 레모네이드[['온도']]
종속 = 레모네이드[['판매량']]
print(독립.shape, 종속.shape)
<출력>
(6,1), (6,1)
---------------------------------------------------------
x = tf.keras.layers.Input(shape=[1])
y = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(x, y)
model.compile(loss='mse')
---------------------------------------------------------
model.fit(독립, 종속, epochs=1000, verbose=0)
---------------------------------------------------------
model.predict(독립[:3])
<출력>
[[11.286754]
[11.800876]
[12.314999]]
print(종속[:3])
<실제값> <예측값>
40 22.71
42 23.74
44 24.76
===> 오차는 5정도 됨을 알 수 있다.
---------------------------------------------------------
model.get_weights()
<출력>
[array([[1.9862037]], dtype=float32), array([0.30032754], dtype=float32)]
=> 판매량 = 1.9862037 * 온도 + 0.30032754
target = ['medv']
x = data.drop(target, axis=1)
y = data.loc[:, target]
print(x_train.shape, y_train.shape)
<출력>
(506, 13) (506, 1)
---------------------------------------------------------
x_train = tf.keras.layers.Input(shape=[13])
y_train = tf.keras.layers.Dense(1)(x_train)
model = tf.keras.Model(x,y)
model.compile(loss='mse')
---------------------------------------------------------
model.fit(x_train, y_train, epchs=500, verbose=0)
---------------------------------------------------------
model.predict(x[:5])
<출력>
array([[28.015047],
[24.721672],
[28.998194],
[28.731518],
[27.936375]], dtype=float32)
y[:5]
medv
0 24.0
1 21.6
2 34.7
3 33.4
4 36.2
==> 오차는 2정도 됨을 알 수 있다.
---------------------------------------------------------
w, b =model.get_weights()
variables = ['crim', 'zn', 'indus', 'chas', 'nox', 'rm', 'age', 'dis', 'rad', 'tax', 'ptratio', 'b', 'lstat']
weights = [f"{e[0]} * {v}" for e, v in zip(w, variables)]
print("y =", "\n + ".join(weights), f"\n + {b[0]}")
weights = [f'{e[0]} * x{i}' for i, e in enumerate(w)]
print('y =', '\n '.join(weights), f'+\n {b[0]}')
<출력>
y = -0.0763624757528305 * x0
0.08369160443544388 * x1
-0.031242644414305687 * x2
3.136476516723633 * x3
1.007555365562439 * x4
2.3621747493743896 * x5
0.06257478892803192 * x6
-0.3087534010410309 * x7
0.12276262044906616 * x8
-0.007904508151113987 * x9
0.25756800174713135 * x10
0.019396038725972176 * x11
-0.7171409130096436 * x12 +
1.8400682210922241
target = ['품종']
x = data.drop(target, axis=1)
y = data.loc[:, target]
y = pd.get_dummies(y, columns = target)
print(x.shape, y.shape)
<출력>
(150, 4) (150, 3)
---------------------------------------------------------
x_train = tf.keras.layers.Input(shape=[4])
y_train = tf.keras.layers.Dense(3, activation='softmax)(x_train)
model = tf.keras.Model(x, y)
model.compile(loss = 'categorical_crossentropy', metrics='accuracy')
---------------------------------------------------------
model.fit(x, y, epochs=500, verbose=0)
방법동일
---------------------------------------------------------
model.save('my_model.keras')
mymodel = tf.keras.models.load_model('my_model.keras')
---------------------------------------------------------