Multi-Classification Regression Model (두 개 이상의 독립변수에 대해 종속 변수의 값을 분류하는 모델)을 이용

국가 생활 수준과 불평등.pdf

termpaper_1.ipynb

사용한 data file

termpaper_dataWls.csv

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.preprocessing import MinMaxScaler, StandardScaler
df = pd.read_csv('./data1/termpaper_dataWls.csv', header = 'infer', thousands=',')

x_train = df.iloc[:, 1:5].values
y_train = df.iloc[:, [6]].values      

x_train=np.array(x_train, dtype='float32')
y_train=np.array(y_train, dtype='float32')

one_hot_dummy = tf.one_hot(y_train, depth=3).numpy()
y_train = one_hot_dummy

scaler = MinMaxScaler()
x_train = scaler.fit_transform(x_train)
df.head()

learning_rate  = 0.1
training_steps = 10000
display_step   = 1000

nb_inputs  = x_train.shape[1]
nb_classes = 3
print(nb_inputs, nb_classes)

4 3

tf.random.set_seed(2020)
W = tf.Variable(tf.random.normal([nb_inputs, nb_classes], mean=0.0))
b = tf.Variable(tf.random.normal([nb_classes], mean=0.0))
print('Weight: \n', W.numpy(), '\nBias: \n', b.numpy())

Weight: [[-0.10099822 0.6847899 1.6258513 ] [ 0.88112587 -0.63692456 -0.1427695 ] [ 0.82411087 -0.91326994 -0.4510184 ] [ 0.58053356 1.3066356 -0.60428965]] Bias: [ 0.38414612 -0.6159301 -0.5453214 ]

# logits Function
def hypothesis_logits(X):
    return tf.matmul(X, W) + b

# Softmax Function = hypothesis
def hypothesis_softmax(X):
    z = tf.matmul(X, W) + b
    return tf.nn.softmax(z)

# cost/loss function
def cost(X, Y):
    logits_ = hypothesis_logits(X)
    return tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits_, labels=Y)) 
# Y= one-hot encoded

optimizer = tf.optimizers.SGD(learning_rate)

# training process
loss_record = []
def train(X, Y):
    with tf.GradientTape(persistent=True) as g:
        pred = hypothesis_softmax(X)
        loss= cost(X,Y)
        loss_record.append(loss.numpy())
        
    gradients = g.gradient(loss, [W, b])
    optimizer.apply_gradients(zip(gradients, [W, b]))

for step in range(training_steps):
    train(x_train, y_train)
    
    if step % display_step == 0:
        pred = hypothesis_softmax(x_train)
        loss= cost(x_train, y_train)
        print("step = %s, \t cost = %s" % (step, loss.numpy()))
        
print('*'*50)
print(f'y = {W.numpy()}x + {b.numpy()}')

step = 0, cost = 0.7200843 step = 1000, cost = 0.56405705 step = 2000, cost = 0.53636575 step = 3000, cost = 0.5231778 step = 4000, cost = 0.51505435 step = 5000, cost = 0.50936806 step = 6000, cost = 0.5050739 step = 7000, cost = 0.5016686 step = 8000, cost = 0.4988757 step = 9000, cost = 0.49652833


y = [[ 0.46224073 -1.7377465 3.4851444 ] [ 1.5899377 1.9632527 -3.4517481 ] [ 1.3539491 0.9027825 -2.7969115 ] [ 4.6522527 2.0927877 -5.462206 ]]x + [-2.7056875 -1.2459426 3.1745615]

plt.figure(figsize = (5,4))
plt.plot(loss_record)
plt.xlabel('iteration', fontsize = 15)
plt.ylabel('loss', fontsize = 15)
plt.show()

# 성과평가
from sklearn.metrics import confusion_matrix
import seaborn as sns

plt.figure(figsize=(6,6))
predict_value = hypothesis_softmax(x_train)
print(predict_value)
cm = confusion_matrix(np.argmax(y_train, axis=-1), np.argmax(predict_value, axis=-1))
print(cm)

sns.heatmap(cm, annot=True, fmt='d')
plt.show()

from sklearn.metrics import classification_report
y_actual    = np.argmax(y_train, axis=-1)
y_predicted = np.argmax(predict_value, axis=-1)
print('Report = \n', classification_report(y_actual, y_predicted))

# Accuracy
def accuracy(X, Y):  # Y = one_hot encoding
    predicted = np.argmax(hypothesis_softmax(X), axis=-1)
    label = np.argmax(Y, axis= -1).reshape(1,-1)
    return tf.reduce_mean(tf.cast(tf.equal(predicted, label), dtype=tf.float32))

acc = accuracy(x_train, y_train).numpy()
print("Accuracy: %s" % (acc*100))