import numpy as np
import pandas as pd
import matplotlib. pyplot as plt
path = '1.txt'
data = pd. read_csv( path, names= [ 'Population' , 'Profit' ] )
data. head( )
Population Profit
0 6.1101 17.5920
1 5.5277 9.1302
2 8.5186 13.6620
3 7.0032 11.8540
4 5.8598 6.8233
data. describe ( )
Population Profit
count 97.000000 97.000000
mean 8.159800 5.839135
std 3.869884 5.510262
min 5.026900 - 2.680700
25 % 5.707700 1.986900
50 % 6.589400 4.562300
75 % 8.578100 7.046700
max 22.203000 24.147000
data. plot ( kind= 'scatter' , x= 'Population' , y= 'Profit' , figsize= ( 12 , 12 ) )
plt. show ( )
def computeCost ( X, y, theta) :
inner = np. power ( ( ( X@theta. T) - y) , 2 )
return np. sum ( inner) / ( 2 * len ( X) )
data. insert ( 0 , 'Ones' , 1 )
cols = data. shape[ 1 ]
X= data. iloc[ : , 0 : cols- 1 ]
y = data. iloc[ : , cols- 1 : cols]
theta = np. matrix ( np. array ( [ 0 , 0 ] ) )
X = np. matrix ( X. values)
y = np. matrix ( y. values)
def gradientDescend ( X, y, theta, alpha, iters) :
temp = np. matrix ( np. zeros ( theta. shape) )
parameter = int ( theta. ravel ( ) . shape[ 1 ] )
cost = np. zeros ( iters)
for i in range ( iters) :
error = ( X* theta. T) - y
for j in range ( parameter) :
term = np. multiply ( error, X[ : , j] ) #对应位置相乘,
temp[ 0 , j] = theta[ 0 , j] - ( ( alpha/ len ( X) ) * np. sum ( term) ) # 求导之后的结果
theta = temp
cost[ i] = computeCost ( X, y, theta)
return theta, cost
alpha = 0.01 #设置学习率
iters= 1000 #设置迭代次数
g, cost = gradientDescend ( X, y, theta, alpha, iters)
x= np. linspace ( data. Population. min ( ) , data. Population. max ( ) , 100 )
f = g[ 0 , 0 ] + g[ 0 , 1 ] * x
fig, axis = plt. subplots ( figsize= ( 12 , 8 ) )
axis. plot ( x, f, 'r' , label= 'Prediction' )
axis. scatter ( data. Population, data. Profit, label= 'Train Data' )
axis. legend ( )
axis. set_xlabel ( 'Population' )
axis. set_ylabel ( 'Profit' )
axis. set_title ( "Predicted Profit vs Population size" )
plt. show ( )
fig, axis = plt. subplots ( )
axis. plot ( np. arange ( iters) , cost, 'r' )
axis. set_xlabel ( 'Iteration' )
axis. set_ylabel ( 'ERROR' )
axis. set_title ( 'ERROR vs TRAINNG EPOCH' )
多变量回归
path = '2.txt'
data2 = pd. read_csv ( path, header= None, names= [ 'Size' , 'Bedroom' , 'Price' ] )
data2 = ( data2- data2. mean ( ) ) / data2. std ( )
data2. insert ( 0 , 'Ones' , 1 )
cols = data2. shape[ 1 ]
x2 = data2. iloc[ : , 0 : cols- 1 ]
y2 = data2. iloc[ : , cols- 1 : cols]
x2 = np. matrix ( x2. values)
y2 = np. matrix ( y2. values)
theat2 = np. matrix ( np. array ( [ 0 , 0 , 0 ] ) )
g2, cost2 = gradientDescend ( x2, y2, theat2, alpha, iters)
fig, ax = plt. subplots ( figsize= ( 12 , 8 ) )
ax. plot ( np. arange ( iters) , cost2, 'r' )
ax. set_xlabel ( 'Iterations' )
ax. set_ylabel ( 'Cost' )
ax. set_title ( 'Error vs. Training Epoch' )
plt. show ( )
使用库函数
from sklearn import linear_model
model = linear_model. LinearRegression ( )
model. fit ( X, y)
x = np. array ( X[ : , 1 ] . A1)
f = model. predict ( X) . flatten ( )
fig, ax = plt. subplots ( figsize= ( 12 , 8 ) )
ax. plot ( x, f, 'r' , label= 'Prediction' )
ax. scatter ( data. Population, data. Profit, label= 'Traning Data' )
ax. legend ( loc= 2 )
ax. set_xlabel ( 'Population' )
ax. set_ylabel ( 'Profit' )
ax. set_title ( 'Predicted Profit vs. Population Size' )
plt. show ( )