## Modeling 하시오
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
lm = LinearRegression()
train_set.head()
train_x = train_set[['Temperature', 'Leaflets']]
train_y = train_set[['Lemon']]

test_x = test_set[['Temperature', 'Leaflets']]
test_y = test_set[['Lemon']]

lm.fit(train_x, train_y)

y_pred = lm.predict(test_x)

mse = mean_squared_error(y_pred, test_y)

print(lm.coef_)
print(lm.intercept_)
print(mse)

'Develop > AI' 카테고리의 다른 글

MNIST  (0) 2019.07.14
placeholder / feed_dick  (0) 2019.07.14

import tensorflow as tf

#############
# 데이터 준비
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)

y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)

print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

###############
# 모델 선택

# 데이터 받을 곳 정의
X = tf.placeholder(tf.float32, shape=[None, 784])
Y = tf.placeholder(tf.float32, shape=[None, 10])

# 머신이 학습할 값 정의
W = tf.Variable(tf.random_normal(shape=[784,10]))
B = tf.Variable(tf.random_normal(shape=[10]))

# logit 계산
logit = tf.matmul(X,W)+B

# 값 예측
pred = tf.nn.softmax(logit)

# loss 계산
loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits = logit, labels=Y)
loss = tf.reduce_mean(loss)

# optimizer 설정
optimizer = tf.train.AdamOptimizer(0.01).minimize(loss)

# 정확도 계산
acc = tf.equal(tf.argmax(pred, axis=1), tf.argmax(Y, axis=1))
acc = tf.reduce_mean(tf.cast(acc, tf.float32))

###################
# 모델학습

# 세션 정의 및 초기화
sess = tf.Session()
sess.run(tf.global_variables_initializer())

#배치 학습
epochs = 20
batch = 512

for e in range(epochs):
  n = len (x_train) // batch
  for b in range(n):
    x = x_train[b * batch : (b+1) * batch]
    y = y_train[b * batch : (b+1) * batch]
    sess.run(optimizer, feed_dict={X:x, Y:y})
    
 #   print (sess.run(acc, feed_dict={X:x_train, Y:y_train}))

    
############
# 모델평가
print("모델평가")
print(sess.run(acc, feed_dict={X:x_test, Y:y_test}))

'Develop > AI' 카테고리의 다른 글

Scikit-learn LinearRegression  (0) 2019.07.15
placeholder / feed_dick  (0) 2019.07.14

# 데이터 준비
x = 3.0
y = 10.0

tf.reset_default_graph()
# 모델만들기
X = tf.placeholder(tf.float32, shape=())
Y = tf.placeholder(tf.float32, shape=())

W = tf.Variable(tf.random_normal(shape=()))
B = tf.Variable(tf.random_normal(shape=()))
H = tf.add(tf.multiply(x, W), B)

loss = tf.square(tf.subtract(y, H))
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss)

# 학습
sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(20):
  sess.run(optimizer, feed_dict = {X:x, Y:y})
  print(sess.run([loss, W, B], feed_dict={X:x, Y:y}))

'Develop > AI' 카테고리의 다른 글

Scikit-learn LinearRegression  (0) 2019.07.15
MNIST  (0) 2019.07.14

+ Recent posts