写字楼租金模型beta
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import csv
|
||||
import random
|
||||
|
||||
|
||||
def generate_office_rent_data():
|
||||
# 定义可能的值
|
||||
cities = ["1", "2", "3", "4"]
|
||||
districts = {
|
||||
"1": ["1", "2", "3", "4"],
|
||||
"2": ["1", "2", "3", "4"],
|
||||
"3": ["1", "2", "3", "4"],
|
||||
"4": ["1", "2", "3", "4"]
|
||||
}
|
||||
directions = [1, 2, 3, 4]
|
||||
fitments = [1, 2, 3]
|
||||
near_facilities =[1, 2, 3, 4]
|
||||
lease_terms = [1, 2, 3, 4]
|
||||
office_grades = [1, 2, 3]
|
||||
|
||||
data = []
|
||||
|
||||
for _ in range(10000):
|
||||
city = random.choice(cities)
|
||||
district = random.choice(districts[city])
|
||||
|
||||
area = round(random.uniform(50, 300), 2) # 面积在50到300平方米之间
|
||||
floor = random.randint(1, 30)
|
||||
direction = random.choice(directions)
|
||||
fitment = random.choice(fitments)
|
||||
near_facility = random.choice(near_facilities)
|
||||
lease_term = random.choice(lease_terms)
|
||||
rent_price = round(random.uniform(5000, 50000), 2) # 租金在5000到50000之间
|
||||
office_grade = random.choice(office_grades)
|
||||
building_age = random.randint(1, 50) # 建筑年龄
|
||||
free_rent_period = round(random.uniform(0, 3), 2) # 免租期,0到3个月
|
||||
parking_spaces = random.randint(0, 49) # 停车位数量
|
||||
elevators = random.randint(1, 10) # 电梯数量
|
||||
green_building = random.randint(0, 1) # 是否是绿色建筑
|
||||
management_fee = round(random.uniform(0, 1000), 2) # 管理费
|
||||
|
||||
data.append([
|
||||
area, city, district, floor, direction, fitment, near_facility, lease_term,
|
||||
rent_price, office_grade, building_age, free_rent_period, parking_spaces,
|
||||
elevators, green_building, management_fee
|
||||
])
|
||||
|
||||
# 写入CSV文件
|
||||
with open('data.csv', 'w', newline='', encoding='utf-8') as file:
|
||||
writer = csv.writer(file)
|
||||
# 写入标题行
|
||||
writer.writerow([
|
||||
"area", "city", "district", "floor", "direction", "fitment", "near_facilities",
|
||||
"lease_term", "rent_price", "office_grade", "building_age", "free_rent_period",
|
||||
"parking_spaces", "elevators", "green_building", "management_fee"
|
||||
])
|
||||
# 写入数据
|
||||
writer.writerows(data)
|
||||
|
||||
print("Data has been generated and written to data.csv")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
generate_office_rent_data()
|
||||
@@ -0,0 +1,27 @@
|
||||
import numpy as np
|
||||
from tensorflow import keras
|
||||
|
||||
# Load the model
|
||||
model = keras.models.load_model('office_rent_model')
|
||||
|
||||
# Example input data (replace this with your actual data)
|
||||
# Assuming your model expects 15 features as per your previous setup
|
||||
input_data = np.array([[1, 0, 1, 0, 24, 1, 0, 1, 0, 6, 0, 0.7, 6, 7, 323.3]]) # One sample, 15 features
|
||||
|
||||
# Make predictions
|
||||
predictions = model.predict(input_data)
|
||||
|
||||
# Print the prediction
|
||||
print(f"Predicted rent price: {predictions[0][0]}")
|
||||
|
||||
# If you have multiple samples to predict
|
||||
# input_data_multiple = np.array([
|
||||
# [100.19, 0, 1, 0, 24, 1, 0, 1, 0, 6, 0, 0.7, 6, 7, 323.3],
|
||||
# [52.79, 0, 1, 1, 9, 1, 0, 0, 1, 19, 1, 0.41, 26, 3, 392.3],
|
||||
# # Add more samples here...
|
||||
# ])
|
||||
|
||||
# predictions_multiple = model.predict(input_data_multiple)
|
||||
|
||||
# for i, pred in enumerate(predictions_multiple):
|
||||
# print(f"Sample {i + 1} predicted rent price: {pred[0]}")
|
||||
@@ -0,0 +1,23 @@
|
||||
import pandas as pd
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
# 读取CSV文件
|
||||
data = pd.read_csv('data.csv')
|
||||
|
||||
# 假设'rent_price'是我们要预测的目标变量
|
||||
X = data.drop('rent_price', axis=1) # 所有的特征列,除了目标变量
|
||||
y = data['rent_price'] # 目标变量
|
||||
|
||||
# 使用train_test_split函数进行数据集拆分
|
||||
# test_size参数表示测试集所占的比例,这里设置为20%
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
||||
|
||||
# 将训练集和测试集重新组合为DataFrame
|
||||
train_data = pd.concat([X_train, y_train], axis=1)
|
||||
test_data = pd.concat([X_test, y_test], axis=1)
|
||||
|
||||
# 保存到新的CSV文件
|
||||
train_data.to_csv('train.csv', index=False)
|
||||
test_data.to_csv('test.csv', index=False)
|
||||
|
||||
print("数据已成功拆分为train.csv和test.csv")
|
||||
@@ -0,0 +1,47 @@
|
||||
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加载。")
|
||||
Reference in New Issue
Block a user