机器学习当中的学习和预测,就是线性回归

from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression

import numpy as np
def price_prediction():
#https://scikit-earn.org/stable/tutore_learningma
#数据有三个特征:距离地铁距离,附近小学数量,小区绿化率,三个单位是不统一
#P = W1A + W2B + W3*C
X = np.array([[500.0, 3.0, 0.3], [1000.0, 1.0, 0.6],
[750.0, 2.0, 0.3], [600.0, 5.0, 0.2], [1200.0, 1.0, 0.6]])
#具有三个特征的房屋对应的房价,Y是表示目标
Y = np.array([10000., 9000., 8000., 12000., 8500.])
#把X上面不统一的单位,直接转换成统一的单位,防止一些大的数据对权重的影响,就是对应的与自己的方差相减除以方差
#标准量化类
stand_x = StandardScaler()
#训练标准量化模型
x_std_model = stand_x.fit(X)
#将训练数据集量化
x_std_result = x_std_model.transform(X)

std_y = StandardScaler()
#-1是表示有多少行,电脑自动计算,1是有多少列
y_std_model = std_y.fit(Y.reshape(-1,1))
y_std_result = y_std_model.transform(Y.reshape(-1,1))

#开始训练
line_model = LinearRegression()
#训练的样本和训练的目标传进去
line_model.fit(x_std_result,y_std_result)
#预测[1300,3.0,0.4]特征的数据房屋价格多少,预测之前需要按照训练集的标准化方法进行标准化
x_predict = x_std_model.transform(np.array([[1300,3.0,0.4]]))
#预测完成的结果需要按照目标集的标准化方法进行反向标准化
print(std_y.inverse_transform((line_model.predict(x_predict))))


# print(y_std_result)
#

#函数的调用
price_prediction()

猜你喜欢

转载自blog.csdn.net/weixin_44274975/article/details/88681036