Predict Gold value
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
data = {'Date': ['2021-01-01', '2021-02-01', '2021-03-01', '2021-04-01', '2021-05-01'],
'Gold_Price': [1850, 1875, 1900, 1950, 2000]}<--!範例數據-->
df = pd.DataFrame(data)
#日期转换为数字格式
df['Date'] = pd.to_datetime(df['Date'])
df['Date'] = df['Date'].map(pd.Timestamp.to_julian_date)
#定义特征和目标变量
X = df[['Date']] # 特征变量
y = df['Gold_Price'] # 目标变量
#将数据拆分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
#创建线性回归模型并进行训练
model = LinearRegression()
model.fit(X_train, y_train)
#进行预测并评估模型:
y_pred = model.predict(X_test)