Resources

  • homework answer: https://www.kesci.com/home/project/5da16a37037db3002d441810

    Univariate Linear Regression

    Code analyse

    Process data

    1. #Error: dataSet = pd.read_csv('ex1data1.txt')
    2. dataSet = pd.read_csv('ex1data1.txt', header=None, names=['Population', 'Profit'])
    3. dataSet.head()
    4. dataSet.plot(kind='scatter', x='Population', y='Profit')
    5. plt.show()

    Define variables

    1. dataSet.insert(0, 'theta0', 1)
    2. cols = dataSet.shape[1]
    3. X = dataSet.iloc[:, :-1]
    4. y = dataSet.iloc[:, cols-1:cols]
    5. alpha = 0.01
    6. iters = 1500
    7. '''
    8. 以下两种写法可能会导致最后结果不同
    9. X = np.matrix(X)
    10. y = np.matrix(y)
    11. X = np.matrix(X.values)
    12. y = np.matrix(y.values)
    13. '''
    14. X = np.matrix(X.values)
    15. y = np.matrix(y.values)
    16. theta = np.matrix(np.array([0,0]))

    cost function

    1. # Cost function
    2. def ComputeCost(X, y, theta):
    3. inner = np.power(((X * theta.T) - y), 2)
    4. cost = np.sum(inner) / (2 * len(X))
    5. return cost

    Gradient descent

    1. def gradientDescent(X, y, theta, alpha, iters):
    2. parameters = int(theta.ravel().shape[1])
    3. temp = np.matrix(np.zeros(theta.shape))
    4. cost = np.zeros(iters)
    5. for i in range(iters):
    6. inner = X * theta.T - y
    7. for j in range(parameters):
    8. term = np.multiply(inner, X[:,j])
    9. temp[0,j] = temp[0,j] - alpha * np.sum(term) / len(X)
    10. theta = temp
    11. cost[i] = computeCost(X, y, theta)
    12. return theta, cost
    13. '''
    14. return:
    15. matrix([[-3.63029144, 1.16636235]]),
    16. array([6.73719046, 5.93159357, 5.90115471, ..., 4.48343473, 4.48341145,
    17. 4.48338826])
    18. '''
    1. # Error code
    2. def gradientDescent2(X, y, theta, alpha, iters):
    3. parameters = int(theta.ravel().shape[1])
    4. cost = np.zeros(iters)
    5. for i in range(iters):
    6. error = (X * theta.T) - y
    7. for j in range(parameters):
    8. term = np.multiply(error, X[:,j])
    9. theta[0,j] = theta[0,j] - ((alpha / len(X)) * np.sum(term))
    10. cost[i] = ComputeCost(X, y, theta)
    11. return theta, cost
    12. '''
    13. return:
    14. matrix([[0, 0]]),
    15. array([32.07273388, 32.07273388, 32.07273388, ..., 32.07273388,
    16. 32.07273388, 32.07273388])
    17. ''''

    Normal equations ```python def normalEqn(X, y): theta = np.linalg.inv(X.T@X)@X.T@y #X.T@X等价于X.T.dot(X) return theta ‘’’ return: matrix([[-3.89578088], [ 1.19303364]])

梯度下降得到的结果是matrix([[-3.63029144, 1.16636235]]) 正规方程得到的结果是matrix([[-3.89578088], [1.19303364]]) ‘’’ ```

Process chart

屏幕快照 2020-07-19 下午4.31.27.png