BÀI TẬP THỰC HÀNH MÔN TTNT K28,29 - NĂM 2026
BÀI TẬP THỰC HÀNH 6
Học máy - Hồi quy tuyến tính sử dụng ngôn ngữ lập trình python
Yêu cầu: Sử dụng phương pháp hồi quy tuyến tính - dự đoán giá nhà với các tập
dữ liệu sau:
Tập dữ liệu 1: Gồm các thông tin sau:
Diện tích nhà (m²)
Giá nhà (triệu đồng)
Dữ liệu minh họa trong bảng sau:
TT Diện tích (m
2
) Giá nhà (Tỷ)
1 30 1.5
2 50 2.5
3 70 3.5
4 90 4.5
5 110 5.5
Tập dữ liệu 2: Gồm các thông tin sau:
num_bed
year_built
longitude
latitude
num_room
num_bath
living_area
property_type
num_parking
accessible_buildings
family_quality
art_expos
emergency_shelters
emergency_water
Facilities
fire_stations
Cultural
Monuments
police_stations
Vacant
Free_Parking
askprice
#Google CodLab
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# Dữ liệu
X = np.array([30, 50, 70, 90, 110]).reshape(-1, 1)
y = np.array([1.5, 2.5, 3.5, 4.5, 5.5])
# Tạo mô hình
model = LinearRegression()
model.fit(X, y)
# Hệ số
a = model.coef_[0]
b = model.intercept_
print("Hệ số góc a =", a)
print("Hệ số chặn b =", b)
# Dự đoán thử
dien_tich_moi = np.array([[80]])
gia_du_doan = model.predict(dien_tich_moi)
print("Giá dự đoán cho 80m2:", gia_du_doan[0], "tỷ")
# Vẽ đồ thị
plt.scatter(X, y, color='blue')
plt.plot(X, model.predict(X), color='red')
plt.xlabel("Diện tích (m2)")
plt.ylabel("Giá nhà (tỷ)")
plt.title("Hồi quy tuyến tính đơn")
plt.show()
#Kết quả
Hệ số góc a = 0.05000000000000001
Hệ số chặn b = -8.881784197001252e-16
Giá dự đoán cho 80m2: 4.0 tỷ

Preview text:

BÀI TẬP THỰC HÀNH MÔN TTNT K28,29 - NĂM 2026

BÀI TẬP THỰC HÀNH 6

Học máy - Hồi quy tuyến tính sử dụng ngôn ngữ lập trình python

Yêu cầu: Sử dụng phương pháp hồi quy tuyến tính - dự đoán giá nhà với các tập dữ liệu sau:

Tập dữ liệu 1: Gồm các thông tin sau:

  • Diện tích nhà (m²)
  • Giá nhà (triệu đồng)

Dữ liệu minh họa trong bảng sau:

TT

Diện tích (m2)

Giá nhà (Tỷ)

1

30

1.5

2

50

2.5

3

70

3.5

4

90

4.5

5

110

5.5

Tập dữ liệu 2: Gồm các thông tin sau:

  • num_bed
  • year_built
  • longitude
  • latitude
  • num_room
  • num_bath
  • living_area
  • property_type
  • num_parking
  • accessible_buildings
  • family_quality
  • art_expos
  • emergency_shelters
  • emergency_water
  • Facilities
  • fire_stations
  • Cultural
  • Monuments
  • police_stations
  • Vacant
  • Free_Parking
  • askprice

#Google CodLab

import numpy as np

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

# Dữ liệu

X = np.array([30, 50, 70, 90, 110]).reshape(-1, 1)

y = np.array([1.5, 2.5, 3.5, 4.5, 5.5])

# Tạo mô hình

model = LinearRegression()

model.fit(X, y)

# Hệ số

a = model.coef_[0]

b = model.intercept_

print("Hệ số góc a =", a)

print("Hệ số chặn b =", b)

# Dự đoán thử

dien_tich_moi = np.array([[80]])

gia_du_doan = model.predict(dien_tich_moi)

print("Giá dự đoán cho 80m2:", gia_du_doan[0], "tỷ")

# Vẽ đồ thị

plt.scatter(X, y, color='blue')

plt.plot(X, model.predict(X), color='red')

plt.xlabel("Diện tích (m2)")

plt.ylabel("Giá nhà (tỷ)")

plt.title("Hồi quy tuyến tính đơn")

plt.show()

#Kết quả

Hệ số góc a = 0.05000000000000001

Hệ số chặn b = -8.881784197001252e-16

Giá dự đoán cho 80m2: 4.0 tỷ