-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_actor_based_recommender.py
More file actions
65 lines (50 loc) · 2.52 KB
/
Copy pathstreamlit_actor_based_recommender.py
File metadata and controls
65 lines (50 loc) · 2.52 KB
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
# streamlit run actor_based_recommender.py
'''
핵심 추천 로직 (TF-IDF + Cosine Similarity)
1. 배우 입력 → 출연작 목록 필터링
사용자가 출연작 중 하나 선택
해당 콘텐츠의 설명(Description), 장르, 유형(Type) 등을 기반으로 콘텐츠 벡터화
머신러닝 모델은 텍스트를 직접 이해하지 못하므로, 단어의 중요도를 계산해 수치화합니다.
TfidfVectorizer는 텍스트 데이터를 숫자로 변환해주는 도구입니다.
Cosine Similarity를 활용해 유사한 콘텐츠 추천
Streamlit UI로 결과 출력
'''
import streamlit as st
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# 데이터 로드
@st.cache_data
def load_data():
df = pd.read_csv('data/netflix_titles.csv')
df['cast'] = df['cast'].fillna('No Data')
df['description'] = df['description'].fillna('')
df['listed_in'] = df['listed_in'].fillna('')
return df
df = load_data()
st.title(" 출연작 기반 콘텐츠 추천 시스템")
st.write("선호 배우 → 출연작 선택 → 유사한 넷플릭스 콘텐츠 추천")
# 1. 배우 이름 입력
actor = st.text_input(" 좋아하는 배우를 입력하세요 (예: Leonardo DiCaprio)")
if actor:
# 2. 해당 배우 출연작 추출
actor_movies = df[df['cast'].str.contains(actor, case=False, na=False)]
if actor_movies.empty:
st.warning(" 해당 배우의 출연작이 없습니다.")
else:
st.success(f"{actor} 출연작 {len(actor_movies)}편 발견!")
# 3. 출연작 중 하나 선택
selected_title = st.selectbox(" 추천 기준으로 삼을 작품을 선택하세요", actor_movies['title'].unique())
# 4. TF-IDF 기반 콘텐츠 벡터화
df['content'] = df['type'] + " " + df['listed_in'] + " " + df['description']
tfidf = TfidfVectorizer(stop_words='english')
tfidf_matrix = tfidf.fit_transform(df['content'])
# 5. 선택한 작품 인덱스
target_idx = df[df['title'] == selected_title].index[0]
# 6. 코사인 유사도 계산
cosine_sim = cosine_similarity(tfidf_matrix[target_idx], tfidf_matrix).flatten()
# 7. 유사도 높은 콘텐츠 상위 5개 추천 (자기 자신 제외)
similar_indices = cosine_sim.argsort()[::-1][1:6]
recommendations = df.iloc[similar_indices][['title', 'type', 'release_year', 'listed_in']]
st.subheader(" 추천 콘텐츠 TOP 5")
st.dataframe(recommendations)