1

I want to calculate the mode of a big numpy array for every 16 elements in the array. I saved the array in a csv file. I opened the file and for every 16 elements, I want to calculate the mode and save the result in another file. If there are no more than 16 elements, I want to perform majority voting anyway. I uploaded the file of predictions. I tried this code below

Thank you

import csv
from scipy.stats import mode
import numpy as np

with open("prediction-pairs-overall.csv") as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')

    for i in readCSV:
        values,counts = np.unique(readCSV, return_counts=True)   
        index = np.argmax(counts)  ##maximum values



        mode=values[index]
        print(mode)
        #input()
Nischay
  • 11
  • 2

1 Answers1

0

You should calculate unique(i) instead of unique(readCSV).

import csv
import numpy as np

with open("prediction-pairs-overall.csv") as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for i in readCSV:
        values, counts = np.unique(i, return_counts=True)   
        index = np.argmax(counts)  ##maximum values

        mode = values[index]
        print(mode)
AboAmmar
  • 5,439
  • 2
  • 13
  • 24