<jupyter_start><jupyter_text>Create Dummies Variable for the Category<jupyter_code>df_dummies = pd.get_dummies(df, prefix_sep='_', drop_first=True,columns = ['Category'])
df_dummies.head()
# import labelencoder
from sklearn.preprocessing import LabelEncoder
# instantiate labelencoder object
le = LabelEncoder()
# apply le on categorical feature columns
#label the Category in different numbers in the same column 
df_Multi_Category = df
df_Multi_Category.head()
df_Multi_Category['Category'].head()
df_Multi_Category['Category'] = le.fit_transform(df_Multi_Category['Category'])
df_Multi_Category.head(10)
unique_apps = to_keep

print("Creating an array containing arrays of reviews of different apps")
reviews_by_app = [df[df["App"] == unique_apps[i]]["Preprocessed_Review"].to_numpy() for i in range(len(to_keep))]
reviews_by_app
print("Creating an array containing arrays of reviews' sentiment polarity of different apps")
review_sentiment_by_app = [df[df["App"] == unique_apps[i]]["Sentiment_Rating"].to_numpy() for i in range(len(to_keep))]

print("Creating an array containing the actual average rating of different apps")
avr_rating_per_app = [df[df["App"] == unique_apps[i]]["Average_Rating"].to_numpy() for i in range(len(to_keep))]

print(len(review_sentiment_by_app))<jupyter_output>Creating an array containing arrays of reviews of different apps
Creating an array containing arrays of reviews' sentiment polarity of different apps
Creating an array containing the actual average rating of different apps
104<jupyter_text>Using the sentiment polarity as the `x` input with a simple neural network model consisting of 2 layers.<jupyter_code>from torch import tensor
from torch.autograd import Variable
from torch.utils.data import Dataset
from torch.utils.data import DataLoader, random_split

class SentimentDataset(Dataset):
  def __init__(self, x, y):
    self.samples = x
    self.labels = y
  
  def __len__(self):
    return len(self.samples)
  
  def __getitem__(self,idx):
    item = self.samples[idx]
    item = np.random.choice(item, size=50) # sampling 28 reviews from each app
    return tensor(item, dtype=torch.float), tensor(self.labels[idx][0], dtype=torch.float)

x = review_sentiment_by_app
y = avr_rating_per_app

train_size = int(0.7*len(x))
val_size = len(x) - train_size

data = SentimentDataset(review_sentiment_by_app, avr_rating_per_app)
trainset, valset = random_split(data, [train_size, val_size])
BATCH_SIZE = 5
train_dataloader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)
val_dataloader = DataLoader(valset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)

print("Training dataloader has ", len(train_dataloader), "batches of ", BATCH_SIZE)
print("Validation dataloader has ", len(val_dataloader), "batches of ", BATCH_SIZE)<jupyter_output><empty_output><jupyter_text>NORMALIZE LABEL FROM 0 - 1, INPUT NORMALIZE TO 0 - 1<jupyter_code># !pip install tensorboard
# !tensorboard --logdir=runs
# from torch.utils.tensorboard import SummaryWriter
# import torchvision

import torch
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
import matplotlib.pyplot as plt

#RNN module
class Net(nn.Module):
  def __init__(self, n_feature, n_hidden, n_output):
        super(Net, self).__init__()
        self.hidden = nn.Linear(n_feature, n_hidden)   # hidden layer
        self.predict = nn.Linear(n_hidden, n_output)   # output layer

  def forward(self, x):
        #xn = F.sigmoid(self.hidden(x))
        x = F.relu(self.hidden(x))  # activation function for hidden layer
        x = self.predict(x)             # linear output
        return x

INPUT_SIZE = num_reviews_per_app
OUTPUT_SIZE = 1 # regression to reach average rating
HIDDEN_SIZE = 30 # arbitrary
learning_rate = 0.0002

net = Net(n_feature=INPUT_SIZE, n_hidden=HIDDEN_SIZE, n_output=OUTPUT_SIZE)
print(net)
optimizer = optim.Adam(net.parameters(), lr=learning_rate)
loss_func = nn.MSELoss()
# writer = SummaryWriter()

num_epochs = 100
losses = []
for i in range(num_epochs):
  for batch_idx, samples in enumerate(train_dataloader):
    x, y = samples
    
    prediction = net(x)
    loss = loss_func(prediction, y)
    optimizer.zero_grad()   # clear gradients for next train
    loss.backward()         # backpropagation, compute gradients
    optimizer.step()        # apply gradients

    # for plotting
  if i % 10 == 0:
      print("Epoch ", i+10, ", Loss: ", loss)
  losses.append(loss)

plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.plot(range(len(losses)),losses)
losses = []
for batch_idx, samples in enumerate(val_dataloader):
  x, y = samples
  prediction = net(x)
  #print("Given reviews", x)
  #print("Actual Average Rating", y)
  #print("Predicted ", prediction)
  loss = loss_func(prediction, y)
  print("Loss", loss)
  losses.append(loss)
  aver_MSE_loss = sum(losses)/len(losses)
print(aver_MSE_loss)  
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.plot(range(len(losses)),losses)
# #LSTM module
# class Net(nn.Module):
#     def __init__(self, n_feature, n_hidden, n_output):
#         super(Net, self).__init__()
        
#         # Recurrent layer
#         self.lstm = nn.LSTM(input_size=n_feature,
#                          hidden_size=n_hidden,
#                          num_layers=1,
#                          bidirectional=False)
        
#         # Output layer
#         self.l_out = nn.Linear(in_features=50,
#                             out_features=n_output,
#                             bias=False)
        
#     def forward(self, x):
#         # RNN returns output and last hidden state
#         x, (h, c) = self.lstm(x)
        
#         # Flatten output for feed-forward layer
#         x = x.view(-1, self.lstm.hidden_size)
        
#         # Output layer
#         x = self.l_out(x)

#         return x

# net = Net(n_feature=INPUT_SIZE, n_hidden=HIDDEN_SIZE, n_output=OUTPUT_SIZE)
# print(net)
# optimizer = optim.Adam(net.parameters(), lr=learning_rate)
# loss_func = nn.MSELoss()
# # writer = SummaryWriter()

# num_epochs = 100
# losses = []
# for i in range(num_epochs):
#   for batch_idx, samples in enumerate(train_dataloader):
#     x, y = samples
    
#     prediction = net(x)
#     #print(prediction.dim)
#     loss = loss_func(prediction, y)
#     optimizer.zero_grad()   # clear gradients for next train
#     loss.backward()         # backpropagation, compute gradients
#     optimizer.step()        # apply gradients

#     # for plotting
#   if i % 10 == 0:
#       print("Epoch ", i, ", Loss: ", loss)
#   losses.append(loss)

# plt.xlabel("Epoch")
# plt.ylabel("Loss")
# plt.plot(range(len(losses)),losses)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11324722138780415, 'fraction_numerical': 0.006908981676179033, 'mean_word_length': 3.9546130952380953, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '525486', 'license': 'MIT', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2201, 'n_words': 538, 'path': 'analysis/Predict_Rating_via_RNN.ipynb', 'repo_name': 'lyqht/googleplaystore-analytics', 'size': '6658'}