单变量线性回归(Linear Regression with One Variable)¶
In [54]:
#初始化工作
import random
import numpy as np
import matplotlib.pyplot as plt
# This is a bit of magic to make matplotlib figures appear inline in the notebook
# rather than in a new window.
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# Some more magic so that the notebook will reload external python modules;
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
1、加载数据与可视化¶
In [55]:
print('Plotting Data ...')
def load_exdata(filename):
data = []
with open(filename, 'r') as f:
for line in f.readlines():
line = line.split(',')
current = [float(item) for item in line]
#5.5277,9.1302
data.append(current)
return data
data = load_exdata('ex1data1.txt');
data = np.array(data)
print(data.shape)
x = data[:, 0]; y = data[:,1]
m = data.shape[0]
#number of training examples
plt.plot(x,y,'rx')
plt.ylabel('Profit in $10,000s');
plt.xlabel('Population of City in 10,000s');
plt.title("Training data")