<jupyter_start><jupyter_text>View Original Data<jupyter_code>train_df.shape<jupyter_output><empty_output><jupyter_text>Exmine NULL values<jupyter_code>train_df.isna().sum()
### Exmine How many row has missing brewy names
brew_name_null = train_df[train_df['brewery_name'].isnull()]
brew_name_null
## 15 rows missing for brewy-names 1193 x 9 and 27 X 6
### Drop the rows if "brewy_name" is NULL
### Select only non-null value of brewry_name
df_cleaned = train_df.copy()
df_cleaned = df_cleaned[df_cleaned['brewery_name'].notnull()]
df_cleaned.head()
#df_cleaned = train_df.copy()
col_to_drop = ['beer_beerid', 'brewery_id', "review_profilename", "beer_name", "review_time"]
target_column = 'beer_style'

df_cleaned = df_cleaned.drop(col_to_drop, axis = 1)

#df_cleaned.head()
df_cleaned.isna().sum()
df_cleaned.describe()<jupyter_output><empty_output><jupyter_text>Label Encoding to Target Column<jupyter_code>import pandas as pd
from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
df_cleaned['beer_style_encoded'] = le.fit_transform(df_cleaned['beer_style'])


# df_cleaned['brewery_name_encoded'] = le.fit_transform(df_cleaned['brewery_name'])
# df_cleaned.head()
df_cleaned['beer_style_encoded'].value_counts().reset_index()<jupyter_output><empty_output><jupyter_text>Drop the Beer_Style column<jupyter_code>beer_style = df_cleaned.pop('beer_style')
#brewery_name = df_cleaned.pop('brewery_name')
df_cleaned.head()
#df_cleaned['brewery_name_encoded'].value_counts().reset_index()
#np.unique(df_cleaned[['brewery_name_encoded']])<jupyter_output><empty_output><jupyter_text>Subset the dataset for experiment as the original dataset is huge in size ,1500K observations<jupyter_code>from src.data.sets import split_sets_random, save_sets, load_sets, subset_x_y

#target_column_new = 'beer_style_encoded'
features = ["review_overall", "review_aroma","review_appearance", "review_palate", "review_taste", "beer_abv" , "brewery_name"]


y_main, X_main = subset_x_y(df_cleaned[features], df_cleaned['beer_style_encoded'], 0, 200000)

# y_val, X_val = subset_x_y(df_cleaned[features], df_cleaned['beer_style_encoded'], 100000, 200000)
# y_test, X_test = subset_x_y(df_cleaned[features], df_cleaned['beer_style_encoded'], 200000, 300000)
#len(X_train[0])
y_main<jupyter_output><empty_output><jupyter_text>Scaling<jupyter_code>from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
num_cols = ['review_overall', 'review_aroma', 'review_appearance', 'review_palate', 'review_taste', 'beer_abv']

X_main_copy = X_main.copy()
X_main_copy.head()
X_main_copy[num_cols] = sc.fit_transform(X_main_copy[num_cols])
X_main_scaled = X_main_copy.copy()
###### AFTER SCALING #####
X_main_scaled['beer_style'] = y_main
X_main_scaled.head()<jupyter_output><empty_output><jupyter_text>One Hot Encoding the feature - "brewery_name"<jupyter_code>from sklearn.preprocessing import OneHotEncoder

ohe = OneHotEncoder(sparse=False)

X_main_cat_enc = pd.DataFrame(ohe.fit_transform(X_main_scaled[['brewery_name']]))
X_main_cat_enc.columns = ohe.get_feature_names(['brewery_name'])

X_main_scaled.drop('brewery_name', axis = 1, inplace=True)
X_main_scaled_encoded = pd.concat([X_main_scaled, X_main_cat_enc], axis=1)
X_main_scaled_encoded.head()

# X_train_cat_enc
X_main_scaled_encoded.shape<jupyter_output><empty_output><jupyter_text>Split the encoded and scaled 200k dataset into Train-Val-Test sets<jupyter_code>X_train, y_train, X_val, y_val, X_test, y_test = split_sets_random(X_main_scaled_encoded, "beer_style", test_ratio=0.2, to_numpy=True)
X_train<jupyter_output><empty_output><jupyter_text>Load Pytorch Dataset<jupyter_code>from src.models.pytorch import PytorchDataset

train_dataset = PytorchDataset(X=X_train, y=y_train)
val_dataset = PytorchDataset(X=X_val, y=y_val)
test_dataset = PytorchDataset(X=X_test, y=y_test)
# print(X_train.shape)
# print(X_val.shape)
# print(X_test.shape)
train_dataset<jupyter_output><empty_output><jupyter_text>Baseline Model<jupyter_code>from src.models.null import NullModel

base_model = NullModel(target_type="classification")
y_base = base_model.fit_predict(y_train)
y_base
from src.models.performance import print_reg_perf
print_reg_perf(y_base, y_train, set_name='Training')<jupyter_output>RMSE Training: 1586.89245
MAE Training: 26.471083333333333<jupyter_text>Build Neural Net Model<jupyter_code>import torch as torch
import torch.nn as nn
from src.models.pytorch import PytorchMultiClass
import torch.nn.functional as F
%load_ext autoreload
%autoreload 2

model = PytorchMultiClass(X_train.shape[1])
from src.models.pytorch import get_device
device = get_device()
model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)
from src.models.pytorch import train_classification, test_classification

N_EPOCHS = 10
BATCH_SIZE = 100

for epoch in range(N_EPOCHS):
    train_loss, train_acc = train_classification(train_dataset, model=model, criterion=criterion, optimizer=optimizer, batch_size=BATCH_SIZE, device=device)
    valid_loss, valid_acc = test_classification(val_dataset, model=model, criterion=criterion, batch_size=BATCH_SIZE, device=device)

    print(f'Epoch: {epoch}')
    print(f'\t(train)\t|\tLoss: {train_loss:.4f}\t|\tAcc: {train_acc * 100:.1f}%')
    print(f'\t(valid)\t|\tLoss: {valid_loss:.4f}\t|\tAcc: {valid_acc * 100:.1f}%')
test_loss, test_acc =  test_classification(test_dataset, model=model, criterion=criterion, batch_size=BATCH_SIZE, device=device)
print(f'\tLoss: {test_loss:4f}\t | \tAccuracy:{test_acc:.1f}')
torch.save(model, "../models/pytorch_multi_beer_type_prediction.pt")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12360347579358043, 'fraction_numerical': 0.017910977123603476, 'mean_word_length': 9.502793296089385, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '445794', 'license': 'MIT', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2148, 'n_words': 333, 'path': 'notebooks_old/6_Preparation_Encoding_Scaling_withoutReviewTime_sampleSize200k.ipynb', 'repo_name': 'HninPwint/beer_type_prediction', 'size': '5639'}