<jupyter_start><jupyter_text><jupyter_code>import json

def change_database(path: str, key_transaction: str, start: int) -> list:
    with open(path, 'r') as json_file:
        file = json.load(json_file)
        customers = dict(file[key_transaction])
    id_data = list(customers)
    new_customers = []
    for i in id_data[start:]:
        data = customers[i]
        data['email_private'] = data.pop('emailliff')
        if data.get('month_check'):
            del data['month_check']
        if data.get('Tag'):
            del data['Tag']
        del data['authorized']
        del data['tax']
        del data['position']
        new_customers.append(data)
    return new_customers

data = change_database('/content/sample_data/chatbot-mango-export-3.json', 'RestCustomer', 5)
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, OneHotEncoder, MultiLabelBinarizer
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline
from sklearn.cluster import KMeans

df = pd.DataFrame(data)
df = df.drop(['day', 'img', 'month', 'year', 'Tag', 'id', 'datetime', 'date_insert', 'time_insert', 'datetime_insert'], axis=1)
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].dt.strftime('%m/%d/%Y')
df

# drop dataframe and chang object to type datetime
df['channel'].loc[df['channel'] == 'contact'] = 'Contact'
df['product'].loc[df['product'] == 'Mango ERP (Construction)'] = 'Construction'
df['product'].loc[df['product'] == 'Mango ERP (Real Estate)'] = 'RealEstate'
df['product'].loc[df['product'] == 'Real Estates'] = 'RealEstate'
df['product'].loc[df['product'] == 'Pusit (Consulting)'] = 'Consulting'

events = df.loc[df['channel'] == 'event Impact']
df.drop(events.index, inplace=True)
df.info()


# drop channel of event Impact
event_realtech = df.loc[df['channel'] == 'Event Realtech']
facebook = df.loc[df['channel'] == 'facebook']

df.drop(event_realtech.index, inplace=True)
df.drop(facebook.index, inplace=True)

df = df.reset_index()
df

# cleaning delete channel 
print(set(df['product']))
print(set(df['channel']))


df1 = df[['product', 'channel', 'date']]
# df1['date'] = pd.to_datetime(df1['date'])
# x['time'] = pd.to_datetime(x['time'], format='%H:%M:%S')
# x['date'] = [date.date() for date in x['date']]
# x['time'] = [time.time() for time in x['time']]

df1
# iloc (row, col)
x = df1.reset_index()[['date']]

x['date'] = pd.to_datetime(df1['date'])
x['year'] = [x.iloc[i, 0].year for i in range(len(x))]
x['month'] = [x.iloc[i, 0].month for i in range(len(x))]
x['day'] = [x.iloc[i, 0].day for i in range(len(x))]
x['english_day'] = x.date.dt.strftime('%a')
x = x.drop(['date'], axis=1)

x
# split day, month, year from date
x = x.astype({
    'month': 'category',
    'day': 'category',
    'english_day': 'category'
    })

x.info()
# set change type
# x = x.merge(df1[['channel', 'product']], left_index=True, right_index=True).set_index(df['date'])
x = x.merge(df1[['channel']], left_index=True, right_index=True).set_index(df['date'])
x

cat_df = x.select_dtypes(include=['category']).columns.to_list()
one_hot_df = pd.get_dummies(x, columns=cat_df)
one_hot_df
class MultiColumnLabelEncoder:

    def __init__(self, columns=None):
        self.columns = columns # array of column names to encode


    def fit(self, X, y=None):
        self.encoders = {}
        columns = X.columns if self.columns is None else self.columns
        for col in columns:
            self.encoders[col] = LabelEncoder().fit(X[col])
        return self


    def transform(self, X):
        output = X.copy()
        columns = X.columns if self.columns is None else self.columns
        for col in columns:
          output[col] = self.encoders[col].transform(X[col])
        return output


    def fit_transform(self, X, y=None):
        return self.fit(X,y).transform(X)


    def inverse_transform(self, X):
        output = X.copy()
        columns = X.columns if self.columns is None else self.columns
        for col in columns:
            output[col] = self.encoders[col].inverse_transform(X[col])
        return output
# multi = MultiColumnLabelEncoder(columns=['channel', 'product'])
multi = MultiColumnLabelEncoder(columns=['channel'])
X = multi.fit_transform(one_hot_df)
# display(set(X['product']))
display(set(X['channel']))
X
inv = multi.inverse_transform(X)
# display(set(inv['product']))
display(set(inv['channel']))
inv
# transform _y
le = LabelEncoder()
y = df1[['product']]
y = le.fit_transform(y)
display(set(le.inverse_transform(y)))
y
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=.4)


classifier = KNeighborsClassifier()
classifier.fit(x_train, y_train)
prediction = classifier.predict(x_test)
print('predict', prediction)
print('\n')
print('actual', y_test)

acc = accuracy_score(y_test, prediction)
print(acc)
!pip freeze > requirements.txt<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16928207140054924, 'fraction_numerical': 0.0033346410357002745, 'mean_word_length': 4.55446623093682, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '434982', 'license': 'Apache-2.0', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1743, 'n_words': 395, 'path': 'machine_leanning/predict_product_each_day.ipynb', 'repo_name': 'watcharap0n/customers-mango', 'size': '5098'}