机器学习(周志华)-python编程练习-习题3-3

 

习题3.3  编程实现对率回归,并给出西瓜数据集 3.0α 上的结果.

数据集3.0α

sndensitysuger_ratiogood_melon
10.6970.461
20.7740.3761
30.6340.2641
40.6080.3181
50.5560.2151
60.4030.2371
70.4810.1491
80.4370.2111
90.6660.0910
100.2430.2670
110.2450.0570
120.3430.0990
130.6390.1610
140.6570.1980
150.360.370
160.5930.0420
170.7190.1030

 

对应的python实现代码

#encoding:UTF-8
import csv
import numpy as np
from math import *
from numpy.linalg import *
from matplotlib import pyplot as plt

#牛顿迭代法实现对数几率回归

old_ml=0 #初始似然函数值
n=0 
beta=np.mat([[0],[0],[1]]) #初始参数[w1;w2;b]  

#读取数据集
wm_data_30a = csv.reader(open('../data_set/watermelon_data_set_30a.csv','r'))

xi_d = np.mat(np.zeros((17,3)))
yi_d = np.mat(np.zeros((17,1)))

#遍历数据集
sn=0
for stu in wm_data_30a:
	#[sn x1 x2 y]
	if(stu[0].isdigit()==True):
		xi_d[sn,:] = np.mat([float(stu[1]),float(stu[2]),1])
		yi_d[sn,0] = float(stu[3])
		sn = sn+1
#print('xi_d=\n',xi_d)
#print('yi_d=\n',yi_d)
#print(wm_data_30a)
iter=0
xi = np.mat(np.zeros((3,1)))
while(iter<10):
	sn=0
	#遍历数据
	print('iter=',iter)
	new_ml=0
	for idx in range(17):#0~16
		#print('idx=',idx)
				
		xi = xi_d[idx,:].T #3*1的矩阵
		yi=yi_d[idx,0]
		#print(yi)
		#计算当前ml函数
		# ln(1+beta*xi) - yi*beta*xi
		new_ml = new_ml + log(1+e**det(beta.T*xi)) - yi*det(beta.T*xi)
	print('new ml=',new_ml)
	if abs(new_ml-old_ml)<0.001:
		print(new_ml-old_ml)
		print(beta)
		break

	#牛顿迭代法更新参数
	old_ml = new_ml
	dml_beta_1 = np.mat([[0],[0],[0]])#3*1矩阵
	dml_beta_2 = np.mat(np.zeros((3,3))) #3*3矩阵
	#遍历数据
	for idx in range(17):#0~16			
		xi = xi_d[idx,:].T 
		yi=yi_d[idx,0]
		#print(det(beta.T*xi))
		p1_xi = 1-1/(1+e**(det(beta.T*xi)))
		#计算一阶导
		dml_beta_1 = dml_beta_1 - xi*(yi-p1_xi) #矩阵
		#计算二阶导
		dml_beta_2 = dml_beta_2 + xi*(xi.T)*p1_xi*(1-p1_xi)
		#print('Neton iter',dml_beta_2)
	beta = beta - (dml_beta_2.I)*dml_beta_1
	print('beta=',beta)
	iter=iter+1

#%画出散点图以及计算出的直线
#%逐点画  分别表示是否好瓜

for idx in range(17):
	if yi_d[idx,0]==1:
		plt.plot(xi_d[idx,0],xi_d[idx,1],'+r')
	else:
		plt.plot(xi_d[idx,0],xi_d[idx,1],'ob')
#画出回归线
ply=-(0.1*beta[0,0]+beta[2,0])/beta[1,0];
pry=-(0.9*beta[0,0]+beta[2,0])/beta[1,0];

px=[0.1,0.9]
py=[ply,pry]

plt.plot(px,py)


plt.xlabel('density')
plt.ylabel('suger ratio')
plt.title('logistic function regression')
plt.show()

运算结果

beta=

[[ 3.15832738]
 [12.52119012]
 [-4.42886222]]

机器学习习题3-3

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Programming Exercise 1: Linear Regression Machine Learning Introduction In this exercise, you will implement linear regression and get to see it work on data. Before starting on this programming exercise, we strongly recom- mend watching the video lectures and completing the review questions for the associated topics. To get started with the exercise, you will need to download the starter code and unzip its contents to the directory where you wish to complete the exercise. If needed, use the cd command in Octave/MATLAB to change to this directory before starting this exercise. You can also find instructions for installing Octave/MATLAB in the “En- vironment Setup Instructions” of the course website. Files included in this exercise ex1.m - Octave/MATLAB script that steps you through the exercise ex1 multi.m - Octave/MATLAB script for the later parts of the exercise ex1data1.txt - Dataset for linear regression with one variable ex1data2.txt - Dataset for linear regression with multiple variables submit.m - Submission script that sends your solutions to our servers [?] warmUpExercise.m - Simple example function in Octave/MATLAB [?] plotData.m - Function to display the dataset [?] computeCost.m - Function to compute the cost of linear regression [?] gradientDescent.m - Function to run gradient descent [†] computeCostMulti.m - Cost function for multiple variables [†] gradientDescentMulti.m - Gradient descent for multiple variables [†] featureNormalize.m - Function to normalize features [†] normalEqn.m - Function to compute the normal equations ? indicates files you will need to complete † indicates optional exercises

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值