Predict a price with Machine Learning Python

I’m starting Machine Learning. I would like to estimate the price of an article on a certain date. For example, if I know the price between 07/01/24 and 07/11/24, what is the price on 07/12/24 ?

I tried with linear or polynomial regression, RandomForestRegresor and GradientBoostingRegresor. These models return very accurate results on the data provided, but when I try to use it outside of this range, the result is either the same as the last value or an impossible result (I sometimes end up with prices negative).

Does anyone know how to do this? Should I use another method?

For example, I tried a GradientBoosting on the price of gasoline between 01/1992 and 05/2024:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
data = pd.read_csv("essence2.csv", sep=";").iloc[::-1]
x = data["date"].values.reshape(-1, 1)
y = data["price"].values
x_train, x_test, y_train, y_test = train_test_split(
x,
y,
test_size=0.01,
random_state=2,
)
model = GradientBoostingRegressor(
n_estimators=200,
learning_rate=1,
random_state=2,
)
model.fit(x_train, y_train)
y_predictions = np.round(model.predict(x), 4)
residues = y_predictions - y
fig, ax = plt.subplots(2)
ax[0].plot(x, y, color="blue", label="Prix réel")
ax[0].plot(x, y_predictions, color="orange", linestyle="--", label="Gradient Boosting Regressor")
ax[0].xaxis.set_major_locator(MaxNLocator(8))
ax[0].tick_params(axis="x", rotation=45)
ax[0].legend()
ax[1].plot(x, residues, color="orange", label="Résidues")
ax[1].xaxis.set_major_locator(MaxNLocator(8))
ax[1].tick_params(axis="x", rotation=45)
ax[1].legend()
plt.tight_layout()
plt.show()
</code>
<code>import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator from sklearn.model_selection import train_test_split from sklearn.ensemble import GradientBoostingRegressor data = pd.read_csv("essence2.csv", sep=";").iloc[::-1] x = data["date"].values.reshape(-1, 1) y = data["price"].values x_train, x_test, y_train, y_test = train_test_split( x, y, test_size=0.01, random_state=2, ) model = GradientBoostingRegressor( n_estimators=200, learning_rate=1, random_state=2, ) model.fit(x_train, y_train) y_predictions = np.round(model.predict(x), 4) residues = y_predictions - y fig, ax = plt.subplots(2) ax[0].plot(x, y, color="blue", label="Prix réel") ax[0].plot(x, y_predictions, color="orange", linestyle="--", label="Gradient Boosting Regressor") ax[0].xaxis.set_major_locator(MaxNLocator(8)) ax[0].tick_params(axis="x", rotation=45) ax[0].legend() ax[1].plot(x, residues, color="orange", label="Résidues") ax[1].xaxis.set_major_locator(MaxNLocator(8)) ax[1].tick_params(axis="x", rotation=45) ax[1].legend() plt.tight_layout() plt.show() </code>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor

data = pd.read_csv("essence2.csv", sep=";").iloc[::-1]

x = data["date"].values.reshape(-1, 1)
y = data["price"].values

x_train, x_test, y_train, y_test = train_test_split(
    x,
    y,
    test_size=0.01,
    random_state=2,
)

model = GradientBoostingRegressor(
    n_estimators=200,
    learning_rate=1,
    random_state=2,
)
model.fit(x_train, y_train)

y_predictions = np.round(model.predict(x), 4)
residues = y_predictions - y

fig, ax = plt.subplots(2)

ax[0].plot(x, y, color="blue", label="Prix réel")
ax[0].plot(x, y_predictions, color="orange", linestyle="--", label="Gradient Boosting Regressor")
ax[0].xaxis.set_major_locator(MaxNLocator(8))
ax[0].tick_params(axis="x", rotation=45)
ax[0].legend()

ax[1].plot(x, residues, color="orange", label="Résidues")
ax[1].xaxis.set_major_locator(MaxNLocator(8))
ax[1].tick_params(axis="x", rotation=45)
ax[1].legend()

plt.tight_layout()
plt.show()

Essence prections

How to predict the price for 06/2024 ?

New contributor

Aaa Zzz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật