tf.contrib.learn.DNNRegressor 활용한 모델링하기

2019. 6. 16. 20:55분석 Python/Tensorflow

이번 편은 Regression 방법으로 하기, 개인적으로 Regression이 훨씬 잘 될 줄 알았는데,

다른 모델과 비교했을 때 그렇게 잘되지는 않았음.

 

boston = datasets.load_boston()
X_train, X_test, y_train, y_test = train_test_split(
    boston.data, boston.target, test_size=0.4, random_state=0)
    
X_train.shape, y_train.shape , X_test.shape, y_test.shape

## ((303, 13), (303,), (203, 13), (203,))


import os
import warnings
import tensorflow as tf
warnings.filterwarnings(action='ignore') 
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # or any {'0', '1', '2'}

def main():
    # Specify that all features have real-value data
    feature_columns = [tf.contrib.layers.real_valued_column("", dimension=13)]
    classifier = tf.contrib.learn.DNNRegressor(feature_columns=feature_columns,
                                                hidden_units=[ 512 , 256 ,  128 , 64 ],
                                                optimizer=tf.train.AdamOptimizer(
                                                    learning_rate=0.001,
                                                ))
    def get_train_inputs():
        x = tf.constant(X_train)
        y = tf.constant(y_train)
        return x , y 
    def get_test_inputs():
        x = tf.constant(X_test)
        y = tf.constant(y_test)

        return x, y

    classifier.fit(
                   input_fn=get_train_inputs , 
                   steps=3000  
    ) 
    # 
    loss = classifier.evaluate(input_fn=get_test_inputs, steps=1)["loss"]
    #graph_location = '/tmp/tensorflow/car-evaluation'
    #print('Saving graph to: %s' % graph_location)
    #train_writer = tf.summary.FileWriter(graph_location)
    #train_writer.add_graph(tf.get_default_graph())
    print("Test loss: {0:f}".format(loss))
    pred = classifier.predict(input_fn=get_test_inputs)
    return pred
    
    
if __name__ == "__main__":
    pred  = main()
    
    
from sklearn.metrics import r2_score as r2
from sklearn.metrics import mean_squared_error as mse
pred = np.array(list(pred))
print( "R2 : {} , MSE : {}".format(r2(pred , y_test)   , mse(pred , y_test) ))

R2 : 0.7183232299666991 , MSE : 19.87999695855605

 

 

암튼 tensorflow에도 쉽게 모델링이 가능함.

 

점점 이런 툴이 발전하다보면, 분석가들은 뭘 먹고살아야 할까? ㅠㅠㅠ

 

결국 잘해야한다! 졸 꾸!

728x90