销售分析次日:线性回归

y = groupall.values[:, 2:] x = range(np.shape(y)[1]) plt.plot(x, y[0], "b.") x2 = np.array(x).reshape(-1,1) y2 = np.array(y[0]).reshape(-1,1) plt.plot(x2, y2, "r.") sgd_reg2 = SGDRegressor(n_iter_no_change=5000, penalty=None, eta0=0.1, random_state=42, max_iter=100000) sgd_reg2.fit(x2, y2.ravel()) y_predict = sgd_reg2.predict(x2) plt.plot(x2, y_predict, "y-") print(y_predict) 程序员

注意,一旦我把 eta0=0.1改成0.2以后,图形便失真了: 数组

到了0.3,学习结果基本失控了: dom

 

x=[[1],[2],[3],[4],[5]] y=[[1],[2],[3],[4],[5]] x=[1,2,3,4,5] y=[1,2,3,4,5] plt.plot(x, y, "b.") sgd_reg = SGDRegressor(n_iter_no_change=5000, penalty=None, eta0=0.5, random_state=42, max_iter=100000) x_reshape = np.array(x).reshape(-1,1) y_reshape = np.array(y).reshape(-1,1) print("x_re:", x_reshape) print("y_re:", y_reshape) sgd_reg.fit(x_reshape, y_reshape) y_predict = sgd_reg.predict(x_reshape) plt.plot(x_reshape, y_predict, "y-") 函数

这是一段代码想要说明,对于plot画图而言x,y两种形式:一种shape是(5,1),另一种是(5,)),暂且称之为矩阵类型,一种是列表类型,二者都是能够的;可是对于学习器的fit函数而言,必定是要矩阵形式;列表形式转为矩阵类型须要使用reshape来作。 学习

reshape这里我要多说一句,正常第一个参数表明要分割为多少个元素,第二个表明要每一个元素里面有几个维度;在数组角度就是一维数组和二维数组的维度数;这里有一个特殊值-1,表明忽略指定,根据另一个维度来自动进行计算: blog

arr = np.arange(6) brr = arr.reshape((2,-1)) print(brr) brr = arr.reshape((-1,3)) print(brr) brr = arr.reshape((2,3)) print(brr) it

上述代码中第一段表明arr要被分为两个元素,维度据此计算;第二段代码指定每一个元素维度是3,划分几个numpy.array自动算;第三段代码就比较苦逼,是程序员本身来算,其实已经没有第三段的必要了。sso

相关文章
相关标签/搜索