# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="jBruMxYBLkIE" colab_type="code" colab={}
from keras.models import Model
from keras.layers import Input, Dense

class NN():
 
  def __init__(self,train_business,alpha,output_dim):
        
        # the shape of one training example is

        self.alpha = alpha
        business_inp, business_model = self.createModel(train_business)
   
        if output_dim>1:
                  output_layer = Dense(output_dim,activation='softmax')(business_model)
                  self.model = Model(inputs=business_inp,outputs=output_layer)
                  self.model.compile(optimizer=keras.optimizers.Adam(alpha), loss='categorical_crossentropy', metrics=['accuracy'])
        else:
                  output_layer = Dense(output_dim,activation='sigmoid')(business_model)
                  self.model = Model(inputs=business_inp,outputs=output_layer)
                  self.model.compile(optimizer=keras.optimizers.Adam(alpha), loss='mse', metrics=['accuracy'])
        
        
        
  def createModel(self,training_data):
        input_shape = training_data.shape[1]
        inp_layer = Input(shape=(input_shape,))
        dense_layer = Dense(units=64, activation='relu')(inp_layer)
        dense_layer = Dense(units=32, activation='relu')(dense_layer)

        #model.add(Dense(units=5, activation='softmax'))
        return inp_layer, dense_layer
   
  def train(self,train_business,output_rating,num_iter):
    
        
        self.model.fit(train_business, output_rating, epochs=num_iter)
  
 
  
      


# + id="Iug0FPSNME83" colab_type="code" outputId="5903f435-9241-4d28-d785-495c0e032862" colab={"base_uri": "https://localhost:8080/", "height": 33}
import pandas as pd
from keras.utils import to_categorical
import numpy as np
from keras.layers import Concatenate
import keras

# + id="-bdfKM1fVk95" colab_type="code" outputId="fc3cf1f6-c1a6-4cf4-dc6b-fea6374449e4" colab={"base_uri": "https://localhost:8080/", "height": 120}
from google.colab import drive
drive.mount('/content/gdrive')

# + id="dg0LVmMDC85a" colab_type="code" colab={}
attr_list = ['attributes.Ambience.casual','attributes.BikeParking','attributes.BusinessAcceptsCreditCards','attributes.BusinessParking.street','attributes.HasTV','attributes.RestaurantsDelivery','attributes.GoodForMeal.breakfast','attributes.GoodForMeal.brunch','attributes.GoodForMeal.dessert','attributes.GoodForMeal.dinner','attributes.GoodForMeal.latenight','attributes.GoodForMeal.lunch','attributes.Alcohol','attributes.NoiseLevel']
train_business = pd.read_csv("business_reviews_topic_attr.csv").drop(["business_id"],axis=1).fillna(0)
train_business['attributes.Alcohol'] = train_business['attributes.Alcohol'].astype('category').cat.codes
train_business['attributes.NoiseLevel'] = train_business['attributes.NoiseLevel'].astype('category').cat.codes
#train_business=train_business.drop(["topic_0","topic_1","topic_2","topic_3","topic_4","topic_5","topic_6","topic_7","topic_8","topic_9","topic_10","topic_11","topic_12","topic_13","topic_14","topic_15","topic_16","topic_17","topic_18","topic_19"],axis=1)
#train_business = train_business.loc[:,["topic_0","topic_1","topic_2","topic_3","topic_4","topic_5","topic_6","topic_7","topic_8","topic_9","topic_10","topic_11","topic_12","topic_13","topic_14","topic_15","topic_16","topic_17","topic_18","topic_19","business_stars","is_open","attributes.Ambience.casual","attributes.BikeParking","attributes.BusinessAcceptsCreditCards","attributes.BusinessParking.street","attributes.HasTV","attributes.RestaurantsDelivery","attributes.GoodForMeal.breakfast","attributes.GoodForMeal.brunch","attributes.GoodForMeal.dessert","attributes.GoodForMeal.dinner","attributes.GoodForMeal.latenight","attributes.GoodForMeal.lunch","attributes.Alcohol","attributes.NoiseLevel","attributes.RestaurantsPriceRange2"]]
stacked= train_business[['attributes.Ambience.casual','attributes.BikeParking','attributes.BusinessAcceptsCreditCards','attributes.BusinessParking.street','attributes.HasTV','attributes.RestaurantsDelivery','attributes.GoodForMeal.breakfast','attributes.GoodForMeal.brunch','attributes.GoodForMeal.dessert','attributes.GoodForMeal.dinner','attributes.GoodForMeal.latenight','attributes.GoodForMeal.lunch','attributes.Alcohol','attributes.NoiseLevel']].stack()
train_business[['attributes.Ambience.casual','attributes.BikeParking','attributes.BusinessAcceptsCreditCards','attributes.BusinessParking.street','attributes.HasTV','attributes.RestaurantsDelivery','attributes.GoodForMeal.breakfast','attributes.GoodForMeal.brunch','attributes.GoodForMeal.dessert','attributes.GoodForMeal.dinner','attributes.GoodForMeal.latenight','attributes.GoodForMeal.lunch','attributes.Alcohol','attributes.NoiseLevel']]=pd.Series(stacked.factorize()[0], index=stacked.index).unstack()


# + id="cfGeACqQP1Nf" colab_type="code" colab={}
# with open('/content/gdrive/My Drive/NN-model/train_business.csv', 'w') as train_file:
#   train_business.to_csv(train_file)
  

# + [markdown] id="sZwLx6S_AmSC" colab_type="text"
#
#
# ```
# # This is formatted as code
# ```
#
#

# + id="Kd0qi_CAWm6o" colab_type="code" outputId="aaa4de2a-8575-4279-f900-0f49b930cedf" colab={"base_uri": "https://localhost:8080/", "height": 33471}
import numpy as np
import copy
attr_list = ['attributes.Ambience.casual','attributes.BikeParking','attributes.BusinessAcceptsCreditCards','attributes.BusinessParking.street','attributes.HasTV','attributes.RestaurantsDelivery','attributes.GoodForMeal.breakfast','attributes.GoodForMeal.brunch','attributes.GoodForMeal.dessert','attributes.GoodForMeal.dinner','attributes.GoodForMeal.latenight','attributes.GoodForMeal.lunch','attributes.Alcohol','attributes.NoiseLevel']
best_scores = [0]*14
best_alpha = [0]*14
 
model = NN(train_business,0.00001,1)


## train neural net for each of the attributes in attr_list
attr='attributes.Alcohol'
best_score=100000

alpha = 0.0000001#,0.000005]:



train_output = train_business[attr]
train_output_labels = keras.utils.to_categorical(train_output, num_classes=4)
business_model = NN(train_business,alpha,4)
business_model.train(train_business,train_output_labels,1000)




# + id="9K_bw7SfadVe" colab_type="code" outputId="8a2d4c4b-9f78-4410-9b6a-2ec47a6e5d7b" colab={"base_uri": "https://localhost:8080/", "height": 33}
train_output_labels.shape

# + id="aH_WY9Em2VXp" colab_type="code" outputId="ce815b36-8df1-48c7-9d38-063ccbca70b6" colab={"base_uri": "https://localhost:8080/", "height": 268}
business_model.model.summary()

# + id="fVvPaJO9Wyg6" colab_type="code" colab={}
from sklearn.metrics import mean_squared_error


# + id="XqPuHPD4Xg9O" colab_type="code" outputId="f76ca2d5-a045-440b-a6c7-ad9a0d0a9bb2" colab={"base_uri": "https://localhost:8080/", "height": 67}
print("Training Loss and accuracy: ",business_model.model.evaluate(train_business,train_output_labels))
pred_train=business_model.model.predict(train_business)
# pred_train[pred_train>0.5]=1
# pred_train[pred_train<=0.5]=0

score_train = np.sqrt(mean_squared_error(train_output_labels,pred_train))
print("Training RMSE is: ",score_train)

# + id="gMs2kgJezEyB" colab_type="code" outputId="0bb13023-7b01-4e6d-a93d-b78fd3d87768" colab={"base_uri": "https://localhost:8080/", "height": 569}
test_business = pd.read_csv("business_reviews_topic_attr_test.csv").drop(["business_id"],axis=1).fillna(0)
#train_business = train_business.loc[:,["topic_0","topic_1","topic_2","topic_3","topic_4","topic_5","topic_6","topic_7","topic_8","topic_9","topic_10","topic_11","topic_12","topic_13","topic_14","topic_15","topic_16","topic_17","topic_18","topic_19","business_stars","is_open","attributes.Ambience.casual","attributes.BikeParking","attributes.BusinessAcceptsCreditCards","attributes.BusinessParking.street","attributes.HasTV","attributes.RestaurantsDelivery","attributes.GoodForMeal.breakfast","attributes.GoodForMeal.brunch","attributes.GoodForMeal.dessert","attributes.GoodForMeal.dinner","attributes.GoodForMeal.latenight","attributes.GoodForMeal.lunch","attributes.Alcohol","attributes.NoiseLevel","attributes.RestaurantsPriceRange2"]]
#test_business=test_business.drop(["topic_0","topic_1","topic_2","topic_3","topic_4","topic_5","topic_6","topic_7","topic_8","topic_9","topic_10","topic_11","topic_12","topic_13","topic_14","topic_15","topic_16","topic_17","topic_18","topic_19"],axis=1)
test_business['attributes.Alcohol'] = test_business['attributes.Alcohol'].astype('category').cat.codes
test_business['attributes.NoiseLevel'] = test_business['attributes.NoiseLevel'].astype('category').cat.codes

stacked= test_business[['attributes.Ambience.casual','attributes.BikeParking','attributes.BusinessAcceptsCreditCards','attributes.BusinessParking.street','attributes.HasTV','attributes.RestaurantsDelivery','attributes.GoodForMeal.breakfast','attributes.GoodForMeal.brunch','attributes.GoodForMeal.dessert','attributes.GoodForMeal.dinner','attributes.GoodForMeal.latenight','attributes.GoodForMeal.lunch','attributes.Alcohol','attributes.NoiseLevel']].stack()
test_business[['attributes.Ambience.casual','attributes.BikeParking','attributes.BusinessAcceptsCreditCards','attributes.BusinessParking.street','attributes.HasTV','attributes.RestaurantsDelivery','attributes.GoodForMeal.breakfast','attributes.GoodForMeal.brunch','attributes.GoodForMeal.dessert','attributes.GoodForMeal.dinner','attributes.GoodForMeal.latenight','attributes.GoodForMeal.lunch','attributes.Alcohol','attributes.NoiseLevel']]=pd.Series(stacked.factorize()[0], index=stacked.index).unstack()
#reviews = pd.read_csv("user_business_reviewVec.csv")['review_id']

test_output = test_business[attr]

test_output_labels = keras.utils.to_categorical(test_output, num_classes=4)
#test_output_labels = test_output_labels[:,[0,1,2,4]]
print("Test Loss and accuracy: ",business_model.model.evaluate(test_business,test_output_labels))
pred_test=business_model.model.predict(test_business)
# pred_test[pred_test>0.5]=1
# pred_test[pred_test<=0.5]=0

score_test = np.sqrt(mean_squared_error(test_output_labels,pred_test))
print("Test RMSE is: ",score_test)
pred_test[:30]


# + id="pKMDfBxzzGIp" colab_type="code" outputId="6df4df77-0a83-4454-e09f-f905be178c68" colab={"base_uri": "https://localhost:8080/", "height": 84}
attr_list = ['attributes.Ambience.casual','attributes.BikeParking','attributes.BusinessAcceptsCreditCards','attributes.BusinessParking.street','attributes.HasTV','attributes.RestaurantsDelivery','attributes.GoodForMeal.breakfast','attributes.GoodForMeal.brunch','attributes.GoodForMeal.dessert','attributes.GoodForMeal.dinner','attributes.GoodForMeal.latenight','attributes.GoodForMeal.lunch','attributes.Alcohol','attributes.NoiseLevel']
valid_business = pd.read_csv("business_reviews_topic_attr_valid.csv").drop(["business_id"],axis=1).fillna(0)
#train_business = train_business.loc[:,["topic_0","topic_1","topic_2","topic_3","topic_4","topic_5","topic_6","topic_7","topic_8","topic_9","topic_10","topic_11","topic_12","topic_13","topic_14","topic_15","topic_16","topic_17","topic_18","topic_19","business_stars","is_open","attributes.Ambience.casual","attributes.BikeParking","attributes.BusinessAcceptsCreditCards","attributes.BusinessParking.street","attributes.HasTV","attributes.RestaurantsDelivery","attributes.GoodForMeal.breakfast","attributes.GoodForMeal.brunch","attributes.GoodForMeal.dessert","attributes.GoodForMeal.dinner","attributes.GoodForMeal.latenight","attributes.GoodForMeal.lunch","attributes.Alcohol","attributes.NoiseLevel","attributes.RestaurantsPriceRange2"]]

valid_business['attributes.Alcohol'] = valid_business['attributes.Alcohol'].astype('category').cat.codes
valid_business['attributes.NoiseLevel'] = valid_business['attributes.NoiseLevel'].astype('category').cat.codes


stacked= valid_business[['attributes.Ambience.casual','attributes.BikeParking','attributes.BusinessAcceptsCreditCards','attributes.BusinessParking.street','attributes.HasTV','attributes.RestaurantsDelivery','attributes.GoodForMeal.breakfast','attributes.GoodForMeal.brunch','attributes.GoodForMeal.dessert','attributes.GoodForMeal.dinner','attributes.GoodForMeal.latenight','attributes.GoodForMeal.lunch','attributes.Alcohol','attributes.NoiseLevel']].stack()
valid_business[['attributes.Ambience.casual','attributes.BikeParking','attributes.BusinessAcceptsCreditCards','attributes.BusinessParking.street','attributes.HasTV','attributes.RestaurantsDelivery','attributes.GoodForMeal.breakfast','attributes.GoodForMeal.brunch','attributes.GoodForMeal.dessert','attributes.GoodForMeal.dinner','attributes.GoodForMeal.latenight','attributes.GoodForMeal.lunch','attributes.Alcohol','attributes.NoiseLevel']]=pd.Series(stacked.factorize()[0], index=stacked.index).unstack()
#reviews = pd.read_csv("user_business_reviewVec.csv")['review_id']
valid_output = valid_business[attr]
valid_output = valid_business[attr]
  # Convert labels to categorical one-hot encoding
valid_output_labels = keras.utils.to_categorical(valid_output, num_classes=4)
#valid_output_labels = valid_output_labels[:,[0,1,2,4]]
print("Validation Loss and accuracy: ",business_model.model.evaluate(valid_business,valid_output_labels))
pred_valid = business_model.model.predict(valid_business)
# pred_valid[pred_valid>0.5]=1
# pred_valid[pred_valid<=0.5]=0

score_valid = np.sqrt(mean_squared_error(valid_output_labels,pred_valid))
print("validation RMSE is: ",score_valid)


print(np.sqrt(np.square(np.array(valid_output_labels)-pred_valid)).mean())


# + id="5Xwg04hp5qCn" colab_type="code" colab={}
name1="pred_valid_"+attr+".csv"

with open('/content/gdrive/My Drive/NN-model/'+name1, 'w') as fvalid:
  pred_valid_=np.argmax(pred_valid,axis=1)
  numpy.savetxt(fvalid, pred_valid_, delimiter=",",fmt='%f')


# + id="xQGMWY6f6I46" colab_type="code" colab={}
name2="pred_test_"+attr+".csv"
with open('/content/gdrive/My Drive/NN-model/'+name2, 'w') as ftest:
  pred_test_ = np.argmax(pred_test,axis=1)
  numpy.savetxt(ftest, pred_test_, delimiter=",",fmt='%f')


# + id="RJnisJXjABXl" colab_type="code" outputId="c2e68b3d-d277-4129-e2aa-c8c3dada9b27" colab={"base_uri": "https://localhost:8080/", "height": 33}
rmse_t="rmse_train_"+attr+".csv"

with open('/content/gdrive/My Drive/NN-model/'+rmse_t, 'w') as ftrain:
  ftrain.write("test Loss and accuracy: "+str(business_model.model.evaluate(train_business,train_output_labels)))
  ftrain.write("\nRMSE: "+str(score_train))

# + id="FyPFJWrX5Xgi" colab_type="code" outputId="de7ac4a3-0129-46b9-ca2a-1f4826eda2a6" colab={"base_uri": "https://localhost:8080/", "height": 33}
rmse_v ="rmse_valid_"+attr+".csv"
with open('/content/gdrive/My Drive/NN-model/'+rmse_v, 'w') as fvalid:
  fvalid.write("Validation Loss and accuracy: "+str(business_model.model.evaluate(valid_business,valid_output_labels)[1]))
  fvalid.write("\nRMSE: "+str(score_valid))



# + id="KVsj0SQ39X2e" colab_type="code" outputId="323d551b-55e6-46ab-bebd-7b01ce04cb18" colab={"base_uri": "https://localhost:8080/", "height": 33}
rmse_t="rmse_test_"+attr+".csv"

with open('/content/gdrive/My Drive/NN-model/'+rmse_t, 'w') as ftest:
  ftest.write("test Loss and accuracy: "+str(business_model.model.evaluate(test_business,test_output_labels)))
  ftest.write("\nRMSE: "+str(score_test))


StarcoderdataJupyterScriptsDedupFiltered