<jupyter_start><jupyter_text>ML Pipeline PreparationFollow the instructions below to help you create your ML pipeline. 1. Import libraries and load data from database.- Import Python libraries- Load dataset from database with [`read_sql_table`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql_table.html)- Define feature and target variables X and Y<jupyter_code># import libraries
import pandas as pd
from sqlalchemy import create_engine
import nltk
import re

nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('averaged_perceptron_tagger')


from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import os
print(os.getcwd())
# load data from database
engine = create_engine('sqlite:////home/workspace/matheus_project.db')
df = pd.read_sql("SELECT * FROM ETL_part", engine)
X = df["message"]
Y = df.drop(columns=["id","original","genre","message"])

Y.head()<jupyter_output><empty_output><jupyter_text>1.1 Stop Words<jupyter_code># Normalize text

text = re.sub(r"[^a-zA-Z0-9]", " ", text.lower())
print(text)<jupyter_output>a comitee in delmas 19  rue   street   janvier  impasse charite  2  we have about 500 people in a temporary shelter and we are in dire need of water  food  medications  tents and clothes  please stop by and see us<jupyter_text>1.2 Test part of tokenize<jupyter_code># import statements
from nltk import pos_tag, ne_chunk
from nltk.tokenize import word_tokenize


token = word_tokenize(text)
print(token)

# tag each word with part of speech
pos_tag(token)<jupyter_output>['a', 'comitee', 'in', 'delmas', '19', 'rue', 'street', 'janvier', 'impasse', 'charite', '2', 'we', 'have', 'about', '500', 'people', 'in', 'a', 'temporary', 'shelter', 'and', 'we', 'are', 'in', 'dire', 'need', 'of', 'water', 'food', 'medications', 'tents', 'and', 'clothes', 'please', 'stop', 'by', 'and', 'see', 'us']<jupyter_text>1.3 Lemmatizer<jupyter_code>lemma = nltk.WordNetLemmatizer()
for i in token:
    print(lemma.lemmatize(i))<jupyter_output>a
comitee
in
delmas
19
rue
street
janvier
impasse
charite
2
we
have
about
500
people
in
a
temporary
shelter
and
we
are
in
dire
need
of
water
food
medication
tent
and
clothes
please
stop
by
and
see
u<jupyter_text>2. Write a tokenization function to process your text data<jupyter_code>def tokenize(text):
    text =  re.sub(r"[^a-zA-Z0-9]", " ", text.lower())
    token = word_tokenize(text)
    lemma = nltk.WordNetLemmatizer()
    return [lemma.lemmatize(i).strip() for i in token]<jupyter_output><empty_output><jupyter_text>3. Build a machine learning pipelineThis machine pipeline should take in the `message` column as input and output classification results on the other 36 categories in the dataset. You may find the [MultiOutputClassifier](http://scikit-learn.org/stable/modules/generated/sklearn.multioutput.MultiOutputClassifier.html) helpful for predicting multiple target variables.<jupyter_code>pipeline = Pipeline([('cvt', CountVectorizer(tokenizer=tokenize)),
                     ('tf', TfidfTransformer()),
                     ('model', MultiOutputClassifier(RandomForestClassifier())),])<jupyter_output><empty_output><jupyter_text>4. Train pipeline- Split data into train and test sets- Train pipeline<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state = 42)

pipeline.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>5. Test your modelReport the f1 score, precision and recall for each output category of the dataset. You can do this by iterating through the columns and calling sklearn's `classification_report` on each.<jupyter_code>prediction = pipeline.predict(X_test)
prediction[:1]
enumareted = enumerate(y_test)
for i, j in enumareted:
    print('''Column: {} 
    {}'''.format(j,classification_report(y_test[j], prediction[:, i])))<jupyter_output>Column: related 
                 precision    recall  f1-score   support

          0       0.62      0.33      0.44      2054
          1       0.81      0.94      0.87      6534
          2       0.58      0.30      0.39        64

avg / total       0.77      0.79      0.76      8652

Column: request 
                 precision    recall  f1-score   support

          0       0.89      0.98      0.93      7180
          1       0.83      0.38      0.52      1472

avg / total       0.88      0.88      0.86      8652

Column: offer 
                 precision    recall  f1-score   support

          0       1.00      1.00      1.00      8614
          1       0.00      0.00      0.00        38

avg / total       0.99      1.00      0.99      8652

Column: aid_related 
                 precision    recall  f1-score   support

          0       0.73      0.87      0.79      5107
          1       0.74      0.53      0.62      3545

avg / total       0.73      0.73      0.72      8652

C[...]<jupyter_text>6. Improve your modelUse grid search to find better parameters.<jupyter_code>parameters = {'cvt__min_df': [1, 3],
              'tf__use_idf':[True, False],
              'model__estimator__n_estimators':[15, 25], 
              'model__estimator__min_samples_split':[2, 5, 10]}

cv = GridSearchCV(pipeline, param_grid = parameters, verbose = 10)<jupyter_output><empty_output><jupyter_text>7. Test your modelShow the accuracy, precision, and recall of the tuned model.  Since this project focuses on code quality, process, and  pipelines, there is no minimum performance metric needed to pass. However, make sure to fine tune your models for accuracy, precision and recall to make your project stand out - especially for your portfolio!<jupyter_code>tuned_model = cv.fit(X_train, y_train)<jupyter_output>Fitting 3 folds for each of 24 candidates, totalling 72 fits
[CV] cvt__min_df=1, model__estimator__min_samples_split=2, model__estimator__n_estimators=15, tf__use_idf=True 
[CV]  cvt__min_df=1, model__estimator__min_samples_split=2, model__estimator__n_estimators=15, tf__use_idf=True, score=0.2201537147736977, total=  48.3s
[CV] cvt__min_df=1, model__estimator__min_samples_split=2, model__estimator__n_estimators=15, tf__use_idf=True<jupyter_text>8. Try improving your model further. Here are a few ideas:* try other machine learning algorithms* add other features besides the TF-IDF<jupyter_code>prediction_improved = tuned_model.predict(X_test)
prediction_improved
enumareted = enumerate(y_test)
for i, j in enumareted:
    print('''Column: {} 
    {}'''.format(j,classification_report(y_test[j], prediction_improved[:, i])))<jupyter_output>Column: related 
                 precision    recall  f1-score   support

          0       0.69      0.30      0.42      2054
          1       0.81      0.95      0.88      6534
          2       0.50      0.39      0.44        64

avg / total       0.78      0.80      0.77      8652

Column: request 
                 precision    recall  f1-score   support

          0       0.90      0.98      0.94      7180
          1       0.85      0.45      0.59      1472

avg / total       0.89      0.89      0.88      8652

Column: offer 
                 precision    recall  f1-score   support

          0       1.00      1.00      1.00      8614
          1       0.00      0.00      0.00        38

avg / total       0.99      1.00      0.99      8652

Column: aid_related 
                 precision    recall  f1-score   support

          0       0.77      0.85      0.81      5107
          1       0.75      0.63      0.68      3545

avg / total       0.76      0.76      0.76      8652

C[...]<jupyter_text>9. Export your model as a pickle file<jupyter_code>with open('model_disaster','wb') as f:
    pickle.dump(tuned_model,f)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup