<jupyter_start><jupyter_text>EVALUATION PARAMETERS<jupyter_code>dataset2_size = 1000<jupyter_output><empty_output><jupyter_text>Class definition<jupyter_code>def encode_column(df, column_name, new_column_name): 
        classes = [] 
        new_values = []
        df[new_column_name] = df[column_name]
        for i in range(len(df.index)): 
            if df.iloc[i][column_name] not in classes: 
                classes.append(df.iloc[i][column_name] ) 
        for i in range(len(classes)): 
            for j in range(len(df.index)): 
                if df.iloc[j][column_name] == classes[i]:
                    df.set_value(j, new_column_name, float(i))

        return df
class NaiveBayesDefinition():
    # the categorical class names are changed to numberic data 
    # eg: yes and no encoded to 1 and 0 
    def encode_class(self, mydata): 
        classes = [] 
        mapped_encoded_classes = []
        for i in range(len(mydata)): 
            if mydata[i][-1] not in classes: 
                classes.append(mydata[i][-1]) 
        for i in range(len(classes)): 
            mapped_encoded_classes.append(i)
            for j in range(len(mydata)): 
                if mydata[j][-1] == classes[i]: 
                    mydata[j][-1] = i
                    
        print(classes)
        print(mapped_encoded_classes)
        return mydata             


    # Splitting the data 
    def splitting(self, mydata, ratio): 
        train_num = int(len(mydata) * ratio) 
        train = [] 
        # initally testset will have all the dataset  
        test = list(mydata) 
        while len(train) < train_num: 
            # index generated randomly from range 0  
            # to length of testset 
            index = random.randrange(len(test)) 
            # from testset, pop data rows and put it in train 
            train.append(test.pop(index)) 
        return train, test 


    # Group the data rows under each class yes or  
    # no in dictionary eg: dict[yes] and dict[no]  
    def groupUnderClass(self, mydata): 
        dict = {} 
        for i in range(len(mydata)): 
            if (mydata[i][-1] not in dict): 
                dict[mydata[i][-1]] = [] 
            dict[mydata[i][-1]].append(mydata[i]) 
        return dict


    # Calculating Mean 
    def mean(self, numbers): 
        return sum(numbers) / float(len(numbers)) 

    # Calculating Standard Deviation 
    def std_dev(self, numbers): 
        avg = self.mean(numbers)
        if (len(numbers) <= 1):
            variance = sum([pow(x - avg, 2) for x in numbers]) / float(2 - 1) 
        else:
            variance = sum([pow(x - avg, 2) for x in numbers]) / float(len(numbers) - 1) 
        return math.sqrt(variance) 

    def trainClass(self, mydata): 
        info = [(self.mean(attribute), self.std_dev(attribute)) for attribute in zip(*mydata)] 
        # eg: list = [ [a, b, c], [m, n, o], [x, y, z]] 
        # here mean of 1st attribute =(a + m+x), mean of 2nd attribute = (b + n+y)/3 
        # delete summaries of last class 
        del info[-1] 
        return info 

    # find Mean and Standard Deviation under each class 
    def train(self, mydata): 
        info = {} 
        dict = self.groupUnderClass(mydata)
        for classValue, instances in dict.items():
            info[classValue] = self.trainClass(instances) 
        return info 


    # Calculate Gaussian Probability Density Function 
    def calculateGaussianProbability(self, x, mean, stdev): 
        if(stdev == 0):
            expo = math.exp(-(math.pow(x - mean, 2) / (2 * math.pow(1, 2))))
            return (1 / (math.sqrt(2 * math.pi) * 1)) * expo 
        else:
            expo = math.exp(-(math.pow(x - mean, 2) / (2 * math.pow(stdev, 2)))) 
            return (1 / (math.sqrt(2 * math.pi) * stdev)) * expo 


    # Calculate Class Probabilities 
    def calculateClassProbabilities(self, info, test): 
        probabilities = {} 
        for classValue, classSummaries in info.items(): 
            probabilities[classValue] = 1
            for i in range(len(classSummaries)): 
                mean, std_dev = classSummaries[i] 
                x = test[i] 
                probabilities[classValue] *= self.calculateGaussianProbability(x, mean, std_dev) 
        return probabilities 


    # Make prediction - highest probability is the prediction 
    def predict(self, info, test): 
        probabilities = self.calculateClassProbabilities(info, test) 
        bestLabel, bestProb = None, -1
        for classValue, probability in probabilities.items(): 
            if bestLabel is None or probability > bestProb: 
                bestProb = probability 
                bestLabel = classValue 
        return bestLabel 


    # returns predictions for a set of examples 
    def getPredictions(self, info, test): 
        predictions = [] 
        for i in range(len(test)): 
            result = self.predict(info, test[i]) 
            predictions.append(result) 
        return predictions 

    # Accuracy score 
    def accuracy_rate(self, test, predictions): 
        correct = 0
        for i in range(len(test)): 
            if test[i][-1] == predictions[i]: 
                correct += 1
        return (correct / float(len(test))) * 100.0
    
    # Accuracy score 
    def error_avg(self, test, predictions): 
        correct = 0
        for i in range(len(test)): 
            if test[i][-1] == predictions[i]: 
                correct += 1
                
        errors = len(test) - correct
        return np.mean(errors)
    
    def visualize(self, y_true, y_pred, target):
        
        tr = pd.DataFrame(data=y_true, columns=[target])
        pr = pd.DataFrame(data=y_pred, columns=[target])
        
        
        fig, ax = plt.subplots(1, 2, sharex='col', sharey='row', figsize=(15,6))
        
        sns.countplot(x=target, data=tr, ax=ax[0], palette='viridis', alpha=0.7, hue=target, dodge=False)
        sns.countplot(x=target, data=pr, ax=ax[1], palette='viridis', alpha=0.7, hue=target, dodge=False)
        

        fig.suptitle('True vs Predicted Comparison', fontsize=20)

        ax[0].tick_params(labelsize=12)
        ax[1].tick_params(labelsize=12)
        ax[0].set_title("True values", fontsize=18)
        ax[1].set_title("Predicted values", fontsize=18)
        plt.show()<jupyter_output><empty_output><jupyter_text>DATASET 1  Read and clean dataset<jupyter_code>df = pd.read_csv("Set1.txt", delimiter = "\t", names=["P1", "P2", "P3", "P4", "P5", "P6", "P7", "Floor"])
df
df.P1.mode()
df.P2.mode()
df.P3.mode()
df.P4.mode()
df.P5.mode()
df.P6.mode()
df.P7.mode()
df_tmp = df.apply(pd.Series.value_counts)
df_tmp.div(df.shape[0] * 0.01 , axis=0)<jupyter_output><empty_output><jupyter_text>Perform analysis  Prepare data<jupyter_code>model = NaiveBayesDefinition()
# set features and target
mydata = df.values.tolist() 
mydata = model.encode_class(mydata) 
for i in range(len(mydata)): 
    mydata[i] = [float(x) for x in mydata[i]]<jupyter_output>[1, 2, 3, 4]
[0, 1, 2, 3]<jupyter_text>Split into train and test data<jupyter_code># split ratio = 0.7  
# 70% of data is training data and 30% is test data used for testing 
ratio = 0.7
train_data, test_data = model.splitting(mydata, ratio) 
print('Total number of examples are: ', len(mydata)) 
print('Out of these, training examples are: ', len(train_data)) 
print("Test examples are: ", len(test_data))<jupyter_output>Total number of examples are:  2000
Out of these, training examples are:  1400
Test examples are:  600<jupyter_text>Train model<jupyter_code>start_time = time.time()
# prepare model 
info = model.train(train_data) 
train_time = time.time() - start_time
print("Trainning time is: ", train_time)<jupyter_output>Trainning time is:  0.33809566497802734<jupyter_text>Test model and get predictions<jupyter_code>start_time = time.time()
# test model
predictions = model.getPredictions(info, test_data) 
prediction_time = time.time() - start_time
print("Prediction time is: ", prediction_time) 
predictions<jupyter_output><empty_output><jupyter_text>Calculate accuracy<jupyter_code>test_data
accuracy = model.accuracy_rate(test_data, predictions) 
print("Accuracy of your model is: ", accuracy) 
mean_error = model.error_avg(test_data, predictions)
print("Mean avg error of your model is: ", mean_error) 
labels = [item[-1] for item in test_data]<jupyter_output><empty_output><jupyter_text>Test other combinations<jupyter_code>trainning_ratios = [0.15, 0.5, 0.7, 0.8]
for tmp_ratio in trainning_ratios:
    tmp_train_data, tmp_test_data = model.splitting(mydata, tmp_ratio) 
    print('Total number of examples are: ', len(mydata)) 
    print('Out of these, training examples are: ', len(tmp_train_data)) 
    print("Test examples are: ", len(tmp_test_data)) 
    
    # prepare model
    tmp_start_time = time.time()
    tmp_info = model.train(tmp_train_data) 
    tmp_train_time = time.time() - tmp_start_time
    print("Trainning time is: ", tmp_train_time) 
    
    # test model
    tmp_start_time = time.time()
    tmp_predictions = model.getPredictions(tmp_info, tmp_test_data) 
    tmp_test_time = time.time() - tmp_start_time
    print("Prediction time is: ", tmp_test_time) 
    
    tmp_accuracy = model.accuracy_rate(tmp_test_data, tmp_predictions) 
    print("Accuracy of your model is: ", tmp_accuracy) 
    
    tmp_mean_error = model.error_avg(tmp_test_data, tmp_predictions)
    print("Mean avg error of your model is: ", tmp_mean_error) 
    print()
    print()<jupyter_output>Total number of examples are:  2000
Out of these, training examples are:  300
Test examples are:  1700
Trainning time is:  0.001995086669921875
Prediction time is:  0.12466621398925781
Accuracy of your model is:  98.05882352941177
Mean avg error of your model is:  33.0


Total number of examples are:  2000
Out of these, training examples are:  1000
Test examples are:  1000
Trainning time is:  0.004986763000488281
Prediction time is:  0.08976078033447266
Accuracy of your model is:  98.3
Mean avg error of your model is:  17.0


Total number of examples are:  2000
Out of these, training examples are:  1400
Test examples are:  600
Trainning time is:  0.005983829498291016
Prediction time is:  0.04986715316772461
Accuracy of your model is:  99.0
Mean avg error of your model is:  6.0


Total number of examples are:  2000
Out of these, training examples are:  1600
Test examples are:  400
Trainning time is:  0.009973526000976562
Prediction time is:  0.02892160415649414
Accuracy of your model is[...]<jupyter_text>Visualization: https://towardsdatascience.com/introduction-to-data-visualization-in-python-89a54c97fbed<jupyter_code># Visualize labels in test data versus predictions and number of 
# equivalente values
model.visualize(labels, predictions, 'Floor')
# Representation of value frequency
df.plot.hist(subplots=True, layout=(2,4), figsize=(10, 10), bins=20)
# Values of P1 for each floor
df.groupby("Floor").P1.mean().sort_values(ascending=False)[:5].plot.bar()
# Values of P2 for each floor
df.groupby("Floor").P2.mean().sort_values(ascending=False)[:5].plot.bar()
# Values of P3 for each floor
df.groupby("Floor").P3.mean().sort_values(ascending=False)[:5].plot.bar()
# Values of P2 for each floor
df.groupby("Floor").P4.mean().sort_values(ascending=False)[:5].plot.bar()<jupyter_output><empty_output><jupyter_text>...<jupyter_code># Scatter of P1 - P2
sns.scatterplot(x='P1', y='P2', hue='Floor', data=df)
# Confusion matrix
sns.heatmap(df.drop(['Floor'], axis=1).corr(), annot=True)
# Confusion matrix
floors = list(df['Floor'].unique())
mat = confusion_matrix(labels, predictions)
sns.heatmap(mat.T, square=True, annot=True, fmt='d', cbar=False,
            xticklabels=floors, yticklabels=floors)
plt.xlabel('true label')
plt.ylabel('predicted label');
# Faceting is the act of breaking data variables up across multiple subplots 
# and combining those subplots into a single figure.
# In this case it will be the points of P1 per floor
g = sns.FacetGrid(df, col='Floor')
g = g.map(sns.kdeplot, 'P1')
# Sparse matrix
fig, ax = plt.subplots(figsize=(12,12))
scatter_matrix(df.drop(['Floor'], axis=1), alpha=1, ax=ax)<jupyter_output>C:\Users\Luis\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: UserWarning: To output multiple subplots, the figure containing the passed axes is being cleared
  This is separate from the ipykernel package so we can avoid doing imports until<jupyter_text>DATASET 2  Read an clean dataset<jupyter_code># read
#xl = pd.ExcelFile("Set2.xlsx")
#xl.sheet_names
#df = xl.parse("f3b1u17p22")
df = pd.read_excel("Set2.xlsx")
df<jupyter_output><empty_output><jupyter_text>As the dataset is big and it could cause an overflow, we will limit it to 5000 elements<jupyter_code>df = df[:dataset2_size]
df["FLOOR_BUILDING"] = df["FLOOR"].astype(str) + "_" + df["BUILDINGID"].astype(str)
# Delete unnecesary columns
df = df.drop(labels=['FLOOR', 'BUILDINGID', 'SPACEID', 'USERID', 'PHONEID', 'TIMESTAMP'], axis=1)
df<jupyter_output><empty_output><jupyter_text>Perform analysis The analisis performed, conssits in a classification with naive bayes, that ....  Prepare data<jupyter_code>model2 = NaiveBayesDefinition()
# set features and target
mydata = df.values.tolist() 
mydata = model2.encode_class(mydata) 
for i in range(len(mydata) -1): 
    mydata[i] = [float(x) for x in mydata[i]]<jupyter_output>['2_1', '0_0', '3_2', '2_2']
[0, 1, 2, 3]<jupyter_text>Split into train and test data<jupyter_code># split ratio = 0.7  
# 70% of data is training data and 30% is test data used for testing 
ratio = 0.7
train_data, test_data = model2.splitting(mydata, ratio) 
print('Total number of examples are: ', len(mydata)) 
print('Out of these, training examples are: ', len(train_data)) 
print("Test examples are: ", len(test_data))<jupyter_output>Total number of examples are:  1000
Out of these, training examples are:  700
Test examples are:  300<jupyter_text>Train model<jupyter_code># prepare model 
info = model2.train(train_data)<jupyter_output><empty_output><jupyter_text>Test model and get predictions<jupyter_code># test model 
predictions = model2.getPredictions(info, test_data)<jupyter_output><empty_output><jupyter_text>Calculate accuracy<jupyter_code>accuracy = model2.accuracy_rate(test_data, predictions) 
print("Accuracy of your model is: ", accuracy) 
mean_error = model.error_avg(test_data, predictions)
print("Mean avg error of your model is: ", mean_error) 
predictions
# 0 => '2_1', 1 => '0_0', 2=> '3_2', 3=>'2_2']
labels = [item[-1] for item in test_data]
### Test other combinations
trainning_ratios = [0.15, 0.5, 0.7, 0.8]
for tmp_ratio in trainning_ratios:
    tmp_train_data, tmp_test_data = model2.splitting(mydata, tmp_ratio) 
    print('Total number of examples are: ', len(mydata)) 
    print('Out of these, training examples are: ', len(tmp_train_data)) 
    print("Test examples are: ", len(tmp_test_data)) 
    
    # prepare model
    tmp_start_time = time.time()
    tmp_info = model2.train(tmp_train_data) 
    tmp_train_time = time.time() - tmp_start_time
    print("Trainning time is: ", tmp_train_time) 
    
    # test model
    tmp_start_time = time.time()
    tmp_predictions = model2.getPredictions(tmp_info, tmp_test_data) 
    tmp_test_time = time.time() - tmp_start_time
    print("Prediction time is: ", tmp_test_time) 
    
    tmp_accuracy = model2.accuracy_rate(tmp_test_data, tmp_predictions) 
    print("Accuracy of your model is: ", tmp_accuracy) 
    
    tmp_mean_error = model2.error_avg(tmp_test_data, tmp_predictions)
    print("Mean avg error of your model is: ", tmp_mean_error) 
    print()
    print()<jupyter_output>Total number of examples are:  1000
Out of these, training examples are:  150
Test examples are:  850
Trainning time is:  0.015956878662109375
Prediction time is:  1.328446865081787
Accuracy of your model is:  55.17647058823529
Mean avg error of your model is:  381.0


Total number of examples are:  1000
Out of these, training examples are:  500
Test examples are:  500
Trainning time is:  0.0498659610748291
Prediction time is:  0.8606975078582764
Accuracy of your model is:  56.8
Mean avg error of your model is:  216.0


Total number of examples are:  1000
Out of these, training examples are:  700
Test examples are:  300
Trainning time is:  0.07081031799316406
Prediction time is:  0.3470728397369385
Accuracy of your model is:  53.333333333333336
Mean avg error of your model is:  140.0


Total number of examples are:  1000
Out of these, training examples are:  800
Test examples are:  200
Trainning time is:  0.08676815032958984
Prediction time is:  0.45976901054382324
Accuracy of your mod[...]<jupyter_text>Visualization<jupyter_code># Visualize labels in test data versus predictions and number of 
# equivalente values
model2.visualize(labels, predictions, 'FLOOR_BUILDING')
sub_df = encode_column(df, "FLOOR_BUILDING", "FLOOR_BUILDING_ENCODED")
sub_df
# Scatter of P1 - P2
#sns.scatterplot(x='WAP005', y='WAP250', hue='FLOOR_BUILDING_ENCODED', data=sub_df)
# Confusion matrix
sns.heatmap(sub_df.drop(['FLOOR_BUILDING_ENCODED'], axis=1).corr(), annot=True)
# Confusion matrix
floors = list(df['FLOOR_BUILDING_ENCODED'].unique())
mat = confusion_matrix(labels, predictions)
sns.heatmap(mat.T, square=True, annot=True, fmt='d', cbar=False,
            xticklabels=floors, yticklabels=floors)
plt.xlabel('true label')
plt.ylabel('predicted label');
# Faceting is the act of breaking data variables up across multiple subplots 
# and combining those subplots into a single figure.
# In this case it will be the points of P1 per floor
g = sns.FacetGrid(sub_df, col='FLOOR_BUILDING_ENCODED')
g = g.map(sns.kdeplot, 'WAP008')
# Sparse matrix
fig, ax = plt.subplots(figsize=(12,12))
scatter_matrix(sub_df.drop(['FLOOR_BUILDING_ENCODED'], axis=1), alpha=1, ax=ax)<jupyter_output>C:\Users\Luis\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: UserWarning: To output multiple subplots, the figure containing the passed axes is being cleared
  This is separate from the ipykernel package so we can avoid doing imports until
{'fraction_non_alphanumeric': 0.11393456236505564, 'fraction_numerical': 0.041189171234014284, 'mean_word_length': 3.572007086813465, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 73, 'https://': 1, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '243634', 'license': 'MIT', 'n_tokens_mistral': 6644, 'n_tokens_neox': 5998, 'n_words': 1611, 'path': 'notebooks/naive-bayes.ipynb', 'repo_name': 'luisblazquezm/mii2-classification-models-study', 'size': '18063'}