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
|
131 Discussions
|
Please Login in order to post a comment
Enter your code here. Read input from STDIN. Print output to STDOUT
I have submitted this code still getting compilation error physics_marks = [15,12,8,8,7,7,7,6,5,3] history_score = [10,25,17,11,13,17,20,13,9,15]
calculate correlation coefficient
x_avg = sum(physics_marks)/10 y_avg = sum(history_score)/10 corr_coeff_num = sum([(physics_marks[i] - x_avg)*(history_score[i]-y_avg) for i in range(10)]) corr_coeff_deno = (sum([(physics_marks[i]**2)for i in range(10)])*sum([(history_score[i]**2)for i in range(10)]))**0.5 correlation_coefficient = corr_coeff_num/corr_coeff_deno print(f"{correlation_coefficient:.3f}")
physics_marks = [15,12,8,8,7,7,7,6,5,3] history_score = [10,25,17,11,13,17,20,13,9,15]
calculate correlation coefficient
correlation_coefficient = np.corrcoef(physics_marks,history_score)[0,1]
x_avg = sum(physics_marks)/10 y_avg = sum(history_score)/10 corr_coeff_num = sum([(physics_marks[i] - x_avg)*(history_score[i]-y_avg) for i in range(10)]) corr_coeff_deno = (sum([(physics_marks[i]**2)for i in range(10)])*sum([(history_score[i]**2)for i in range(10)]))**0.5 correlation_coefficient = corr_coeff_num/corr_coeff_deno print(f"{correlation_coefficient:.3f}")
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