Page 335 - AI_Ver_3.0_class_11
P. 335

# Predicted values
                 y_pred = slope * data_x + intercept



                 # Plot data and regression line
                 plt.scatter(data_x, data_y)
                 plt.plot(data_x, y_pred, color='red')


                 # Add labels and title
                 plt.xlabel('X values')

                 plt.ylabel('Y values')
                 plt.title('Simple Linear Regression')


                 # Show the plot
                 plt.show()


                 # Print slope and intercept
                 print("The slope of the regression line is: {:.2f}".format(slope))

                 print("The intercept of the regression line is: {:.2f}".format(intercept))
                 Output:
                                                           Simple Linear Regression
                                          9


                                          8


                                          7

                                        Y values  6



                                          5

                                          4


                                          3

                                              2.5    5.0    7.5   10.0   12.5   15.0   17.5   20.0
                                                                   X values
                 The slope of the regression line is: 0.21

                 The intercept of the regression line is: 4.56
                 In Program 1,

                 ●  The program imports NumPy for numerical calculations and Matplotlib’s pyplot module for plotting.
                 ●  Sample data for both the independent variable (x) and the dependent variable (y) is provided.

                 ●  Mean, standard deviation, covariance, and slope of the data are computed to understand their relationships.

                                                                                  Machine Learning Algorithms   333
   330   331   332   333   334   335   336   337   338   339   340