diff --git a/pythonProject3/.idea/.gitignore b/pythonProject3/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..35410cacdc5e87f985c93a96520f5e11a5c822e4 --- /dev/null +++ b/pythonProject3/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/pythonProject3/.idea/inspectionProfiles/profiles_settings.xml b/pythonProject3/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000000000000000000000000000000000000..105ce2da2d6447d11dfe32bfb846c3d5b199fc99 --- /dev/null +++ b/pythonProject3/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pythonProject3/.idea/misc.xml b/pythonProject3/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..2a08db1706d0c3bcef43f907d58b10d624f3f7de --- /dev/null +++ b/pythonProject3/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/pythonProject3/.idea/modules.xml b/pythonProject3/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..1fb3c8be37acbe2ac220221f9983c305224ff79c --- /dev/null +++ b/pythonProject3/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/pythonProject3/.idea/pythonProject3.iml b/pythonProject3/.idea/pythonProject3.iml new file mode 100644 index 0000000000000000000000000000000000000000..2c80e1269497d12e018fd6afa29982e56b0fb70d --- /dev/null +++ b/pythonProject3/.idea/pythonProject3.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/pythonProject3/7.5/__pycache__/datas.cpython-312.pyc b/pythonProject3/7.5/__pycache__/datas.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6b01ee82aa311f96a60f7f93d2e3393c1824b82d Binary files /dev/null and b/pythonProject3/7.5/__pycache__/datas.cpython-312.pyc differ diff --git a/pythonProject3/7.5/datas.py b/pythonProject3/7.5/datas.py new file mode 100644 index 0000000000000000000000000000000000000000..b6d19e6db8525f0c3b69e158a7c5465eb5ad96ec --- /dev/null +++ b/pythonProject3/7.5/datas.py @@ -0,0 +1,152 @@ +datas = [ + # 吕梁第一类 + { + "city": "吕梁", + "area": 85, + "rooms": 2, + "school": 1, + "style": 2, + "price": 4500, + }, + { + "city": "吕梁", + "area": 105, + "rooms": 3, + "school": 1, + "style": 2, + "price": 5800, + }, + { + "city": "吕梁", + "area": 125, + "rooms": 3, + "school": 3, + "style": 1, + "price": 6400, + }, + # 吕梁第二类 + { + "city": "吕梁", + "area": 115, + "rooms": 2, + "school": 1, + "style": 2, + "price": 6700, + }, + { + "city": "吕梁", + "area": 118, + "rooms": 2, + "school": 3, + "style": 2, + "price": 7500, + }, + { + "city": "吕梁", + "area": 112, + "rooms": 2, + "school": 3, + "style": 2, + "price": 8100, + }, + # 吕梁第三类 + { + "city": "吕梁", + "area": 138, + "rooms": 3, + "school": 2, + "style": 2, + "price": 8550, + }, + { + "city": "吕梁", + "area": 145, + "rooms": 3, + "school": 2, + "style": 1, + "price": 8950, + }, + { + "city": "吕梁", + "area": 142, + "rooms": 3, + "school": 1, + "style": 3, + "price": 9600, + }, + # 太原第一类 + { + "city": "太原", + "area": 115, + "rooms": 3, + "school": 1, + "style": 2, + "price": 8900, + }, + { + "city": "太原", + "area": 125, + "rooms": 3, + "school": 1, + "style": 2, + "price": 9400, + }, + { + "city": "太原", + "area": 122, + "rooms": 3, + "school": 2, + "style": 1, + "price": 9700, + }, + # 太原第二类 + { + "city": "太原", + "area": 135, + "rooms": 3, + "school": 2, + "style": 1, + "price": 10100, + }, + { + "city": "太原", + "area": 138, + "rooms": 2, + "school": 1, + "style": 2, + "price": 11200, + }, + { + "city": "太原", + "area": 132, + "rooms": 3, + "school": 2, + "style": 2, + "price": 12100, + }, + # 太原第三类 + { + "city": "太原", + "area": 123, + "rooms": 3, + "school": 2, + "style": 1, + "price": 11100, + }, + { + "city": "太原", + "area": 128, + "rooms": 3, + "school": 3, + "style": 1, + "price": 12150, + }, + { + "city": "太原", + "area": 124, + "rooms": 3, + "school": 3, + "style": 2, + "price": 12600, + } +] diff --git a/pythonProject3/7.5/predict.py b/pythonProject3/7.5/predict.py new file mode 100644 index 0000000000000000000000000000000000000000..ce306749519d81df385a08bc950c0256e7607bc3 --- /dev/null +++ b/pythonProject3/7.5/predict.py @@ -0,0 +1,59 @@ +import numpy as np +from datas import datas + +def get_user_input(): + city = input("请输入城市(吕梁或太原):") + area = float(input("请输入面积(平方米):")) + rooms = int(input("请输入房间数:")) + school = int(input("请输入学区(1-3):")) + style = int(input("请输入装修风格(1-3):")) + return city, area, rooms, school, style + +def prepare_data(datas): + X = [] + Y = [] + cityMark = {"吕梁": 1, "太原": 2} + + for item in datas: + single = [] + # 偏置项 + single.append(1) + # 城市 + single.append(cityMark[item["city"]]) + # 面积 + single.append(item["area"]) + # 房间数 + single.append(item["rooms"]) + # 装修 + single.append(item["style"]) + X.append(single) + Y.append(item["price"]) + + X = np.array(X) + Y = np.array(Y) + return X, Y, cityMark + +def train_model(X, Y): + # 计算theta + theta = np.linalg.pinv(X.T.dot(X)).dot(X.T).dot(Y) + return theta + +def predict_price(theta, user_input, cityMark): + city, area, rooms, school, style = user_input + new_data = [1, cityMark[city], area, rooms, style] # 偏置项, 城市, 面积, 房间数, 装修 + predicted_price = theta.dot(np.array(new_data)) + return predicted_price + +# 准备数据 +X, Y, cityMark = prepare_data(datas) + +# 训练模型 +theta = train_model(X, Y) + +# 获取用户输入 +user_input = get_user_input() + +# 预测房价 +predicted_price = predict_price(theta, user_input, cityMark) + +print(f"预测的房价是: {predicted_price} 元/平方米") diff --git a/pythonProject3/app.py b/pythonProject3/app.py new file mode 100644 index 0000000000000000000000000000000000000000..28f1337042dd7cb862bc428587faed37e0137d92 --- /dev/null +++ b/pythonProject3/app.py @@ -0,0 +1,51 @@ +from flask import Flask, render_template, request +import math + +app = Flask(__name__) + +datas = [ + { "name": "电影1", "img": "movie1.png", "info": "这是电影1的简介。", "feature": [1, 0, 0, 1, 0] }, + { "name": "电影2", "img": "movie2.webp", "info": "这是电影2的简介。", "feature": [0, 1, 0, 1, 0] }, + { "name": "电影3", "img": "movie3.webp", "info": "这是电影3的简介。", "feature": [1, 1, 0, 0, 0] }, + { "name": "电影4", "img": "movie4.webp", "info": "这是电影4的简介。", "feature": [0, 0, 1, 0, 1] }, + { "name": "电影5", "img": "movie5.webp", "info": "这是电影5的简介。", "feature": [1, 0, 1, 1, 0] }, + { "name": "电影6", "img": "movie6.webp", "info": "这是电影6的简介。", "feature": [0, 1, 1, 0, 0] }, + { "name": "电影7", "img": "movie7.webp", "info": "这是电影7的简介。", "feature": [1, 1, 0, 0, 1] }, + { "name": "电影8", "img": "movie8.webp", "info": "这是电影8的简介。", "feature": [0, 0, 0, 1, 1] }, + { "name": "电影9", "img": "movie9.jpg", "info": "这是电影9的简介。", "feature": [1, 0, 0, 0, 1] }, + { "name": "电影10", "img": "movie10.jpg", "info": "这是电影10的简介。", "feature": [0, 1, 0, 0, 1] }, + { "name": "电影11", "img": "movie11.jpg", "info": "这是电影11的简介。", "feature": [1, 0, 1, 0, 0] }, + { "name": "电影12", "img": "movie12.jpg", "info": "这是电影12的简介。", "feature": [0, 1, 1, 0, 1] }, + { "name": "电影13", "img": "movie13.jpg", "info": "这是电影13的简介。", "feature": [1, 0, 0, 1, 1] }, + { "name": "电影14", "img": "movie14.jpg", "info": "这是电影14的简介。", "feature": [0, 0, 1, 1, 0] }, + { "name": "电影15", "img": "movie15.jpg", "info": "这是电影15的简介。", "feature": [1, 1, 1, 0, 0] }, + { "name": "电影16", "img": "movie16.jpg", "info": "这是电影16的简介。", "feature": [0, 0, 0, 0, 1] } +] + +def vector_angle(v1, v2): + dot_product = sum(a * b for a, b in zip(v1, v2)) + magnitude1 = math.sqrt(sum(a * a for a in v1)) + magnitude2 = math.sqrt(sum(b * b for b in v2)) + if magnitude1 * magnitude2 == 0: + return 90 + cosine = dot_product / (magnitude1 * magnitude2) + angle = math.acos(cosine) + return math.degrees(angle) + +@app.route("/") +def index(): + return render_template("demo.html", movies=datas) + +@app.route("/recommend/") +def recommend(index): + current_movie = datas[index] + recommendations = [] + for i, movie in enumerate(datas): + if i != index: + angle = vector_angle(current_movie["feature"], movie["feature"]) + recommendations.append({ "name": movie["name"], "img": movie["img"], "angle": angle }) + recommendations = sorted(recommendations, key=lambda x: x["angle"]) + return render_template("recommend.html", recommendations=recommendations) + +if __name__ == "__main__": + app.run(debug=True) diff --git a/pythonProject3/datas.py b/pythonProject3/datas.py new file mode 100644 index 0000000000000000000000000000000000000000..e2bae2b17046c207e056a9e6d7d7c5d217efbb17 --- /dev/null +++ b/pythonProject3/datas.py @@ -0,0 +1,98 @@ +datas = [ + { + "name": "电影1", + "img": "movie1.png", + "info": "这是电影1的简介。", + "feature": [1, 0, 0, 1, 0] + }, + { + "name": "电影2", + "img": "movie2.webp", + "info": "这是电影2的简介。", + "feature": [0, 1, 0, 1, 0] + }, + { + "name": "电影3", + "img": "movie3.webp", + "info": "这是电影3的简介。", + "feature": [1, 1, 0, 0, 0] + }, + { + "name": "电影4", + "img": "movie4.webp", + "info": "这是电影4的简介。", + "feature": [0, 0, 1, 0, 1] + }, + { + "name": "电影5", + "img": "movie5.webp", + "info": "这是电影5的简介。", + "feature": [1, 0, 1, 1, 0] + }, + { + "name": "电影6", + "img": "movie6.webp", + "info": "这是电影6的简介。", + "feature": [0, 1, 1, 0, 0] + }, + { + "name": "电影7", + "img": "movie7.webp", + "info": "这是电影7的简介。", + "feature": [1, 1, 0, 0, 1] + }, + { + "name": "电影8", + "img": "movie8.webp", + "info": "这是电影8的简介。", + "feature": [0, 0, 0, 1, 1] + }, + { + "name": "电影9", + "img": "movie9.jpg", + "info": "这是电影9的简介。", + "feature": [1, 0, 0, 0, 1] + }, + { + "name": "电影10", + "img": "movie10.jpg", + "info": "这是电影10的简介。", + "feature": [0, 1, 0, 0, 1] + }, + { + "name": "电影11", + "img": "movie11.jpg", + "info": "这是电影11的简介。", + "feature": [1, 0, 1, 0, 0] + }, + { + "name": "电影12", + "img": "movie12.jpg", + "info": "这是电影12的简介。", + "feature": [0, 1, 1, 0, 1] + }, + { + "name": "电影13", + "img": "movie13.jpg", + "info": "这是电影13的简介。", + "feature": [1, 0, 0, 1, 1] + }, + { + "name": "电影14", + "img": "movie14.jpg", + "info": "这是电影14的简介。", + "feature": [0, 0, 1, 1, 0] + }, + { + "name": "电影15", + "img": "movie15.jpg", + "info": "这是电影15的简介。", + "feature": [1, 1, 1, 0, 0] + }, + { + "name": "电影16", + "img": "movie16.jpg", + "info": "这是电影16的简介。", + "feature": [0, 0, 0, 0, 1] + } +] diff --git a/pythonProject3/predict.py b/pythonProject3/predict.py new file mode 100644 index 0000000000000000000000000000000000000000..89c8934d4d64d267605d5553bd623c351e1eeb10 --- /dev/null +++ b/pythonProject3/predict.py @@ -0,0 +1,33 @@ +import numpy as np + +# a = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) +# b = np.array([1, 2, 3]) +# print(a.T) +# print(a.dot(b)) +# print(np.linalg.pinv(a)) +import numpy as np +## 求点乘 求点乘 求转置 求逆 +## 特征矩阵 X +x = np.array([ + [180, 3, 2, 2], + [220, 4, 1, 2], + [150, 2, 2, 1], + [250, 5, 2, 2], + [200, 3, 2, 2], + [180, 4, 1, 1], + [130, 2, 1, 2], + [280, 5, 2, 2], + [210, 3, 2, 1], + [240, 4, 2, 2] +]) + +# 目标向量 y +y = np.array([500, 600, 400, 700, 550, 580, 360, 720, 530, 620]) + +# 假设一个简单的系数向量 w(实际应用中需要根据模型训练得到) +w = np.array([1, 2, 3, 4]) + +# 使用矩阵乘法计算预测值 +predicted_y = np.dot(x, w) + +print("预测的房价:", predicted_y) diff --git a/pythonProject3/static/img/movie1.png b/pythonProject3/static/img/movie1.png new file mode 100644 index 0000000000000000000000000000000000000000..0f9a9bb41b05208a41bfa0c68f19d0ec3041f05b Binary files /dev/null and b/pythonProject3/static/img/movie1.png differ diff --git a/pythonProject3/static/img/movie10.jpg b/pythonProject3/static/img/movie10.jpg new file mode 100644 index 0000000000000000000000000000000000000000..3d9fb41f63bfbd15cb1ba492137f52e5cdae9430 Binary files /dev/null and b/pythonProject3/static/img/movie10.jpg differ diff --git a/pythonProject3/static/img/movie11.jpg b/pythonProject3/static/img/movie11.jpg new file mode 100644 index 0000000000000000000000000000000000000000..337bcfcf506b1c1ab15d221ce4d9d9bdbc19f8ab Binary files /dev/null and b/pythonProject3/static/img/movie11.jpg differ diff --git a/pythonProject3/static/img/movie12.jpg b/pythonProject3/static/img/movie12.jpg new file mode 100644 index 0000000000000000000000000000000000000000..8e35df47c485da06a26c92b9a0cde6b0bc7d1097 Binary files /dev/null and b/pythonProject3/static/img/movie12.jpg differ diff --git a/pythonProject3/static/img/movie13.jpg b/pythonProject3/static/img/movie13.jpg new file mode 100644 index 0000000000000000000000000000000000000000..38025bd44ee0f897e3c2702f9624fb26aa0a1564 Binary files /dev/null and b/pythonProject3/static/img/movie13.jpg differ diff --git a/pythonProject3/static/img/movie14.jpg b/pythonProject3/static/img/movie14.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5884ef5b556e89d74e44310b46d2e0cb0597d80e Binary files /dev/null and b/pythonProject3/static/img/movie14.jpg differ diff --git a/pythonProject3/static/img/movie15.jpg b/pythonProject3/static/img/movie15.jpg new file mode 100644 index 0000000000000000000000000000000000000000..d94b598bd0691c54098101b06056373a61b47f36 Binary files /dev/null and b/pythonProject3/static/img/movie15.jpg differ diff --git a/pythonProject3/static/img/movie16.jpg b/pythonProject3/static/img/movie16.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2dbc06c3fc2c9c99049df335ea316f32672f61e0 Binary files /dev/null and b/pythonProject3/static/img/movie16.jpg differ diff --git a/pythonProject3/static/img/movie2.webp b/pythonProject3/static/img/movie2.webp new file mode 100644 index 0000000000000000000000000000000000000000..9b8c5fd1d5dd57236332ce413642cd79f8a94758 Binary files /dev/null and b/pythonProject3/static/img/movie2.webp differ diff --git a/pythonProject3/static/img/movie3.webp b/pythonProject3/static/img/movie3.webp new file mode 100644 index 0000000000000000000000000000000000000000..d59433079f3a2d99308ae55198428281374dd91e Binary files /dev/null and b/pythonProject3/static/img/movie3.webp differ diff --git a/pythonProject3/static/img/movie4.webp b/pythonProject3/static/img/movie4.webp new file mode 100644 index 0000000000000000000000000000000000000000..20dd70d2c2b2083f4094e5fc24e02a6493e6331f Binary files /dev/null and b/pythonProject3/static/img/movie4.webp differ diff --git a/pythonProject3/static/img/movie5.webp b/pythonProject3/static/img/movie5.webp new file mode 100644 index 0000000000000000000000000000000000000000..72d93d34cd4973ad4020cb83318d1e5dfaff1f11 Binary files /dev/null and b/pythonProject3/static/img/movie5.webp differ diff --git a/pythonProject3/static/img/movie6.webp b/pythonProject3/static/img/movie6.webp new file mode 100644 index 0000000000000000000000000000000000000000..95eace357967c833ca694e6be66dc928d0c168cb Binary files /dev/null and b/pythonProject3/static/img/movie6.webp differ diff --git a/pythonProject3/static/img/movie7.webp b/pythonProject3/static/img/movie7.webp new file mode 100644 index 0000000000000000000000000000000000000000..bf420cb8da7574b0ef8b6f90d2e4b7040ea56613 Binary files /dev/null and b/pythonProject3/static/img/movie7.webp differ diff --git a/pythonProject3/static/img/movie8.webp b/pythonProject3/static/img/movie8.webp new file mode 100644 index 0000000000000000000000000000000000000000..2e875b6b4daa289ec8b157a51b4633bada4087f0 Binary files /dev/null and b/pythonProject3/static/img/movie8.webp differ diff --git a/pythonProject3/static/img/movie9.jpg b/pythonProject3/static/img/movie9.jpg new file mode 100644 index 0000000000000000000000000000000000000000..9208070d86ac306b58f1561b8ec5991c94660366 Binary files /dev/null and b/pythonProject3/static/img/movie9.jpg differ diff --git a/pythonProject3/templates/demo.html b/pythonProject3/templates/demo.html new file mode 100644 index 0000000000000000000000000000000000000000..6870ee57034d8d8496c29c7233d3280dcbf562f0 --- /dev/null +++ b/pythonProject3/templates/demo.html @@ -0,0 +1,90 @@ + + + + + 电影推荐系统 + + + + + + {% for movie in movies %} + + + + + + + + {{ movie.name }} + 推荐电影 + + + {% endfor %} + + + diff --git a/pythonProject3/templates/recommend.html b/pythonProject3/templates/recommend.html new file mode 100644 index 0000000000000000000000000000000000000000..ca8ee17d951d3a4fb037a84ba088c80576b7c8df --- /dev/null +++ b/pythonProject3/templates/recommend.html @@ -0,0 +1,83 @@ + + + + + 推荐电影 + + + + + + {% for movie in recommendations %} + + + + + + {{ movie.name }} + 夹角 {{ movie.angle }}° + + + {% endfor %} + + +