Andrew Ng课程作业第二周(python版)

作业目的:学习二维选择的逻辑回归模型

作业内容:根据学生两次考试成绩判断是否应该被学校录取

提供的数据:ex2data1.txt(mooc可下载),数据集第一列成绩1,第二列是成绩2,最后一列是、1(不录取/录取)

处理过程:

  • step1:读取数据,dataframe数据格式,确定列头
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

path = 'd:\jupyter\ipython-notebooks-master' + '\data\ex2data1.txt'
data = pd.read_csv(path, header=None, names=['Exam 1', 'Exam 2', 'Admitted'])
data.head()
  • step2:画出散点图,横纵坐标为两次考试成绩,用点的样式和颜色区分是否被录取
#将admitted=1的数据筛选出来放在positive dataframe数据集中
positive = data[data['Admitted'].isin([1])]
#将admitted=0的数据筛选出来放在negative dataframe数据集中
negative = data[data['Admitted'].isin([0])]

fig,ax=plt.subplots(figsize=(12,8))
#画出positive数据,横坐标exam1,纵坐标exam 2
ax.scatter(positive['Exam 1'], positive['Exam 2'], s=50, c='b', marker='o', label='Admitted')

#画出negative数据,横坐标exam1,纵坐标exam 2
ax.scatter(negative['Exam 1'], negative['Exam 2'], s=50, c='r', marker='x', label='Not Admitted')

ax.legend()
ax.set_xlabel('Exam 1 Score')
ax.set_ylabel('Exam 2 Score')
  • step3:计算cost function
#定义sigmoid function
def sigmoid(z):
    return 1 / (1+np.exp(-z))

#画出图形
nums=np.arange(-10,10,step=1)
fig,ax = plt.subplots(figsize=(12,8))
ax.plot(nums,sigmoid(nums),'r')

#cost function
def cost(theta,X,y):
    theta = np.matrix(theta)
    X= np.matrix(X)
    y= np.matrix(y)
    first = np.multiply(-y, np.log(sigmoid(X*theta.T)))
    second= np.multiply((1-y),np.log(1-sigmoid(X*theta.T)))
    return np.sum(first-second)/(len(x))
  • step4:准备X,y,theta数据集
data.insert(0,'Ones',1)

cols = data.shape[1]
X = data.iloc[:,0:cols-1]
y = data.iloc[:,cols-1:cols]

X = np.array(X.values)
y = np.array(y.values)
theta = np.zeros(3)

X.shape, theta.shape, y.shape
  • step5:将初始theta和X,y带入cost function
cost(theta, X, y)
  • step6:计算梯度
def gradient(theta, X, y):
    theta = np.matrix(theta)
    X = np.matrix(X)
    y = np.matrix(y)
 
    parameters = int(theta.ravel().shape[1])
    grad = np.zeros(parameters)

    error = sigmoid(X * theta.T) - y
   
    for i in range(parameters):
         term = np.multiply(error, X[:,i])
         grad[i] = np.sum(term) / len(X)
    
    return grad

#或者使用如下方式
def gradient2(theta, X, y):
    theta = np.matrix(theta)
    X = np.matrix(X)
    y = np.matrix(y)
    
    parameters = int(theta.ravel().shape[1])
    grad = np.zeros(parameters)
    
    error = sigmoid(X * theta.T) - y
    term =np.multiply(error,X)
    #得到grad二维数组/矩阵
    grad=np.sum(term,axis=0)/len(X)

    grad=np.ravel(grad)
    
    return grad
  • 为了求出最优的参数theta,除了和线性回归类似梯度下降算法同步更新theta,迭代找cost function最小之外,可以使用更高级的算法,如truncated newton
import scipy.optimize as opt
result = opt.fmin_tnc(func=cost, x0=theta, fprime=gradient, args=(X, y))
result

       得到最优cost结果

cost(result[0], X, y)
  • 定义predict函数
def predict(theta, X):
   probability = sigmoid(X* theta.T)
   return[1 if p>=0.5 else 0 for p in probability]
  • 计算准确率
theta_min = np.matrix(result[0])
predictions = predict(theta_min, X)
correct = [1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0 for (a, b) in zip(predictions, y)]
#map(int,correct)是指将correct数值转成Int格式,map是映射Int功能
accuracy = (sum(map(int, correct)) % len(correct))
print('accuracy = {0}%'.format(accuracy))
posted @ 2017-04-13 16:18  chaye_shui  阅读(68)  评论(0)    收藏  举报