47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import tensorflow as tf
|
|
from tensorflow.keras.models import Sequential
|
|
from tensorflow.keras.layers import Dense
|
|
from tensorflow.keras.optimizers import Adam
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.preprocessing import StandardScaler
|
|
import pandas as pd
|
|
import numpy as np
|
|
|
|
# 读取数据
|
|
data = pd.read_csv('train.csv')
|
|
test_data = pd.read_csv('test.csv')
|
|
|
|
# 分离特征和目标变量
|
|
X = data.drop('rent_price', axis=1)
|
|
y = data['rent_price']
|
|
X_test = test_data.drop('rent_price', axis=1)
|
|
y_test = test_data['rent_price']
|
|
|
|
# 数据标准化
|
|
scaler = StandardScaler()
|
|
X = scaler.fit_transform(X)
|
|
X_test = scaler.transform(X_test)
|
|
|
|
# 创建模型
|
|
model = Sequential([
|
|
Dense(50, activation='relu', input_shape=(15,)), # 输入层和第一隐藏层
|
|
Dense(50, activation='relu'), # 第二隐藏层
|
|
Dense(50, activation='relu'), # 第三隐藏层
|
|
Dense(1) # 输出层
|
|
])
|
|
|
|
# 编译模型
|
|
model.compile(optimizer=Adam(learning_rate=0.01), loss='mse')
|
|
|
|
# 训练模型
|
|
model.fit(X, y, epochs=500, batch_size=32, verbose=1)
|
|
|
|
# 评估模型
|
|
predictions = model.predict(X_test)
|
|
for i in range(len(predictions)):
|
|
print(f"Predicted vs Actual: {predictions[i][0]} vs {y_test.iloc[i]}")
|
|
|
|
# 保存模型为SavedModel格式,以便Java可以加载
|
|
model.save('office_rent_model')
|
|
|
|
print("模型已保存为 office_rent_model,可以由Java加载。") |