<jupyter_start><jupyter_text>Boot<jupyter_code>!pip install scikit-multilearn

%reload_ext tensorboard
%tensorflow_version 2.x
import pandas as pd
import numpy as np

import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import MultinomialNB

import matplotlib.pyplot as plt
from wordcloud import WordCloud,STOPWORDS
from google.colab import drive
drive.mount('/content/drive')
finalDF = pd.read_csv('/content/drive/My Drive/Search Project/finalDF.csv')
# finalDF.head()<jupyter_output><empty_output><jupyter_text>Remove stop words<jupyter_code>stop = stopwords.words('english')

finalDF['clean_text'] = finalDF['text'].str.lower().str.split()
finalDF['clean_text'] = finalDF['clean_text'].apply(lambda x: [item for item in x if item not in stop])<jupyter_output><empty_output><jupyter_text>Word Clouds<jupyter_code>plt.figure(figsize=(40,25))

# RestaurantsReservations
subset = finalDF[finalDF.RestaurantsReservations==True]
text = subset.text.values
cloud = WordCloud(
                          stopwords=STOPWORDS,
                          background_color='black',
                          collocations=False,
                          width=2500,
                          height=1800
                         ).generate(" ".join(text))
plt.axis('off')
plt.title("RestaurantsReservations",fontsize=40)
plt.imshow(cloud)
plt.figure(figsize=(40,25))

# RestaurantsGoodForGroups
subset = finalDF[finalDF.RestaurantsGoodForGroups==True]
text = subset.text.values
cloud = WordCloud(
                          stopwords=STOPWORDS,
                          background_color='black',
                          collocations=False,
                          width=2500,
                          height=1800
                         ).generate(" ".join(text))
plt.axis('off')
plt.title("RestaurantsGoodForGroups",fontsize=40)
plt.imshow(cloud)
plt.figure(figsize=(40,25))

# OutdoorSeating
subset = finalDF[finalDF.OutdoorSeating==True]
text = subset.text.values
cloud = WordCloud(
                          stopwords=STOPWORDS,
                          background_color='black',
                          collocations=False,
                          width=2500,
                          height=1800
                         ).generate(" ".join(text))
plt.axis('off')
plt.title("OutdoorSeating",fontsize=40)
plt.imshow(cloud)
plt.figure(figsize=(40,25))

# RestaurantsTakeOut
subset = finalDF[finalDF.RestaurantsTakeOut==True]
text = subset.text.values
cloud = WordCloud(
                          stopwords=STOPWORDS,
                          background_color='black',
                          collocations=False,
                          width=2500,
                          height=1800
                         ).generate(" ".join(text))
plt.axis('off')
plt.title("RestaurantsTakeOut",fontsize=40)
plt.imshow(cloud)
plt.figure(figsize=(40,25))

# GoodForKids
subset = finalDF[finalDF.GoodForKids==True]
text = subset.text.values
cloud = WordCloud(
                          stopwords=STOPWORDS,
                          background_color='black',
                          collocations=False,
                          width=2500,
                          height=1800
                         ).generate(" ".join(text))
plt.axis('off')
plt.title("GoodForKids",fontsize=40)
plt.imshow(cloud)
plt.figure(figsize=(40,25))

# RestaurantsDelivery
subset = finalDF[finalDF.RestaurantsDelivery==True]
text = subset.text.values
cloud = WordCloud(
                          stopwords=STOPWORDS,
                          background_color='black',
                          collocations=False,
                          width=2500,
                          height=1800
                         ).generate(" ".join(text))
plt.axis('off')
plt.title("RestaurantsDelivery",fontsize=40)
plt.imshow(cloud)
plt.figure(figsize=(40,25))

# BikeParking
subset = finalDF[finalDF.BikeParking==True]
text = subset.text.values
cloud = WordCloud(
                          stopwords=STOPWORDS,
                          background_color='black',
                          collocations=False,
                          width=2500,
                          height=1800
                         ).generate(" ".join(text))
plt.axis('off')
plt.title("BikeParking",fontsize=40)
plt.imshow(cloud)
plt.figure(figsize=(40,25))

# Wifi
subset = finalDF[finalDF.Wifi==True]
text = subset.text.values
cloud = WordCloud(
                          stopwords=STOPWORDS,
                          background_color='black',
                          collocations=False,
                          width=2500,
                          height=1800
                         ).generate(" ".join(text))
plt.axis('off')
plt.title("Wifi",fontsize=40)
plt.imshow(cloud)<jupyter_output><empty_output><jupyter_text>Data Preparation<jupyter_code>train, test = train_test_split(finalDF, test_size=0.30, shuffle=True)
print(train.shape, test.shape)<jupyter_output>(1316, 10) (564, 10)<jupyter_text>Method 1: TF-IDF using *sci-kit learn*<jupyter_code>x_train = train.text.values
x_test = test.text.values
categories = ['RestaurantsReservations',
       'RestaurantsGoodForGroups', 'OutdoorSeating', 'RestaurantsTakeOut',
       'GoodForKids', 'RestaurantsDelivery', 'BikeParking', 'Wifi']

# Define a pipeline combining a text feature extractor with multi lable classifier
NB_pipeline = Pipeline([
                ('tfidf', TfidfVectorizer(stop_words=stop)),
                ('clf', OneVsRestClassifier(MultinomialNB(
                    fit_prior=True, class_prior=None))),
            ])
for category in categories:
    print('... Processing {}'.format(category))
    # train the model using X_dtm & y
    NB_pipeline.fit(x_train, train[category])
    # compute the testing accuracy
    prediction = NB_pipeline.predict(x_test)
    print('Test accuracy is {}'.format(accuracy_score(test[category], prediction)))
SVC_pipeline = Pipeline([
                ('tfidf', TfidfVectorizer(stop_words=stop)),
                ('clf', OneVsRestClassifier(LinearSVC(), n_jobs=1)),
            ])
for category in categories:
    print('... Processing {}'.format(category))
    # train the model using X_dtm & y
    SVC_pipeline.fit(x_train, train[category])
    # compute the testing accuracy
    prediction = SVC_pipeline.predict(x_test)
    print('Test accuracy is {}'.format(accuracy_score(test[category], prediction)))
LogReg_pipeline = Pipeline([
                ('tfidf', TfidfVectorizer(stop_words=stop)),
                ('clf', OneVsRestClassifier(LogisticRegression(solver='sag'), n_jobs=1)),
            ])
for category in categories:
    print('... Processing {}'.format(category))
    # train the model using X_dtm & y
    LogReg_pipeline.fit(x_train, train[category])
    # compute the testing accuracy
    prediction = LogReg_pipeline.predict(x_test)
    print('Test accuracy is {}'.format(accuracy_score(test[category], prediction)))<jupyter_output>... Processing RestaurantsReservations
Test accuracy is 0.8563829787234043
... Processing RestaurantsGoodForGroups
Test accuracy is 0.8421985815602837
... Processing OutdoorSeating
Test accuracy is 0.774822695035461
... Processing RestaurantsTakeOut
Test accuracy is 0.9060283687943262
... Processing GoodForKids
Test accuracy is 0.8953900709219859
... Processing RestaurantsDelivery
Test accuracy is 0.7588652482269503
... Processing BikeParking
Test accuracy is 0.9095744680851063
... Processing Wifi
Test accuracy is 0.7074468085106383
{'fraction_non_alphanumeric': 0.10861182519280206, 'fraction_numerical': 0.03406169665809768, 'mean_word_length': 2.634283045305932, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '407449', 'license': 'Apache-2.0', 'n_tokens_mistral': 2504, 'n_tokens_neox': 2265, 'n_words': 417, 'path': 'attributes_from_text.ipynb', 'repo_name': 'pati-dev/text-multi-label', 'size': '7780'}