라떼는말이야

[머신러닝] 분류 모델 - 소프트맥스 (아이리스 품종 분류) 본문

공부

[머신러닝] 분류 모델 - 소프트맥스 (아이리스 품종 분류)

MangBaam 2021. 1. 8. 14:13
반응형
# 라이브러리 사용
import tensorflow as tf
import pandas as pd

# 학습할 데이터를 준비
파일경로 = 'https://raw.githubusercontent.com/blackdew/tensorflow1/master/csv/iris.csv'
아이리스 = pd.read_csv(파일경로)

# 원핫 인코딩
아이리스 = pd.get_dummies(아이리스)

# 독립변수, 종속변수
print(아이리스.columns)
독립 = 아이리스[['꽃잎길이', '꽃잎폭', '꽃받침길이', '꽃받침폭']]
종속 = 아이리스[['품종_setosa', '품종_versicolor', '품종_virginica']]
print(독립.shape, 종속.shape)

Index(['꽃잎길이', '꽃잎폭', '꽃받침길이', '꽃받침폭', '품종_setosa', '품종_versicolor', '품종_virginica'], dtype='object') (150, 4) (150, 3)

# 2. 모델의 구조를 만듭니다.
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')

ativation = 활성 함수

분류에 사용하는 loss = crossentropy

회귀에 사용하는 loss = mse

# 3. 데이터로 모델을 학습(FIT)합니다.
model.fit(독립, 종속, epochs=1000)
# 모델을 이용합니다.
model.predict(독립[-5:])

array([[6.2059939e-08, 1.1649571e-01, 8.8350415e-01], [1.9578130e-07, 1.7423630e-01, 8.2576352e-01], [3.6132553e-07, 2.2182100e-01, 7.7817857e-01], [4.3966246e-08, 8.5262723e-02, 9.1473722e-01], [8.4002801e-07, 2.4967657e-01, 7.5032258e-01]], dtype=float32)

 

종속[-5:]

# 학습한 가중치
model.get_weights()

[array([[ 1.19577 , 0.39463952, -0.7861807 ],

[ 3.353496 , 0.6629314 , -0.9050863 ],

[-4.2781935 , -0.73708016, 1.1811959 ],

[-5.7790146 , -1.5193857 , 1.83768 ]], dtype=float32),

array([ 1.8570591, 1.5306565, -1.5240403], dtype=float32)]

반응형
Comments