diff --git a/building-features-from-text-data/02-VectorizeTextAsABagOfWords_CountVectorizer.ipynb b/building-features-from-text-data/02-VectorizeTextAsABagOfWords_CountVectorizer.ipynb index d7cfba2..5fc06b7 100644 --- a/building-features-from-text-data/02-VectorizeTextAsABagOfWords_CountVectorizer.ipynb +++ b/building-features-from-text-data/02-VectorizeTextAsABagOfWords_CountVectorizer.ipynb @@ -1,6 +1,7 @@ { "cells": [ { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -466,6 +467,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -636,6 +638,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -733,7 +736,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.1" + "version": "3.10.12" } }, "nbformat": 4, diff --git a/doc/chapters/chapter2.tex b/doc/chapters/chapter2.tex index 9856abd..25eed66 100644 --- a/doc/chapters/chapter2.tex +++ b/doc/chapters/chapter2.tex @@ -24,8 +24,6 @@ Elasticsearch uses \acf{Tf-Idf}. \acs{Tf-Idf} is a technique to generate numeric % \begin{math} x_i=tf(w_i) x idf(w_i) \end{math} - - \section {Feature selection and dataset sources} A feature represents a set of data inputed in a training model to predict the the masked related data. In simple terms, in a classication model which predicts category of a product, product name, description can be labeled as its feature. @@ -84,4 +82,4 @@ The table \ref{table:l5} lists the feature description aling with the sample of \end{table} -\section {Missing category level - data imputation.} \label{dataimput} \ No newline at end of file +\section { Data imputation - on missing category levels} \label{dataimput} \ No newline at end of file diff --git a/doc/chapters/chapter3.tex b/doc/chapters/chapter3.tex index 66119e7..1e19a76 100644 --- a/doc/chapters/chapter3.tex +++ b/doc/chapters/chapter3.tex @@ -1,25 +1,4 @@ -\chapter{Development environment and hardware setup} -\section {Ubuntu server 20.04 configuration} +\chapter{Multi level category vectorization techniques} -\section {Python virtual environment} \label {pyenv} - -pyenv facilitaes to isolate python version dependent source codes and avoid version conflicts. A virtual environment will install packages to a specific python module instad of installing globally. Asuming the directrory root directory name is "dev", follow linux bash code to activate virtual environment. -The bash script below will create and activate virtual environment and all the packages will be installed within the venv directory -\begin{lstlisting}[language=bash] - pyenv install 3.10 / - cd ~/dev / - pyenv local 3.10 / - python -m venv .venv / - source .venv/bin/activate - -\end{lstlisting} - -Compatible python clients to interact with project dependent module installation can be installed by specifying the version range. -Python Elastic search client's official low version installation details are mentioned on elasticsearch-py \footnote{https://elasticsearch-py.readthedocs.io/en/6.8.2/} website. - -\begin{lstlisting}[language=Python] - python -m pip install "elasticsearch>=6.4.0,<7.0.0" -\end{lstlisting} - -\section {Docker containers - CouchDB, Elastic search, Redis} \ No newline at end of file +Vectorization of a text is a process of transforming a text into a real number representation. ff \ No newline at end of file diff --git a/doc/chapters/chapter4.tex b/doc/chapters/chapter4.tex new file mode 100644 index 0000000..0dbf5c7 --- /dev/null +++ b/doc/chapters/chapter4.tex @@ -0,0 +1,27 @@ +\chapter{Development environment and hardware setup} + +\section {Ubuntu server 20.04 configuration} + +\section {Python virtual environment} \label {pyenv} + +pyenv facilitaes to isolate python version dependent source codes and avoid version conflicts. A virtual environment will install packages to a specific python module instad of installing globally. Asuming the directrory root directory name is "dev", follow linux bash code to activate virtual environment. +The bash script below will create and activate virtual environment and all the packages will be installed within the venv directory +\begin{lstlisting}[language=bash] + pyenv install 3.10 / + cd ~/dev / + pyenv local 3.10 / + python -m venv .venv / + source .venv/bin/activate + +\end{lstlisting} + +Compatible python clients to interact with project dependent module installation can be installed by specifying the version range. +Python Elastic search client's official low version installation details are mentioned on elasticsearch-py \footnote{https://elasticsearch-py.readthedocs.io/en/6.8.2/} website. + +vGPU \footnote{https://docs.nvidia.com/grid/5.0/grid-vgpu-user-guide/index.html} + +\begin{lstlisting}[language=Python] + python -m pip install "elasticsearch>=6.4.0,<7.0.0" +\end{lstlisting} + +\section {Docker containers - CouchDB, Elastic search, Redis} \ No newline at end of file diff --git a/doc/index.tex b/doc/index.tex index 112d966..bb24b3a 100644 --- a/doc/index.tex +++ b/doc/index.tex @@ -50,7 +50,8 @@ preliminary/acronyms.tex,% chapters/introduction.tex,% chapters/chapter1.tex,% chapters/chapter2.tex,% -chapters/chapter3.tex +chapters/chapter3.tex,% +chapters/chapter4.tex } \begin{document} @@ -86,6 +87,7 @@ chapters/chapter3.tex \include{chapters/chapter1} \include{chapters/chapter2} \include{chapters/chapter3} +\include{chapters/chapter4} \appendix % \include{chapters/appendix.tex} diff --git a/fetchDatafromDb/getProductDetails.ipynb b/fetchDatafromDb/getProductDetails.ipynb index 6ff88da..b355025 100644 --- a/fetchDatafromDb/getProductDetails.ipynb +++ b/fetchDatafromDb/getProductDetails.ipynb @@ -6,7 +6,8 @@ "metadata": {}, "outputs": [], "source": [ - "!python -m pip install seaborn" + "!python -m pip install seaborn\n", + "!python -m pip install scikit-learn" ] }, { @@ -15,17 +16,17 @@ "metadata": {}, "outputs": [], "source": [ - "!python -m pip install scikit-learn" + "!python -m pip install \"elasticsearch>=6.4.0,<7.0.0\"" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from elasticsearch import Elasticsearch\n", - "client = Elasticsearch(\"http://localhost:9200\")\n", + "client = Elasticsearch(\"http://172.17.212.35:9200\")\n", "resp = client.search(index=\"retromotion-indexer_development_products\",\n", " body={\"_source\":[\"descriptions\",\"descriptionsSource\",\"nameSource\",\"shortDescriptionSource\",\"categoriesSource\"],\n", " \"query\": {\"match_all\": {}}})\n" @@ -33,33 +34,16 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Got 22329 Hits:\n" - ] - } - ], "source": [ "print(\"Got %d Hits:\" % resp['hits']['total'])" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -74,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -84,64 +68,36 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 18, "metadata": {}, "outputs": [ { "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
namesdesccategorycatlevel0catlevel1catlevel2catlevel3catlevel4catlevel5
\n", - "
" - ], "text/plain": [ - "Empty DataFrame\n", - "Columns: [name, sdesc, category, catlevel0, catlevel1, catlevel2, catlevel3, catlevel4, catlevel5]\n", - "Index: []" + "name denso dtm82363 thermostat coolant\n", + "sdesc thermostat coolant\n", + "category thermostat\n", + "catlevel0 sparepart\n", + "catlevel1 cooling system\n", + "catlevel2 thermostat\n", + "catlevel3 NaN\n", + "catlevel4 NaN\n", + "catlevel5 NaN\n", + "Name: 0, dtype: object" ] }, - "execution_count": 6, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "df_eng.head()" + "df_eng.iloc[0]" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -165,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -176,7 +132,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -266,167 +222,18 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "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", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
namesdesccategorycatlevel0catlevel1catlevel2catlevel3catlevel4catlevel5
0denso dtm82363 thermostat coolantthermostat coolantthermostatsparepartcooling systemthermostatNaNNaNNaN
1zf 1043 010 297 automatic transmissionautomatic transmissionautomatic transmissionspareparttransmissionautomatic transmissionautomatic transmissionNaNNaN
2zf 1060 040 003 automatic transmissionautomatic transmissionautomatic transmissionspareparttransmissionautomatic transmissionautomatic transmissionNaNNaN
3vaico v10 0131 camshaftcamshaftcamshaftsparepartengineengine timingcamshaftNaNNaN
4vaico v26 0302 track control armtrack control armtrack control armsparepartpowertrain suspensionwheel suspensiontrack control armNaNNaN
\n", - "
" - ], - "text/plain": [ - " name sdesc \\\n", - "0 denso dtm82363 thermostat coolant thermostat coolant \n", - "1 zf 1043 010 297 automatic transmission automatic transmission \n", - "2 zf 1060 040 003 automatic transmission automatic transmission \n", - "3 vaico v10 0131 camshaft camshaft \n", - "4 vaico v26 0302 track control arm track control arm \n", - "\n", - " category catlevel0 catlevel1 \\\n", - "0 thermostat sparepart cooling system \n", - "1 automatic transmission sparepart transmission \n", - "2 automatic transmission sparepart transmission \n", - "3 camshaft sparepart engine \n", - "4 track control arm sparepart powertrain suspension \n", - "\n", - " catlevel2 catlevel3 catlevel4 catlevel5 \n", - "0 thermostat NaN NaN NaN \n", - "1 automatic transmission automatic transmission NaN NaN \n", - "2 automatic transmission automatic transmission NaN NaN \n", - "3 engine timing camshaft NaN NaN \n", - "4 wheel suspension track control arm NaN NaN " - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "df_eng.head()" + "df_eng[0]" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\\begin{tabular}{llllllllrr}\n", - "\\toprule\n", - " & name & sdesc & category & catlevel0 & catlevel1 & catlevel2 & catlevel3 & catlevel4 & catlevel5 \\\\\n", - "\\midrule\n", - "0 & denso dtm82363 thermostat coolant & thermostat coolant & thermostat & sparepart & cooling system & thermostat & NaN & NaN & NaN \\\\\n", - "1 & zf 1043 010 297 automatic transmission & automatic transmission & automatic transmission & sparepart & transmission & automatic transmission & automatic transmission & NaN & NaN \\\\\n", - "2 & zf 1060 040 003 automatic transmission & automatic transmission & automatic transmission & sparepart & transmission & automatic transmission & automatic transmission & NaN & NaN \\\\\n", - "3 & vaico v10 0131 camshaft & camshaft & camshaft & sparepart & engine & engine timing & camshaft & NaN & NaN \\\\\n", - "4 & vaico v26 0302 track control arm & track control arm & track control arm & sparepart & powertrain suspension & wheel suspension & track control arm & NaN & NaN \\\\\n", - "5 & vaico v30 1250 1 suspension kit & suspension kit & suspension kit & sparepart & powertrain suspension & wheel suspension & suspension kit & NaN & NaN \\\\\n", - "6 & vaico v10 0474 1 tensioner pulley timing belt & tensioner pulley timing belt & tensioner pulley & sparepart & belt drive & tensioner pulley & NaN & NaN & NaN \\\\\n", - "7 & vaico v10 2660 mounting automatic transmission & mounting automatic transmission & mounting & sparepart & transmission & automatic transmission & mounting & NaN & NaN \\\\\n", - "8 & vaico v10 4245 shock absorber & shock absorber & shock absorber & sparepart & powertrain suspension & suspension & shock absorber & NaN & NaN \\\\\n", - "9 & vaico v24 0908 wheel hub & wheel hub & wheel hub & sparepart & powertrain suspension & wheel suspension & wheel hub & NaN & NaN \\\\\n", - "\\bottomrule\n", - "\\end{tabular}\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "print (df_eng.to_latex())" ] @@ -499,27 +306,247 @@ "metadata": {}, "outputs": [], "source": [ - "df_eng = pd.get_dummies(df_eng,columns=[\"category\"])\n", - "df_eng.head()" + "# df_eng = pd.get_dummies(df_eng,columns=[\"category\"])\n", + "# df_eng.head()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "name vaico v10 2660 mounting automatic transmission\n", + "sdesc mounting automatic transmission\n", + "category mounting\n", + "catlevel0 sparepart\n", + "catlevel1 transmission\n", + "catlevel2 automatic transmission\n", + "catlevel3 mounting\n", + "catlevel4 NaN\n", + "catlevel5 NaN\n", + "Name: 7, dtype: object" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_eng.iloc[7]" + ] + }, + { + "cell_type": "code", + "execution_count": 66, "metadata": {}, "outputs": [], "source": [ - "df_eng.shape" + "from sklearn.feature_extraction.text import CountVectorizer\n", + "n_gram_vectorizer = CountVectorizer(ngram_range=(1, 1))\n", + "transformed_vector_name = n_gram_vectorizer.fit_transform(df_eng['name'])\n", + "transformed_vector_sdesc = n_gram_vectorizer.fit_transform(df_eng['sdesc'])\n", + "transformed_vector_category = n_gram_vectorizer.fit_transform(df_eng['category'])\n", + "\n", + "# vocabulary\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<10x40 sparse matrix of type ''\n", + "\twith 54 stored elements in Compressed Sparse Row format>" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transformed_vector_name" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [], + "source": [ + "n_gram_vectorizer.vocabulary_.get('automatic transmission')" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('thermostat', 14), ('coolant', 6), ('automatic', 2), ('transmission', 17), ('camshaft', 4), ('track', 16), ('control', 5), ('arm', 1), ('suspension', 12), ('kit', 8), ('tensioner', 13), ('pulley', 10), ('timing', 15), ('belt', 3), ('mounting', 9), ('shock', 11), ('absorber', 0), ('wheel', 18), ('hub', 7)])" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vocabulary.items()" + ] + }, + { + "cell_type": "code", + "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", - "\n" + "\n", + "X_train, x_test, Y_train, y_test = train_test_split(transformed_vector_sdesc,\n", + " transformed_vector_category,\n", + " test_size=0.2,\n", + " random_state=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((8, 19), (8, 16))" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_train.shape, Y_train.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'torch'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mtorch\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[39m# import numpy as np\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[39m# Xtrain_ = torch.from_numpy(X_train.values).float()\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[39m# Xtest_ = torch.from_numpy(x_test.values).float()\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[39m# Ytest_ = torch.from_numpy(y_test.values).view(1,-1)[0]\u001b[39;00m\n\u001b[1;32m 8\u001b[0m \u001b[39m# Ytrain_.shape\u001b[39;00m\n", + "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'torch'" + ] + } + ], + "source": [ + "import torch\n", + "# import numpy as np\n", + "# Xtrain_ = torch.from_numpy(X_train.values).float()\n", + "# Xtest_ = torch.from_numpy(x_test.values).float()\n", + "# Xtrain_.shape\n", + "# Ytrain_ = torch.from_numpy(Y_train.values).view(1,-1)[0]\n", + "# Ytest_ = torch.from_numpy(y_test.values).view(1,-1)[0]\n", + "# Ytrain_.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Net(nn.Module):\n", + " \n", + " def __init__(self):\n", + " super(Net, self).__init__()\n", + " self.fc1 = nn.Linear(input_size, hidden_size) \n", + " self.fc2 = nn.Linear(hidden_size, hidden_size) \n", + " self.fc3 = nn.Linear(hidden_size, output_size) \n", + " \n", + " def forward(self, x):\n", + " x = F.sigmoid(self.fc1(x))\n", + " x = F.sigmoid(self.fc2(x)) \n", + " x = self.fc3(x)\n", + " \n", + " return F.log_softmax(x, dim=-1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model = Net()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import torch.optim as optim\n", + "\n", + "optimizer = optim.Adam(model.parameters())\n", + "\n", + "loss_fn = nn.NLLLoss()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "epoch_data = []\n", + "epochs = 1001\n", + "\n", + "for epoch in range(1, epochs):\n", + "\n", + " optimizer.zero_grad()\n", + " Ypred = model(Xtrain_)\n", + "\n", + " loss = loss_fn(Ypred , Ytrain_)\n", + " loss.backward()\n", + "\n", + " optimizer.step()\n", + " \n", + " Ypred_test = model(Xtest_)\n", + " loss_test = loss_fn(Ypred_test, Ytest_)\n", + " \n", + " _,pred = Ypred_test.data.max(1)\n", + " \n", + " accuracy = pred.eq(Ytest_.data).sum().item() / y_test.values.size\n", + " epoch_data.append([epoch, loss.data.item(), loss_test.data.item(), accuracy])\n", + " \n", + " if epoch % 100 == 0:\n", + " print ('epoch - %d (%d%%) train loss - %.2f test loss - %.2f accuracy - %.4f'\\\n", + " % (epoch, epoch/150 * 10 , loss.data.item(), loss_test.data.item(), accuracy))" ] } ], @@ -539,7 +566,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.10.12" }, "orig_nbformat": 4 }, diff --git a/fetchDatafromDb/vectorization.ipynb b/fetchDatafromDb/vectorization.ipynb new file mode 100644 index 0000000..e69de29