Advertisement
brandblox

numpy lab(19/02/2024)

Feb 18th, 2024
1,103
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. # using logic
  2. import math
  3.  
  4. def get_input():
  5.     size = int(input("Enter length of array: "))
  6.     num_data = []
  7.     for i in range(size):
  8.         number = int(input("Enter element {}: ".format(i+1)))
  9.         num_data.append(number)
  10.     return num_data
  11.  
  12. def mean(data):
  13.     return sum(data) / len(data)
  14.  
  15. def variance(data):
  16.     mu = mean(data)
  17.     return sum((x - mu) ** 2 for x in data) / len(data)
  18.  
  19. def std_deviation(data):
  20.     return math.sqrt(variance(data))
  21.  
  22. data = get_input()
  23. print("Mean:", mean(data))
  24. print("Variance:", variance(data))
  25. print("Standard Deviation:", std_deviation(data))
  26.  
  27. #using library
  28. import numpy as np
  29.  
  30. def get_input():
  31.     size = int(input("Enter length of array: "))
  32.     num_data = []
  33.     for i in range(size):
  34.         number = int(input("Enter element {}: ".format(i+1)))
  35.         num_data.append(number)
  36.     return num_data
  37.  
  38. def mean(data):
  39.     return np.mean(data)
  40.  
  41. def variance(data):
  42.     return np.var(data)
  43.  
  44. def std_deviation(data):
  45.     return np.std(data)
  46.  
  47. data = get_input()
  48. print("Mean:", mean(data))
  49. print("Variance:", variance(data))
  50. print("Standard Deviation:", std_deviation(data))
  51.  
  52. #Output
  53. Enter length of array: 12
  54. Enter element 1: 103
  55. Enter element 2: 85
  56. Enter element 3: 117
  57. Enter element 4: 98
  58. Enter element 5: 203
  59. Enter element 6: 119
  60. Enter element 7: 48
  61. Enter element 8: 65
  62. Enter element 9: 86
  63. Enter element 10: 110
  64. Enter element 11: 180
  65. Enter element 12: 156
  66. Mean: 114.16666666666667
  67. Variance: 1904.138888888889
  68. Standard Deviation: 43.63643991996699
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement