CS/Machine Learning

colab에 google drive 마운트로 csv데이터불러오기

dawonny 2021. 7. 18. 02:33
728x90
반응형

아아아!!! 저번에 데이터 불러오는게 안되어가지고 머리 아팠었는데

오늘 스터디 실습시간에 제대로 배웠다.

기록해둬야지!

일단 내 구글 계정 드라이브에다가 데이터를 올려놓는다.

그리고

왼쪽에 파일 버튼을 누르면 나오는 세개 아이콘 중에 3번째꺼를 누르면

계정을 연결하겠냐고 물어본다.

OK 눌러주면 저렇게 불러온다.

일단 맨 위에

from google.colab import drive

drive.mount('/content/drive')

이 코드를 작성해주고

path라고 경로 넣어줄 변수를 하나 만들어 준다음

오른쪽 마우스를 클릭해서 경로복사를 해준다

그리고 path에 넣어주고

데이터인 xy 에 path를 넣어준다.

그러면 끝!

잘 동작된다.

# lab 05 Logistic Classification 구현하기 (diabetes)
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as np

path = '/content/drive/MyDrive/data-03-diabetes.csv'

xy = np.loadtxt(path, delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]

print(x_data.shape, y_data.shape)


X = tf.placeholder(tf.float32, shape=[None, 8])
Y = tf.placeholder(tf.float32, shape=[None, 1])

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

hypothesis = tf.sigmoid(tf.matmul(X, W) + b)

cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
                       tf.log(1 - hypothesis))

train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for step in range(10001):
        cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})
        if step % 200 == 0:
            print(step, cost_val)

    # Accuracy report
    h, c, a = sess.run([hypothesis, predicted, accuracy],
                       feed_dict={X: x_data, Y: y_data})
    print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)

728x90
반응형