Answer to Question #305111 in Python for Mohit

Question #305111

Write A phyton code to read A dataset and print all Features i.e. columns of the dataset. Determine the descriptive statics i.e. maximum, minimum mean median, count, variance, standard deviation etc. of the numeric Features like age, salary, etc. May be present in the dataset.


1
Expert's answer
2022-03-02T15:34:00-0500
import numpy as np
import matplotlib.pyplot as plt

responses = [1,2,5,4,3,5,2,1,3,3,1,4,3,3,3,2,3,3,2,5] # Responses list
uni_responses = {} # Initialize Dictionary
for r in responses: # Iterate through the list
  if r in uni_responses: # Get ratings frequency. if rating already in dictionary
    uni_responses[r] += 1 # Add one to it's value
  else: # If not present in dictionary
    uni_responses[r] = 1 # Add key value pair

sorted_keys = sorted(uni_responses) # Get sorted keys list
sort_d={} # Initialize dictionary
for f in sorted_keys: # Iterate through list
  sort_d[f] = uni_responses[f] # Sort dictionary
prcnt = {} # Initialize list
for k, v in sort_d.items(): # Iterate through the sorted dictionary
  print(f"Number of {k} ratings = {v}") # Print frequencies
  prcnt[k] = (v/len(responses))*100 # Add percentages of each rating into a dictionary

print(f"Minimum: {min(responses)}") # print Minimum value
print(f"Maximum: {max(responses)}") # Print Maximum value
print(f"Range: {max(responses) - min(responses)}") # Print Range
print(f"Mean: {np.mean(responses)}") # Print mean
print(f"Median: {np.median(responses)}") # Print median
print(f"Variance: {np.var(responses)}") # Print Variance
print(f"Standard Deviation: {round(np.std(responses),2)}") # Print standard deviation

figure, ax = plt.subplots(figsize=(10,12)) # Initialize the figure size 
ax.bar(prcnt.keys(), prcnt.values()) # Plot percentages
ax.bar(sort_d.keys(), sort_d.values()) # Plot frequencies
ax.legend(['Percentage', 'Frequency']) # Set legend
ax.set_xlabel('Ratings') # Set X label
ax.set_title("Frequency and Percentages vs Rating") # Set title


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS