Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

get mode using python

# --------------------- MODE ---------------------

# Dataset for questions -
dataset = [2, 1, 1, 4, 5, 8, 12, 4, 3, 8, 21, 1, 18, 5]

# Sorting dataset in ascending order
dataset.sort()

# Creating an empty list to append the occurrence of each number
myList = []

# Counting the occurrence of each number
i = 0
while i < len(dataset) :
	myList.append(dataset.count(dataset[i]))
	i += 1

# Creating a dictionary for K : V, K = values of sorted dataset, V = occurrence of each number in dataset
myDict = dict(zip(dataset, myList))

# Now we have to differentiate the k values with the highest v values
myDict2 = {k for (k,v) in myDict.items() if v == max(myList) }

# Printing the mode of dataset
print(myDict2)
Source by nbviewer.org #
 
PREVIOUS NEXT
Tagged: #mode #python
ADD COMMENT
Topic
Name
2+7 =