<jupyter_start><jupyter_text>Run the A/B test<jupyter_code>"""To run the A/B test we will write python script in the Jupyter notebook that will simulate real life A/B testing. The script will:

read test data,
send sample by sample to the server,
get the server response and send the feedback to the server."""<jupyter_output><empty_output><jupyter_text>import packages<jupyter_code>import json # will be needed for saving preprocessing details
import numpy as np # for data manipulation
import pandas as pd # for data manipulation
from sklearn.model_selection import train_test_split # will be used for data split
import requests<jupyter_output><empty_output><jupyter_text>Importing/Load dataset<jupyter_code>dataset = pd.read_csv('https://raw.githubusercontent.com/pplonski/datasets-for-start/master/adult/data.csv', skipinitialspace=True)

new_dataset = dataset.fillna(method='ffill', axis=0)

new_dataset.head()<jupyter_output><empty_output><jupyter_text>Splitting the dataset into the Training set and Test set<jupyter_code>X = new_dataset.iloc[:, :-1]
y = new_dataset.iloc[:, -1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state=1234)
print(X_train)
print(X_test)<jupyter_output>age  workclass  fnlwgt     education  education-num  \
1055    32    Private   87643     Bachelors             13   
26305   27    Private  207352     Bachelors             13   
9568    73  State-gov   74040       7th-8th              4   
15071   39    Private  174924       HS-grad              9   
2012    31    Private  264936  Some-college             10   
...    ...        ...     ...           ...            ...   
19433   59    Private  367984     Bachelors             13   
12825   26    Private  144483     Assoc-voc             11   
23420   55    Private   98361     Bachelors             13   
6259    24  Local-gov  150084  Some-college             10   
1439    47    Private   67716       HS-grad              9   

           marital-status       occupation   relationship                race  \
1055   Married-civ-spouse            Sales        Husband               White   
26305  Married-civ-spouse     Tech-support        Husband  Asian-Pac-Islander   
9568        [...]<jupyter_text>Let's use first 100 rows of test data for A/B test.<jupyter_code>for i in range(100):
    input_data = dict(X_test.iloc[i])
    target = y_test.iloc[i]
    r = requests.post("http://127.0.0.1:8000/api/v1/income_classifier/predict?status=ab_testing", input_data)
    response =  r.json()
    #provide feedback
    requests.put("http://127.0.0.1:8000/api/v1/mlrequests/{}".format(response["request_id"]), {"feedback": target})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08847583643122676, 'fraction_numerical': 0.07063197026022305, 'mean_word_length': 2.5176470588235293, 'pattern_counts': {'":': 1, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '92867', 'license': 'MIT', 'n_tokens_mistral': 978, 'n_tokens_neox': 864, 'n_words': 222, 'path': 'research/.ipynb_checkpoints/ab_test.ipynb', 'repo_name': 'Kumar021/ml_web_services', 'size': '2690'}