6. Learn Math With Python – Central Tendencies
Central tendency is defined as the statistical measure that identifies a single value as representative of an entire distribution. It aims to provide an accurate description of the entire data. It is the single value that is most typical/representative of the collected data. The most common used central tendencies are:
- Mean
- Median
- Mode
6.1 Sum of Numbers in an Array
Adding numbers in an array is one of the most common procedures while programming with arrays. It is done while calculating total or averages of a given set of values.
import array as arr numbers = arr.array('i', []) num = input("How many elements do you want? ") #user tells the range(optional) #iterating till user's range is reached for i in range(int(num)): n = input("Enter a value ")#asking for input of 1 value numbers.append(int(n))#adding that value to the array sum = 0; #Loop through the array to calculate sum of elements for i in range(0, len(numbers)): sum = sum + numbers[i]; print("Sum of all the elements of an array: ", sum);
6.2 Mean of Data Set
The mean is the same as the average value of a data set and is found using a calculation. Add up all of the numbers and divide by the number of numbers in the data set.
Mean/Average = (Sum of all values in a data set)/(Count of all values in a data set)
import array as arr numbers = arr.array('i', []) num = input("How many elements do you want? ") #user tells the range(optional) #iterating till user's range is reached for i in range(int(num)): n = input("Enter a value ")#asking for input of 1 value numbers.append(int(n))#adding that value to the array sum = 0; avg = 0; #Loop through the array to calculate sum of elements for i in range(0, len(numbers)): sum = sum + numbers[i]; avg = sum / len(numbers); print("Average of all the elements of an array: ", avg);
6.3 Median of Data Set
The median is the central number of a data set. Arrange data points from smallest to largest and locate the central number. This is the median. If there are 2 numbers in the middle, the median is the average of those 2 numbers.
import array as arr numbers = arr.array('i', []) num = int(input("How many elements do you want? ")) #user tells the range(optional) #iterating till user's range is reached for i in range(int(num)): n = input("Enter a value ")#asking for input of 1 value numbers.append(int(n))#adding that value to the array # Sorting numbers in ascending order using bubble sort for i in range(0,len(numbers) - 1): for j in range(len(numbers)-1): if(numbers[j]>numbers[j+1]): temp = numbers[j] numbers[j] = numbers[j+1] numbers[j+1] = temp # Finding median of numbers if(num%2==0): med_pos1 = int(num/2) med_pos2 = med_pos1 + 1 n1 = int(numbers[med_pos1]) n2 = int(numbers[med_pos2]) else: med_pos = int((num + 1)/2) if(num%2==0): median = numbers[int((n1 + n2) * 0.5)] else: median = numbers[med_pos] print('Median of given data set is ', median)
6.4 Mode of Data Set
The mode is the number in a data set that occurs most frequently. Count how many times each number occurs in the data set. The mode is the number with the highest tally. It’s ok if there is more than one mode. And if all numbers occur the same number of times there is no mode.
import array as arr numbers = arr.array('i', []) ctr = arr.array('i', []) num = input("How many elements do you want? ") #user tells the range(optional) #iterating till user's range is reached for i in range(int(num)): n = input("Enter a value ")#asking for input of 1 value numbers.append(int(n))#adding that value to the array #Counting number of occurrences of each element in an array for j in range(int(num)): ctr[j] = 0 for i in range(int(num)): if(numbers[j]=numbers[i]): ctr[j] += 1 #Finding the maximum frequency of numbers in an array max = ctr[0]; #Loop through the array for i in range(0, len(ctr)): #Compare elements of array with max if(ctr[i] > max): max = ctr[i]; #Printing mode(s) for i in range(0, len(ctr)): if(ctr[i] = max): print(numbers[i])