# 机器学习 **Repository Path**: wcscsw/learn ## Basic Information - **Project Name**: 机器学习 - **Description**: No description available - **Primary Language**: JavaScript - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2020-05-23 - **Last Updated**: 2021-01-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #第一节 简单线性回归 ## 1.1基于线性最小二乘法 ```` import numpy as np import matplotlib.pyplot as plt # 尺寸 x_train = np.array([[6], [8], [10], [14], [18]]).reshape(-1, 1) # 价格 price_train = [7, 9, 13, 17.5, 18] plt.figure() plt.title('') # 披萨直径价格散点图 plt.plot(x_train, price_train, 'k.') plt.axis([0, 25, 0, 25]) plt.grid(True) plt.show() # 基于线性最小二乘的方法 from sklearn.linear_model import LinearRegression # 创建一个估计器实例训练数据拟合模型 model = LinearRegression().fit(x_train, price_train) test_pizza = np.array([[12]]) predicted_price = model.predict(test_pizza)[0] print('12寸披萨的价格:', predicted_price) # 代价函数->残差平方和代价函数 Rss = np.mean((model.predict(x_train) - price_train) ** 2) print('Rss:%0.2f' % Rss) # x的均值 x_bar = x_train.mean() print('x的均值', x_bar) # 11.2 variamce = ((x_train - x_bar) ** 2).sum() / (x_train.shape[0] - 1) print('方差:', variamce) varx = np.var(x_train, ddof=1) #print(varx) # 计算price_train的均值 priceArr = np.array(price_train) price_bar = priceArr.mean() # 转至成行向量 # covariance = np.multiply((X-x_bar).transpose(),price-price_bar).sum()/(X.shape[0]-1) covariance = np.cov(x_train.transpose(), priceArr)[0][1] beta = covariance / variamce rfa = price_bar - beta * x_bar #模型评价 x_test = np.array([8,9,11,16,12]).reshape(-1, 1) prince_test = np.array([11,8.5,15,18,11]).reshape(-1, 1) model1 = LinearRegression().fit(x_train,price_train) #R方的方法 R方称为 决定系数 = 皮尔森极差相关系数的平方 r_squared =model1.score(x_test,prince_test) print('决定系数:%.2f' % r_squared) ````