경기도 인공지능 개발 과정/Python

[Python] 텐서플로를 이용한 아이리스 분류 예측

agingcurve 2022. 7. 11. 11:08
반응형

붓꽃 데이터 분류

  • 붓꽃 데이터를 분류실시 하고자 함
  • 독립변수 : 꽃잎길이, 꽃잎 폭, 꽃받침 길이, 꽃받침 폭
  • 종속변수 : 품종
  • 분류 알고리즘으로서 범주형인 종속변수를 더미화 시켜줘야 함
  • 활성화 함수 = 확률값으로 만들어주기,(softmax 사용)
In [6]:
import pandas as pd
import tensorflow as tf
import numpy as np
In [8]:
파일경로 = 'https://raw.githubusercontent.com/blackdew/tensorflow1/master/csv/iris.csv'
아이리스 = pd.read_csv(파일경로)
아이리스.head()
Out[8]:
꽃잎길이꽃잎폭꽃받침길이꽃받침폭품종01234
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa

원 핫 인코딩

In [11]:
아이리스 = pd.get_dummies(아이리스)
아이리스.columns
Out[11]:
Index(['꽃잎길이', '꽃잎폭', '꽃받침길이', '꽃받침폭', '품종_setosa', '품종_versicolor',
       '품종_virginica'],
      dtype='object')

종속변수, 독립변수 만들기

In [13]:
독립 = 아이리스[["꽃잎길이", "꽃잎폭", "꽃받침길이", "꽃받침폭"]]
종속 = 아이리스[['품종_setosa', '품종_versicolor','품종_virginica']]
print(독립.shape, 종속.shape)
(150, 4) (150, 3)

모델의 구조를 만들기

In [15]:
X = tf.keras.layers.Input(shape=[4])
Y = tf.keras.layers.Dense(3, activation="softmax")(X)
model = tf.keras.models.Model(X, Y)
model.compile(loss="categorical_crossentropy", metrics="accuracy")

데이터로 모델을 학습(fit) 합니다.

In [16]:
model.fit(독립, 종속, epochs=1000, verbose=0)
Out[16]:
<keras.callbacks.History at 0x179764de100>
In [17]:
model.fit(독립, 종속, epochs=10)
Epoch 1/10
5/5 [==============================] - 0s 745us/step - loss: 0.1599 - accuracy: 0.9800
Epoch 2/10
5/5 [==============================] - 0s 748us/step - loss: 0.1599 - accuracy: 0.9800
Epoch 3/10
5/5 [==============================] - 0s 748us/step - loss: 0.1598 - accuracy: 0.9733
Epoch 4/10
5/5 [==============================] - 0s 997us/step - loss: 0.1593 - accuracy: 0.9800
Epoch 5/10
5/5 [==============================] - 0s 997us/step - loss: 0.1595 - accuracy: 0.9800
Epoch 6/10
5/5 [==============================] - 0s 748us/step - loss: 0.1591 - accuracy: 0.9800
Epoch 7/10
5/5 [==============================] - 0s 2ms/step - loss: 0.1588 - accuracy: 0.9800
Epoch 8/10
5/5 [==============================] - 0s 814us/step - loss: 0.1594 - accuracy: 0.9800
Epoch 9/10
5/5 [==============================] - 0s 1ms/step - loss: 0.1586 - accuracy: 0.9800
Epoch 10/10
5/5 [==============================] - 0s 747us/step - loss: 0.1584 - accuracy: 0.9800
Out[17]:
<keras.callbacks.History at 0x1797deb02b0>
In [20]:
# 맨 처음 데이터 5개
print(model.predict(독립[:5]))
print(종속[:5])
[[9.9888462e-01 1.1152660e-03 1.1650093e-07]
 [9.9612755e-01 3.8716306e-03 8.7968226e-07]
 [9.9795592e-01 2.0436603e-03 3.8017203e-07]
 [9.9426842e-01 5.7299598e-03 1.6930873e-06]
 [9.9902654e-01 9.7337767e-04 9.8086687e-08]]
   품종_setosa  품종_versicolor  품종_virginica
0          1              0             0
1          1              0             0
2          1              0             0
3          1              0             0
4          1              0             0
In [22]:
# 맨 마지막 데이터 5개
print(model.predict(독립[-5:]))
print(종속[:5])
[[1.2744212e-06 1.0694206e-01 8.9305663e-01]
 [2.6538328e-06 1.9060923e-01 8.0938804e-01]
 [4.1453363e-06 2.1975918e-01 7.8023660e-01]
 [8.1190069e-07 8.9488134e-02 9.1051108e-01]
 [6.9935118e-06 2.7548695e-01 7.2450608e-01]]
   품종_setosa  품종_versicolor  품종_virginica
0          1              0             0
1          1              0             0
2          1              0             0
3          1              0             0
4          1              0             0
In [24]:
# weight & bias 출력
print(model.get_weights())
[array([[ 1.0517439 ,  0.24280372, -0.6103556 ],
       [ 2.4374871 ,  0.26637012, -0.94652283],
       [-3.6200664 ,  0.06324821,  1.3438181 ],
       [-3.411646  , -1.3415276 ,  2.0782392 ]], dtype=float32), array([ 1.9017988,  1.2580926, -1.7891243], dtype=float32)]