Mô hình tuyến tính trong Hải dương học
Dữ liệu chuỗi thời gian cung cấp bối cảnh mà trong đó “độ dốc” của mô hình tuyến tính biểu thị “tốc độ thay đổi”.
Trong bài tập này, bạn sẽ dùng các phép đo mực nước biển thay đổi từ 1970 đến 2010, xây dựng một mô hình tuyến tính cho mực nước biển đang thay đổi đó và dùng mô hình để dự đoán mức dâng mực nước biển trong tương lai.

Bài tập này là một phần của khóa học
Nhập môn Mô hình tuyến tính với Python
Hướng dẫn bài tập
- Import và dùng
LinearRegression(fit_intercept=True)để khởi tạo một mô hình tuyến tính. - Truyền dữ liệu
yearsvàlevels(đã được nạp sẵn và reshape) vàomodel.fit()để huấn luyện mô hình. - Dùng
model.predict()để dự đoán mộtfuture_levelchofuture_year = 2100vàprint()kết quả. - Dùng
model.predict()để dự báo nhiềulevels_forecastvà vẽ kết quả với hàm đã định nghĩa sẵnplot_data_and_forecast().
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# Import LinearRegression class, build a model, fit to the data
from sklearn.linear_model import ____
model = ____(fit_intercept=True)
model.fit(years, levels)
# Use model to make a prediction for one year, 2100
future_year = np.array(2100).reshape(1, -1)
future_level = model.predict(____)
print("Prediction: year = {}, level = {:.02f}".format(future_year, future_level[0,0]))
# Use model to predict for many years, and over-plot with measured data
years_forecast = np.linspace(1970, 2100, 131).reshape(-1, 1)
levels_forecast = model.predict(____)
fig = plot_data_and_forecast(years, levels, ____, ____)