{ "cells": [ { "cell_type": "markdown", "id": "d79795ff", "metadata": {}, "source": [ "## Introduction" ] }, { "cell_type": "markdown", "id": "555745b9", "metadata": {}, "source": [ "이 노트북은 Scikit-Learn 및 MXNet을 사용하여 Content 기반의 추천 시스템을 구축하는 방법을 보여줍니다. Collaborative Filtering 기반의 추천 시스템의 예는 다른 노트북, [mxnet-gluon-recommender](.mexnet-gluon-recommender.ipynb) 를 참고하세요. 또한, 이 노트북은 SageMaker Notebook 에서 로컬 환경의 인스턴스를 활용합니다.\n", "\n", "이 추천 시스템은 각 뉴스 기사의 내용을 바탕으로 상위 N 개의 추천 뉴스 기사를 요청합니다." ] }, { "cell_type": "markdown", "id": "4ad35827", "metadata": {}, "source": [ "## News Article Recommendation System" ] }, { "cell_type": "markdown", "id": "54c518bb", "metadata": {}, "source": [ "데이터셋은 하위 Kaggle 사이트에서 다운로드 받을 수 있습니다.\n", "\n", "https://www.kaggle.com/datasets/snapcrack/all-the-news?resource=download" ] }, { "cell_type": "markdown", "id": "cb75a912", "metadata": {}, "source": [ "데이터를 다운받은 후 data 폴더를 만들고 csv 파일들을 저장합니다." ] }, { "cell_type": "markdown", "id": "acd9ee86", "metadata": {}, "source": [ "## Collaborative Filtering 과 Content-based Filtering\n", "\n", "Collaborative Filtering 은 'cold start' 문제로 어려움을 겪습니다. 즉, 시스템에 사용자 데이터가 충분히 없는 경우 시스템에서 적절한 권장 사항을 작성하는 데 어려움이 있습니다. 일반적인 해결책은 제품데이터를 “전면 적재” 하는 것입니다. \n", "\n", "지금 막 시작했거나 최소한의 데이터를 보유한 비즈니스의 경우 Content-based 추천 시스템은 사용자에게 추천을 제공하는 CF 의 대안입니다.\n", "\n", "Content-based 추천 시스템은 사용자별로 고도로 맞춤화되어 있지만 콜드 스타트 문제는 존재하지 않습니다. 이 때문에 비즈니스는 데이터가 많지 않은 초기 단계부터 추천을 제공 할 수 있습니다." ] }, { "cell_type": "markdown", "id": "7604ce35", "metadata": {}, "source": [ "## Preparation" ] }, { "cell_type": "code", "execution_count": 17, "id": "3bf6c3ff", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import glob\n", "import os\n", "import numpy as np\n", "\n", "from sklearn.feature_extraction.text import TfidfVectorizer\n", "import mxnet as mx" ] }, { "cell_type": "code", "execution_count": 4, "id": "00a61609", "metadata": {}, "outputs": [], "source": [ "file_path = \"data/\"\n", "all_files = glob.glob(file_path + \"*.csv\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "7b672d4c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['data/articles1.csv']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all_files" ] }, { "cell_type": "code", "execution_count": 6, "id": "2c2c8950", "metadata": {}, "outputs": [], "source": [ "extract_features = lambda f : pd.read_csv(f, usecols = [\"id\", \"title\", \"publication\", \"content\"])" ] }, { "cell_type": "code", "execution_count": 7, "id": "ea21422f", "metadata": {}, "outputs": [], "source": [ "articles = pd.concat((extract_features(f) for f in all_files))\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "f0948cd8", "metadata": {}, "outputs": [], "source": [ "articles = articles.head(1000)" ] }, { "cell_type": "code", "execution_count": 9, "id": "1b5710c3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | id | \n", "title | \n", "publication | \n", "content | \n", "
---|---|---|---|---|
0 | \n", "17283 | \n", "House Republicans Fret About Winning Their Hea... | \n", "New York Times | \n", "WASHINGTON — Congressional Republicans have... | \n", "
1 | \n", "17284 | \n", "Rift Between Officers and Residents as Killing... | \n", "New York Times | \n", "After the bullet shells get counted, the blood... | \n", "
2 | \n", "17285 | \n", "Tyrus Wong, ‘Bambi’ Artist Thwarted by Racial ... | \n", "New York Times | \n", "When Walt Disney’s “Bambi” opened in 1942, cri... | \n", "
3 | \n", "17286 | \n", "Among Deaths in 2016, a Heavy Toll in Pop Musi... | \n", "New York Times | \n", "Death may be the great equalizer, but it isn’t... | \n", "
4 | \n", "17287 | \n", "Kim Jong-un Says North Korea Is Preparing to T... | \n", "New York Times | \n", "SEOUL, South Korea — North Korea’s leader, ... | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
995 | \n", "18412 | \n", "Russians Implicated in Doping Still Compete, A... | \n", "New York Times | \n", "Many of the world’s winter athletes, now prepa... | \n", "
996 | \n", "18413 | \n", "Mother Jones Is Named Magazine of the Year - T... | \n", "New York Times | \n", "Mother Jones was named magazine of the year on... | \n", "
997 | \n", "18414 | \n", "Florida Woman Whose ‘Stand Your Ground’ Defens... | \n", "New York Times | \n", "Marissa Alexander no longer wears an ankle mon... | \n", "
998 | \n", "18416 | \n", "First Amendment Support Climbing Among High Sc... | \n", "New York Times | \n", "Support among American high school students fo... | \n", "
999 | \n", "18417 | \n", "Damien Chazelle, ‘La La Land’ Director, on Cal... | \n", "New York Times | \n", "Damien Chazelle, the writer and director of th... | \n", "
1000 rows × 4 columns
\n", "fit_transform
함수는 NumPy 희소 행렬 (Sparse Matrix) 을 반환합니다. 즉, 이 유형의 행렬에서 대부분의 요소는 0입니다. 요소의 대부분이 0이 아니면 dense 행렬로 간주됩니다. "
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "b36bdd1e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.25 s, sys: 144 ms, total: 2.39 s\n",
"Wall time: 2.39 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"\n",
"tf = TfidfVectorizer(analyzer=\"word\", ngram_range=(1, 3),min_df=0.2, stop_words=\"english\")\n",
"\n",
"tfidf_matrix = tf.fit_transform(articles[\"content\"])"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "d79df46f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<1000x189 sparse matrix of type '\n", " | id | \n", "title | \n", "publication | \n", "content | \n", "similarity | \n", "
---|---|---|---|---|---|
3 | \n", "17286 | \n", "Among Deaths in 2016, a Heavy Toll in Pop Musi... | \n", "New York Times | \n", "Death may be the great equalizer, but it isn’t... | \n", "1.000000 | \n", "
65 | \n", "17360 | \n", "My Canada - The New York Times | \n", "New York Times | \n", "Canada, our No. 1 pick for this year’s 52 Plac... | \n", "0.595626 | \n", "
658 | \n", "18023 | \n", "Love and Black Lives, in Pictures Found on a B... | \n", "New York Times | \n", "One night six years ago, on a quiet side stree... | \n", "0.540853 | \n", "
740 | \n", "18134 | \n", "Postcard From My Past: Crossing Into Syria - T... | \n", "New York Times | \n", "Seventeen years ago, the Middle East was a mor... | \n", "0.518828 | \n", "
941 | \n", "18354 | \n", "Review: ‘Lincoln in the Bardo’ Shows a Preside... | \n", "New York Times | \n", "LINCOLN IN THE BARDOBy George Saunders343 page... | \n", "0.513843 | \n", "
239 | \n", "17553 | \n", "Danielle Brooks: The First Time I Saw Myself o... | \n", "New York Times | \n", "It was June 2012, and I had just sped out of t... | \n", "0.511749 | \n", "
270 | \n", "17588 | \n", "Neanderthals Were People, Too - The New York T... | \n", "New York Times | \n", "Joachim Neander was a Calvinist theologian w... | \n", "0.509455 | \n", "
469 | \n", "17812 | \n", "‘I’m Nobody’? Not a Chance, Emily Dickinson - ... | \n", "New York Times | \n", "“In the Trumpian sense of the term, she’s the ... | \n", "0.503491 | \n", "
206 | \n", "17518 | \n", "Is Edward Snowden a Spy? A New Book Calls Him ... | \n", "New York Times | \n", "HOW AMERICA LOST ITS SECRETSEdward Snowden, th... | \n", "0.494410 | \n", "
196 | \n", "17506 | \n", "Here’s How Clemson Won the College Football Pl... | \n", "New York Times | \n", "Clemson upset Alabama and won the college foot... | \n", "0.479991 | \n", "
260 | \n", "17577 | \n", "President Obama’s Farewell Address: Full Video... | \n", "New York Times | \n", "President Obama delivered his farewell address... | \n", "0.461906 | \n", "