# -*- coding: utf-8 -*-
"""320 HS_.ipynb

Automatically generated by Colaboratory.

"""

# Libraries
import pandas as pd
import re
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS
from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsRestClassifier
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn import linear_model
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sn
import matplotlib.pyplot as plt

# Data loading
print(data)

#data["PRODUCT_DESCRIPTION"] = data["PRODUCT_DESCRIPTION"].str.replace('[\d{1,2}.\d{1,2}.\d{2,4}]',"")

data["PRODUCT_DESCRIPTION"] = data["PRODUCT_DESCRIPTION"].str.replace('\(|\)'," ")

data["PRODUCT_DESCRIPTION"] = data["PRODUCT_DESCRIPTION"].str.replace('[^a-zA-Z\d\s+]',"")

data["PRODUCT_DESCRIPTION"] = data["PRODUCT_DESCRIPTION"].apply(lambda x: re.sub('\d\d\d\d\d+', '',x))

data["PRODUCT_DESCRIPTION"]

data['Product_ID'] = data.Product_ID.astype(str).str.replace('\[|\]|\'', '')

data["Product_ID"]

data.head()

fe = TfidfVectorizer(
    min_df=1,
    max_df=1.0,
    ngram_range=(1, 4),
    max_features=5000,
    stop_words=ENGLISH_STOP_WORDS,
    decode_error='ignore'
    
)

x_data = fe.fit_transform(data["PRODUCT_DESCRIPTION"])

X_train, X_test, y_train, y_test = train_test_split(x_data, data['Product_ID'], test_size=0.2, random_state=1)

X_train.shape, X_test.shape,y_train.shape, y_test.shape

"""logistic = linear_model.LogisticRegression()
penalty = ['l1', 'l2']
C = np.logspace(0, 4, 10)
hyperparameters = dict(C=C, penalty=penalty)
clf1 = GridSearchCV(logistic, hyperparameters, cv=5, verbose=0)


best_model = clf1.fit(X_train, y_train)
best_model.best_estimator_.get_params()"""

model = OneVsRestClassifier(estimator=LogisticRegression(C=1, class_weight=None,
                                                 dual=False, fit_intercept=True,
                                                 intercept_scaling=1,
                                                 l1_ratio=None, max_iter=100,
                                                 multi_class='warn',
                                                 n_jobs=None, penalty='l1',
                                                 random_state=None,
                                                 solver='warn', tol=0.0001,
                                                 verbose=0, warm_start=False))

model.fit(X_train, y_train)

predicted = model.predict(X_test)

print(classification_report(y_test, predicted))

print(confusion_matrix(y_test, predicted))

test_data=pd.read_excel("sampe_test_.xlsx")

test_data["PRODUCT_DESCRIPTION"] = test_data["PRODUCT_DESCRIPTION"].str.replace('[^a-zA-Z\d\s+]',"")

test_data['Product_ID'] = test_data.Product_ID.astype(str).str.replace('\[|\]|\'', '')

test_data["Product_ID"]

x_test_data = fe.transform(test_data["PRODUCT_DESCRIPTION"])

predictedtest = model.predict(x_test_data)

test_data['predicted'] = predictedtest
