Skip to content

Commit e3584bc

Browse files
committed
서버 연동 후 코드 수정
1 parent 8ad9014 commit e3584bc

7 files changed

Lines changed: 211 additions & 234 deletions

File tree

AI/ALS.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import pandas as pd
22
import numpy as np
33

4-
def get_ratings(path, users_file_name):
5-
df_users = pd.read_csv(path + users_file_name, encoding='cp949')
4+
def get_ratings(path, users_file_name, books_file_name):
5+
df_users = pd.read_csv(path + users_file_name, encoding='UTF8')
6+
df_books = pd.read_csv(path + books_file_name, encoding='UTF8')
67
df_users_books = pd.DataFrame(df_users, columns=['user_id', 'like'])
78
sr_users = []
89
sr_books = []
@@ -14,19 +15,28 @@ def get_ratings(path, users_file_name):
1415
i = i.split(', ')
1516
list_like.append(i)
1617

17-
for user_id in df_users_books['user_id']:
18-
for book_id in list_like[user_id]:
19-
sr_users.append(user_id)
20-
sr_books.append(book_id)
18+
for user_idx in df_users_books['user_id']:
19+
for book_idx in list_like[user_idx]:
20+
if book_idx == '':
21+
break
22+
sr_users.append(user_idx)
23+
sr_books.append(book_idx)
2124
sr_ratings.append(1)
2225

26+
for book_idx in range(len(df_books)):
27+
if sr_users[0]:
28+
sr_users.append(sr_users[0])
29+
if sr_books[0]:
30+
sr_books.append(book_idx)
31+
if sr_ratings[0]:
32+
sr_ratings.append(0)
2333
R = pd.DataFrame({
24-
'user_id': sr_users,
25-
'book_id': sr_books,
34+
'user_idx': sr_users,
35+
'book_idx': sr_books,
2636
'ratings': sr_ratings
2737
})
2838

29-
R = R.pivot_table('ratings', index='user_id', columns='book_id').fillna(0)
39+
R = R.pivot_table('ratings', index='user_idx', columns='book_idx').fillna(0)
3040
R.rename(columns= lambda x: int(x), inplace=True)
3141
R = R.sort_index(axis=1)
3242
return R
@@ -76,7 +86,7 @@ def predict(file_path, users_file_name, pred_score_file_name):
7686
regularization_list = []
7787
total_losses = []
7888

79-
for i in range(15):
89+
for i in range(6):
8090
if i != 0:
8191
optimize_user(X, Y, C, P, nu, nf, r_lambda)
8292
optimize_item(X, Y, C, P, ni, nf, r_lambda)
@@ -100,11 +110,3 @@ def predict(file_path, users_file_name, pred_score_file_name):
100110

101111
df_predict = pd.DataFrame(predict, columns=range(len(R[0]))).fillna(0) # user-item = 1400 x 1125
102112
df_predict.to_csv(file_path + pred_score_file_name)
103-
104-
'''
105-
file_path = '/var/www/python_flask/main/models/'
106-
users_file_name = "API_test_users.csv"
107-
books_file_name = "API_test_books.csv"
108-
pred_file_name = "rec_pred_score_1.csv"
109-
110-
predict(file_path, users_file_name, pred_file_name)'''

AI/data_update.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
import sys
3+
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
4+
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))))
5+
from main.models import database
6+
import pandas as pd
7+
import numpy as np
8+
9+
def update():
10+
'''
11+
books = database.Book.objects()
12+
isbn = []
13+
for b in books:
14+
isbn.append(b.isbn)
15+
'''
16+
file_path = '/var/www/python_flask/main/recommendation/'
17+
books_file_name = 'API_test_books.csv'
18+
df_books = pd.read_csv(file_path + books_file_name)
19+
isbn = list(np.array(df_books['isbn']).tolist())
20+
21+
dummy_file_name = 'dummy_users.csv'
22+
df_dummy_user = pd.read_csv(file_path + dummy_file_name)
23+
24+
users = database.User.objects()
25+
name = []
26+
email = []
27+
password = []
28+
user_id = []
29+
like = []
30+
rank = []
31+
unit = []
32+
for u in users:
33+
t_list = []
34+
for l in u.like:
35+
t = l.rstrip('/')
36+
try:
37+
t = isbn.index(t)
38+
except:
39+
continue
40+
t_list.append(t)
41+
like.append(t_list)
42+
name.append(u.name)
43+
email.append(u.email)
44+
password.append(u.password)
45+
user_id.append(u.user_id)
46+
rank.append(u.rank)
47+
unit.append(u.unit)

AI/dummy_generator.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import pandas as pd
2+
import random
3+
4+
file_path = '/var/www/python_flask/main/recommendation/' # 서버 폴더경로 맞춰서 다시 설정
5+
save_path = 'recommend_list/'
6+
users_file_name = "dummy_users.csv"
7+
books_file_name = "API_test_books.csv"
8+
categories_file_name = "rec_category_2.csv"
9+
10+
df_books = pd.read_csv(file_path + books_file_name, encoding='cp949')
11+
df_category = pd.read_csv(file_path + categories_file_name, encoding='cp949')
12+
df_category = df_category[['category', 'count']].dropna()
13+
14+
def make_like_list(category):
15+
cat_idx = category
16+
cat_count = df_category.iloc[cat_idx]['count']
17+
book_list = list(df_books.index[df_books['categoryName'] == df_category.iloc[cat_idx]['category']])
18+
pick_count = random.randint(1, min(10, cat_count))
19+
like_list = []
20+
for i in range(pick_count):
21+
pick_idx = random.randint(0, cat_count-1)
22+
cnt = 0
23+
while book_list[pick_idx] in like_list and cnt < 10:
24+
pick_idx = random.randint(0, cat_count - 1)
25+
if cnt < 10:
26+
like_list.append(book_list[pick_idx])
27+
return like_list
28+
29+
def make_like_category():
30+
cat_len = len(df_category)
31+
cat_num = random.randint(1, min(10, cat_len))
32+
cat_list = []
33+
like_list = []
34+
for i in range(cat_num):
35+
pick_cat = random.randint(0, cat_len-1)
36+
cnt = 0
37+
while pick_cat in cat_list and cnt < 10:
38+
pick_cat = random.randint(0, cat_len - 1)
39+
if cnt < 10:
40+
cat_list.append(pick_cat)
41+
42+
for category in cat_list:
43+
like_list += make_like_list(category)
44+
45+
return like_list
46+
47+
like_list = []
48+
for i in range(50):
49+
like_list.append(make_like_category())
50+
51+
def make_dataframe(like_list):
52+
n = len(like_list)
53+
list_unit = []
54+
list_rank = []
55+
list_user_id = []
56+
57+
int_rank = ['이등병', '일병', '상병', '병장']
58+
for i in range(n):
59+
list_unit.append("53사단" if random.randint(0, 1) else "31사단")
60+
list_rank.append(int_rank[random.randint(0, 3)])
61+
62+
for i in range(n):
63+
list_user_id.append('userid_'+str(i))
64+
65+
to_df = {
66+
'name': range(n),
67+
'email': range(n),
68+
'password': range(n),
69+
'user_id': list_user_id,
70+
'like':like_list,
71+
'rank': list_rank,
72+
'unit': list_unit
73+
}
74+
75+
df = pd.DataFrame(to_df)
76+
df = df.fillna(0)
77+
return df
78+
79+
df = make_dataframe(like_list)
80+
df.to_csv(file_path + users_file_name)

AI/random_user_generator.py

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)