From cb469f2f41d63b6a93df6b47305fe765622b3106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=81=A5?= Date: Fri, 17 Jan 2025 18:40:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=99=E5=AD=97=E6=A5=BC=E7=A7=9F=E9=87=91?= =?UTF-8?q?=E6=A8=A1=E5=9E=8Bbeta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dags/train/__init__.py | 0 dags/train/office/__init__.py | 0 dags/train/office/rent/__init__.py | 0 .../office/rent/generate_office_rent_data.py | 63 +++++++++++++++++++ dags/train/office/rent/predict.py | 27 ++++++++ dags/train/office/rent/split_data.py | 23 +++++++ dags/train/office/rent/train.py | 47 ++++++++++++++ 7 files changed, 160 insertions(+) create mode 100644 dags/train/__init__.py create mode 100644 dags/train/office/__init__.py create mode 100644 dags/train/office/rent/__init__.py create mode 100644 dags/train/office/rent/generate_office_rent_data.py create mode 100644 dags/train/office/rent/predict.py create mode 100644 dags/train/office/rent/split_data.py create mode 100644 dags/train/office/rent/train.py diff --git a/dags/train/__init__.py b/dags/train/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dags/train/office/__init__.py b/dags/train/office/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dags/train/office/rent/__init__.py b/dags/train/office/rent/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dags/train/office/rent/generate_office_rent_data.py b/dags/train/office/rent/generate_office_rent_data.py new file mode 100644 index 0000000..aa573ce --- /dev/null +++ b/dags/train/office/rent/generate_office_rent_data.py @@ -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() \ No newline at end of file diff --git a/dags/train/office/rent/predict.py b/dags/train/office/rent/predict.py new file mode 100644 index 0000000..c75642d --- /dev/null +++ b/dags/train/office/rent/predict.py @@ -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]}") \ No newline at end of file diff --git a/dags/train/office/rent/split_data.py b/dags/train/office/rent/split_data.py new file mode 100644 index 0000000..6ca47da --- /dev/null +++ b/dags/train/office/rent/split_data.py @@ -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") \ No newline at end of file diff --git a/dags/train/office/rent/train.py b/dags/train/office/rent/train.py new file mode 100644 index 0000000..7968cf1 --- /dev/null +++ b/dags/train/office/rent/train.py @@ -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加载。") \ No newline at end of file