main.py 2.66 KB
Newer Older
1
2
3
4
5
6
7
"""
    # main.py

    - Load된 데이터들에 대해 Linear Regression을 진행합니다.
    - 진행된 후의 Weights를 파일로 저장합니다.
"""

8
import datetime
9
from os import getcwd, path, makedirs
10
11
12
import sys
import pymysql
from preprocessing import preprocessingData
13
from linear_regression import LinearRegression
14
15


16
17
18
19
20
def storeParameters(link, filename, data):
    today = datetime.datetime.today()
    year = str(today.year)
    month = str(today.month) if today.month >= 10 else '0'+str(today.month)
    day = str(today.day) if today.day >= 10 else '0'+str(today.day)
21

22
    time_dir = '/' + year + '/' + year+month + '/' + year + month + day
23

24
    file_dir = getcwd() + link + time_dir
25

26
27
28
29
30
31
32
    try:
        if not path.exists(file_dir):
            makedirs(file_dir)
    except:
        print("Error : Creating Directory - ", file_dir)

    file = open(file_dir + filename, "w")
33
34
35
36

    file.write(data)

    file.close()
37
38
39
40
41


dbconfig = {"host": sys.argv[1], "user": sys.argv[2],
            "password": sys.argv[3], "database": sys.argv[4]}

42
43
44
45
46
47
48
49
eue_db = pymysql.connect(user=dbconfig["user"], password=dbconfig["password"],
                         host=dbconfig["host"], db=dbconfig["database"], charset='utf8')
cursor = eue_db.cursor(pymysql.cursors.DictCursor)

query = "SELECT ID,DATALINK FROM USER;"
cursor.execute(query)
result = cursor.fetchall()

50
# Main Process
51
for userdata in result:
52

53
54
    print("User ID : ", userdata["ID"])
    print("Data Processing Start...")
55
56
57
58
59

    # Get Data links
    # ./data/DO/SGG/EMD/Users/ID
    user_datalink = userdata["DATALINK"]
    dir_ls = user_datalink.split("/")
60

61
62
63
64
    # ./data/DO/SGG/EMD/Outside
    outside_datalink = ("/").join(dir_ls[:-2]) + "/Outside"

    # data load
65
66
    train_x, train_t, weights, bias, mean, std_d = preprocessingData(
        user_datalink, outside_datalink)
67
68

    # linear regression
69
70
71
72
    model = LinearRegression(train_x, train_t, weights,
                             bias, learning_rate=0.05)
    model.gradientDescent()

73
74
75
    # Save the Parameters.
    # - analysis_parameters
    analysis_data = ""
76

77
78
    for i in range(len(model.weights[0])):
        analysis_data += str(model.weights[0][i]) + ','
79

80
    analysis_data += str(model.bias)
81

82
    storeParameters(user_datalink, "/analysis_parameters.csv", analysis_data)
83

84
85
    # - prediction_parameters
    prediction_data = ""
86

87
88
89
90
    for i in range(len(mean)):
        prediction_data += str(mean[i][0]) + ','
    prediction_data = prediction_data[:-1]
    prediction_data += '\n'
91

92
93
94
    for i in range(len(std_d)):
        prediction_data += str(std_d[i][0]) + ','
    prediction_data = prediction_data[:-1]
95

96
97
    storeParameters(
        user_datalink, "/prediction_parameters.csv", prediction_data)