본문 바로가기
AI/Deep Learning

딥러닝: 03. 다중 선형 회귀(Multi Variable linear regression)

by KIha_Jung 2019. 7. 30.
muli_varible_linear_regression
In [6]:
import numpy as np
import tensorflow as tf
In [14]:
xy = np.loadtxt('./dataset/data-01-test-score.csv',delimiter=','
                , dtype = np.float32 )
x = xy[:,0:-1]
y = xy[:,[-1]]
In [16]:
print(x.shape, x, len(x))
print(y.shape, y, len(y))
(25, 3) [[ 73.  80.  75.]
 [ 93.  88.  93.]
 [ 89.  91.  90.]
 [ 96.  98. 100.]
 [ 73.  66.  70.]
 [ 53.  46.  55.]
 [ 69.  74.  77.]
 [ 47.  56.  60.]
 [ 87.  79.  90.]
 [ 79.  70.  88.]
 [ 69.  70.  73.]
 [ 70.  65.  74.]
 [ 93.  95.  91.]
 [ 79.  80.  73.]
 [ 70.  73.  78.]
 [ 93.  89.  96.]
 [ 78.  75.  68.]
 [ 81.  90.  93.]
 [ 88.  92.  86.]
 [ 78.  83.  77.]
 [ 82.  86.  90.]
 [ 86.  82.  89.]
 [ 78.  83.  85.]
 [ 76.  83.  71.]
 [ 96.  93.  95.]] 25
(25, 1) [[152.]
 [185.]
 [180.]
 [196.]
 [142.]
 [101.]
 [149.]
 [115.]
 [175.]
 [164.]
 [141.]
 [141.]
 [184.]
 [152.]
 [148.]
 [192.]
 [147.]
 [183.]
 [177.]
 [159.]
 [177.]
 [175.]
 [175.]
 [149.]
 [192.]] 25
In [18]:
# shape=[None, 3] n행의 3열을 받는다
X = tf.placeholder(tf.float32, shape=[None, 3])
Y = tf.placeholder(tf.float32, shape=[None, 1])

W = tf.Variable(tf.random_normal([3, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = tf.matmul(X, W) + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))

optimizer = tf.train.GradientDescentOptimizer(learning_rate = 1e-5)
train = optimizer.minimize(cost)
WARNING:tensorflow:From C:\Anaconda3\envs\venv\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
In [19]:
sess = tf.Session()
sess.run(tf.global_variables_initializer())
In [21]:
for step in range(2001):
    cost_val, hy_val, _ = sess.run(
    [cost, hypothesis, train], feed_dict={X:x, Y:y})
    if step % 100 == 0:
        print(step, "cost:", cost_val, "\nprediction:\n", hy_val)
0 cost: 6.38678 
prediction:
 [[152.75853 ]
 [184.82779 ]
 [181.30756 ]
 [198.56973 ]
 [140.5325  ]
 [105.832954]
 [150.22824 ]
 [113.47417 ]
 [174.56834 ]
 [164.50934 ]
 [143.96481 ]
 [143.03796 ]
 [186.09277 ]
 [153.23457 ]
 [151.25551 ]
 [188.54831 ]
 [144.96683 ]
 [180.54341 ]
 [177.04828 ]
 [158.57103 ]
 [175.79578 ]
 [174.51813 ]
 [167.18971 ]
 [151.23013 ]
 [190.74042 ]]
100 cost: 6.3805504 
prediction:
 [[152.76428]
 [184.81989]
 [181.30716]
 [198.57243]
 [140.52019]
 [105.82689]
 [150.23901]
 [113.49484]
 [174.56131]
 [164.50836]
 [143.96756]
 [143.03531]
 [186.08893]
 [153.22586]
 [151.26445]
 [188.54465]
 [144.95013]
 [180.56178]
 [177.04639]
 [158.57149]
 [175.80518]
 [174.51471]
 [167.19905]
 [151.228  ]
 [190.73311]]
200 cost: 6.374616 
prediction:
 [[152.76991 ]
 [184.81218 ]
 [181.30681 ]
 [198.57506 ]
 [140.50816 ]
 [105.820946]
 [150.24951 ]
 [113.51499 ]
 [174.55443 ]
 [164.50734 ]
 [143.97023 ]
 [143.0327  ]
 [186.08519 ]
 [153.21739 ]
 [151.27316 ]
 [188.54105 ]
 [144.93387 ]
 [180.5797  ]
 [177.04456 ]
 [158.57196 ]
 [175.81438 ]
 [174.51135 ]
 [167.20818 ]
 [151.22595 ]
 [190.72597 ]]
300 cost: 6.3689895 
prediction:
 [[152.77544 ]
 [184.80467 ]
 [181.30646 ]
 [198.57765 ]
 [140.49644 ]
 [105.815125]
 [150.25975 ]
 [113.53461 ]
 [174.54768 ]
 [164.5063  ]
 [143.97284 ]
 [143.03014 ]
 [186.08156 ]
 [153.20918 ]
 [151.28166 ]
 [188.53755 ]
 [144.91806 ]
 [180.59717 ]
 [177.04282 ]
 [158.57246 ]
 [175.82332 ]
 [174.50807 ]
 [167.21706 ]
 [151.22403 ]
 [190.71902 ]]
400 cost: 6.3636303 
prediction:
 [[152.78082]
 [184.79732]
 [181.30612]
 [198.58017]
 [140.48499]
 [105.80942]
 [150.26971]
 [113.55375]
 [174.54108]
 [164.50525]
 [143.97537]
 [143.0276 ]
 [186.07805]
 [153.20117]
 [151.28992]
 [188.53409]
 [144.90265]
 [180.6142 ]
 [177.04114]
 [158.57294]
 [175.83203]
 [174.50485]
 [167.22572]
 [151.22217]
 [190.71223]]
500 cost: 6.358531 
prediction:
 [[152.78612 ]
 [184.79016 ]
 [181.30582 ]
 [198.58261 ]
 [140.47382 ]
 [105.803825]
 [150.27945 ]
 [113.5724  ]
 [174.5346  ]
 [164.50415 ]
 [143.97784 ]
 [143.02512 ]
 [186.07465 ]
 [153.19342 ]
 [151.29797 ]
 [188.53073 ]
 [144.88766 ]
 [180.6308  ]
 [177.03954 ]
 [158.57346 ]
 [175.84055 ]
 [174.5017  ]
 [167.23418 ]
 [151.22043 ]
 [190.70563 ]]
600 cost: 6.3536835 
prediction:
 [[152.79128 ]
 [184.78314 ]
 [181.30553 ]
 [198.585   ]
 [140.4629  ]
 [105.79835 ]
 [150.28893 ]
 [113.590576]
 [174.52826 ]
 [164.50304 ]
 [143.98024 ]
 [143.02267 ]
 [186.07133 ]
 [153.18587 ]
 [151.3058  ]
 [188.52742 ]
 [144.87306 ]
 [180.64699 ]
 [177.03798 ]
 [158.57397 ]
 [175.84882 ]
 [174.4986  ]
 [167.24242 ]
 [151.21875 ]
 [190.69917 ]]
700 cost: 6.349066 
prediction:
 [[152.79637 ]
 [184.77632 ]
 [181.30528 ]
 [198.58737 ]
 [140.45229 ]
 [105.792984]
 [150.29817 ]
 [113.60831 ]
 [174.52208 ]
 [164.50194 ]
 [143.98259 ]
 [143.02028 ]
 [186.06815 ]
 [153.17854 ]
 [151.31346 ]
 [188.52422 ]
 [144.85887 ]
 [180.6628  ]
 [177.03651 ]
 [158.57451 ]
 [175.85692 ]
 [174.49559 ]
 [167.25047 ]
 [151.21718 ]
 [190.6929  ]]
800 cost: 6.344679 
prediction:
 [[152.80132]
 [184.76965]
 [181.30501]
 [198.58965]
 [140.4419 ]
 [105.78773]
 [150.30717]
 [113.62558]
 [174.51599]
 [164.50078]
 [143.98486]
 [143.0179 ]
 [186.06503]
 [153.17139]
 [151.32089]
 [188.52103]
 [144.84503]
 [180.67819]
 [177.03506]
 [158.57504]
 [175.86478]
 [174.49261]
 [167.2583 ]
 [151.21565]
 [190.68677]]
900 cost: 6.3405013 
prediction:
 [[152.80617]
 [184.76312]
 [181.30476]
 [198.59186]
 [140.43175]
 [105.78258]
 [150.31595]
 [113.64241]
 [174.51004]
 [164.49963]
 [143.98708]
 [143.01556]
 [186.06201]
 [153.16446]
 [151.32812]
 [188.51793]
 [144.83154]
 [180.6932 ]
 [177.03369]
 [158.57559]
 [175.87244]
 [174.4897 ]
 [167.26595]
 [151.2142 ]
 [190.68077]]
1000 cost: 6.33653 
prediction:
 [[152.81091]
 [184.75676]
 [181.30452]
 [198.59404]
 [140.42186]
 [105.77755]
 [150.32451]
 [113.65883]
 [174.50423]
 [164.49846]
 [143.98923]
 [143.01326]
 [186.05908]
 [153.15771]
 [151.33519]
 [188.5149 ]
 [144.81844]
 [180.70784]
 [177.03236]
 [158.57614]
 [175.87993]
 [174.48686]
 [167.27339]
 [151.21281]
 [190.67494]]
1100 cost: 6.3327436 
prediction:
 [[152.81557 ]
 [184.75056 ]
 [181.3043  ]
 [198.59618 ]
 [140.41222 ]
 [105.77261 ]
 [150.33286 ]
 [113.674835]
 [174.49852 ]
 [164.4973  ]
 [143.99135 ]
 [143.01102 ]
 [186.05623 ]
 [153.15117 ]
 [151.34207 ]
 [188.51193 ]
 [144.80565 ]
 [180.72212 ]
 [177.03111 ]
 [158.5767  ]
 [175.8872  ]
 [174.48409 ]
 [167.28069 ]
 [151.2115  ]
 [190.66927 ]]
1200 cost: 6.329145 
prediction:
 [[152.8201  ]
 [184.7445  ]
 [181.30411 ]
 [198.59825 ]
 [140.40279 ]
 [105.767784]
 [150.341   ]
 [113.69044 ]
 [174.49295 ]
 [164.49612 ]
 [143.99338 ]
 [143.0088  ]
 [186.0535  ]
 [153.14479 ]
 [151.34877 ]
 [188.50903 ]
 [144.79321 ]
 [180.73605 ]
 [177.0299  ]
 [158.57726 ]
 [175.89432 ]
 [174.48135 ]
 [167.28778 ]
 [151.21027 ]
 [190.66373 ]]
1300 cost: 6.325725 
prediction:
 [[152.82455 ]
 [184.73859 ]
 [181.30392 ]
 [198.60027 ]
 [140.39362 ]
 [105.763054]
 [150.34894 ]
 [113.705635]
 [174.48749 ]
 [164.49493 ]
 [143.99539 ]
 [143.00664 ]
 [186.05081 ]
 [153.1386  ]
 [151.35532 ]
 [188.5062  ]
 [144.78111 ]
 [180.74965 ]
 [177.02873 ]
 [158.57782 ]
 [175.90125 ]
 [174.4787  ]
 [167.2947  ]
 [151.20908 ]
 [190.65833 ]]
1400 cost: 6.322458 
prediction:
 [[152.8289  ]
 [184.7328  ]
 [181.30374 ]
 [198.60222 ]
 [140.38463 ]
 [105.758415]
 [150.35664 ]
 [113.72046 ]
 [174.48215 ]
 [164.49373 ]
 [143.99731 ]
 [143.00449 ]
 [186.04822 ]
 [153.13255 ]
 [151.36165 ]
 [188.5034  ]
 [144.7693  ]
 [180.76286 ]
 [177.0276  ]
 [158.57838 ]
 [175.90799 ]
 [174.47606 ]
 [167.30144 ]
 [151.20795 ]
 [190.65305 ]]
1500 cost: 6.319367 
prediction:
 [[152.83315]
 [184.72716]
 [181.30357]
 [198.60414]
 [140.37587]
 [105.75388]
 [150.36418]
 [113.7349 ]
 [174.47691]
 [164.49254]
 [143.99919]
 [143.0024 ]
 [186.0457 ]
 [153.1267 ]
 [151.36784]
 [188.50069]
 [144.75781]
 [180.77579]
 [177.02652]
 [158.57893]
 [175.91455]
 [174.4735 ]
 [167.308  ]
 [151.20686]
 [190.6479 ]]
1600 cost: 6.316433 
prediction:
 [[152.83731 ]
 [184.72165 ]
 [181.3034  ]
 [198.60605 ]
 [140.36732 ]
 [105.74946 ]
 [150.37155 ]
 [113.748985]
 [174.47182 ]
 [164.49136 ]
 [144.00104 ]
 [143.00034 ]
 [186.04326 ]
 [153.12099 ]
 [151.37389 ]
 [188.49802 ]
 [144.74661 ]
 [180.78838 ]
 [177.02548 ]
 [158.5795  ]
 [175.92096 ]
 [174.471   ]
 [167.31442 ]
 [151.20584 ]
 [190.6429  ]]
1700 cost: 6.3136125 
prediction:
 [[152.8414 ]
 [184.7163 ]
 [181.30328]
 [198.60788]
 [140.35901]
 [105.74511]
 [150.37872]
 [113.76272]
 [174.46683]
 [164.49017]
 [144.00284]
 [142.99832]
 [186.0409 ]
 [153.11545]
 [151.37976]
 [188.49542]
 [144.73572]
 [180.80066]
 [177.02449]
 [158.58008]
 [175.92722]
 [174.46855]
 [167.3207 ]
 [151.20486]
 [190.63803]]
1800 cost: 6.3109455 
prediction:
 [[152.84537]
 [184.71104]
 [181.30312]
 [198.60966]
 [140.35086]
 [105.74087]
 [150.3857 ]
 [113.7761 ]
 [174.46193]
 [164.489  ]
 [144.00456]
 [142.99632]
 [186.03859]
 [153.11006]
 [151.38548]
 [188.49287]
 [144.72511]
 [180.81262]
 [177.02351]
 [158.58061]
 [175.9333 ]
 [174.46614]
 [167.32678]
 [151.20393]
 [190.63326]]
1900 cost: 6.3083925 
prediction:
 [[152.84926]
 [184.70595]
 [181.30301]
 [198.61143]
 [140.34294]
 [105.73671]
 [150.3925 ]
 [113.78914]
 [174.45714]
 [164.48781]
 [144.00627]
 [142.99438]
 [186.03636]
 [153.10481]
 [151.39108]
 [188.4904 ]
 [144.71478]
 [180.82433]
 [177.0226 ]
 [158.58119]
 [175.93925]
 [174.46382]
 [167.33275]
 [151.20305]
 [190.62863]]
2000 cost: 6.305976 
prediction:
 [[152.85306 ]
 [184.70094 ]
 [181.30287 ]
 [198.61311 ]
 [140.33517 ]
 [105.73263 ]
 [150.39914 ]
 [113.801865]
 [174.45245 ]
 [164.48663 ]
 [144.0079  ]
 [142.99245 ]
 [186.03418 ]
 [153.0997  ]
 [151.3965  ]
 [188.48795 ]
 [144.70468 ]
 [180.8357  ]
 [177.0217  ]
 [158.58173 ]
 [175.94504 ]
 [174.46149 ]
 [167.33853 ]
 [151.2022  ]
 [190.62408 ]]
In [ ]:
 

댓글