From aa73ebe77a76c07c0a6a8b459a95359ffbaed97b Mon Sep 17 00:00:00 2001 From: song122333 Date: Sat, 28 Dec 2024 00:25:36 +0900 Subject: [PATCH 1/3] =?UTF-8?q?14=EC=A3=BC=EC=B0=A8=20=EB=B3=B5=EC=8A=B5?= =?UTF-8?q?=EA=B3=BC=EC=A0=9C=20=EC=A0=9C=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...355\225\234\354\206\241\355\235\254.ipynb" | 491 ++++++++++++++++++ 1 file changed, 491 insertions(+) create mode 100644 "Week14_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" diff --git "a/Week14_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" "b/Week14_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" new file mode 100644 index 0000000..6140aa0 --- /dev/null +++ "b/Week14_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" @@ -0,0 +1,491 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "fetch_20newsgropus()로 데이터를 내려받고 메모리로 데이터 로딩" + ], + "metadata": { + "id": "oB7bnbikyYvs" + } + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "id": "sV7bKTpmvymI" + }, + "outputs": [], + "source": [ + "from sklearn.datasets import fetch_20newsgroups\n", + "\n", + "news_data=fetch_20newsgroups(subset='all',random_state=156)" + ] + }, + { + "cell_type": "code", + "source": [ + "print(news_data.keys())" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "bcZDEVygyUAp", + "outputId": "3ae1d88e-e4e1-43f8-e552-08cf7d3ce27e" + }, + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "dict_keys(['data', 'filenames', 'target_names', 'target', 'DESCR'])\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import pandas as pd\n", + "\n", + "print('target 클래스의 값과 분포도\\n',pd.Series(news_data.target).value_counts().sort_index())\n", + "print('target 클래스의 이름들\\n',news_data.target_names)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "x_EPSsZXyghC", + "outputId": "30efee70-c527-46b2-99c7-63bf7f7bab51" + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "target 클래스의 값과 분포도\n", + " 0 799\n", + "1 973\n", + "2 985\n", + "3 982\n", + "4 963\n", + "5 988\n", + "6 975\n", + "7 990\n", + "8 996\n", + "9 994\n", + "10 999\n", + "11 991\n", + "12 984\n", + "13 990\n", + "14 987\n", + "15 997\n", + "16 910\n", + "17 940\n", + "18 775\n", + "19 628\n", + "Name: count, dtype: int64\n", + "target 클래스의 이름들\n", + " ['alt.atheism', 'comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware', 'comp.windows.x', 'misc.forsale', 'rec.autos', 'rec.motorcycles', 'rec.sport.baseball', 'rec.sport.hockey', 'sci.crypt', 'sci.electronics', 'sci.med', 'sci.space', 'soc.religion.christian', 'talk.politics.guns', 'talk.politics.mideast', 'talk.politics.misc', 'talk.religion.misc']\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(news_data.data[0])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "esz6TH9ozEFM", + "outputId": "9098481e-d94d-485a-975d-8ff0dc1b6450" + }, + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "From: egreen@east.sun.com (Ed Green - Pixel Cruncher)\n", + "Subject: Re: Observation re: helmets\n", + "Organization: Sun Microsystems, RTP, NC\n", + "Lines: 21\n", + "Distribution: world\n", + "Reply-To: egreen@east.sun.com\n", + "NNTP-Posting-Host: laser.east.sun.com\n", + "\n", + "In article 211353@mavenry.altcit.eskimo.com, maven@mavenry.altcit.eskimo.com (Norman Hamer) writes:\n", + "> \n", + "> The question for the day is re: passenger helmets, if you don't know for \n", + ">certain who's gonna ride with you (like say you meet them at a .... church \n", + ">meeting, yeah, that's the ticket)... What are some guidelines? Should I just \n", + ">pick up another shoei in my size to have a backup helmet (XL), or should I \n", + ">maybe get an inexpensive one of a smaller size to accomodate my likely \n", + ">passenger? \n", + "\n", + "If your primary concern is protecting the passenger in the event of a\n", + "crash, have him or her fitted for a helmet that is their size. If your\n", + "primary concern is complying with stupid helmet laws, carry a real big\n", + "spare (you can put a big or small head in a big helmet, but not in a\n", + "small one).\n", + "\n", + "---\n", + "Ed Green, former Ninjaite |I was drinking last night with a biker,\n", + " Ed.Green@East.Sun.COM |and I showed him a picture of you. I said,\n", + "DoD #0111 (919)460-8302 |\"Go on, get to know her, you'll like her!\"\n", + " (The Grateful Dead) --> |It seemed like the least I could do...\n", + "\n", + "\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "from sklearn.datasets import fetch_20newsgroups\n", + "\n", + "#subset='train'으로 학습용 데이터만 추출, remove=('headers','footers','quotes')로 내용만 추출\n", + "train_news=fetch_20newsgroups(subset='train',remove=('headers','footers','quotes'),random_state=156)\n", + "X_train=train_news.data\n", + "y_train=train_news.target\n", + "\n", + "#subset='test'으로 테스트 데이터만 추출, remove=('headers','footers','quotes')로 내용만 추출\n", + "test_news=fetch_20newsgroups(subset='test',remove=('headers','footers','quotes'),random_state=156)\n", + "X_test=test_news.data\n", + "y_test=test_news.target\n", + "print('학습 데이터 크기 {0}, 테스트 데이터 크기 {1}'.format(len(train_news.data),len(test_news.data)))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "A2hpHZ_bzH4-", + "outputId": "db71a046-6366-4c78-9530-2affb86c7c31" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "학습 데이터 크기 11314, 테스트 데이터 크기 7532\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "피처 벡터화 변환과 머신러닝 모델 학습/예측/평가" + ], + "metadata": { + "id": "0ht97SyH3w8b" + } + }, + { + "cell_type": "markdown", + "source": [ + "CountVectorizer를 이용(반드시 학습 데이터를 이용해 fit()이 수행된 CountVectorizer 객체를 이용해 테스트 데이터를 변환해야함+테스트 데이터 피처 벡터화 시 fit_transform()을 사용하면 안됨)" + ], + "metadata": { + "id": "493F4ITx31zc" + } + }, + { + "cell_type": "code", + "source": [ + "from sklearn.feature_extraction.text import CountVectorizer\n", + "\n", + "#Count Vectorization으로 피처 벡터화 변환 수행\n", + "cnt_vect=CountVectorizer()\n", + "cnt_vect.fit(X_train)\n", + "X_train_cnt_vect=cnt_vect.transform(X_train)\n", + "\n", + "#학습 뎅터로 fit()된 CountVectorizer를 이용해 테스트 데이터를 피처 벡터화 변환 수행\n", + "X_test_cnt_vect=cnt_vect.transform(X_test)\n", + "\n", + "print('학습 데이터 텍스트의 CountVectorizer Shape:',X_train_cnt_vect.shape)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "yGVJ9LAa4EVR", + "outputId": "6f55c45b-4eb4-4af4-e8a7-5532057af0f0" + }, + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "학습 데이터 텍스트의 CountVectorizer Shape: (11314, 101631)\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "피처 벡터화된 데이터에 로지스틱 회귀 적용" + ], + "metadata": { + "id": "RB-1eKoT5xBm" + } + }, + { + "cell_type": "code", + "source": [ + "from sklearn.linear_model import LogisticRegression\n", + "from sklearn.metrics import accuracy_score\n", + "\n", + "#LogisticRegression을 이용해 학습/예측/평가 수행\n", + "lr_clf=LogisticRegression()\n", + "lr_clf.fit(X_train_cnt_vect,y_train)\n", + "pred=lr_clf.predict(X_test_cnt_vect)\n", + "print('CountVectorized Logistic Regression의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test,pred)))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "FRKnWJ4F5VXj", + "outputId": "9f807cd0-117b-4079-c54a-e64cdbfe6651" + }, + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "CountVectorized Logistic Regression의 예측 정확도는 0.603\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "/usr/local/lib/python3.10/dist-packages/sklearn/linear_model/_logistic.py:465: ConvergenceWarning: lbfgs failed to converge (status=1):\n", + "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n", + "\n", + "Increase the number of iterations (max_iter) or scale the data as shown in:\n", + " https://scikit-learn.org/stable/modules/preprocessing.html\n", + "Please also refer to the documentation for alternative solver options:\n", + " https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n", + " n_iter_i = _check_optimize_result(\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "TF-IDF 기반 벡터화를 변경해 예측 모델 수행" + ], + "metadata": { + "id": "0H-DGCtC5zu6" + } + }, + { + "cell_type": "code", + "source": [ + "from sklearn.feature_extraction.text import TfidfVectorizer\n", + "\n", + "#TF-IDF 벡터화를 적용해 학습 데이터 세트와 테스트 데이터 세트 변환\n", + "tfidf_vect=TfidfVectorizer()\n", + "tfidf_vect.fit(X_train)\n", + "X_train_tfidf_vect=tfidf_vect.transform(X_train)\n", + "X_test_tfidf_vect=tfidf_vect.transform(X_test)\n", + "\n", + "#LogisticRegression을 이용해 학습/예측/평가 수행\n", + "lr_clf=LogisticRegression()\n", + "lr_clf.fit(X_train_tfidf_vect,y_train)\n", + "pred=lr_clf.predict(X_test_tfidf_vect)\n", + "print('TF-IDF Logistic Regression의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test,pred)))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wd8-WY0J53Uh", + "outputId": "ef2793a8-3345-4ea9-8c0c-8fe50d9ce777" + }, + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TF-IDF Logistic Regression의 예측 정확도는 0.674\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "---> 일반적으로 문서 내에 텍스트가 많고 많은 문서를 가지는 텍스트 분석에서 카운트 벡터화 보다 TF-IDF 벡터화가 좋은 예측 결과를 도출함" + ], + "metadata": { + "id": "8NYDytpm7vld" + } + }, + { + "cell_type": "code", + "source": [ + "#stop words 필터링을 추가하고 ngram을 기본(1,1)에서 (1,2)로 변경해 피처 벡터화 적용\n", + "tfidf_vect=TfidfVectorizer(stop_words='english',ngram_range=(1,2),max_df=300)\n", + "tfidf_vect.fit(X_train)\n", + "X_train_tfidf_vect=tfidf_vect.transform(X_train)\n", + "X_test_tfidf_vect=tfidf_vect.transform(X_test)\n", + "\n", + "lr_clf=LogisticRegression()\n", + "lr_clf.fit(X_train_tfidf_vect,y_train)\n", + "pred=lr_clf.predict(X_test_tfidf_vect)\n", + "print('TF-IDF Logistic Regression의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test,pred)))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7fHeJcCn768Y", + "outputId": "7f29ab27-f6a5-40c0-f0c0-f3e93f5f0470" + }, + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "TF-IDF Logistic Regression의 예측 정확도는 0.692\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "from sklearn.model_selection import GridSearchCV\n", + "\n", + "#최적 C값 도출 튜닝 수행. CV는 3 폴드 세트로 설정\n", + "params={'C':[0.01,0.1,1,5,10]}\n", + "grid_cv_lr=GridSearchCV(lr_clf,param_grid=params,cv=3,scoring='accuracy',verbose=1)\n", + "grid_cv_lr.fit(X_train_tfidf_vect,y_train)\n", + "print('Logistic Regression best C parameter:',grid_cv_lr.best_params_)\n", + "\n", + "#최적 C 값으로 학습된 grid_cv로 예측 및 정확도 평가\n", + "pred=grid_cv_lr.predict(X_test_tfidf_vect)\n", + "print('TF-IDF Vectorized Logistic Regression의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test,pred)))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "yKtpnKLA9dBF", + "outputId": "0d2cfd0e-0882-4161-d844-a6ad4e29feef" + }, + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Fitting 3 folds for each of 5 candidates, totalling 15 fits\n", + "Logistic Regression best C parameter: {'C': 10}\n", + "TF-IDF Vectorized Logistic Regression의 예측 정확도는 0.701\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "사이킷런 파이프라인 사용 및 GridSearchCV와의 결합" + ], + "metadata": { + "id": "BzRa8V1UBRkI" + } + }, + { + "cell_type": "code", + "source": [ + "from sklearn.pipeline import Pipeline\n", + "\n", + "#TfidfVectorizer 객체를 tfidf_vect로, LogisticRegression객체를 lr_clf로 생성하는 Pipeline 생성\n", + "pipeline=Pipeline([('tfidf_vect',TfidfVectorizer(stop_words='english',ngram_range=(1,2),max_df=300)),('lr_clf',LogisticRegression(C=10))])\n", + "\n", + "#별도의 TfidfVectorizer 객체의 fit(), transform()과 LogisticRegression의 fit(),predict()가 필요없음\n", + "#pipeline의 fit()과 predict()만으로 한꺼번에 피처 벡터화와 ML학습/예측 가능\n", + "pipeline.fit(X_train,y_train)\n", + "pred=pipeline.predict(X_test)\n", + "print('Pipeline을 통한 Logistic Regression의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test,pred)))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "DnYtrPsiBhRF", + "outputId": "d083919e-f9ee-410b-ea08-d610ca944a46" + }, + "execution_count": 21, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Pipeline을 통한 Logistic Regression의 예측 정확도는 0.701\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "from sklearn.pipeline import Pipeline\n", + "\n", + "pipeline = Pipeline([\n", + " ('tfidf_vect', TfidfVectorizer(stop_words='english')),('lr_clf', LogisticRegression())])\n", + "\n", + "# Pipeline에 기술된 각각의 객체 변수에 언더바(_)2개를 연달아 붙여 GridSearchCV에 사용될 파라미터/하이퍼 파라미터 이름과 값을 설정\n", + "params = { 'tfidf_vect__ngram_range': [(1,1), (1,2), (1,3)],\n", + " 'tfidf_vect__max_df': [100, 300, 700],\n", + " 'lr_clf__C': [1,5,10]\n", + "}\n", + "\n", + "# GridSearchCV의 생성자에 Estimator가 아닌 Pipeline 객체 입력\n", + "grid_cv_pipe = GridSearchCV(pipeline, param_grid=params, cv=3 , scoring='accuracy',verbose=1)\n", + "grid_cv_pipe.fit(X_train , y_train)\n", + "print(grid_cv_pipe.best_params_ , grid_cv_pipe.best_score_)\n", + "\n", + "pred = grid_cv_pipe.predict(X_test)\n", + "print('Pipeline을 통한 Logistic Regression 의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test ,pred)))" + ], + "metadata": { + "id": "E1ZlsMErttcQ" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file From 5999245f51de0831b851417f00342b2f5815f2f1 Mon Sep 17 00:00:00 2001 From: song122333 Date: Sat, 28 Dec 2024 00:25:49 +0900 Subject: [PATCH 2/3] =?UTF-8?q?Delete=20Week14=5F=EB=B3=B5=EC=8A=B5?= =?UTF-8?q?=EA=B3=BC=EC=A0=9C=5F=ED=95=9C=EC=86=A1=ED=9D=AC.ipynb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...355\225\234\354\206\241\355\235\254.ipynb" | 491 ------------------ 1 file changed, 491 deletions(-) delete mode 100644 "Week14_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" diff --git "a/Week14_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" "b/Week14_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" deleted file mode 100644 index 6140aa0..0000000 --- "a/Week14_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" +++ /dev/null @@ -1,491 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, - "cells": [ - { - "cell_type": "markdown", - "source": [ - "fetch_20newsgropus()로 데이터를 내려받고 메모리로 데이터 로딩" - ], - "metadata": { - "id": "oB7bnbikyYvs" - } - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "id": "sV7bKTpmvymI" - }, - "outputs": [], - "source": [ - "from sklearn.datasets import fetch_20newsgroups\n", - "\n", - "news_data=fetch_20newsgroups(subset='all',random_state=156)" - ] - }, - { - "cell_type": "code", - "source": [ - "print(news_data.keys())" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "bcZDEVygyUAp", - "outputId": "3ae1d88e-e4e1-43f8-e552-08cf7d3ce27e" - }, - "execution_count": 2, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "dict_keys(['data', 'filenames', 'target_names', 'target', 'DESCR'])\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "import pandas as pd\n", - "\n", - "print('target 클래스의 값과 분포도\\n',pd.Series(news_data.target).value_counts().sort_index())\n", - "print('target 클래스의 이름들\\n',news_data.target_names)" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "x_EPSsZXyghC", - "outputId": "30efee70-c527-46b2-99c7-63bf7f7bab51" - }, - "execution_count": 3, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "target 클래스의 값과 분포도\n", - " 0 799\n", - "1 973\n", - "2 985\n", - "3 982\n", - "4 963\n", - "5 988\n", - "6 975\n", - "7 990\n", - "8 996\n", - "9 994\n", - "10 999\n", - "11 991\n", - "12 984\n", - "13 990\n", - "14 987\n", - "15 997\n", - "16 910\n", - "17 940\n", - "18 775\n", - "19 628\n", - "Name: count, dtype: int64\n", - "target 클래스의 이름들\n", - " ['alt.atheism', 'comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware', 'comp.windows.x', 'misc.forsale', 'rec.autos', 'rec.motorcycles', 'rec.sport.baseball', 'rec.sport.hockey', 'sci.crypt', 'sci.electronics', 'sci.med', 'sci.space', 'soc.religion.christian', 'talk.politics.guns', 'talk.politics.mideast', 'talk.politics.misc', 'talk.religion.misc']\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "print(news_data.data[0])" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "esz6TH9ozEFM", - "outputId": "9098481e-d94d-485a-975d-8ff0dc1b6450" - }, - "execution_count": 4, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "From: egreen@east.sun.com (Ed Green - Pixel Cruncher)\n", - "Subject: Re: Observation re: helmets\n", - "Organization: Sun Microsystems, RTP, NC\n", - "Lines: 21\n", - "Distribution: world\n", - "Reply-To: egreen@east.sun.com\n", - "NNTP-Posting-Host: laser.east.sun.com\n", - "\n", - "In article 211353@mavenry.altcit.eskimo.com, maven@mavenry.altcit.eskimo.com (Norman Hamer) writes:\n", - "> \n", - "> The question for the day is re: passenger helmets, if you don't know for \n", - ">certain who's gonna ride with you (like say you meet them at a .... church \n", - ">meeting, yeah, that's the ticket)... What are some guidelines? Should I just \n", - ">pick up another shoei in my size to have a backup helmet (XL), or should I \n", - ">maybe get an inexpensive one of a smaller size to accomodate my likely \n", - ">passenger? \n", - "\n", - "If your primary concern is protecting the passenger in the event of a\n", - "crash, have him or her fitted for a helmet that is their size. If your\n", - "primary concern is complying with stupid helmet laws, carry a real big\n", - "spare (you can put a big or small head in a big helmet, but not in a\n", - "small one).\n", - "\n", - "---\n", - "Ed Green, former Ninjaite |I was drinking last night with a biker,\n", - " Ed.Green@East.Sun.COM |and I showed him a picture of you. I said,\n", - "DoD #0111 (919)460-8302 |\"Go on, get to know her, you'll like her!\"\n", - " (The Grateful Dead) --> |It seemed like the least I could do...\n", - "\n", - "\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "from sklearn.datasets import fetch_20newsgroups\n", - "\n", - "#subset='train'으로 학습용 데이터만 추출, remove=('headers','footers','quotes')로 내용만 추출\n", - "train_news=fetch_20newsgroups(subset='train',remove=('headers','footers','quotes'),random_state=156)\n", - "X_train=train_news.data\n", - "y_train=train_news.target\n", - "\n", - "#subset='test'으로 테스트 데이터만 추출, remove=('headers','footers','quotes')로 내용만 추출\n", - "test_news=fetch_20newsgroups(subset='test',remove=('headers','footers','quotes'),random_state=156)\n", - "X_test=test_news.data\n", - "y_test=test_news.target\n", - "print('학습 데이터 크기 {0}, 테스트 데이터 크기 {1}'.format(len(train_news.data),len(test_news.data)))" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "A2hpHZ_bzH4-", - "outputId": "db71a046-6366-4c78-9530-2affb86c7c31" - }, - "execution_count": 6, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "학습 데이터 크기 11314, 테스트 데이터 크기 7532\n" - ] - } - ] - }, - { - "cell_type": "markdown", - "source": [ - "피처 벡터화 변환과 머신러닝 모델 학습/예측/평가" - ], - "metadata": { - "id": "0ht97SyH3w8b" - } - }, - { - "cell_type": "markdown", - "source": [ - "CountVectorizer를 이용(반드시 학습 데이터를 이용해 fit()이 수행된 CountVectorizer 객체를 이용해 테스트 데이터를 변환해야함+테스트 데이터 피처 벡터화 시 fit_transform()을 사용하면 안됨)" - ], - "metadata": { - "id": "493F4ITx31zc" - } - }, - { - "cell_type": "code", - "source": [ - "from sklearn.feature_extraction.text import CountVectorizer\n", - "\n", - "#Count Vectorization으로 피처 벡터화 변환 수행\n", - "cnt_vect=CountVectorizer()\n", - "cnt_vect.fit(X_train)\n", - "X_train_cnt_vect=cnt_vect.transform(X_train)\n", - "\n", - "#학습 뎅터로 fit()된 CountVectorizer를 이용해 테스트 데이터를 피처 벡터화 변환 수행\n", - "X_test_cnt_vect=cnt_vect.transform(X_test)\n", - "\n", - "print('학습 데이터 텍스트의 CountVectorizer Shape:',X_train_cnt_vect.shape)" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "yGVJ9LAa4EVR", - "outputId": "6f55c45b-4eb4-4af4-e8a7-5532057af0f0" - }, - "execution_count": 7, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "학습 데이터 텍스트의 CountVectorizer Shape: (11314, 101631)\n" - ] - } - ] - }, - { - "cell_type": "markdown", - "source": [ - "피처 벡터화된 데이터에 로지스틱 회귀 적용" - ], - "metadata": { - "id": "RB-1eKoT5xBm" - } - }, - { - "cell_type": "code", - "source": [ - "from sklearn.linear_model import LogisticRegression\n", - "from sklearn.metrics import accuracy_score\n", - "\n", - "#LogisticRegression을 이용해 학습/예측/평가 수행\n", - "lr_clf=LogisticRegression()\n", - "lr_clf.fit(X_train_cnt_vect,y_train)\n", - "pred=lr_clf.predict(X_test_cnt_vect)\n", - "print('CountVectorized Logistic Regression의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test,pred)))" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "FRKnWJ4F5VXj", - "outputId": "9f807cd0-117b-4079-c54a-e64cdbfe6651" - }, - "execution_count": 8, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "CountVectorized Logistic Regression의 예측 정확도는 0.603\n" - ] - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "/usr/local/lib/python3.10/dist-packages/sklearn/linear_model/_logistic.py:465: ConvergenceWarning: lbfgs failed to converge (status=1):\n", - "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n", - "\n", - "Increase the number of iterations (max_iter) or scale the data as shown in:\n", - " https://scikit-learn.org/stable/modules/preprocessing.html\n", - "Please also refer to the documentation for alternative solver options:\n", - " https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n", - " n_iter_i = _check_optimize_result(\n" - ] - } - ] - }, - { - "cell_type": "markdown", - "source": [ - "TF-IDF 기반 벡터화를 변경해 예측 모델 수행" - ], - "metadata": { - "id": "0H-DGCtC5zu6" - } - }, - { - "cell_type": "code", - "source": [ - "from sklearn.feature_extraction.text import TfidfVectorizer\n", - "\n", - "#TF-IDF 벡터화를 적용해 학습 데이터 세트와 테스트 데이터 세트 변환\n", - "tfidf_vect=TfidfVectorizer()\n", - "tfidf_vect.fit(X_train)\n", - "X_train_tfidf_vect=tfidf_vect.transform(X_train)\n", - "X_test_tfidf_vect=tfidf_vect.transform(X_test)\n", - "\n", - "#LogisticRegression을 이용해 학습/예측/평가 수행\n", - "lr_clf=LogisticRegression()\n", - "lr_clf.fit(X_train_tfidf_vect,y_train)\n", - "pred=lr_clf.predict(X_test_tfidf_vect)\n", - "print('TF-IDF Logistic Regression의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test,pred)))" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "wd8-WY0J53Uh", - "outputId": "ef2793a8-3345-4ea9-8c0c-8fe50d9ce777" - }, - "execution_count": 11, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "TF-IDF Logistic Regression의 예측 정확도는 0.674\n" - ] - } - ] - }, - { - "cell_type": "markdown", - "source": [ - "---> 일반적으로 문서 내에 텍스트가 많고 많은 문서를 가지는 텍스트 분석에서 카운트 벡터화 보다 TF-IDF 벡터화가 좋은 예측 결과를 도출함" - ], - "metadata": { - "id": "8NYDytpm7vld" - } - }, - { - "cell_type": "code", - "source": [ - "#stop words 필터링을 추가하고 ngram을 기본(1,1)에서 (1,2)로 변경해 피처 벡터화 적용\n", - "tfidf_vect=TfidfVectorizer(stop_words='english',ngram_range=(1,2),max_df=300)\n", - "tfidf_vect.fit(X_train)\n", - "X_train_tfidf_vect=tfidf_vect.transform(X_train)\n", - "X_test_tfidf_vect=tfidf_vect.transform(X_test)\n", - "\n", - "lr_clf=LogisticRegression()\n", - "lr_clf.fit(X_train_tfidf_vect,y_train)\n", - "pred=lr_clf.predict(X_test_tfidf_vect)\n", - "print('TF-IDF Logistic Regression의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test,pred)))" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "7fHeJcCn768Y", - "outputId": "7f29ab27-f6a5-40c0-f0c0-f3e93f5f0470" - }, - "execution_count": 12, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "TF-IDF Logistic Regression의 예측 정확도는 0.692\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "from sklearn.model_selection import GridSearchCV\n", - "\n", - "#최적 C값 도출 튜닝 수행. CV는 3 폴드 세트로 설정\n", - "params={'C':[0.01,0.1,1,5,10]}\n", - "grid_cv_lr=GridSearchCV(lr_clf,param_grid=params,cv=3,scoring='accuracy',verbose=1)\n", - "grid_cv_lr.fit(X_train_tfidf_vect,y_train)\n", - "print('Logistic Regression best C parameter:',grid_cv_lr.best_params_)\n", - "\n", - "#최적 C 값으로 학습된 grid_cv로 예측 및 정확도 평가\n", - "pred=grid_cv_lr.predict(X_test_tfidf_vect)\n", - "print('TF-IDF Vectorized Logistic Regression의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test,pred)))" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "yKtpnKLA9dBF", - "outputId": "0d2cfd0e-0882-4161-d844-a6ad4e29feef" - }, - "execution_count": 13, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Fitting 3 folds for each of 5 candidates, totalling 15 fits\n", - "Logistic Regression best C parameter: {'C': 10}\n", - "TF-IDF Vectorized Logistic Regression의 예측 정확도는 0.701\n" - ] - } - ] - }, - { - "cell_type": "markdown", - "source": [ - "사이킷런 파이프라인 사용 및 GridSearchCV와의 결합" - ], - "metadata": { - "id": "BzRa8V1UBRkI" - } - }, - { - "cell_type": "code", - "source": [ - "from sklearn.pipeline import Pipeline\n", - "\n", - "#TfidfVectorizer 객체를 tfidf_vect로, LogisticRegression객체를 lr_clf로 생성하는 Pipeline 생성\n", - "pipeline=Pipeline([('tfidf_vect',TfidfVectorizer(stop_words='english',ngram_range=(1,2),max_df=300)),('lr_clf',LogisticRegression(C=10))])\n", - "\n", - "#별도의 TfidfVectorizer 객체의 fit(), transform()과 LogisticRegression의 fit(),predict()가 필요없음\n", - "#pipeline의 fit()과 predict()만으로 한꺼번에 피처 벡터화와 ML학습/예측 가능\n", - "pipeline.fit(X_train,y_train)\n", - "pred=pipeline.predict(X_test)\n", - "print('Pipeline을 통한 Logistic Regression의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test,pred)))" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "DnYtrPsiBhRF", - "outputId": "d083919e-f9ee-410b-ea08-d610ca944a46" - }, - "execution_count": 21, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Pipeline을 통한 Logistic Regression의 예측 정확도는 0.701\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "from sklearn.pipeline import Pipeline\n", - "\n", - "pipeline = Pipeline([\n", - " ('tfidf_vect', TfidfVectorizer(stop_words='english')),('lr_clf', LogisticRegression())])\n", - "\n", - "# Pipeline에 기술된 각각의 객체 변수에 언더바(_)2개를 연달아 붙여 GridSearchCV에 사용될 파라미터/하이퍼 파라미터 이름과 값을 설정\n", - "params = { 'tfidf_vect__ngram_range': [(1,1), (1,2), (1,3)],\n", - " 'tfidf_vect__max_df': [100, 300, 700],\n", - " 'lr_clf__C': [1,5,10]\n", - "}\n", - "\n", - "# GridSearchCV의 생성자에 Estimator가 아닌 Pipeline 객체 입력\n", - "grid_cv_pipe = GridSearchCV(pipeline, param_grid=params, cv=3 , scoring='accuracy',verbose=1)\n", - "grid_cv_pipe.fit(X_train , y_train)\n", - "print(grid_cv_pipe.best_params_ , grid_cv_pipe.best_score_)\n", - "\n", - "pred = grid_cv_pipe.predict(X_test)\n", - "print('Pipeline을 통한 Logistic Regression 의 예측 정확도는 {0:.3f}'.format(accuracy_score(y_test ,pred)))" - ], - "metadata": { - "id": "E1ZlsMErttcQ" - }, - "execution_count": null, - "outputs": [] - } - ] -} \ No newline at end of file From b23dd9a3482fbcf3633f4ec058899854f5275148 Mon Sep 17 00:00:00 2001 From: song122333 Date: Sun, 12 Jan 2025 18:26:30 +0900 Subject: [PATCH 3/3] =?UTF-8?q?16=EC=A3=BC=EC=B0=A8=20=EB=B3=B5=EC=8A=B5?= =?UTF-8?q?=EA=B3=BC=EC=A0=9C=20=EC=A0=9C=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...355\225\234\354\206\241\355\235\254.ipynb" | 6218 +++++++++++++++++ 1 file changed, 6218 insertions(+) create mode 100644 "Week16_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" diff --git "a/Week16_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" "b/Week16_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" new file mode 100644 index 0000000..adf9c57 --- /dev/null +++ "b/Week16_\353\263\265\354\212\265\352\263\274\354\240\234_\355\225\234\354\206\241\355\235\254.ipynb" @@ -0,0 +1,6218 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "#9.5 콘텐츠 기반 필터링 실습-TMDB 5000 영화 데이터 세트" + ], + "metadata": { + "id": "-TsLeOeRmEnE" + } + }, + { + "cell_type": "markdown", + "source": [ + "<데이터 로딩 및 가공>" + ], + "metadata": { + "id": "8zMswPK-mMqs" + } + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 205 + }, + "id": "5EC4LoPokp7f", + "outputId": "0a4a54a7-5b19-44fc-a8af-96cd8580b873" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(4803, 20)\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " budget genres \\\n", + "0 237000000 [{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"nam... \n", + "\n", + " homepage id \\\n", + "0 http://www.avatarmovie.com/ 19995 \n", + "\n", + " keywords original_language \\\n", + "0 [{\"id\": 1463, \"name\": \"culture clash\"}, {\"id\":... en \n", + "\n", + " original_title overview \\\n", + "0 Avatar In the 22nd century, a paraplegic Marine is di... \n", + "\n", + " popularity production_companies \\\n", + "0 150.437577 [{\"name\": \"Ingenious Film Partners\", \"id\": 289... \n", + "\n", + " production_countries release_date revenue \\\n", + "0 [{\"iso_3166_1\": \"US\", \"name\": \"United States o... 2009-12-10 2787965087 \n", + "\n", + " runtime spoken_languages status \\\n", + "0 162.0 [{\"iso_639_1\": \"en\", \"name\": \"English\"}, {\"iso... Released \n", + "\n", + " tagline title vote_average vote_count \n", + "0 Enter the World of Pandora. Avatar 7.2 11800 " + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
budgetgenreshomepageidkeywordsoriginal_languageoriginal_titleoverviewpopularityproduction_companiesproduction_countriesrelease_daterevenueruntimespoken_languagesstatustaglinetitlevote_averagevote_count
0237000000[{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"nam...http://www.avatarmovie.com/19995[{\"id\": 1463, \"name\": \"culture clash\"}, {\"id\":...enAvatarIn the 22nd century, a paraplegic Marine is di...150.437577[{\"name\": \"Ingenious Film Partners\", \"id\": 289...[{\"iso_3166_1\": \"US\", \"name\": \"United States o...2009-12-102787965087162.0[{\"iso_639_1\": \"en\", \"name\": \"English\"}, {\"iso...ReleasedEnter the World of Pandora.Avatar7.211800
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "movies", + "summary": "{\n \"name\": \"movies\",\n \"rows\": 4803,\n \"fields\": [\n {\n \"column\": \"budget\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 40722391,\n \"min\": 0,\n \"max\": 380000000,\n \"num_unique_values\": 436,\n \"samples\": [\n 439000,\n 68000000,\n 700000\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genres\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1175,\n \"samples\": [\n \"[{\\\"id\\\": 14, \\\"name\\\": \\\"Fantasy\\\"}, {\\\"id\\\": 12, \\\"name\\\": \\\"Adventure\\\"}, {\\\"id\\\": 16, \\\"name\\\": \\\"Animation\\\"}]\",\n \"[{\\\"id\\\": 28, \\\"name\\\": \\\"Action\\\"}, {\\\"id\\\": 35, \\\"name\\\": \\\"Comedy\\\"}, {\\\"id\\\": 80, \\\"name\\\": \\\"Crime\\\"}, {\\\"id\\\": 18, \\\"name\\\": \\\"Drama\\\"}]\",\n \"[{\\\"id\\\": 12, \\\"name\\\": \\\"Adventure\\\"}, {\\\"id\\\": 16, \\\"name\\\": \\\"Animation\\\"}, {\\\"id\\\": 10751, \\\"name\\\": \\\"Family\\\"}, {\\\"id\\\": 14, \\\"name\\\": \\\"Fantasy\\\"}, {\\\"id\\\": 878, \\\"name\\\": \\\"Science Fiction\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"homepage\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1691,\n \"samples\": [\n \"https://www.warnerbros.com/running-scared\",\n \"http://www.51birchstreet.com/index.php\",\n \"http://movies2.foxjapan.com/glee/\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 88694,\n \"min\": 5,\n \"max\": 459488,\n \"num_unique_values\": 4803,\n \"samples\": [\n 8427,\n 13006,\n 18041\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"keywords\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4222,\n \"samples\": [\n \"[{\\\"id\\\": 782, \\\"name\\\": \\\"assassin\\\"}, {\\\"id\\\": 1872, \\\"name\\\": \\\"loss of father\\\"}, {\\\"id\\\": 2908, \\\"name\\\": \\\"secret society\\\"}, {\\\"id\\\": 3045, \\\"name\\\": \\\"mission of murder\\\"}, {\\\"id\\\": 9748, \\\"name\\\": \\\"revenge\\\"}]\",\n \"[{\\\"id\\\": 2987, \\\"name\\\": \\\"gang war\\\"}, {\\\"id\\\": 4942, \\\"name\\\": \\\"victim of murder\\\"}, {\\\"id\\\": 5332, \\\"name\\\": \\\"greed\\\"}, {\\\"id\\\": 6062, \\\"name\\\": \\\"hostility\\\"}, {\\\"id\\\": 156212, \\\"name\\\": \\\"spaghetti western\\\"}]\",\n \"[{\\\"id\\\": 703, \\\"name\\\": \\\"detective\\\"}, {\\\"id\\\": 1299, \\\"name\\\": \\\"monster\\\"}, {\\\"id\\\": 6101, \\\"name\\\": \\\"engine\\\"}, {\\\"id\\\": 10988, \\\"name\\\": \\\"based on tv series\\\"}, {\\\"id\\\": 15162, \\\"name\\\": \\\"dog\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"original_language\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 37,\n \"samples\": [\n \"xx\",\n \"ta\",\n \"es\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"original_title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4801,\n \"samples\": [\n \"I Spy\",\n \"Love Letters\",\n \"Sleepover\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"overview\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4800,\n \"samples\": [\n \"When the Switchblade, the most sophisticated prototype stealth fighter created yet, is stolen from the U.S. government, one of the United States' top spies, Alex Scott, is called to action. What he doesn't expect is to get teamed up with a cocky civilian, World Class Boxing Champion Kelly Robinson, on a dangerous top secret espionage mission. Their assignment: using equal parts skill and humor, catch Arnold Gundars, one of the world's most successful arms dealers.\",\n \"When \\\"street smart\\\" rapper Christopher \\\"C-Note\\\" Hawkins (Big Boi) applies for a membership to all-white Carolina Pines Country Club, the establishment's proprietors are hardly ready to oblige him.\",\n \"As their first year of high school looms ahead, best friends Julie, Hannah, Yancy and Farrah have one last summer sleepover. Little do they know they're about to embark on the adventure of a lifetime. Desperate to shed their nerdy status, they take part in a night-long scavenger hunt that pits them against their popular archrivals. Everything under the sun goes on -- from taking Yancy's father's car to sneaking into nightclubs!\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"popularity\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 31.816649749537806,\n \"min\": 0.0,\n \"max\": 875.581305,\n \"num_unique_values\": 4802,\n \"samples\": [\n 13.267631,\n 0.010909,\n 5.842299\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"production_companies\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3697,\n \"samples\": [\n \"[{\\\"name\\\": \\\"Paramount Pictures\\\", \\\"id\\\": 4}, {\\\"name\\\": \\\"Cherry Alley Productions\\\", \\\"id\\\": 2232}]\",\n \"[{\\\"name\\\": \\\"Twentieth Century Fox Film Corporation\\\", \\\"id\\\": 306}, {\\\"name\\\": \\\"Dune Entertainment\\\", \\\"id\\\": 444}, {\\\"name\\\": \\\"Regency Enterprises\\\", \\\"id\\\": 508}, {\\\"name\\\": \\\"Guy Walks into a Bar Productions\\\", \\\"id\\\": 2645}, {\\\"name\\\": \\\"Deep River Productions\\\", \\\"id\\\": 2646}, {\\\"name\\\": \\\"Friendly Films (II)\\\", \\\"id\\\": 81136}]\",\n \"[{\\\"name\\\": \\\"Twentieth Century Fox Film Corporation\\\", \\\"id\\\": 306}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"production_countries\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 469,\n \"samples\": [\n \"[{\\\"iso_3166_1\\\": \\\"ES\\\", \\\"name\\\": \\\"Spain\\\"}, {\\\"iso_3166_1\\\": \\\"GB\\\", \\\"name\\\": \\\"United Kingdom\\\"}, {\\\"iso_3166_1\\\": \\\"US\\\", \\\"name\\\": \\\"United States of America\\\"}, {\\\"iso_3166_1\\\": \\\"FR\\\", \\\"name\\\": \\\"France\\\"}]\",\n \"[{\\\"iso_3166_1\\\": \\\"US\\\", \\\"name\\\": \\\"United States of America\\\"}, {\\\"iso_3166_1\\\": \\\"CA\\\", \\\"name\\\": \\\"Canada\\\"}, {\\\"iso_3166_1\\\": \\\"DE\\\", \\\"name\\\": \\\"Germany\\\"}]\",\n \"[{\\\"iso_3166_1\\\": \\\"DE\\\", \\\"name\\\": \\\"Germany\\\"}, {\\\"iso_3166_1\\\": \\\"ES\\\", \\\"name\\\": \\\"Spain\\\"}, {\\\"iso_3166_1\\\": \\\"GB\\\", \\\"name\\\": \\\"United Kingdom\\\"}, {\\\"iso_3166_1\\\": \\\"US\\\", \\\"name\\\": \\\"United States of America\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"release_date\",\n \"properties\": {\n \"dtype\": \"object\",\n \"num_unique_values\": 3280,\n \"samples\": [\n \"1966-10-16\",\n \"1987-07-31\",\n \"1993-09-23\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"revenue\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 162857100,\n \"min\": 0,\n \"max\": 2787965087,\n \"num_unique_values\": 3297,\n \"samples\": [\n 11833696,\n 10462500,\n 17807569\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"runtime\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 22.611934588844207,\n \"min\": 0.0,\n \"max\": 338.0,\n \"num_unique_values\": 156,\n \"samples\": [\n 74.0,\n 85.0,\n 170.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"spoken_languages\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 544,\n \"samples\": [\n \"[{\\\"iso_639_1\\\": \\\"es\\\", \\\"name\\\": \\\"Espa\\\\u00f1ol\\\"}, {\\\"iso_639_1\\\": \\\"en\\\", \\\"name\\\": \\\"English\\\"}, {\\\"iso_639_1\\\": \\\"fr\\\", \\\"name\\\": \\\"Fran\\\\u00e7ais\\\"}, {\\\"iso_639_1\\\": \\\"hu\\\", \\\"name\\\": \\\"Magyar\\\"}]\",\n \"[{\\\"iso_639_1\\\": \\\"en\\\", \\\"name\\\": \\\"English\\\"}, {\\\"iso_639_1\\\": \\\"it\\\", \\\"name\\\": \\\"Italiano\\\"}, {\\\"iso_639_1\\\": \\\"pt\\\", \\\"name\\\": \\\"Portugu\\\\u00eas\\\"}]\",\n \"[{\\\"iso_639_1\\\": \\\"de\\\", \\\"name\\\": \\\"Deutsch\\\"}, {\\\"iso_639_1\\\": \\\"it\\\", \\\"name\\\": \\\"Italiano\\\"}, {\\\"iso_639_1\\\": \\\"la\\\", \\\"name\\\": \\\"Latin\\\"}, {\\\"iso_639_1\\\": \\\"pl\\\", \\\"name\\\": \\\"Polski\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"status\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Released\",\n \"Post Production\",\n \"Rumored\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"tagline\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3944,\n \"samples\": [\n \"When you're 17, every day is war.\",\n \"An Unspeakable Horror. A Creative Genius. Captured For Eternity.\",\n \"May the schwartz be with you\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4800,\n \"samples\": [\n \"I Spy\",\n \"Who's Your Caddy?\",\n \"Sleepover\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_average\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1.1946121628478925,\n \"min\": 0.0,\n \"max\": 10.0,\n \"num_unique_values\": 71,\n \"samples\": [\n 5.1,\n 7.2,\n 4.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_count\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1234,\n \"min\": 0,\n \"max\": 13752,\n \"num_unique_values\": 1609,\n \"samples\": [\n 7604,\n 3428,\n 225\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 1 + } + ], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import warnings; warnings.filterwarnings('ignore')\n", + "\n", + "movies=pd.read_csv('/content/tmdb_5000_movies.csv')\n", + "print(movies.shape)\n", + "movies.head(1)" + ] + }, + { + "cell_type": "code", + "source": [ + "movies_df = movies[['id','title', 'genres', 'vote_average', 'vote_count','popularity', 'keywords', 'overview']]" + ], + "metadata": { + "id": "WuFDPKInlhUF" + }, + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "pd.set_option('max_colwidth',100)\n", + "movies_df[['genres','keywords']][:1]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 81 + }, + "id": "nwU91DgQlkP0", + "outputId": "598ef378-324b-4a49-c3bc-5309d367709f" + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " genres \\\n", + "0 [{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"name\": \"Adventure\"}, {\"id\": 14, \"name\": \"Fantasy\"}, {... \n", + "\n", + " keywords \n", + "0 [{\"id\": 1463, \"name\": \"culture clash\"}, {\"id\": 2964, \"name\": \"future\"}, {\"id\": 3386, \"name\": \"sp... " + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
genreskeywords
0[{\"id\": 28, \"name\": \"Action\"}, {\"id\": 12, \"name\": \"Adventure\"}, {\"id\": 14, \"name\": \"Fantasy\"}, {...[{\"id\": 1463, \"name\": \"culture clash\"}, {\"id\": 2964, \"name\": \"future\"}, {\"id\": 3386, \"name\": \"sp...
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"movies_df[['genres','keywords']][:1]\",\n \"rows\": 1,\n \"fields\": [\n {\n \"column\": \"genres\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"[{\\\"id\\\": 28, \\\"name\\\": \\\"Action\\\"}, {\\\"id\\\": 12, \\\"name\\\": \\\"Adventure\\\"}, {\\\"id\\\": 14, \\\"name\\\": \\\"Fantasy\\\"}, {\\\"id\\\": 878, \\\"name\\\": \\\"Science Fiction\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"keywords\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"[{\\\"id\\\": 1463, \\\"name\\\": \\\"culture clash\\\"}, {\\\"id\\\": 2964, \\\"name\\\": \\\"future\\\"}, {\\\"id\\\": 3386, \\\"name\\\": \\\"space war\\\"}, {\\\"id\\\": 3388, \\\"name\\\": \\\"space colony\\\"}, {\\\"id\\\": 3679, \\\"name\\\": \\\"society\\\"}, {\\\"id\\\": 3801, \\\"name\\\": \\\"space travel\\\"}, {\\\"id\\\": 9685, \\\"name\\\": \\\"futuristic\\\"}, {\\\"id\\\": 9840, \\\"name\\\": \\\"romance\\\"}, {\\\"id\\\": 9882, \\\"name\\\": \\\"space\\\"}, {\\\"id\\\": 9951, \\\"name\\\": \\\"alien\\\"}, {\\\"id\\\": 10148, \\\"name\\\": \\\"tribe\\\"}, {\\\"id\\\": 10158, \\\"name\\\": \\\"alien planet\\\"}, {\\\"id\\\": 10987, \\\"name\\\": \\\"cgi\\\"}, {\\\"id\\\": 11399, \\\"name\\\": \\\"marine\\\"}, {\\\"id\\\": 13065, \\\"name\\\": \\\"soldier\\\"}, {\\\"id\\\": 14643, \\\"name\\\": \\\"battle\\\"}, {\\\"id\\\": 14720, \\\"name\\\": \\\"love affair\\\"}, {\\\"id\\\": 165431, \\\"name\\\": \\\"anti war\\\"}, {\\\"id\\\": 193554, \\\"name\\\": \\\"power relations\\\"}, {\\\"id\\\": 206690, \\\"name\\\": \\\"mind and soul\\\"}, {\\\"id\\\": 209714, \\\"name\\\": \\\"3d\\\"}]\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 3 + } + ] + }, + { + "cell_type": "code", + "source": [ + "from ast import literal_eval\n", + "movies_df['genres']=movies_df['genres'].apply(literal_eval)\n", + "movies_df['keywords']=movies_df['keywords'].apply(literal_eval)" + ], + "metadata": { + "id": "gtINzWoJlxSV" + }, + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "movies_df['genres'] = movies_df['genres'].apply(lambda x : [ y['name'] for y in x])\n", + "movies_df['keywords'] = movies_df['keywords'].apply(lambda x : [ y['name'] for y in x])\n", + "movies_df[['genres', 'keywords']][:1]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 81 + }, + "id": "DJgD1SPrmBLF", + "outputId": "2ebd87a5-8019-4598-8450-697f851e1c6c" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " genres \\\n", + "0 [Action, Adventure, Fantasy, Science Fiction] \n", + "\n", + " keywords \n", + "0 [culture clash, future, space war, space colony, society, space travel, futuristic, romance, spa... " + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
genreskeywords
0[Action, Adventure, Fantasy, Science Fiction][culture clash, future, space war, space colony, society, space travel, futuristic, romance, spa...
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"movies_df[['genres', 'keywords']][:1]\",\n \"rows\": 1,\n \"fields\": [\n {\n \"column\": \"genres\",\n \"properties\": {\n \"dtype\": \"object\",\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"keywords\",\n \"properties\": {\n \"dtype\": \"object\",\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 5 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "<장르 콘텐츠 유사도 측정>" + ], + "metadata": { + "id": "J2MgQeirmPF8" + } + }, + { + "cell_type": "markdown", + "source": [ + "genres를 문자열로 변경한 뒤 이를 CountVectorizer로 피처 벡터화한 행렬 데이터 값을 코사인 유사도로 비교함" + ], + "metadata": { + "id": "kkzXGyRmmTmB" + } + }, + { + "cell_type": "code", + "source": [ + "from sklearn.feature_extraction.text import CountVectorizer\n", + "\n", + "#CountVectorizer를 적용하기 위해 공백문자로 word단위가 구분되는 문자열로 변환\n", + "movies_df['genres_literal']=movies_df['genres'].apply(lambda x:(' ').join(x))\n", + "count_vect=CountVectorizer(min_df=0.0,ngram_range=(1,2))\n", + "genre_mat=count_vect.fit_transform(movies_df['genres_literal'])\n", + "print(genre_mat.shape)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6LKSJ_p4m3Af", + "outputId": "dc2fe349-8878-4142-addf-5c1c2f130ab2" + }, + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(4803, 276)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "from sklearn.metrics.pairwise import cosine_similarity\n", + "\n", + "genre_sim=cosine_similarity(genre_mat,genre_mat)\n", + "print(genre_sim.shape)\n", + "print(genre_sim[:1])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "HZQ-77n3ppAa", + "outputId": "39986ffd-9019-426d-b8d9-974044767f63" + }, + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(4803, 4803)\n", + "[[1. 0.59628479 0.4472136 ... 0. 0. 0. ]]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "genre_sim_sorted_ind=genre_sim.argsort()[:,::-1]\n", + "print(genre_sim_sorted_ind[:1])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "mUG2giy-pz7l", + "outputId": "a56b9c66-6983-4755-cee3-34d258ddbdcc" + }, + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[ 0 3494 813 ... 3038 3037 2401]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "<장르 콘텐츠 필터링을 이용한 영화 추천>" + ], + "metadata": { + "id": "GbvMhueTp7VS" + } + }, + { + "cell_type": "code", + "source": [ + "def find_sim_movie(df, sorted_ind, title_name, top_n=10):\n", + " # df, 기준인덱스, 기준영화, 10개 추천영화, 10개 추천\n", + "\n", + " # 인자로 입력된 movies_df DataFrame에서 'title' 컬럼이 입력된 title_name 값인 DataFrame추출\n", + " title_movie = df[df['title'] == title_name]\n", + "\n", + " # title_named을 가진 DataFrame의 index 객체를 ndarray로 반환하고\n", + " # sorted_ind 인자로 입력된 genre_sim_sorted_ind 객체에서 유사도 순으로 top_n 개의 index 추출\n", + " title_index = title_movie.index.values\n", + " similar_indexes = sorted_ind[title_index, :(top_n)]\n", + "\n", + " # 추출된 top_n index들 출력. top_n index는 2차원 데이터 임.\n", + " #dataframe에서 index로 사용하기 위해서 1차원 array로 변경\n", + " print(similar_indexes) # 코사인유사도가 가장 높은\n", + " similar_indexes = similar_indexes.reshape(-1)\n", + "\n", + " return df.iloc[similar_indexes]" + ], + "metadata": { + "id": "WAXXT1dyqA65" + }, + "execution_count": 15, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "similar_movies = find_sim_movie(movies_df, genre_sim_sorted_ind, 'The Godfather',10)\n", + "similar_movies[['title', 'vote_average']]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 381 + }, + "id": "kY17EuiHqE4T", + "outputId": "3c14bbfd-1628-4504-a7a0-2c19fb77a6c2" + }, + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[2731 1243 3636 1946 2640 4065 1847 4217 883 3866]]\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " title vote_average\n", + "2731 The Godfather: Part II 8.3\n", + "1243 Mean Streets 7.2\n", + "3636 Light Sleeper 5.7\n", + "1946 The Bad Lieutenant: Port of Call - New Orleans 6.0\n", + "2640 Things to Do in Denver When You're Dead 6.7\n", + "4065 Mi America 0.0\n", + "1847 GoodFellas 8.2\n", + "4217 Kids 6.8\n", + "883 Catch Me If You Can 7.7\n", + "3866 City of God 8.1" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
titlevote_average
2731The Godfather: Part II8.3
1243Mean Streets7.2
3636Light Sleeper5.7
1946The Bad Lieutenant: Port of Call - New Orleans6.0
2640Things to Do in Denver When You're Dead6.7
4065Mi America0.0
1847GoodFellas8.2
4217Kids6.8
883Catch Me If You Can7.7
3866City of God8.1
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"similar_movies[['title', 'vote_average']]\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Catch Me If You Can\",\n \"Mean Streets\",\n \"Mi America\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_average\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.4467892793981623,\n \"min\": 0.0,\n \"max\": 8.3,\n \"num_unique_values\": 10,\n \"samples\": [\n 7.7,\n 7.2,\n 0.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 16 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "=> [대부]와 비슷한 영화를 잘 추천해주지만 평점이 낮은 영화를 추천해주는 문제가 있음" + ], + "metadata": { + "id": "wRbJX7gJqJG4" + } + }, + { + "cell_type": "code", + "source": [ + "movies_df[['title','vote_average','vote_count']].sort_values('vote_average', ascending=False)[:10]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 363 + }, + "id": "vHh77CCJqWjw", + "outputId": "cc585fe0-7377-4838-ab89-083a570713a0" + }, + "execution_count": 17, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " title vote_average vote_count\n", + "3519 Stiff Upper Lips 10.0 1\n", + "4247 Me You and Five Bucks 10.0 2\n", + "4045 Dancer, Texas Pop. 81 10.0 1\n", + "4662 Little Big Top 10.0 1\n", + "3992 Sardaarji 9.5 2\n", + "2386 One Man's Hero 9.3 2\n", + "2970 There Goes My Baby 8.5 2\n", + "1881 The Shawshank Redemption 8.5 8205\n", + "2796 The Prisoner of Zenda 8.4 11\n", + "3337 The Godfather 8.4 5893" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
titlevote_averagevote_count
3519Stiff Upper Lips10.01
4247Me You and Five Bucks10.02
4045Dancer, Texas Pop. 8110.01
4662Little Big Top10.01
3992Sardaarji9.52
2386One Man's Hero9.32
2970There Goes My Baby8.52
1881The Shawshank Redemption8.58205
2796The Prisoner of Zenda8.411
3337The Godfather8.45893
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"movies_df[['title','vote_average','vote_count']]\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"The Prisoner of Zenda\",\n \"Me You and Five Bucks\",\n \"One Man's Hero\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_average\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.7366591251499343,\n \"min\": 8.4,\n \"max\": 10.0,\n \"num_unique_values\": 5,\n \"samples\": [\n 9.5,\n 8.4,\n 9.3\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_count\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3020,\n \"min\": 1,\n \"max\": 8205,\n \"num_unique_values\": 5,\n \"samples\": [\n 2,\n 5893,\n 8205\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 17 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "=> 왜곡된 평점 데이터가 반영되는 문제가 있으므로 가중 평점 방식을 도입해야함" + ], + "metadata": { + "id": "T4fCBFz5qajH" + } + }, + { + "cell_type": "code", + "source": [ + "C = movies_df['vote_average'].mean()\n", + "m = movies_df['vote_count'].quantile(0.6)\n", + "print('C:',round(C,3), 'm:',round(m,3))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "QXVWr86hqkzv", + "outputId": "fcd5a8fc-db14-499e-8776-8e535e0b19e2" + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "C: 6.092 m: 370.2\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "percentile = 0.6\n", + "m = movies_df['vote_count'].quantile(percentile)\n", + "C = movies_df['vote_average'].mean()\n", + "\n", + "def weighted_vote_average(record):\n", + " v = record['vote_count']\n", + " R = record['vote_average']\n", + "\n", + " return ( (v/(v+m)) * R ) + ( (m/(m+v)) * C )\n", + "\n", + "movies_df['weighted_vote'] = movies_df.apply(weighted_vote_average, axis=1)" + ], + "metadata": { + "id": "kvqmnRC4qnh3" + }, + "execution_count": 19, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "movies_df[['title','vote_average','weighted_vote','vote_count']].sort_values('weighted_vote',ascending=False)[:10]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 363 + }, + "id": "T1HcpovEqqZR", + "outputId": "4a0a0957-364c-47a1-ff95-1fedb0004b43" + }, + "execution_count": 20, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " title vote_average weighted_vote vote_count\n", + "1881 The Shawshank Redemption 8.5 8.396052 8205\n", + "3337 The Godfather 8.4 8.263591 5893\n", + "662 Fight Club 8.3 8.216455 9413\n", + "3232 Pulp Fiction 8.3 8.207102 8428\n", + "65 The Dark Knight 8.2 8.136930 12002\n", + "1818 Schindler's List 8.3 8.126069 4329\n", + "3865 Whiplash 8.3 8.123248 4254\n", + "809 Forrest Gump 8.2 8.105954 7927\n", + "2294 Spirited Away 8.3 8.105867 3840\n", + "2731 The Godfather: Part II 8.3 8.079586 3338" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
titlevote_averageweighted_votevote_count
1881The Shawshank Redemption8.58.3960528205
3337The Godfather8.48.2635915893
662Fight Club8.38.2164559413
3232Pulp Fiction8.38.2071028428
65The Dark Knight8.28.13693012002
1818Schindler's List8.38.1260694329
3865Whiplash8.38.1232484254
809Forrest Gump8.28.1059547927
2294Spirited Away8.38.1058673840
2731The Godfather: Part II8.38.0795863338
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"movies_df[['title','vote_average','weighted_vote','vote_count']]\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Spirited Away\",\n \"The Godfather\",\n \"Schindler's List\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_average\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.08755950357709151,\n \"min\": 8.2,\n \"max\": 8.5,\n \"num_unique_values\": 4,\n \"samples\": [\n 8.4,\n 8.2,\n 8.5\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"weighted_vote\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.09696608479450805,\n \"min\": 8.07958629828635,\n \"max\": 8.39605162693645,\n \"num_unique_values\": 10,\n \"samples\": [\n 8.105867158639835,\n 8.263590802034972,\n 8.126068673669016\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_count\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2866,\n \"min\": 3338,\n \"max\": 12002,\n \"num_unique_values\": 10,\n \"samples\": [\n 3840,\n 5893,\n 4329\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 20 + } + ] + }, + { + "cell_type": "code", + "source": [ + "def find_sim_movie(df, sorted_ind, title_name, top_n=10):\n", + " title_movie = df[df['title'] == title_name]\n", + " title_index = title_movie.index.values\n", + "\n", + " # top_n의 2배에 해당하는 쟝르 유사성이 높은 index 추출\n", + " similar_indexes = sorted_ind[title_index, :(top_n*2)] # 20개\n", + " similar_indexes = similar_indexes.reshape(-1)\n", + " # 기준 영화 index는 제외\n", + " similar_indexes = similar_indexes[similar_indexes != title_index]\n", + "\n", + " # top_n의 2배에 해당하는 후보군에서 weighted_vote 높은 순으로 top_n 만큼 추출\n", + " return df.iloc[similar_indexes].sort_values('weighted_vote', ascending=False)[:top_n]\n", + "\n", + "similar_movies = find_sim_movie(movies_df, genre_sim_sorted_ind, 'The Godfather',10)\n", + "similar_movies[['title', 'vote_average', 'weighted_vote']]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 363 + }, + "id": "2lcFHeBrqu4C", + "outputId": "380ae57c-999e-46a3-8f79-3c13e5282d50" + }, + "execution_count": 21, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " title vote_average weighted_vote\n", + "2731 The Godfather: Part II 8.3 8.079586\n", + "1847 GoodFellas 8.2 7.976937\n", + "3866 City of God 8.1 7.759693\n", + "1663 Once Upon a Time in America 8.2 7.657811\n", + "883 Catch Me If You Can 7.7 7.557097\n", + "281 American Gangster 7.4 7.141396\n", + "4041 This Is England 7.4 6.739664\n", + "1149 American Hustle 6.8 6.717525\n", + "1243 Mean Streets 7.2 6.626569\n", + "2839 Rounders 6.9 6.530427" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
titlevote_averageweighted_vote
2731The Godfather: Part II8.38.079586
1847GoodFellas8.27.976937
3866City of God8.17.759693
1663Once Upon a Time in America8.27.657811
883Catch Me If You Can7.77.557097
281American Gangster7.47.141396
4041This Is England7.46.739664
1149American Hustle6.86.717525
1243Mean Streets7.26.626569
2839Rounders6.96.530427
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"similar_movies[['title', 'vote_average', 'weighted_vote']]\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Mean Streets\",\n \"GoodFellas\",\n \"American Gangster\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vote_average\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.5613475849338901,\n \"min\": 6.8,\n \"max\": 8.3,\n \"num_unique_values\": 8,\n \"samples\": [\n 8.2,\n 6.8,\n 8.3\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"weighted_vote\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.5954507780784589,\n \"min\": 6.530427473190107,\n \"max\": 8.07958629828635,\n \"num_unique_values\": 10,\n \"samples\": [\n 6.626568667932654,\n 7.976937256676415,\n 7.1413961709782265\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 21 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "#9.6 아이템 기반 최근접 이웃 협업 필터링 실습" + ], + "metadata": { + "id": "aQZKUsshqysC" + } + }, + { + "cell_type": "markdown", + "source": [ + "<데이터 가공 및 변환>" + ], + "metadata": { + "id": "WwPnz1Ncro7a" + } + }, + { + "cell_type": "code", + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "movies=pd.read_csv('/content/movies.csv')\n", + "ratings=pd.read_csv('/content/ratings.csv')\n", + "print(movies.shape)\n", + "print(ratings.shape)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7QRxzeD1rq8b", + "outputId": "f033c1a6-434a-4682-bd9d-2f4ac4059cee" + }, + "execution_count": 23, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(9742, 3)\n", + "(100836, 4)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "ratings = ratings[['userId', 'movieId', 'rating']]\n", + "ratings_matrix = ratings.pivot_table('rating', index='userId', columns='movieId')\n", + "ratings_matrix.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 204 + }, + "id": "7P_imnL2sllr", + "outputId": "b58f9259-e561-47d2-9adc-c4c55b598e4c" + }, + "execution_count": 24, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "movieId 1 2 3 4 5 6 7 8 \\\n", + "userId \n", + "1 4.0 NaN 4.0 NaN NaN 4.0 NaN NaN \n", + "2 NaN NaN NaN NaN NaN NaN NaN NaN \n", + "3 NaN NaN NaN NaN NaN NaN NaN NaN \n", + "\n", + "movieId 9 10 ... 193565 193567 193571 193573 193579 193581 \\\n", + "userId ... \n", + "1 NaN NaN ... NaN NaN NaN NaN NaN NaN \n", + "2 NaN NaN ... NaN NaN NaN NaN NaN NaN \n", + "3 NaN NaN ... NaN NaN NaN NaN NaN NaN \n", + "\n", + "movieId 193583 193585 193587 193609 \n", + "userId \n", + "1 NaN NaN NaN NaN \n", + "2 NaN NaN NaN NaN \n", + "3 NaN NaN NaN NaN \n", + "\n", + "[3 rows x 9724 columns]" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
movieId12345678910...193565193567193571193573193579193581193583193585193587193609
userId
14.0NaN4.0NaNNaN4.0NaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
3NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
\n", + "

3 rows × 9724 columns

\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "ratings_matrix" + } + }, + "metadata": {}, + "execution_count": 24 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#title 칼럼을 얻기 위해 movies와 조인\n", + "rating_movies=pd.merge(ratings,movies,on='movieId')\n", + "\n", + "#columns='title'로 title 칼럼으로 피벗 수행\n", + "ratings_matrix=rating_movies.pivot_table('rating',index='userId',columns='title')\n", + "\n", + "#NaN값을 모두 0으로 변환\n", + "ratings_matrix=ratings_matrix.fillna(0)\n", + "ratings_matrix.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 346 + }, + "id": "gdn4LUq5sny4", + "outputId": "81e4b425-723f-422b-fbea-5186828c2934" + }, + "execution_count": 25, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "title '71 (2014) 'Hellboy': The Seeds of Creation (2004) \\\n", + "userId \n", + "1 0.0 0.0 \n", + "2 0.0 0.0 \n", + "3 0.0 0.0 \n", + "\n", + "title 'Round Midnight (1986) 'Salem's Lot (2004) \\\n", + "userId \n", + "1 0.0 0.0 \n", + "2 0.0 0.0 \n", + "3 0.0 0.0 \n", + "\n", + "title 'Til There Was You (1997) 'Tis the Season for Love (2015) \\\n", + "userId \n", + "1 0.0 0.0 \n", + "2 0.0 0.0 \n", + "3 0.0 0.0 \n", + "\n", + "title 'burbs, The (1989) 'night Mother (1986) (500) Days of Summer (2009) \\\n", + "userId \n", + "1 0.0 0.0 0.0 \n", + "2 0.0 0.0 0.0 \n", + "3 0.0 0.0 0.0 \n", + "\n", + "title *batteries not included (1987) ... Zulu (2013) [REC] (2007) \\\n", + "userId ... \n", + "1 0.0 ... 0.0 0.0 \n", + "2 0.0 ... 0.0 0.0 \n", + "3 0.0 ... 0.0 0.0 \n", + "\n", + "title [REC]² (2009) [REC]³ 3 Génesis (2012) \\\n", + "userId \n", + "1 0.0 0.0 \n", + "2 0.0 0.0 \n", + "3 0.0 0.0 \n", + "\n", + "title anohana: The Flower We Saw That Day - The Movie (2013) \\\n", + "userId \n", + "1 0.0 \n", + "2 0.0 \n", + "3 0.0 \n", + "\n", + "title eXistenZ (1999) xXx (2002) xXx: State of the Union (2005) \\\n", + "userId \n", + "1 0.0 0.0 0.0 \n", + "2 0.0 0.0 0.0 \n", + "3 0.0 0.0 0.0 \n", + "\n", + "title ¡Three Amigos! (1986) À nous la liberté (Freedom for Us) (1931) \n", + "userId \n", + "1 4.0 0.0 \n", + "2 0.0 0.0 \n", + "3 0.0 0.0 \n", + "\n", + "[3 rows x 9719 columns]" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
title'71 (2014)'Hellboy': The Seeds of Creation (2004)'Round Midnight (1986)'Salem's Lot (2004)'Til There Was You (1997)'Tis the Season for Love (2015)'burbs, The (1989)'night Mother (1986)(500) Days of Summer (2009)*batteries not included (1987)...Zulu (2013)[REC] (2007)[REC]² (2009)[REC]³ 3 Génesis (2012)anohana: The Flower We Saw That Day - The Movie (2013)eXistenZ (1999)xXx (2002)xXx: State of the Union (2005)¡Three Amigos! (1986)À nous la liberté (Freedom for Us) (1931)
userId
10.00.00.00.00.00.00.00.00.00.0...0.00.00.00.00.00.00.00.04.00.0
20.00.00.00.00.00.00.00.00.00.0...0.00.00.00.00.00.00.00.00.00.0
30.00.00.00.00.00.00.00.00.00.0...0.00.00.00.00.00.00.00.00.00.0
\n", + "

3 rows × 9719 columns

\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "ratings_matrix" + } + }, + "metadata": {}, + "execution_count": 25 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "<영화 간 유사도 산출>" + ], + "metadata": { + "id": "ah47hAHss_4O" + } + }, + { + "cell_type": "code", + "source": [ + "ratings_matrix_T=ratings_matrix.transpose()\n", + "ratings_matrix_T.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 204 + }, + "id": "LDQcHg6atCQz", + "outputId": "1a5035e8-b494-4622-ac72-288332173ab4" + }, + "execution_count": 26, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "userId 1 2 3 4 5 6 7 \\\n", + "title \n", + "'71 (2014) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n", + "'Round Midnight (1986) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n", + "\n", + "userId 8 9 10 ... 601 602 603 \\\n", + "title ... \n", + "'71 (2014) 0.0 0.0 0.0 ... 0.0 0.0 0.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 0.0 0.0 ... 0.0 0.0 0.0 \n", + "'Round Midnight (1986) 0.0 0.0 0.0 ... 0.0 0.0 0.0 \n", + "\n", + "userId 604 605 606 607 608 609 610 \n", + "title \n", + "'71 (2014) 0.0 0.0 0.0 0.0 0.0 0.0 4.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n", + "'Round Midnight (1986) 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n", + "\n", + "[3 rows x 610 columns]" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
userId12345678910...601602603604605606607608609610
title
'71 (2014)0.00.00.00.00.00.00.00.00.00.0...0.00.00.00.00.00.00.00.00.04.0
'Hellboy': The Seeds of Creation (2004)0.00.00.00.00.00.00.00.00.00.0...0.00.00.00.00.00.00.00.00.00.0
'Round Midnight (1986)0.00.00.00.00.00.00.00.00.00.0...0.00.00.00.00.00.00.00.00.00.0
\n", + "

3 rows × 610 columns

\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "ratings_matrix_T" + } + }, + "metadata": {}, + "execution_count": 26 + } + ] + }, + { + "cell_type": "code", + "source": [ + "from sklearn.metrics.pairwise import cosine_similarity\n", + "\n", + "item_sim=cosine_similarity(ratings_matrix_T,ratings_matrix_T)\n", + "\n", + "#cosine_similarity()로 반환된 넘파이 행렬을 영화명을 매핑해 DataFrame으로 변환\n", + "item_sim_df=pd.DataFrame(data=item_sim,index=ratings_matrix.columns,columns=ratings_matrix.columns)\n", + "print(item_sim_df.shape)\n", + "item_sim_df.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 485 + }, + "id": "yFOtOKTctIof", + "outputId": "d63a63aa-1a4e-4bfc-9719-cbd256d7ad74" + }, + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(9719, 9719)\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "title '71 (2014) \\\n", + "title \n", + "'71 (2014) 1.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 \n", + "'Round Midnight (1986) 0.0 \n", + "\n", + "title 'Hellboy': The Seeds of Creation (2004) \\\n", + "title \n", + "'71 (2014) 0.000000 \n", + "'Hellboy': The Seeds of Creation (2004) 1.000000 \n", + "'Round Midnight (1986) 0.707107 \n", + "\n", + "title 'Round Midnight (1986) \\\n", + "title \n", + "'71 (2014) 0.000000 \n", + "'Hellboy': The Seeds of Creation (2004) 0.707107 \n", + "'Round Midnight (1986) 1.000000 \n", + "\n", + "title 'Salem's Lot (2004) \\\n", + "title \n", + "'71 (2014) 0.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 \n", + "'Round Midnight (1986) 0.0 \n", + "\n", + "title 'Til There Was You (1997) \\\n", + "title \n", + "'71 (2014) 0.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 \n", + "'Round Midnight (1986) 0.0 \n", + "\n", + "title 'Tis the Season for Love (2015) \\\n", + "title \n", + "'71 (2014) 0.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 \n", + "'Round Midnight (1986) 0.0 \n", + "\n", + "title 'burbs, The (1989) \\\n", + "title \n", + "'71 (2014) 0.000000 \n", + "'Hellboy': The Seeds of Creation (2004) 0.000000 \n", + "'Round Midnight (1986) 0.176777 \n", + "\n", + "title 'night Mother (1986) \\\n", + "title \n", + "'71 (2014) 0.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 \n", + "'Round Midnight (1986) 0.0 \n", + "\n", + "title (500) Days of Summer (2009) \\\n", + "title \n", + "'71 (2014) 0.141653 \n", + "'Hellboy': The Seeds of Creation (2004) 0.000000 \n", + "'Round Midnight (1986) 0.000000 \n", + "\n", + "title *batteries not included (1987) ... \\\n", + "title ... \n", + "'71 (2014) 0.0 ... \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 ... \n", + "'Round Midnight (1986) 0.0 ... \n", + "\n", + "title Zulu (2013) [REC] (2007) \\\n", + "title \n", + "'71 (2014) 0.0 0.342055 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 0.000000 \n", + "'Round Midnight (1986) 0.0 0.000000 \n", + "\n", + "title [REC]² (2009) \\\n", + "title \n", + "'71 (2014) 0.543305 \n", + "'Hellboy': The Seeds of Creation (2004) 0.000000 \n", + "'Round Midnight (1986) 0.000000 \n", + "\n", + "title [REC]³ 3 Génesis (2012) \\\n", + "title \n", + "'71 (2014) 0.707107 \n", + "'Hellboy': The Seeds of Creation (2004) 0.000000 \n", + "'Round Midnight (1986) 0.000000 \n", + "\n", + "title anohana: The Flower We Saw That Day - The Movie (2013) \\\n", + "title \n", + "'71 (2014) 0.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 \n", + "'Round Midnight (1986) 0.0 \n", + "\n", + "title eXistenZ (1999) xXx (2002) \\\n", + "title \n", + "'71 (2014) 0.0 0.139431 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 0.000000 \n", + "'Round Midnight (1986) 0.0 0.000000 \n", + "\n", + "title xXx: State of the Union (2005) \\\n", + "title \n", + "'71 (2014) 0.327327 \n", + "'Hellboy': The Seeds of Creation (2004) 0.000000 \n", + "'Round Midnight (1986) 0.000000 \n", + "\n", + "title ¡Three Amigos! (1986) \\\n", + "title \n", + "'71 (2014) 0.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 \n", + "'Round Midnight (1986) 0.0 \n", + "\n", + "title À nous la liberté (Freedom for Us) (1931) \n", + "title \n", + "'71 (2014) 0.0 \n", + "'Hellboy': The Seeds of Creation (2004) 0.0 \n", + "'Round Midnight (1986) 0.0 \n", + "\n", + "[3 rows x 9719 columns]" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
title'71 (2014)'Hellboy': The Seeds of Creation (2004)'Round Midnight (1986)'Salem's Lot (2004)'Til There Was You (1997)'Tis the Season for Love (2015)'burbs, The (1989)'night Mother (1986)(500) Days of Summer (2009)*batteries not included (1987)...Zulu (2013)[REC] (2007)[REC]² (2009)[REC]³ 3 Génesis (2012)anohana: The Flower We Saw That Day - The Movie (2013)eXistenZ (1999)xXx (2002)xXx: State of the Union (2005)¡Three Amigos! (1986)À nous la liberté (Freedom for Us) (1931)
title
'71 (2014)1.00.0000000.0000000.00.00.00.0000000.00.1416530.0...0.00.3420550.5433050.7071070.00.00.1394310.3273270.00.0
'Hellboy': The Seeds of Creation (2004)0.01.0000000.7071070.00.00.00.0000000.00.0000000.0...0.00.0000000.0000000.0000000.00.00.0000000.0000000.00.0
'Round Midnight (1986)0.00.7071071.0000000.00.00.00.1767770.00.0000000.0...0.00.0000000.0000000.0000000.00.00.0000000.0000000.00.0
\n", + "

3 rows × 9719 columns

\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "item_sim_df" + } + }, + "metadata": {}, + "execution_count": 29 + } + ] + }, + { + "cell_type": "code", + "source": [ + "item_sim_df[\"Godfather, The (1972)\"].sort_values(ascending=False)[:6]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 304 + }, + "id": "TdbHYBD0ul7A", + "outputId": "0291853e-1a1e-42cb-d335-0b727eec6d93" + }, + "execution_count": 30, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "title\n", + "Godfather, The (1972) 1.000000\n", + "Godfather: Part II, The (1974) 0.821773\n", + "Goodfellas (1990) 0.664841\n", + "One Flew Over the Cuckoo's Nest (1975) 0.620536\n", + "Star Wars: Episode IV - A New Hope (1977) 0.595317\n", + "Fargo (1996) 0.588614\n", + "Name: Godfather, The (1972), dtype: float64" + ], + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Godfather, The (1972)
title
Godfather, The (1972)1.000000
Godfather: Part II, The (1974)0.821773
Goodfellas (1990)0.664841
One Flew Over the Cuckoo's Nest (1975)0.620536
Star Wars: Episode IV - A New Hope (1977)0.595317
Fargo (1996)0.588614
\n", + "

" + ] + }, + "metadata": {}, + "execution_count": 30 + } + ] + }, + { + "cell_type": "code", + "source": [ + "item_sim_df[\"Inception (2010)\"].sort_values(ascending=False)[1:6]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 272 + }, + "id": "EzjnfJuEup2_", + "outputId": "aab5f182-d819-4ccf-d43a-8205ebb97ebe" + }, + "execution_count": 31, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "title\n", + "Dark Knight, The (2008) 0.727263\n", + "Inglourious Basterds (2009) 0.646103\n", + "Shutter Island (2010) 0.617736\n", + "Dark Knight Rises, The (2012) 0.617504\n", + "Fight Club (1999) 0.615417\n", + "Name: Inception (2010), dtype: float64" + ], + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Inception (2010)
title
Dark Knight, The (2008)0.727263
Inglourious Basterds (2009)0.646103
Shutter Island (2010)0.617736
Dark Knight Rises, The (2012)0.617504
Fight Club (1999)0.615417
\n", + "

" + ] + }, + "metadata": {}, + "execution_count": 31 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "<아이템 기반 최근접 이웃 협업 필터링으로 개인화된 영화 추천>" + ], + "metadata": { + "id": "-dQXQbayvG_u" + } + }, + { + "cell_type": "code", + "source": [ + "def predict_rating(ratings_arr,item_sim_arr):\n", + " ratings_pred=ratings_arr.dot(item_sim_arr)/np.array([np.abs(item_sim_arr).sum(axis=1)])\n", + " return ratings_pred" + ], + "metadata": { + "id": "4AoYLUQ5vLQc" + }, + "execution_count": 32, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "ratings_pred = predict_rating(ratings_matrix.values , item_sim_df.values)\n", + "ratings_pred_matrix = pd.DataFrame(data=ratings_pred, index= ratings_matrix.index, columns = ratings_matrix.columns)\n", + "ratings_pred_matrix.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 346 + }, + "id": "g_JimBjMvcho", + "outputId": "d7bcd798-e542-4211-fee9-744b900508f3" + }, + "execution_count": 33, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "title '71 (2014) 'Hellboy': The Seeds of Creation (2004) \\\n", + "userId \n", + "1 0.070345 0.577855 \n", + "2 0.018260 0.042744 \n", + "3 0.011884 0.030279 \n", + "\n", + "title 'Round Midnight (1986) 'Salem's Lot (2004) \\\n", + "userId \n", + "1 0.321696 0.227055 \n", + "2 0.018861 0.000000 \n", + "3 0.064437 0.003762 \n", + "\n", + "title 'Til There Was You (1997) 'Tis the Season for Love (2015) \\\n", + "userId \n", + "1 0.206958 0.194615 \n", + "2 0.000000 0.035995 \n", + "3 0.003749 0.002722 \n", + "\n", + "title 'burbs, The (1989) 'night Mother (1986) (500) Days of Summer (2009) \\\n", + "userId \n", + "1 0.249883 0.102542 0.157084 \n", + "2 0.013413 0.002314 0.032213 \n", + "3 0.014625 0.002085 0.005666 \n", + "\n", + "title *batteries not included (1987) ... Zulu (2013) [REC] (2007) \\\n", + "userId ... \n", + "1 0.178197 ... 0.113608 0.181738 \n", + "2 0.014863 ... 0.015640 0.020855 \n", + "3 0.006272 ... 0.006923 0.011665 \n", + "\n", + "title [REC]² (2009) [REC]³ 3 Génesis (2012) \\\n", + "userId \n", + "1 0.133962 0.128574 \n", + "2 0.020119 0.015745 \n", + "3 0.011800 0.012225 \n", + "\n", + "title anohana: The Flower We Saw That Day - The Movie (2013) \\\n", + "userId \n", + "1 0.006179 \n", + "2 0.049983 \n", + "3 0.000000 \n", + "\n", + "title eXistenZ (1999) xXx (2002) xXx: State of the Union (2005) \\\n", + "userId \n", + "1 0.212070 0.192921 0.136024 \n", + "2 0.014876 0.021616 0.024528 \n", + "3 0.008194 0.007017 0.009229 \n", + "\n", + "title ¡Three Amigos! (1986) À nous la liberté (Freedom for Us) (1931) \n", + "userId \n", + "1 0.292955 0.720347 \n", + "2 0.017563 0.000000 \n", + "3 0.010420 0.084501 \n", + "\n", + "[3 rows x 9719 columns]" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
title'71 (2014)'Hellboy': The Seeds of Creation (2004)'Round Midnight (1986)'Salem's Lot (2004)'Til There Was You (1997)'Tis the Season for Love (2015)'burbs, The (1989)'night Mother (1986)(500) Days of Summer (2009)*batteries not included (1987)...Zulu (2013)[REC] (2007)[REC]² (2009)[REC]³ 3 Génesis (2012)anohana: The Flower We Saw That Day - The Movie (2013)eXistenZ (1999)xXx (2002)xXx: State of the Union (2005)¡Three Amigos! (1986)À nous la liberté (Freedom for Us) (1931)
userId
10.0703450.5778550.3216960.2270550.2069580.1946150.2498830.1025420.1570840.178197...0.1136080.1817380.1339620.1285740.0061790.2120700.1929210.1360240.2929550.720347
20.0182600.0427440.0188610.0000000.0000000.0359950.0134130.0023140.0322130.014863...0.0156400.0208550.0201190.0157450.0499830.0148760.0216160.0245280.0175630.000000
30.0118840.0302790.0644370.0037620.0037490.0027220.0146250.0020850.0056660.006272...0.0069230.0116650.0118000.0122250.0000000.0081940.0070170.0092290.0104200.084501
\n", + "

3 rows × 9719 columns

\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "ratings_pred_matrix" + } + }, + "metadata": {}, + "execution_count": 33 + } + ] + }, + { + "cell_type": "code", + "source": [ + "from sklearn.metrics import mean_squared_error\n", + "\n", + "#사용자가 평점을 부여한 영화에 대해서만 예측 성능 평가 MSE를 구함\n", + "def get_mse(pred,actual):\n", + " #평점이 있는 실제 영화만 추출\n", + " pred=pred[actual.nonzero()].flatten()\n", + " actual=actual[actual.nonzero()].flatten()\n", + " return mean_squared_error(pred,actual)\n", + "\n", + "print('아이템 기반 모든 최근접 이웃 MSE:',get_mse(ratings_pred,ratings_matrix.values))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "1ZGRil9Nvg1v", + "outputId": "4fdb4e90-93eb-4217-832f-00c1710623c5" + }, + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "아이템 기반 모든 최근접 이웃 MSE: 9.895354759094706\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "def predict_rating_topsim(ratings_arr, item_sim_arr, n=20):\n", + " # 사용자-아이템 평점 행렬 크기만큼 0으로 채운 예측 행렬 초기화\n", + " pred = np.zeros(ratings_arr.shape)\n", + "\n", + " # 사용자-아이템 평점 행렬의 열 크기만큼 Loop 수행.\n", + " for col in range(ratings_arr.shape[1]):\n", + " # 유사도 행렬에서 유사도가 큰 순으로 n개 데이터 행렬의 index 반환\n", + " top_n_items = [np.argsort(item_sim_arr[:, col])[:-n-1:-1]]\n", + " # 개인화된 예측 평점을 계산\n", + " for row in range(ratings_arr.shape[0]):\n", + " pred[row, col] = item_sim_arr[col, :][top_n_items].dot(ratings_arr[row, :][top_n_items].T)\n", + " pred[row, col] /= np.sum(np.abs(item_sim_arr[col, :][top_n_items]))\n", + " return pred" + ], + "metadata": { + "id": "BiM2KCrHwDRR" + }, + "execution_count": 35, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "ratings_pred=predict_rating_topsim(ratings_matrix.values,item_sim_df.values,n=20)\n", + "print('아이템 기반 최근접 TOP-20 이웃 MSE:',get_mse(ratings_pred,ratings_matrix.values))\n", + "\n", + "#계산된 예측 평점 데이터는 DataFrame으로 재생성\n", + "ratings_pred_matrix=pd.DataFrame(data=ratings_pred,index=ratings_matrix.index,columns=ratings_matrix.columns)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "MVXcxgWgwEcD", + "outputId": "439388aa-c7d1-4258-f7db-ec5be8cd876d" + }, + "execution_count": 36, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "아이템 기반 최근접 TOP-20 이웃 MSE: 3.6949827608772314\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "user_rating_id=ratings_matrix.loc[9,:]\n", + "user_rating_id[user_rating_id>0].sort_values(ascending=False)[:10]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 429 + }, + "id": "BE6mVEQgw1us", + "outputId": "b28455f5-bdc6-429f-b3ef-7da9f31a6492" + }, + "execution_count": 37, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "title\n", + "Adaptation (2002) 5.0\n", + "Citizen Kane (1941) 5.0\n", + "Raiders of the Lost Ark (Indiana Jones and the Raiders of the Lost Ark) (1981) 5.0\n", + "Producers, The (1968) 5.0\n", + "Lord of the Rings: The Two Towers, The (2002) 5.0\n", + "Lord of the Rings: The Fellowship of the Ring, The (2001) 5.0\n", + "Back to the Future (1985) 5.0\n", + "Austin Powers in Goldmember (2002) 5.0\n", + "Minority Report (2002) 4.0\n", + "Witness (1985) 4.0\n", + "Name: 9, dtype: float64" + ], + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
9
title
Adaptation (2002)5.0
Citizen Kane (1941)5.0
Raiders of the Lost Ark (Indiana Jones and the Raiders of the Lost Ark) (1981)5.0
Producers, The (1968)5.0
Lord of the Rings: The Two Towers, The (2002)5.0
Lord of the Rings: The Fellowship of the Ring, The (2001)5.0
Back to the Future (1985)5.0
Austin Powers in Goldmember (2002)5.0
Minority Report (2002)4.0
Witness (1985)4.0
\n", + "

" + ] + }, + "metadata": {}, + "execution_count": 37 + } + ] + }, + { + "cell_type": "code", + "source": [ + "def get_unseen_movies(ratings_matrix, userId):\n", + " # userId로 입력받은 사용자의 모든 영화정보 추출하여 Series로 반환함.\n", + " # 반환된 user_rating 은 영화명(title)을 index로 가지는 Series 객체임.\n", + " user_rating = ratings_matrix.loc[userId,:]\n", + "\n", + " # user_rating이 0보다 크면 기존에 관람한 영화임. 대상 index를 추출하여 list 객체로 만듬\n", + " already_seen = user_rating[ user_rating > 0].index.tolist()\n", + "\n", + " # 모든 영화명을 list 객체로 만듬.\n", + " movies_list = ratings_matrix.columns.tolist()\n", + "\n", + " # list comprehension으로 already_seen에 해당하는 movie는 movies_list에서 제외함.\n", + " unseen_list = [ movie for movie in movies_list if movie not in already_seen]\n", + "\n", + " return unseen_list" + ], + "metadata": { + "id": "e8lOm6yWxDgb" + }, + "execution_count": 38, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def recomm_movie_by_userid(pred_df, userId, unseen_list, top_n=10):\n", + " # 예측 평점 DataFrame에서 사용자id index와 unseen_list로 들어온 영화명 컬럼을 추출하여\n", + " # 가장 예측 평점이 높은 순으로 정렬함.\n", + " recomm_movies = pred_df.loc[userId, unseen_list].sort_values(ascending=False)[:top_n]\n", + " return recomm_movies\n", + "\n", + "# 사용자가 관람하지 않는 영화명 추출\n", + "unseen_list = get_unseen_movies(ratings_matrix, 9)\n", + "\n", + "# 아이템 기반의 인접 이웃 협업 필터링으로 영화 추천\n", + "recomm_movies = recomm_movie_by_userid(ratings_pred_matrix, 9, unseen_list, top_n=10)\n", + "\n", + "# 평점 데이타를 DataFrame으로 생성.\n", + "recomm_movies = pd.DataFrame(data=recomm_movies.values,index=recomm_movies.index,columns=['pred_score'])\n", + "recomm_movies" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 394 + }, + "id": "eI95Q1fKxGlF", + "outputId": "b531b7f1-9ebf-4f91-c6e5-8595da627eb0" + }, + "execution_count": 39, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " pred_score\n", + "title \n", + "Shrek (2001) 0.866202\n", + "Spider-Man (2002) 0.857854\n", + "Last Samurai, The (2003) 0.817473\n", + "Indiana Jones and the Temple of Doom (1984) 0.816626\n", + "Matrix Reloaded, The (2003) 0.800990\n", + "Harry Potter and the Sorcerer's Stone (a.k.a. Harry Potter and the Philosopher's Stone) (2001) 0.765159\n", + "Gladiator (2000) 0.740956\n", + "Matrix, The (1999) 0.732693\n", + "Pirates of the Caribbean: The Curse of the Black Pearl (2003) 0.689591\n", + "Lord of the Rings: The Return of the King, The (2003) 0.676711" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
pred_score
title
Shrek (2001)0.866202
Spider-Man (2002)0.857854
Last Samurai, The (2003)0.817473
Indiana Jones and the Temple of Doom (1984)0.816626
Matrix Reloaded, The (2003)0.800990
Harry Potter and the Sorcerer's Stone (a.k.a. Harry Potter and the Philosopher's Stone) (2001)0.765159
Gladiator (2000)0.740956
Matrix, The (1999)0.732693
Pirates of the Caribbean: The Curse of the Black Pearl (2003)0.689591
Lord of the Rings: The Return of the King, The (2003)0.676711
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "recomm_movies", + "summary": "{\n \"name\": \"recomm_movies\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Pirates of the Caribbean: The Curse of the Black Pearl (2003)\",\n \"Spider-Man (2002)\",\n \"Harry Potter and the Sorcerer's Stone (a.k.a. Harry Potter and the Philosopher's Stone) (2001)\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"pred_score\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.06614432811511851,\n \"min\": 0.6767108283499336,\n \"max\": 0.8662018746933645,\n \"num_unique_values\": 10,\n \"samples\": [\n 0.6895905595608812,\n 0.8578535950426878,\n 0.7651586070058114\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 39 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "#9.7 행렬 분해를 이용한 잠재 요인 협업 필터링 실습" + ], + "metadata": { + "id": "Ki6XuyM7xIS5" + } + }, + { + "cell_type": "markdown", + "source": [ + "+) 4장 참고" + ], + "metadata": { + "id": "khee-wysx86e" + } + }, + { + "cell_type": "code", + "source": [ + "from sklearn.metrics import mean_squared_error\n", + "\n", + "def get_rmse(R, P, Q, non_zeros):\n", + " error = 0\n", + " # 두개의 분해된 행렬 P와 Q.T의 내적으로 예측 R 행렬 생성\n", + " full_pred_matrix = np.dot(P, Q.T)\n", + "\n", + " # 실제 R 행렬에서 널이 아닌 값의 위치 인덱스 추출하여 실제 R 행렬과 예측 행렬의 RMSE 추출\n", + " x_non_zero_ind = [non_zero[0] for non_zero in non_zeros]\n", + " y_non_zero_ind = [non_zero[1] for non_zero in non_zeros]\n", + " R_non_zeros = R[x_non_zero_ind, y_non_zero_ind]\n", + " full_pred_matrix_non_zeros = full_pred_matrix[x_non_zero_ind, y_non_zero_ind]\n", + "\n", + " mse = mean_squared_error(R_non_zeros, full_pred_matrix_non_zeros)\n", + " rmse = np.sqrt(mse)\n", + "\n", + " return rmse" + ], + "metadata": { + "id": "ZETawaN5yg2V" + }, + "execution_count": 44, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def matrix_factorization(R, K, steps=200, learning_rate=0.01, r_lambda = 0.01):\n", + " num_users, num_items = R.shape\n", + " # P와 Q 매트릭스의 크기를 지정하고 정규분포를 가진 랜덤한 값으로 입력합니다.\n", + " np.random.seed(1)\n", + " P = np.random.normal(scale=1./K, size=(num_users, K))\n", + " Q = np.random.normal(scale=1./K, size=(num_items, K))\n", + "\n", + " break_count = 0\n", + "\n", + " # R > 0 인 행 위치, 열 위치, 값을 non_zeros 리스트 객체에 저장.\n", + " non_zeros = [ (i, j, R[i,j]) for i in range(num_users) for j in range(num_items) if R[i,j] > 0 ]\n", + "\n", + " # SGD기법으로 P와 Q 매트릭스를 계속 업데이트.\n", + " for step in range(steps):\n", + " for i, j, r in non_zeros:\n", + " # 실제 값과 예측 값의 차이인 오류 값 구함\n", + " eij = r - np.dot(P[i, :], Q[j, :].T)\n", + " # Regularization을 반영한 SGD 업데이트 공식 적용\n", + " P[i,:] = P[i,:] + learning_rate*(eij * Q[j, :] - r_lambda*P[i,:])\n", + " Q[j,:] = Q[j,:] + learning_rate*(eij * P[i, :] - r_lambda*Q[j,:])\n", + "\n", + " rmse = get_rmse(R, P, Q, non_zeros)\n", + " if (step % 10) == 0 :\n", + " print(\"### iteration step : \", step,\" rmse : \", rmse)\n", + "\n", + " return P, Q" + ], + "metadata": { + "id": "aH_1Apo8x7bi" + }, + "execution_count": 45, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "movies = pd.read_csv('/content/movies.csv')\n", + "ratings = pd.read_csv('/content/ratings.csv')\n", + "ratings = ratings[['userId', 'movieId', 'rating']]\n", + "ratings_matrix = ratings.pivot_table('rating', index='userId', columns='movieId')\n", + "\n", + "# title 컬럼을 얻기 이해 movies 와 조인 수행\n", + "rating_movies = pd.merge(ratings, movies, on='movieId')\n", + "\n", + "# columns='title' 로 title 컬럼으로 pivot 수행.\n", + "ratings_matrix = rating_movies.pivot_table('rating', index='userId', columns='title')" + ], + "metadata": { + "id": "ejv6NwPRyCwE" + }, + "execution_count": 42, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "P,Q=matrix_factorization(ratings_matrix.values,K=50,steps=200,learning_rate=0.01,r_lambda=0.01)\n", + "pred_matrix=np.dot(P,Q.T)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "oReuunDdyMwD", + "outputId": "8a721f28-9bb8-4bd1-d3fd-2a74320bd38a" + }, + "execution_count": 47, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "### iteration step : 0 rmse : 2.9023619751336867\n", + "### iteration step : 10 rmse : 0.7335768591017927\n", + "### iteration step : 20 rmse : 0.5115539026853442\n", + "### iteration step : 30 rmse : 0.37261628282537446\n", + "### iteration step : 40 rmse : 0.2960818299181014\n", + "### iteration step : 50 rmse : 0.2520353192341642\n", + "### iteration step : 60 rmse : 0.22487503275269854\n", + "### iteration step : 70 rmse : 0.2068545530233154\n", + "### iteration step : 80 rmse : 0.19413418783028685\n", + "### iteration step : 90 rmse : 0.18470082002720406\n", + "### iteration step : 100 rmse : 0.17742927527209104\n", + "### iteration step : 110 rmse : 0.1716522696470749\n", + "### iteration step : 120 rmse : 0.16695181946871726\n", + "### iteration step : 130 rmse : 0.16305292191997542\n", + "### iteration step : 140 rmse : 0.15976691929679646\n", + "### iteration step : 150 rmse : 0.1569598699945732\n", + "### iteration step : 160 rmse : 0.15453398186715425\n", + "### iteration step : 170 rmse : 0.15241618551077643\n", + "### iteration step : 180 rmse : 0.1505508073962831\n", + "### iteration step : 190 rmse : 0.1488947091323209\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "ratings_pred_matrix=pd.DataFrame(data=pred_matrix,index=ratings_matrix.index,columns=ratings_matrix.columns)\n", + "ratings_pred_matrix.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 346 + }, + "id": "PikrAU-Z0rWL", + "outputId": "c9bd16d8-107d-4767-b433-45c972336708" + }, + "execution_count": 48, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "title '71 (2014) 'Hellboy': The Seeds of Creation (2004) \\\n", + "userId \n", + "1 3.055084 4.092018 \n", + "2 3.170119 3.657992 \n", + "3 2.307073 1.658853 \n", + "\n", + "title 'Round Midnight (1986) 'Salem's Lot (2004) \\\n", + "userId \n", + "1 3.564130 4.502167 \n", + "2 3.308707 4.166521 \n", + "3 1.443538 2.208859 \n", + "\n", + "title 'Til There Was You (1997) 'Tis the Season for Love (2015) \\\n", + "userId \n", + "1 3.981215 1.271694 \n", + "2 4.311890 1.275469 \n", + "3 2.229486 0.780760 \n", + "\n", + "title 'burbs, The (1989) 'night Mother (1986) (500) Days of Summer (2009) \\\n", + "userId \n", + "1 3.603274 2.333266 5.091749 \n", + "2 4.237972 1.900366 3.392859 \n", + "3 1.997043 0.924908 2.970700 \n", + "\n", + "title *batteries not included (1987) ... Zulu (2013) [REC] (2007) \\\n", + "userId ... \n", + "1 3.972454 ... 1.402608 4.208382 \n", + "2 3.647421 ... 0.973811 3.528264 \n", + "3 2.551446 ... 0.520354 1.709494 \n", + "\n", + "title [REC]² (2009) [REC]³ 3 Génesis (2012) \\\n", + "userId \n", + "1 3.705957 2.720514 \n", + "2 3.361532 2.672535 \n", + "3 2.281596 1.782833 \n", + "\n", + "title anohana: The Flower We Saw That Day - The Movie (2013) \\\n", + "userId \n", + "1 2.787331 \n", + "2 2.404456 \n", + "3 1.635173 \n", + "\n", + "title eXistenZ (1999) xXx (2002) xXx: State of the Union (2005) \\\n", + "userId \n", + "1 3.475076 3.253458 2.161087 \n", + "2 4.232789 2.911602 1.634576 \n", + "3 1.323276 2.887580 1.042618 \n", + "\n", + "title ¡Three Amigos! (1986) À nous la liberté (Freedom for Us) (1931) \n", + "userId \n", + "1 4.010495 0.859474 \n", + "2 4.135735 0.725684 \n", + "3 2.293890 0.396941 \n", + "\n", + "[3 rows x 9719 columns]" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
title'71 (2014)'Hellboy': The Seeds of Creation (2004)'Round Midnight (1986)'Salem's Lot (2004)'Til There Was You (1997)'Tis the Season for Love (2015)'burbs, The (1989)'night Mother (1986)(500) Days of Summer (2009)*batteries not included (1987)...Zulu (2013)[REC] (2007)[REC]² (2009)[REC]³ 3 Génesis (2012)anohana: The Flower We Saw That Day - The Movie (2013)eXistenZ (1999)xXx (2002)xXx: State of the Union (2005)¡Three Amigos! (1986)À nous la liberté (Freedom for Us) (1931)
userId
13.0550844.0920183.5641304.5021673.9812151.2716943.6032742.3332665.0917493.972454...1.4026084.2083823.7059572.7205142.7873313.4750763.2534582.1610874.0104950.859474
23.1701193.6579923.3087074.1665214.3118901.2754694.2379721.9003663.3928593.647421...0.9738113.5282643.3615322.6725352.4044564.2327892.9116021.6345764.1357350.725684
32.3070731.6588531.4435382.2088592.2294860.7807601.9970430.9249082.9707002.551446...0.5203541.7094942.2815961.7828331.6351731.3232762.8875801.0426182.2938900.396941
\n", + "

3 rows × 9719 columns

\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "ratings_pred_matrix" + } + }, + "metadata": {}, + "execution_count": 48 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#사용자가 관람하지 않은 영화명 추출\n", + "unseen_list=get_unseen_movies(ratings_matrix,9)\n", + "#잠재 요인 협업 필터링으로 영화 추천\n", + "recomm_movies=recomm_movie_by_userid(ratings_pred_matrix,9,unseen_list,top_n=10)\n", + "#평점 데이터를 DataRame으로 생성\n", + "recomm_movies=pd.DataFrame(data=recomm_movies.values,index=recomm_movies.index,columns=['pred_score'])\n", + "recomm_movies" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 394 + }, + "id": "tuelkXrk00Vd", + "outputId": "9d03be06-430e-4491-e264-7540f94e7439" + }, + "execution_count": 49, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " pred_score\n", + "title \n", + "Rear Window (1954) 5.704612\n", + "South Park: Bigger, Longer and Uncut (1999) 5.451100\n", + "Rounders (1998) 5.298393\n", + "Blade Runner (1982) 5.244951\n", + "Roger & Me (1989) 5.191962\n", + "Gattaca (1997) 5.183179\n", + "Ben-Hur (1959) 5.130463\n", + "Rosencrantz and Guildenstern Are Dead (1990) 5.087375\n", + "Big Lebowski, The (1998) 5.038690\n", + "Star Wars: Episode V - The Empire Strikes Back (1980) 4.989601" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
pred_score
title
Rear Window (1954)5.704612
South Park: Bigger, Longer and Uncut (1999)5.451100
Rounders (1998)5.298393
Blade Runner (1982)5.244951
Roger & Me (1989)5.191962
Gattaca (1997)5.183179
Ben-Hur (1959)5.130463
Rosencrantz and Guildenstern Are Dead (1990)5.087375
Big Lebowski, The (1998)5.038690
Star Wars: Episode V - The Empire Strikes Back (1980)4.989601
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "recomm_movies", + "summary": "{\n \"name\": \"recomm_movies\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 10,\n \"samples\": [\n \"Big Lebowski, The (1998)\",\n \"South Park: Bigger, Longer and Uncut (1999)\",\n \"Gattaca (1997)\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"pred_score\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.21272885538651393,\n \"min\": 4.989601238872484,\n \"max\": 5.704612469838172,\n \"num_unique_values\": 10,\n \"samples\": [\n 5.0386897288205725,\n 5.451100205772531,\n 5.183178550884765\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 49 + } + ] + } + ] +} \ No newline at end of file