diff --git a/7.5/__pycache__/datas.cpython-311.pyc b/7.5/__pycache__/datas.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..938e6ee696c79c8658ea37348a207a688f5a4022 Binary files /dev/null and b/7.5/__pycache__/datas.cpython-311.pyc differ diff --git a/7.5/datas.py b/7.5/datas.py new file mode 100644 index 0000000000000000000000000000000000000000..8a775b9005e58b128a9e5324e8d598b815154155 --- /dev/null +++ b/7.5/datas.py @@ -0,0 +1,156 @@ +#城市:吕梁1、太原2 + +#(city-城市) (area-面积) (rooms-房间数) (school-学区) (style-风格) + +datas=[ + #吕梁第一类 + { + "city":"吕梁", + "area":110, + "rooms":3, + "school":2, + "style":2, + "price":6000, + }, + { + "city":"吕梁", + "area":120, + "rooms":3, + "school":1, + "style":2, + "price":8000, + }, + { + "city":"吕梁", + "area":130, + "rooms":3, + "school":2, + "style":1, + "price":7500, + }, + #吕梁第二类 + { + "city":"吕梁", + "area":110, + "rooms":2, + "school":2, + "style":2, + "price":6500, + }, + { + "city":"吕梁", + "area":120, + "rooms":2, + "school":1, + "style":2, + "price":8600, + }, + { + "city":"吕梁", + "area":115, + "rooms":2, + "school":1, + "style":1, + "price":7000, + }, + #吕梁第三类 + { + "city":"吕梁", + "area":140, + "rooms":3, + "school":2, + "style":2, + "price":8600, + }, + { + "city":"吕梁", + "area":140, + "rooms":3, + "school":2, + "style":1, + "price":9000, + }, + { + "city":"吕梁", + "area":140, + "rooms":3, + "school":1, + "style":2, + "price":9500, + }, + # 太原第一类 + { + "city": "太原", + "area": 120, + "rooms": 3, + "school": 1, + "style": 2, + "price": 9000, + }, + { + "city": "太原", + "area": 120, + "rooms": 3, + "school": 2, + "style": 2, + "price": 9500, + }, + { + "city": "太原", + "area": 120, + "rooms": 3, + "school": 2, + "style": 2, + "price": 9800, + }, + # 太原第二类 + { + "city": "太原", + "area": 130, + "rooms": 3, + "school": 2, + "style": 1, + "price": 10000, + }, + { + "city": "太原", + "area": 135, + "rooms": 2, + "school": 1, + "style": 2, + "price": 11000, + }, + { + "city": "太原", + "area": 130, + "rooms": 3, + "school": 1, + "style": 2, + "price": 12000, + }, + # 太原第三类 + { + "city": "太原", + "area": 120, + "rooms": 3, + "school": 1, + "style": 1, + "price": 11000, + }, + { + "city": "太原", + "area": 125, + "rooms": 3, + "school": 1, + "style": 2, + "price": 12000, + }, + { + "city": "太原", + "area": 120, + "rooms": 3, + "school": 1, + "style": 2, + "price": 12500, + } +] \ No newline at end of file diff --git a/7.5/predict.py b/7.5/predict.py new file mode 100644 index 0000000000000000000000000000000000000000..2349cff2ccaada5f8e4dd19a657dcbf4f99a809e --- /dev/null +++ b/7.5/predict.py @@ -0,0 +1,40 @@ +# a=np.array([[1,2],[4,5]]) +# b=np.array([1,2]) +# #点乘 +# print(a.dot(b)) +# #转置 +# print(a.T) +# #求逆矩阵 +# print(np.linalg.pinv(a)) + +import numpy as np +from datas import datas + +# 初始化特征和标签列表 +X=[] +Y=[] + +# 城市标记字典 +cityMark={"吕梁":1,"太原":2} + +# 遍历数据集,构建特征和标签 +for item in datas: + single=[] + single.append(cityMark[item["city"]]) + single.append(item["area"]) + single.append(item["rooms"]) + single.append(item["school"]) + single.append(item["style"]) + + X.append(single) + Y.append(item["price"]) + +# 将特征和标签转换为numpy数组 +X=np.array(X) +Y=np.array(Y) + +# 计算线性回归的参数theta +theta=np.linalg.pinv(X.T.dot(X)).dot(X.T).dot(Y) +print(theta) + +