numpy Semi main (1/4/24)

Mar 31st, 2024 (edited)
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # Function to calculate mean
2. def calculate_mean(data):
3.     return sum(data) / len(data)
4.
5. # Function to calculate median
6. def calculate_median(data):
7.     sorted_data = sorted(data)
8.     n = len(sorted_data)
9.     if n % 2 == 0:
10.         return (sorted_data[n // 2 - 1] + sorted_data[n // 2]) / 2
11.     else:
12.         return sorted_data[n // 2]
13.
14. # Function to calculate mode
15. def calculate_mode(data):
16.     frequency = {}
17.     for value in data:
18.         frequency[value] = frequency.get(value, 0) + 1
19.
20.     max_frequency = max(frequency.values())
21.     mode = [key for key, val in frequency.items() if val == max_frequency]
22.     return mode
23.
24. # User input for data
25. data = []
26. while True:
27.     value = input("Enter a number (or 'done' to finish): ")
28.     if value.lower() == 'done':
29.         break
30.     data.append(int(value))
31.
32. # Calculate mean
33. mean = calculate_mean(data)
34. print("Mean:", mean)
35.
36. # Calculate median
37. median = calculate_median(data)
38. print("Median:", median)
39.
40. # Calculate mode
41. mode = calculate_mode(data)
42. print("Mode:", mode)
43.
44.
45.
46.
47.
48. #Liner regression
49. import numpy as np
50. from sklearn.linear_model import LinearRegression
51.
52. years = np.array([[1], [2], [3], [4], [5]])
53. speeds = np.array([30, 45, 45, 55, 65])
54.
55.
56. model = LinearRegression()
57. model.fit(years, speeds)
58.
59. x= 15
60. predicted_speed = model.predict([[x]])
61.
62. print(f"Predicted speed after {x} years:", predicted_speed[0], "km/h")
63.