เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

โมเดลเชิงเส้นในสมุทรศาสตร์

ข้อมูลอนุกรมเวลาเป็นบริบทที่ "ความชัน" ของโมเดลเชิงเส้นแทนความหมายของ "อัตราการเปลี่ยนแปลง"

ในแบบฝึกหัดนี้ คุณจะใช้ข้อมูลการเปลี่ยนแปลงระดับน้ำทะเลตั้งแต่ปี 1970 ถึง 2010 สร้างโมเดลเชิงเส้นของระดับน้ำทะเลที่เปลี่ยนแปลง และนำไปพยากรณ์ระดับน้ำทะเลในอนาคต

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Introduction to Linear Modeling in Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • นำเข้าและใช้ LinearRegression(fit_intercept=True) เพื่อสร้างโมเดลเชิงเส้น
  • ส่งข้อมูล years และ levels ที่โหลดและปรับรูปร่างไว้แล้วเข้าสู่ model.fit() เพื่อฟิตโมเดล
  • ใช้ model.predict() เพื่อพยากรณ์ค่า future_level เพียงค่าเดียวสำหรับ future_year = 2100 แล้ว print() ผลลัพธ์ที่ได้
  • ใช้ model.predict() เพื่อพยากรณ์ levels_forecast หลายค่า และพล็อตผลลัพธ์ด้วยฟังก์ชัน plot_data_and_forecast() ที่กำหนดไว้แล้ว

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# 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, ____, ____)
แก้ไขและรันโค้ด