|
1 | 1 | Using NLP Operations |
2 | 2 | ==================== |
3 | 3 |
|
4 | | -This example will show you how to use DFFML operations to clean text data and train a model using DFFML cli. |
| 4 | +These example will show you how to use DFFML operations to clean text data and train Tensorflow DNNClassifier model and Scikit Learn |
| 5 | +Naive Bayes Classifier model using DFFML cli. |
| 6 | + |
| 7 | +Preprocessing data and training DNNClassifier model |
| 8 | +--------------------------------------------------- |
5 | 9 |
|
6 | 10 | DFFML offers several :ref:`plugin_models`. For this example |
7 | 11 | we will be using the tensorflow DNNClassifier model |
@@ -92,4 +96,120 @@ The output is: |
92 | 96 | | sentiment | |
93 | 97 | +------------------------------------------------------------------------------------------------------------------------------+ |
94 | 98 | | Value: 1 | Confidence: 0.5122595429420471 | |
95 | | - +------------------------------------------------------------------------------------------------------------------------------+ |
| 99 | + +------------------------------------------------------------------------------------------------------------------------------+ |
| 100 | +
|
| 101 | + |
| 102 | +Preprocessing data and training Naive Bayes Classifier model |
| 103 | +------------------------------------------------------------ |
| 104 | + |
| 105 | +Now we will see how to use traditional ML algorithm like Naive Bayes Classifier available in ``dffml-model-scikit`` (:ref:`plugin_model_dffml_model_scikit`) for |
| 106 | +classification. |
| 107 | + |
| 108 | +Create training data: |
| 109 | + |
| 110 | +.. literalinclude:: /../examples/nlp/train_data.sh |
| 111 | + |
| 112 | +But before we feed the data to model we need to convert it to vectors of numeric values. |
| 113 | +Here we will use ``tfidf_vectorizer`` operation (:ref:`plugin_operation_dffml_operations_nlp_tfidf_vectorizer`) which is a wrapper around |
| 114 | +sklearn `TfidfVectorizer. <https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html>`_ |
| 115 | + |
| 116 | +The dataflow will be similar to the one used above but with a slight modification. We will add an extra operation |
| 117 | +``collect_output`` (:ref:`plugin_operation_dffml_operations_nlp_collect_output`) which will collect all the records before |
| 118 | +forwarding them to next operation. This is to ensure that `tfidf_vectorizer` receives a list of sentence rather than a single |
| 119 | +sentence at a time. |
| 120 | +The matrix returned by `tfidf_vectorizer` will be passed to ``extract_array_from_matrix`` (:ref:`plugin_operation_dffml_operations_nlp_extract_array_from_matrix`) |
| 121 | +which will return the array corresponding to each sentence. |
| 122 | + |
| 123 | +So, Let's modify the dataflow to use our new operations. |
| 124 | + |
| 125 | +.. literalinclude:: /../examples/nlp/sklearn/create_dataflow.sh |
| 126 | + |
| 127 | +To visualize the dataflow run: |
| 128 | + |
| 129 | +.. literalinclude:: /../examples/nlp/sklearn/dataflow_diagram.sh |
| 130 | + |
| 131 | +We can now use this dataflow to preprocess the data and make it ready to be fed into model: |
| 132 | + |
| 133 | +.. literalinclude:: /../examples/nlp/sklearn/train.sh |
| 134 | + |
| 135 | +Assess accuracy: |
| 136 | + |
| 137 | +.. literalinclude:: /../examples/nlp/sklearn/accuracy.sh |
| 138 | + |
| 139 | +The output is: |
| 140 | + |
| 141 | +.. code-block:: console |
| 142 | +
|
| 143 | + 1.0 |
| 144 | +
|
| 145 | +Create test data: |
| 146 | + |
| 147 | +.. literalinclude:: /../examples/nlp/sklearn/test_data.sh |
| 148 | + |
| 149 | +Make prediction on test data: |
| 150 | + |
| 151 | +.. literalinclude:: /../examples/nlp/sklearn/predict.sh |
| 152 | + |
| 153 | +The output is: |
| 154 | + |
| 155 | +.. code-block:: console |
| 156 | +
|
| 157 | + Key: 1 |
| 158 | + Record Features |
| 159 | + +------------------------------------------------------------------------------------------------+ |
| 160 | + | sentence | Those were good days | |
| 161 | + +------------------------------------------------------------------------------------------------+ |
| 162 | + |extract_array_from_matri| 0.0, 0.0, 0.7071067811865476, 0 ... (length:9) | |
| 163 | + +------------------------------------------------------------------------------------------------+ |
| 164 | +
|
| 165 | + Prediction |
| 166 | + +------------------------------------------------------------------------------------------------+ |
| 167 | + | sentiment | |
| 168 | + +------------------------------------------------------------------------------------------------+ |
| 169 | + | Value: 1 | Confidence: 1.0 | |
| 170 | + +------------------------------------------------------------------------------------------------+ |
| 171 | +
|
| 172 | + Key: 2 |
| 173 | + Record Features |
| 174 | + +------------------------------------------------------------------------------------------------+ |
| 175 | + | sentence | My cat plays all day | |
| 176 | + +------------------------------------------------------------------------------------------------+ |
| 177 | + |extract_array_from_matri| 0.5773502691896257, 0.577350269 ... (length:9) | |
| 178 | + +------------------------------------------------------------------------------------------------+ |
| 179 | +
|
| 180 | + Prediction |
| 181 | + +------------------------------------------------------------------------------------------------+ |
| 182 | + | sentiment | |
| 183 | + +------------------------------------------------------------------------------------------------+ |
| 184 | + | Value: 0 | Confidence: 1.0 | |
| 185 | + +------------------------------------------------------------------------------------------------+ |
| 186 | +
|
| 187 | + Key: 0 |
| 188 | + Record Features |
| 189 | + +------------------------------------------------------------------------------------------------+ |
| 190 | + | sentence | Such a pleasant morning | |
| 191 | + +------------------------------------------------------------------------------------------------+ |
| 192 | + |extract_array_from_matri| 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0 ... (length:9) | |
| 193 | + +------------------------------------------------------------------------------------------------+ |
| 194 | +
|
| 195 | + Prediction |
| 196 | + +------------------------------------------------------------------------------------------------+ |
| 197 | + | sentiment | |
| 198 | + +------------------------------------------------------------------------------------------------+ |
| 199 | + | Value: 1 | Confidence: 1.0 | |
| 200 | + +------------------------------------------------------------------------------------------------+ |
| 201 | +
|
| 202 | + Key: 3 |
| 203 | + Record Features |
| 204 | + +------------------------------------------------------------------------------------------------+ |
| 205 | + | sentence | Dogs are evil | |
| 206 | + +------------------------------------------------------------------------------------------------+ |
| 207 | + |extract_array_from_matri| 0.0, 0.0, 0.0, 0.70710678118654 ... (length:9) | |
| 208 | + +------------------------------------------------------------------------------------------------+ |
| 209 | +
|
| 210 | + Prediction |
| 211 | + +------------------------------------------------------------------------------------------------+ |
| 212 | + | sentiment | |
| 213 | + +------------------------------------------------------------------------------------------------+ |
| 214 | + | Value: 0 | Confidence: 1.0 | |
| 215 | + +------------------------------------------------------------------------------------------------+ |
0 commit comments