Page 267 - Robotics and AI class 10
P. 267

years = [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010]

                 temperatures = [15.2, 15.5, 15.7, 16.0, 16.2, 16.5, 16.8, 17.0, 17.2, 17.5,
                 17.8]

                 plt.figure(figsize=(10, 6))

                 plt.scatter(years, temperatures, color='blue')
                 plt.title('Year vs Average Temperature')
                 plt.xlabel('Year')

                 plt.ylabel('Average Temperature (°C)')
                 plt.grid(True)

                 plt.show()
                 Output:























                 Next, let's perform linear regression on the data to predict average temperatures for future years:


                 from sklearn.linear_model import LinearRegression
                 import numpy as np

                 X = np.array(years).reshape(-1, 1)
                 y = np.array(temperatures)

                 # Create and fit the linear regression model
                 model = LinearRegression()

                 model.fit(X, y)

                 # Predict temperatures for future years
                 future_years = np.array([2021, 2022, 2023, 2024, 2025]).reshape(-1, 1) predicted_
                 temperatures = model.predict(future_years)

                 print("Predicted Temperatures for Future Years:")
                 for year, temp in zip(future_years, predicted_temperatures):

                        print(f"Year: {year[0]}, Predicted Temperature: {temp:.2f} °C")



                                                                                            Assignment     265
   262   263   264   265   266   267   268   269   270   271   272