python MSE
时间: 2023-10-20 08:36:43 浏览: 108
MSE stands for Mean Squared Error. It is a commonly used metric to measure the difference between predicted and actual values in regression problems. In Python, you can calculate MSE using various libraries such as NumPy or scikit-learn.
Here's an example of calculating MSE in Python using NumPy:
```python
import numpy as np
# Assuming you have predicted and actual values stored in arrays
predicted = np.array([2, 4, 6, 8])
actual = np.array([1, 3, 5, 7])
# Calculate the difference between predicted and actual values
diff = predicted - actual
# Square the differences
squared_diff = diff ** 2
# Calculate the mean of squared differences
mse = np.mean(squared_diff)
print("Mean Squared Error:", mse)
```
In this example, the predicted values are [2, 4, 6, 8] and the actual values are [1, 3, 5, 7]. We calculate the difference between predicted and actual values, square the differences, calculate the mean of squared differences, and finally print the MSE.
阅读全文