We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Artificial Intelligence
- Statistics and Machine Learning
- Correlation and Regression Lines - A Quick Recap #1
- Discussions
Correlation and Regression Lines - A Quick Recap #1
Correlation and Regression Lines - A Quick Recap #1
Sort by
recency
|
129 Discussions
|
Please Login in order to post a comment
The problem statement is misleading. You are told that you are required to take the input from stdin (python3 -> input())
The sample also has strings at the begginng that need to be handled on input.
However the actual submission is passed by hardcoding the example data as numerical lists in the code which is not what the instructions state. There is no feedback on the submission either to see if stdin/inputs are even being provided in the submission test cases.
physics = [15, 12, 8, 8, 7, 7, 7, 6, 5, 3]
history = [10, 25, 17, 11, 13, 17, 20, 13, 9, 15]
Step 1: Calculate means of both lists
mean_x = sum(physics) / len(physics)
mean_y = sum(history) / len(history)
Step 2: Calculate numerator (Covariance)
sum_xy = sum((x - mean_x) * (y - mean_y) for x, y in zip(physics, history))
Step 3: Calculate the denominator (Standard deviations of x and y)
sum_x_sq = sum((x - mean_x) ** 2 for x in physics)
sum_y_sq = sum((y - mean_y) ** 2 for y in history)
Step 4: Pearson correlation coefficient formula
r = sum_xy / ( (sum_x_sq ** 0.5) * (sum_y_sq ** 0.5) )
Step 5: Print the result
print(round(r, 3)) # Rounding to 3 decimal places
physics = [15, 12, 8, 8, 7, 7, 7, 6, 5, 3] history = [10, 25, 17, 11, 13, 17, 20, 13, 9, 15]
Step 1: Calculate means of both lists
mean_x = sum(physics) / len(physics) mean_y = sum(history) / len(history)
Step 2: Calculate numerator (Covariance)
sum_xy = sum((x - mean_x) * (y - mean_y) for x, y in zip(physics, history))
Step 3: Calculate the denominator (Standard deviations of x and y)
sum_x_sq = sum((x - mean_x) ** 2 for x in physics) sum_y_sq = sum((y - mean_y) ** 2 for y in history)
Step 4: Pearson correlation coefficient formula
r = sum_xy / ( (sum_x_sq ** 0.5) * (sum_y_sq ** 0.5) )
Step 5: Print the result
print(round(r, 3)) # Rounding to 3 decimal places
Thank you for sharing this quick recap. I was looking for this for my fumot vape project.