numpy - Show confidence limits and prediction limits in scatter plot

Numpy - Show confidence limits and prediction limits in scatter plot

To show confidence limits and prediction limits in a scatter plot using Python with NumPy and Matplotlib, you can use the numpy.polyfit function to fit a polynomial to your data and then plot the results along with confidence and prediction intervals. Here's an example:

import numpy as np import matplotlib.pyplot as plt # Generate some sample data np.random.seed(42) x = np.linspace(0, 10, 50) y = 2 * x + 1 + np.random.normal(scale=2, size=len(x)) # Fit a polynomial of degree 1 (a linear fit) coefficients, covariance = np.polyfit(x, y, deg=1, cov=True) # Get the slope and intercept slope, intercept = coefficients # Compute the predicted values and residuals y_pred = np.polyval(coefficients, x) residuals = y - y_pred # Compute the confidence and prediction intervals confidence_interval = 1.96 * np.sqrt(np.diag(covariance)) prediction_interval = 1.96 * np.sqrt(np.sum(residuals**2) / (len(x) - 2)) # Plot the data plt.scatter(x, y, label='Data') # Plot the regression line plt.plot(x, y_pred, color='red', label='Regression Line') # Plot confidence intervals plt.fill_between(x, y_pred - confidence_interval[0], y_pred + confidence_interval[0], color='red', alpha=0.2, label='95% Confidence Interval') # Plot prediction intervals plt.fill_between(x, y_pred - prediction_interval, y_pred + prediction_interval, color='red', alpha=0.1, label='95% Prediction Interval') # Set labels and title plt.xlabel('X') plt.ylabel('Y') plt.title('Scatter Plot with Confidence and Prediction Intervals') # Show legend plt.legend() # Show the plot plt.show() 

In this example:

  • We generate some sample data with a linear relationship and add random noise.
  • We use numpy.polyfit to fit a linear polynomial to the data.
  • We calculate the predicted values, residuals, and confidence/prediction intervals.
  • We use Matplotlib to create a scatter plot of the data, plot the regression line, and shade the areas representing confidence and prediction intervals.

You can adjust the degree of the polynomial fit, customize plot appearance, and modify the parameters according to your specific requirements.

Examples

  1. "NumPy scatter plot with confidence intervals"

    • Code Implementation:
      import numpy as np import matplotlib.pyplot as plt import seaborn as sns # Generate example data np.random.seed(42) x = np.random.rand(50) y = 2 * x + 1 + np.random.randn(50) # Calculate confidence intervals sns.regplot(x=x, y=y, ci=95) plt.show() 
    • Description: Use seaborn.regplot to create a scatter plot with a linear regression line and confidence intervals (default 95%).
  2. "NumPy scatter plot with prediction intervals"

    • Code Implementation:
      import numpy as np import matplotlib.pyplot as plt import seaborn as sns # Generate example data np.random.seed(42) x = np.random.rand(50) y = 2 * x + 1 + np.random.randn(50) # Calculate prediction intervals sns.regplot(x=x, y=y, ci=None, scatter_kws={"color": "black"}, line_kws={"color": "blue"}) plt.show() 
    • Description: Use seaborn.regplot with ci=None to create a scatter plot with a linear regression line and without confidence intervals. Then, add a separate line for prediction intervals.
  3. "NumPy confidence interval calculation"

    • Code Implementation:
      import numpy as np import statsmodels.api as sm import matplotlib.pyplot as plt # Generate example data np.random.seed(42) x = np.random.rand(50) y = 2 * x + 1 + np.random.randn(50) # Fit linear regression model X = sm.add_constant(x) model = sm.OLS(y, X).fit() # Calculate confidence intervals ci = model.conf_int(alpha=0.05, cols=(1, 2)) print(ci) 
    • Description: Use statsmodels to fit a linear regression model and calculate confidence intervals for the regression coefficients.
  4. "NumPy prediction interval calculation"

    • Code Implementation:
      import numpy as np import statsmodels.api as sm import matplotlib.pyplot as plt # Generate example data np.random.seed(42) x = np.random.rand(50) y = 2 * x + 1 + np.random.randn(50) # Fit linear regression model X = sm.add_constant(x) model = sm.OLS(y, X).fit() # Calculate prediction intervals pred_intervals = model.get_prediction(X).conf_int(alpha=0.05) print(pred_intervals) 
    • Description: Use statsmodels to fit a linear regression model and calculate prediction intervals for the response variable.
  5. "NumPy scatter plot with custom confidence intervals"

    • Code Implementation:
      import numpy as np import matplotlib.pyplot as plt # Generate example data np.random.seed(42) x = np.random.rand(50) y = 2 * x + 1 + np.random.randn(50) # Calculate custom confidence intervals ci_lower = y - 1.96 * np.std(y) ci_upper = y + 1.96 * np.std(y) # Plot scatter plot with custom confidence intervals plt.scatter(x, y, label='Data') plt.plot(x, ci_lower, '--', color='red', label='Custom CI (95%)') plt.plot(x, ci_upper, '--', color='red') plt.legend() plt.show() 
    • Description: Calculate and plot custom confidence intervals for a scatter plot using standard deviation.
  6. "NumPy scatter plot with error bars"

    • Code Implementation:
      import numpy as np import matplotlib.pyplot as plt # Generate example data np.random.seed(42) x = np.random.rand(50) y = 2 * x + 1 + np.random.randn(50) # Calculate standard error of the mean (SEM) as error bars sem = np.std(y) / np.sqrt(len(y)) # Plot scatter plot with error bars plt.errorbar(x, y, yerr=sem, fmt='o', label='Data with SEM error bars') plt.legend() plt.show() 
    • Description: Use plt.errorbar to create a scatter plot with error bars representing the standard error of the mean (SEM).
  7. "NumPy scatter plot with bootstrap confidence intervals"

    • Code Implementation:
      import numpy as np import matplotlib.pyplot as plt from sklearn.utils import resample # Generate example data np.random.seed(42) x = np.random.rand(50) y = 2 * x + 1 + np.random.randn(50) # Perform bootstrap resampling to calculate confidence intervals n_iterations = 1000 bootstrap_ci = [] for _ in range(n_iterations): sample = resample(y) bootstrap_ci.append((np.percentile(sample, 2.5), np.percentile(sample, 97.5))) # Plot scatter plot with bootstrap confidence intervals plt.scatter(x, y, label='Data') for i in range(n_iterations): plt.plot([x[i], x[i]], bootstrap_ci[i], color='red', alpha=0.01) plt.legend() plt.show() 
    • Description: Use bootstrap resampling to calculate and visualize confidence intervals in a scatter plot.
  8. "NumPy scatter plot with seaborn lmplot and confidence intervals"

    • Code Implementation:
      import numpy as np import seaborn as sns import matplotlib.pyplot as plt # Generate example data np.random.seed(42) x = np.random.rand(50) y = 2 * x + 1 + np.random.randn(50) # Create a scatter plot with seaborn lmplot and confidence intervals sns.lmplot(x='x', y='y', data=pd.DataFrame({'x': x, 'y': y}), ci=95) plt.show() 
    • Description: Use seaborn.lmplot to create a scatter plot with a linear regression line and confidence intervals.

More Tags

radar-chart android-handler react-native-maps swig azure-storage-queues postgresql-9.4 aws-appsync css-loader html5-audio http-options-method

More Programming Questions

More General chemistry Calculators

More Organic chemistry Calculators

More Stoichiometry Calculators

More Livestock Calculators