Exemplo n.º 1
0
    title = 'polynomial with bgd: rate=%.2f, maxLoop=%d, epsilon=%.3f \n time: %ds' % (
        rate, maxLoop, epsilon, timeConsumed)
    ax = fittingFig.add_subplot(111, title=title)
    trainingSet = ax.scatter(srcX[:, 1].flatten().A[0], y[:, 0].flatten().A[0])

    print(theta)

    # 打印拟合曲线
    xx = np.linspace(50, 100, 50)
    xx2 = np.power(xx, 2)
    yHat = []
    for i in range(50):
        normalizedSize = (xx[i] - xx.mean()) / xx.std(0)
        normalizedSize2 = (xx2[i] - xx2.mean()) / xx2.std(0)
        x = np.matrix([[1, normalizedSize, normalizedSize2]])
        yHat.append(regression.h(theta, x.T))
    fittingLine, = ax.plot(xx2, yHat, color='g')

    ax.set_xlabel('Yield')
    ax.set_ylabel('temperature')

    plt.legend([trainingSet, fittingLine],
               ['Training Set', 'Polynomial Regression'])
    plt.show()

    # 打印误差曲线
    errorsFig = plt.figure()
    ax = errorsFig.add_subplot(111)
    ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2e'))

    ax.plot(range(len(errors)), errors)
Exemplo n.º 2
0
    rate = 1
    maxLoop = 50
    epsilon = 1

    result, timeConsumed = regression.bgd(rate, maxLoop, epsilon, X, y)
    theta, errors, thetas = result

    print('theta is:')
    print(theta)
    print('........')

    # 预测价格
    normalizedSize = (1650 - srcX[:, 0].mean(0)) / srcX[:, 0].std(0)
    normalizedBr = (3 - srcX[:, 1].mean(0)) / srcX[:, 1].std(0)
    predicateX = np.matrix([[1, normalizedSize, normalizedBr]])
    price = regression.h(theta, predicateX.T)
    print('Predicted price of a 1650 sq-ft, 3 br house: $%.4f' % price)
    print('........')

    # 打印拟合平面
    fittingFig = plt.figure(figsize=(16, 12))
    title = 'polynomial with bgd: rate=%.3f, maxLoop=%d, epsilon=%.3f \n time: %ds' % (
        rate, maxLoop, epsilon, timeConsumed)
    ax = fittingFig.add_subplot(111, projection='3d', title=title)

    xx = np.linspace(0, 5000, 25)
    yy = np.linspace(0, 5, 25)
    zz = np.zeros((25, 25))
    for i in range(25):
        for j in range(25):
            normalizedSize = (xx[i] - srcX[:, 0].mean(0)) / srcX[:, 0].std(0)
Exemplo n.º 3
0
    ax = fittingFig.add_subplot(111, title=title)
    trainingSet = ax.scatter(srcX[:, 0].flatten().A[0], y[:, 0].flatten().A[0])

    print theta
    print theta2

    # 打印拟合曲线
    xx = np.linspace(1, 7, 50)
    xx2 = np.power(xx, 2)
    yHat1 = []
    yHat2 = []
    for i in range(50):
        normalizedSize = (xx[i] - xx.mean()) / xx.std(0)
        normalizedSize2 = (xx2[i] - xx2.mean()) / xx2.std(0)
        x = np.matrix([[1, normalizedSize, normalizedSize2]])
        yHat1.append(regression.h(theta, x.T))
        yHat2.append(regression.h(theta2, x.T))
    fittingLine1, = ax.plot(xx, yHat1, color='g')
    fittingLine2, = ax.plot(xx, yHat2, color='r')

    ax.set_xlabel('temperature')
    ax.set_ylabel('yield')

    plt.legend([trainingSet, fittingLine1, fittingLine2],
               ['Training Set', r'LWR with $\tau$=1', r'LWR with $\tau$=0.1'])
    plt.show()

    # 打印误差曲线
    errorsFig = plt.figure()
    ax = errorsFig.add_subplot(111)
    ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2e'))
Exemplo n.º 4
0
    # 打印特征点
    fittingFig = plt.figure()
    title = 'polynomial with bgd: rate=%.2f, maxLoop=%d, epsilon=%.3f \n time: %ds' % (rate, maxLoop, epsilon, timeConsumed)
    ax = fittingFig.add_subplot(111, title=title)

    trainingSet = ax.scatter(srcX[:, 0].flatten().A[0], y[:, 0].flatten().A[0])

    # 打印拟合曲线
    xx = np.linspace(50, 100, 50)
    xx2 = np.power(xx, 2)
    yHat = []
    for i in range(50):
        normalizedSize = (xx[i] - xx.mean()) / xx.std(0)
        normalizedSize2 = (xx2[i] - xx2.mean()) / xx2.std(0)
        x = np.matrix([[1, normalizedSize, normalizedSize2]])
        yHat.append(re.h(theta, x.T))

    fittingLine, = ax.plot(xx, yHat, color='g')

    ax.set_xlabel('Yield')
    ax.set_ylabel('temperature')

    plt.legend([trainingSet, fittingLine], ['Training Set', 'Polynomial Regression']) #显示图中标签
    plt.show()

    # 打印误差曲线
    errorsFig = plt.figure()
    ax = errorsFig.add_subplot(111)
    ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2e'))

    ax.plot(range(len(errors)), errors)