<jupyter_start><jupyter_text>Hebrew Multi-Class Text Classification with Scikit-Learn  Load Data<jupyter_code>import pandas as pd

df=pd.read_csv("fashion_data.csv",index_col=[0],header=[0])
df=df.dropna()
df=df.drop_duplicates()
df=df.reset_index(drop=True)
df.info()
df.sample(10)<jupyter_output><empty_output><jupyter_text>Explore Data  Number of items per category<jupyter_code>df_group = df.groupby("category")
df_group = df_group.agg({"description": "nunique"})
df_group = df_group.reset_index()
df_group.head(13)
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 100

df_group.plot(x='category', y='description', kind='bar', legend=False, grid=True, figsize=(8, 5))
plt.title("Number of items per class")
plt.ylabel('Number of items', fontsize=12)
plt.xlabel('Class', fontsize=12)<jupyter_output><empty_output><jupyter_text>Top n words<jupyter_code>import operator
def get_freq(lemmas_lst):
    freq_dict = {}
    for lemmas in lemmas_lst:
        try:
            lemma_tokens=lemmas.split()
            for t in lemma_tokens:
                freq_dict[t.strip()] = freq_dict.get(t.strip(), 0) + 1
        except:
            continue
    return freq_dict
def get_top_n_words(freq_dict, n):
    sorted_dict = sorted(freq_dict.items(), key=operator.itemgetter(1), reverse=True)
    return [x[0] for x in sorted_dict][:n]
freq_dict = get_freq(df['description'].values)
top_keywords = get_top_n_words(freq_dict, 20)
top_keywords
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='utf-8', ngram_range=(1, 2))
features = tfidf.fit_transform(df.description).toarray()
labels = df.label
features.shape
category_id_df = df[['category', 'label']].drop_duplicates().sort_values('label')
category_to_id = dict(category_id_df.values)
from sklearn.feature_selection import chi2
import numpy as np
N = 2
for category, label in sorted(category_to_id.items()):
    features_chi2 = chi2(features, labels == label)
    indices = np.argsort(features_chi2[0])
    feature_names = np.array(tfidf.get_feature_names())[indices]
    unigrams = [v for v in feature_names if len(v.split(' ')) == 1]
    bigrams = [v for v in feature_names if len(v.split(' ')) == 2]
    print("# '{}':".format(category))
    print("  - Most correlated unigrams:\n* {}".format('\n* '.join(unigrams[-N:])))
    print("  - Most correlated bigrams:\n* {}".format('\n* '.join(bigrams[-N:])))<jupyter_output># 'belts&ties_women':
  - Most correlated unigrams:
* אבזם
* חגורת
  - Most correlated bigrams:
* חגורת דמוי
* חגורת עור
# 'coats&jackets_women':
  - Most correlated unigrams:
* מעיל
* זקט
  - Most correlated bigrams:
* גקט גינס
* זקט מרופד
# 'dresses_women':
  - Most correlated unigrams:
* שמלה
* שמלת
  - Most correlated bigrams:
* שמלת מיני
* שמלת מידי
# 'hats&scarves_women':
  - Most correlated unigrams:
* כובע
* צעיף
  - Most correlated bigrams:
* סרט לשיער
* כובע גרב
# 'lingerie_women':
  - Most correlated unigrams:
* גוף
* חזיית
  - Most correlated bigrams:
* חזיית ספורט
* בגד גוף
# 'nightwear_women':
  - Most correlated unigrams:
* חלוק
* פיגמה
  - Most correlated bigrams:
* סט פיגמה
* חליפת פנאי
# 'shirts_women':
  - Most correlated unigrams:
* חולצה
* חולצת
  - Most correlated bigrams:
* טי שירט
* חולצת טי
# 'shoes_women':
  - Most correlated unigrams:
* עקב
* נעלי
  - Most correlated bigrams:
* נעלי עקב
* נעלי ספורט
# 'skirts_women':
  - Most correlated unigrams:
* עיפרון
* ח[...]<jupyter_text>Word Cloud<jupyter_code>from wordcloud import WordCloud
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 200
def generate_word_cloud(text):
    wordcloud = WordCloud(width = 600, height = 600, 
                    background_color ='white', 
                    max_words=200, 
                    min_font_size = 10,
                    font_path='davidbd.ttf').generate(text)
    return wordcloud
from bidi.algorithm import get_display
cleaned_text=list()

for i,r in df.iterrows():
    text=r['description']
    if isinstance(text, str):
        bidi_text = get_display(text)
        cleaned_text.append(bidi_text)
wordcloud = generate_word_cloud(''.join(cleaned_text))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()<jupyter_output><empty_output><jupyter_text>Model Data  Building Train and Test Datasets<jupyter_code>from sklearn.model_selection import train_test_split
import numpy as np
X_train, X_test, y_train, y_test = train_test_split(np.array(df['description']), np.array(df['category']), test_size=0.33, random_state=42)
X_train.shape, X_test.shape<jupyter_output><empty_output><jupyter_text>Feature Engineering<jupyter_code>from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import cross_val_score

cv=CountVectorizer(max_features=1500)
cv_train_features = cv.fit_transform(X_train)
cv_test_features = cv.transform(X_test)
from sklearn.naive_bayes import MultinomialNB

mnb = MultinomialNB(alpha=1)
mnb.fit(cv_train_features, y_train)

mnb_test_score = mnb.score(cv_test_features, y_test)
mnb_cv_scores = cross_val_score(mnb, cv_train_features, y_train, cv=5)
mnb_cv_mean_score = np.mean(mnb_cv_scores)
print('Cross Validation Accuracy (5-fold):', mnb_cv_scores)
print('Mean of Cross Validation Accuracy:', mnb_cv_mean_score)
print('Test Accuracy:', mnb_test_score)
cv_pred_features = cv.transform(['שמלה פרחונית ארוכה','מכנסי גינס','חולצה מכופתרת'])
mnb.predict(cv_pred_features)
from sklearn.linear_model import LogisticRegression

lr = LogisticRegression(penalty='l2',solver='lbfgs', max_iter=100, C=1, random_state=42)
lr.fit(cv_train_features, y_train)

lr_test_score = lr.score(cv_test_features, y_test)
lr_cv_scores = cross_val_score(lr, cv_train_features, y_train, cv=5)
lr_cv_mean_score = np.mean(lr_cv_scores)
print('Cross Validation Accuracy (5-fold):', lr_cv_scores)
print('Mean of Cross Validation Accuracy:', lr_cv_mean_score)
print('Test Accuracy:', lr_test_score)
cv_pred_features = cv.transform(['שמלה פרחונית ארוכה','מכנסי גינס','חולצה מכופתרת'])
lr.predict(cv_pred_features)
from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier(n_estimators=10, random_state=42)
rfc.fit(cv_train_features, y_train)
rfc_cv_scores = cross_val_score(rfc, cv_train_features, y_train, cv=5)
rfc_cv_mean_score = np.mean(rfc_cv_scores)
print('Cross Validation Accuracy (5-fold):', rfc_cv_scores)
print('Mean of Cross Validation Accuracy:', rfc_cv_mean_score)
rfc_test_score = rfc.score(cv_test_features, y_test)
print('Test Accuracy:', rfc_test_score)
cv_pred_features = cv.transform(['שמלה פרחונית ארוכה','מכנסי גינס','חולצה מכופתרת'])
rfc.predict(cv_pred_features)
pd.DataFrame([['Naive Bayes', mnb_cv_mean_score, mnb_test_score],
              ['Logistic Regression', lr_cv_mean_score, lr_test_score],
              ['Random Forest', rfc_cv_mean_score, rfc_test_score]],
             columns=['Model', 'CV Score (TF)', 'Test Score (TF)'],
             ).T<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12483930867018997, 'fraction_numerical': 0.010712755320668475, 'mean_word_length': 5.459409594095941, 'pattern_counts': {'":': 1, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '658251', 'license': 'Apache-2.0', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2758, 'n_words': 511, 'path': 'multi_class_sklearn.ipynb', 'repo_name': 'YuliaArKa/Hebrew-Multi-Class-Text-Classification-with-Scikit-Learn', 'size': '7001'}