# Plot the illiteracy rate versus fertility _ = plt.plot(illiteracy, fertility, marker='.', linestyle='none') # Set the margins and label axes plt.margins(0.02) _ = plt.xlabel('percent illiterate') _ = plt.ylabel('fertility') # Show the plot plt.show() # Show the Pearson correlation coefficient print(stats.pearsonr(illiteracy, fertility))
# Plot the illiteracy rate versus fertility _ = plt.plot(illiteracy, fertility, marker='.', linestyle='none') plt.margins(0.02) _ = plt.xlabel('percent illiterate') _ = plt.ylabel('fertility') # Perform a linear regression using np.polyfit(): a, b a, b = np.polyfit(illiteracy, fertility, 1) # Print the results to the screen print('slope =', a, 'children per woman / percent illiterate') print('intercept =', b, 'children per woman') # Make theoretical line to plot x = np.array([0, 100]) y = a * x + b # Add regression line to your plot _ = plt.plot(x, y) # Draw the plot plt.show()
#计算rss# Specify slopes to consider: a_vals a_vals = np.linspace(0,0.1,200) # Initialize sum of square of residuals: rss rss = np.empty_like(a_vals) # Compute sum of square of residuals for each value of a_vals for i, a in enumerate(a_vals): rss[i] = np.sum((fertility - a*illiteracy - b)**2) # Plot the RSS plt.plot(a_vals, rss, '-') plt.xlabel('slope (children per woman / percent illiterate)') plt.ylabel('sum of square of residuals') plt.show()
python-numpy-相关性、回归、画图-np.polyfit(x,y,deg=1)
最新推荐文章于 2024-01-11 10:40:06 发布