根据西瓜的密度,含糖率来判断该瓜是不是好瓜
代码如下:
# coding=UTF-8
from numpy import *
import numpy as np
import matplotlib.pyplot as plt
x=np.array([ [0.697,0.460],[0.774,0.376],[0.634,0.264],[0.608,0.318],[
0.556,0.215],[0.403,0.237],[0.481,0.149],[0.437,0.211],[0.666,0.091],
[0.243,0.267],[0.245,0.057],[0.343,0.099],[0.639,0.161],[0.657,0.198]
,[0.360,0.370],[0.591,0.042],[0.719,0.103]])
y=np.array ([1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0])
#对率回归的实现前期数据处理↓
ones1=np.array ([[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]])
ones1=ones1.T
x1=np.concatenate((x,ones1),axis=1)
beta=np.array([ 1,1,1])
n=1000;
def func_sigmod (beta,x0):
y=math.exp(np.dot (beta,x0))/( 1+math.exp (np.dot(beta, x0)))
return y
#算法实现↓
for k in range (0,n):
B1=0
for j in range (len(x)):
B1=B1-x1[j,:]*(y[j]-func_sigmod(beta,x1[j,:]))
B2=0
for j in range (len(x)) :
B2=B2+(x1[j,:] **2).sum()*func_sigmod(beta,x1[j,:])*(1-
func_sigmod (beta,x1[j,:]))
beta=beta-1/B2*B1
print (beta)
f1 = plt.figure(1)
plt.title('watermelon_3a')
plt.xlabel('density')
plt.ylabel('ratio sugar')
plt.scatter(x[y == 1,0],x[y == 1,1], marker ='+', color = 'r',
s = 100,label= 'good')
plt.scatter(x[y == 0,0],x[y == 0,1], marker ='*',color = 'b',
s = 100,label = 'bad')
plt.legend(loc= 'upper right')
plt.show()
如若代码出现numpy不存在的错误,则需要在file settings中的Project:pythonProject1添加matplot和matplotlib两个包,此时便可正常运行
运行结果:
由图可看出,+为好瓜。