coursera python答案整理_吴恩达老师Coursera经典机器学习课程作业答案(Python版本)-- Week2...

本博客整理了吴恩达Coursera机器学习课程第二周的Python版作业,涉及数据导入、散点图绘制、代价函数计算、梯度下降法实现及拟合效果展示,还包括预测不同人口规模下的利润。
摘要由CSDN通过智能技术生成

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

# Importing data

print('Importing data...')

data = pd.read_csv('ex1data1.txt', sep=',', names=['population', 'profit'])

X = data['population'].values

y = data['profit'].values

# Plotting data

print('Plotting data...')

plt.scatter(X, y, color='r', marker='x')

plt.title('Profit vs Population')

plt.xlabel('Population of City in 10,000s')

plt.ylabel('Profit in $ 10,000s')

plt.show()

input("Press Enter to continue...")

# Define cost function

def compute_cost(X, y, theta):

prediction = np.dot(X, theta)

error_vector = (prediction - y)

error_square_sum = np.sum((error_vector * error_vector))

return error_square_sum/(2*y.shape[0])

# Test correctness of cost function

m = X.shape[0] # number of training examples

X = np.column_stack((np.ones(m), X)) # add constant column to X

y = y.reshape(m, 1)

theta = np.zeros((2, 1)) # initial fitting parameter

iteration = 1500

alpha = 0.01

print("Check the accuracy of cost function...\n")

print('The cost with theta = [0 ; 0] should be around 32.07')

print('The calculated value is ', compute_cost(X, y, theta), '\n')

print('The cost with theta = [-1 ; 2] should be around 54.24')

print('The calculated value is ', compute_cost(X, y, np.array([[-1], [2]])), '\n')

input("Press Enter to continue...\n")

# Define gradient decent method to find the fitting parameters

def gradient_decent(X, y, theta, alpha, iteration):

m = y.shape[0]

j_history = {}

for i in range(iteration):

prediction = np.dot(X, theta)

updates = np.dot(X.T, (prediction - y))

theta = theta - alpha * (1.0 / m) * updates

j_history[i] = compute_cost(X, y, theta)

return theta, j_history

# Test correctness of gradient_decent method

theta, j_history = gradient_decent(X, y, theta, alpha, iteration)

print('Theta found by gradient descent:\n')

print(theta)

print('Expected theta values (approx)\n')

print(' -3.6303\n1.1664\n\n')

# Define function to plot cost history

def plot_cost_as_iteration(j_history):

lists = sorted(j_history.items()) # sorted by key, return a list of tuples

x, y = zip(*lists) # unpack a list of pairs into two tuples

plt.plot(x, y, marker='o', markevery=50)

plt.title('Cost History')

plt.xlabel('Number of iteration')

plt.ylabel('Cost')

plt.show()

# Define function to plot fitted data

def plot_linear_fit(x, y, theta):

m = y.shape[0]

plt.scatter(x, y)

plt.xlabel('Population of City in 10,000s')

plt.ylabel('Profit in $ 10,000s')

x_modified = np.column_stack((np.ones(m), x))

calculated_y = np.dot(x_modified, theta)

plt.plot(x, calculated_y, color='r')

plt.show()

# Plot cost history

print('Plotting j_history\n')

plot_cost_as_iteration(j_history)

# Plot fitted data

print('Plotting linear fit on the raw data\n')

plot_linear_fit(data['population'], data['profit'], theta)

# Predict values for population sizes of 35,000 and 70,000

predict1 = np.array([1, 3.5]).dot(theta)

print('For population = 35,000, we predict a profit of%f\n')

print(predict1*10000)

predict2 = np.array([1, 7]).dot(theta)

print('For population = 70,000, we predict a profit of%f\n')

print(predict2*10000)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值