{ "cells": [ { "cell_type": "code", "execution_count": 76, "id": "configured-dover", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.naive_bayes import CategoricalNB\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import classification_report\n", "from sklearn.preprocessing import OrdinalEncoder\n", "from sklearn.pipeline import Pipeline" ] }, { "cell_type": "code", "execution_count": 5, "id": "tracked-guidance", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('C:/Users/student/Desktop/ipIndustija4/ballons.csv')" ] }, { "cell_type": "code", "execution_count": 6, "id": "diverse-logistics", "metadata": {}, "outputs": [ { "data": { "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", "
colorsizeactageinflated
count7676767676
unique22222
topYELLOWSMALLDIPADULTF
freq4040383841
\n", "
" ], "text/plain": [ " color size act age inflated\n", "count 76 76 76 76 76\n", "unique 2 2 2 2 2\n", "top YELLOW SMALL DIP ADULT F\n", "freq 40 40 38 38 41" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.describe()" ] }, { "cell_type": "code", "execution_count": 12, "id": "increased-actress", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.isna().any().any()" ] }, { "cell_type": "code", "execution_count": 14, "id": "million-appraisal", "metadata": {}, "outputs": [], "source": [ "features=df.columns[:-1].tolist()" ] }, { "cell_type": "code", "execution_count": 15, "id": "psychological-grocery", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['color', 'size', 'act', 'age']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "features" ] }, { "cell_type": "code", "execution_count": 16, "id": "pleased-hawaii", "metadata": {}, "outputs": [], "source": [ "x=df[features]" ] }, { "cell_type": "code", "execution_count": 22, "id": "pressing-complaint", "metadata": {}, "outputs": [], "source": [ "y=df.iloc[:,-1]" ] }, { "cell_type": "code", "execution_count": 24, "id": "broad-department", "metadata": {}, "outputs": [], "source": [ "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)" ] }, { "cell_type": "code", "execution_count": 78, "id": "intimate-gilbert", "metadata": {}, "outputs": [], "source": [ "pipline = Pipeline([('encoder', OrdinalEncoder()), ('clf', CategoricalNB())])\n" ] }, { "cell_type": "code", "execution_count": 79, "id": "coordinated-disaster", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Pipeline(steps=[('encoder', OrdinalEncoder()), ('clf', CategoricalNB())])" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipline.fit(x_train, y_train)" ] }, { "cell_type": "code", "execution_count": 83, "id": "metric-release", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array(['PURPLE', 'YELLOW'], dtype=object),\n", " array(['LARGE', 'SMALL'], dtype=object),\n", " array(['DIP', 'STRETCH'], dtype=object),\n", " array(['ADULT', 'CHILD'], dtype=object)]" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipline['encoder'].categories_" ] }, { "cell_type": "code", "execution_count": 85, "id": "anonymous-environment", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "color\n", " PURPLE YELLOW\n", "F 19.0 11.0\n", "T 6.0 17.0\n", "\n", "size\n", " LARGE SMALL\n", "F 19.0 11.0\n", "T 7.0 16.0\n", "\n", "act\n", " DIP STRETCH\n", "F 20.0 10.0\n", "T 8.0 15.0\n", "\n", "age\n", " ADULT CHILD\n", "F 9.0 21.0\n", "T 16.0 7.0\n", "\n" ] } ], "source": [ "#izvestaj o zastupljenosti \n", "for i in range(len(features)):\n", " print(features[i])\n", " print(pd.DataFrame(pipline['clf'].category_count_[i], index=pipline['clf'].classes_, columns=pipline['encoder'].categories_[i]))\n", " print()" ] }, { "cell_type": "code", "execution_count": 87, "id": "laughing-parade", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Trening skup\n", " precision recall f1-score support\n", "\n", " F 0.79 0.90 0.84 30\n", " T 0.84 0.70 0.76 23\n", "\n", " accuracy 0.81 53\n", " macro avg 0.82 0.80 0.80 53\n", "weighted avg 0.81 0.81 0.81 53\n", "\n" ] } ], "source": [ "print('Trening skup')\n", "print(classification_report(y_train, pipline.predict(x_train)))" ] }, { "cell_type": "code", "execution_count": 89, "id": "widespread-naples", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test skup\n", " precision recall f1-score support\n", "\n", " F 0.62 0.91 0.74 11\n", " T 0.86 0.50 0.63 12\n", "\n", " accuracy 0.70 23\n", " macro avg 0.74 0.70 0.69 23\n", "weighted avg 0.75 0.70 0.68 23\n", "\n" ] } ], "source": [ "print('Test skup')\n", "print(classification_report(y_test, pipline.predict(x_test)))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.6" } }, "nbformat": 4, "nbformat_minor": 5 }