ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Multi-Layer로 확장 (IRIS분류)
    학교/인공지능 2024. 6. 8. 17:14
    import tensorflow as tf
    import pandas as pd

    # 과거 데이터 준비
    filepath = '/content/sample_data/iris.csv'
    iris = pd.read_csv(filepath)
    iris.head()

    # one-hot encoding
    after_encoding = pd.get_dummies(iris)
    after_encoding.head()
    print(after_encoding.columns)

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

    # 2. 모델 구조
    X = tf.keras.layers.Input(shape=[4])
    H = tf.keras.layers.Dense(8, activation='swish')(X)
    Y = tf.keras.layers.Dense(3, activation='softmax')(H)      
    model = tf.keras.models.Model(X, Y)
    model.compile(loss='categorical_crossentropy', metrics='accuracy')

    # 모델 구조 확인
    model.summary()

    # 3. 모델 학습(FIT)
    model.fit(independent, dependent, epochs=90, verbose=0)
    # model.fit(independent, dependent, epochs=10)

    # 4. 모델 이용. 맨 처음 데이터 5개
    print(model.predict(independent[:5]))
    print(dependent[:5])

    # weight & bias 출력
    # print(model.get_weights())

     

     

     

    1. 데이터 준비 및 확인

    import tensorflow as tf
    import pandas as pd

    # 과거 데이터 준비
    filepath = '/content/sample_data/iris.csv'
    iris = pd.read_csv(filepath)
    iris.head()

     

     

    • 먼저 TensorFlow와 Pandas를 가져옵니다.
    • pd.read_csv() 함수를 사용하여 CSV 파일을 읽고, 데이터프레임 형태로 변환합니다.
    • iris.head()를 호출하여 데이터프레임의 처음 5개 행을 출력합니다.

     

     

     

     

     

     

     

    2. One-hot Encoding

    after_encoding = pd.get_dummies(iris)
    after_encoding.head()
    print(after_encoding.columns)
     

     

    • One-hot encoding을 수행합니다. 범주형 데이터를 더미 변수로 변환합니다.
    • pd.get_dummies() 함수를 사용하여 범주형 변수를 더미 변수로 변환합니다.
    • 변환된 데이터프레임의 처음 5개 행을 출력합니다.
    • after_encoding.columns을 사용하여 더미 변수의 열 목록을 출력합니다.

     

     

     

     

     

     

    3. 독립 변수와 종속 변수 설정

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

     

     

    • 독립 변수는 꽃잎과 꽃받침의 길이와 폭입니다.
    • 종속 변수는 세 가지 품종(setosa, versicolor, virginica)을 나타내는 더미 변수입니다.
    • shape를 사용하여 독립 변수와 종속 변수의 모양을 출력합니다.

     

     

     

     

     

     

     

     

     

    4. 모델 구조 생성

    X = tf.keras.layers.Input(shape=[4])
    H = tf.keras.layers.Dense(8, activation='swish')(X)
    Y = tf.keras.layers.Dense(3, activation='softmax')(H)      
    model = tf.keras.models.Model(X, Y)
    model.compile(loss='categorical_crossentropy', metrics='accuracy')

     

     

     

    • 입력 레이어(X)를 생성합니다. 여기서는 4개의 특징이 있는 데이터를 사용합니다.
    • tf.keras.layers.Dense()를 사용하여 은닉층(H)과 출력 레이어(Y)를 생성합니다. 은닉층에는 Swish 활성화 함수를 사용하였고, 출력 레이어에는 Softmax 활성화 함수를 사용합니다.
    • tf.keras.models.Model()로 모델을 생성하고, model.compile() 함수를 사용하여 손실 함수를 categorical crossentropy로 설정하고 정확도 지표를 설정합니다.

     

     

     

     

     

     

     

     

     

    5. 모델 구조 확인

    model.fit(independent, dependent, epochs=90, verbose=0)

     

    • model.fit() 함수를 사용하여 모델을 학습시킵니다. 독립 변수와 종속 변수를 입력하고, 90번의 에포크 동안 학습합니다.

     

     

     

     

     

     

     

     

    6. 모델 이용(예측)

    print(model.predict(independent[:5]))
    print(dependent[:5])

     

     

    • model.predict() 함수를 사용하여 학습된 모델을 사용하여 독립 변수의 일부 데이터에 대한 예측을 수행합니다.
    • 실제 종속 변수 값을 출력하여 예측 값과 비교합니다.

     

     

     

     

     

    '학교 > 인공지능' 카테고리의 다른 글

    Batch Normalization (IRIS 분류)  (0) 2024.06.19
    Multi-Layer로 확장 (보스톤 집값)  (1) 2024.06.08
    IRIS 분류  (0) 2024.06.08
    보스턴 집값 예측  (1) 2024.06.08
    5_인공지능개론  (1) 2024.06.08
Designed by Tistory.