본문 바로가기

잡다한 IT/머신러닝 & 딥러닝

04-3. Mulit-variable linear regression with file input

■ 파일로 부터 데이터 입력 받기.


xy = np.loadtxt('data-01-test-score.csv',delimiter=',',dtpye=np.float32)

- data-01-test-score.csv로 부터 데이터 입력 받음

- 데이터간 구분은 ' ' 스페이스로 한다

- 데이터 타입은 np.float32 로 설정함


x_data = xy[:,0:-1]

- 처음부터 마지막 이전까지의 데이터를 배열 x_data에 저장


y_data = xy[:,[-1]]

- 마지막 데이터를 배열 y_data에 저장


print(x_data.shape,x_data,len(x_data)

- 원하는대로 x 데이터가 들어왔는지 확인


print(y_data.shape,y_data)

- 원하는대로 y 데이터가 들어왔는지 확인





※ colab 에서 데이터 가져오는 방법 (코드를 실행하기 전에 아래 코드를 실행)

1. 

1
2
3
from google.colab import files
uploaded = files.upload()
!ls
cs


2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
이렇게 추가하면 구글 드라이브에 있는 파일을 불러옵니다.
 
!pip install --q PyDrive
 
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
from oauth2client.client import GoogleCredentials
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
 
myfile = drive.CreateFile({'id''FileID'})
myfile.GetContentFile('FileName.Ex')
 
참고 사이트
https://stackoverflow.com/a/50632495
http://nali.org/load-google-drive-csv-panda-dataframe-google-colab/
cs







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Lab04-3 Multi-variable linear regression
 
import tensorflow as tf
 
import numpy as np
 
tf.set_random_seed(777)  # for reproducibility
 
 
xy = np.loadtxt('data-01-test-score.csv', delimiter=',', dtype=np.float32)
 
x_data = xy[:, 0:-1]
 
y_data = xy[:, [-1]]
 
 
# Make sure the shape and data are OK
 
print(x_data.shape, x_data, len(x_data))
 
print(y_data.shape, y_data)
 
 
# placeholders for a tensor that will be always fed.
 
= tf.placeholder(tf.float32, shape=[None, 3])
 
= tf.placeholder(tf.float32, shape=[None, 1])
 
 
= tf.Variable(tf.random_normal([31]), name='weight')
 
= tf.Variable(tf.random_normal([1]), name='bias')
 
 
# Hypothesis
 
hypothesis = tf.matmul(X, W) + b
 
 
# Simplified cost/loss function
 
cost = tf.reduce_mean(tf.square(hypothesis - Y))
 
 
# Minimize
 
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
 
train = optimizer.minimize(cost)
 
 
# Launch the graph in a session.
 
sess = tf.Session()
 
# Initializes global variables in the graph.
 
sess.run(tf.global_variables_initializer())
 
 
for step in range(2001):
 
    cost_val, hy_val, _ = sess.run(
 
        [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data})
 
    if step % 10 == 0:
 
        print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)
cs


반응형