当前位置:网站首页>机器学习习题——对率回归
机器学习习题——对率回归
2022-07-23 21:41:00 【椅糖】
一、代码
#coding=UTF-8
from numpy import * #调用numpy包的所有资源,主要作用是数学
import numpy as np #调用numpy的同时,起名np
import matplotlib.pyplot as plt#调用matplotlib里的pyplot并且起名为plt,主要作用是绘图
#定义一个二维数组18行2列
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])#定义一个一维数组1行18列
##对率回归的实现 前期数据处理
ones1=np.array([[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]])#定义一个二维数组18行1列
ones1=ones1.T#数组转置
x1=np.concatenate((x,ones1),axis=1)#将x数组和ones数组拼接,按行拼接的数组x1
beta=np.array([1,1,1]) #设定beta的初始值是一个1行3列的数组
n=1000; #设定迭代次数为100次
def func_sigmod(beta,x0):#定义一个sigmod的激活函数
y = math.exp(np.dot(beta,x0))/(1+math.exp(np.dot(beta,x0)))#使用公式计算好瓜的概率
return y #将结果返回
#算法实现
for k in range(0,n):#从0到n循环
B1=0 #将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.figure(1)是新建一个名叫 Figure1的画图窗口
plt.title('watermelon_3a')#图的标题叫watermelon_3e
plt.xlabel('density')#设置X轴文字为density
plt.ylabel('ratio_sugar')#设置y轴文字为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()#显示所画的图
【注】
plt.figure()——调用figure创建一个绘图对象
plt.plot()——调用plot函数在当前的绘图对象中绘图
plt.xlable()/plt.ylable()——设置X/Y轴的文字
plt.title()——设置图表的标题
plt.xlim()/plt.ylim()——设置X/Y轴的范围,格式为[x/y的起点,x/y的终点]
plt.axis()——同时设置X轴和Y轴的范围,格式为[x的起点,x的终点,y的起点,y的终点]
plt.legend()——显示label中标记的图示
plt.show()——显示出plt.show()创建出的所有绘图对象
plt.scatter()
绘制散点图。plt.scatter(x,y,s,c,marker,alpha)
x,y:x轴与y轴的数据
s:点的面积
c:点的颜色——'r-':表示红色实线
marker:点的形状
alpha:透明度
二、结果

边栏推荐
- 05_ UE4 advanced_ Material UV scaling
- Basic knowledge of mobile phone testing
- 北大清华2022年在各地录取人数排名
- Tell me the top ten securities companies? Is it safe to open an account online?
- & 9 nodemon automatic restart tool
- Deep learning - NLP classic papers, courses, papers and other resources sorting and sharing
- 宇树A1机器狗手势控制
- Proof of green Tao theorem (1): preparation, notation and Gowers norm
- Chapter 2 回归
- scala编程(初级)
猜你喜欢

Chapter 2 Regression

Edge cloud | 1. overview

LeetCode_ 376_ Wobble sequence

Customer exit variable in query

MySQL数据库索引

Day109. Shangyitong: integrate Nacos, hospital list, drop-down list query, hospital online function, hospital details query

Unity—3D数学-Vector3

Compare kernelshap and treeshap based on speed, complexity and other factors

Day109.尚医通:集成Nacos、医院列表、下拉列表查询、医院上线功能、医院详情查询

The third slam Technology Forum - Professor wuyihong
随机推荐
Pay more attention to which securities company has the lowest commission? Is it safe to open an account online?
集群聊天服务器:数据库表的设计
告诉我十大证券公司?请问网上开户安全么?
Union and union all of Hana SQL
[Yugong series] June 2022.Net architecture class 084- micro service topic ABP vNext micro service communication
At 12 o'clock on July 23, 2022, the deviation from the top of the line of love life hour appeared, maintaining a downward trend and waiting for the rebound signal.
[complex overloaded operator]
Practice data Lake iceberg lesson 37 kakfa write the enfour, not enfour test of iceberg's icberg table
&9 nodemon自动重启工具
js 对象数组去重
Why cluster chat server introduces load balancer
Unity—3D数学-Vector3
[attack and defense world web] difficulty four-star 12 point advanced question: confusion1
LeetCode_ 376_ Wobble sequence
Tell me the top ten securities companies? Is it safe to open an account online?
SQLite数据库
High numbers | calculation of triple integral 1 | high numbers | handwritten notes
Day109. Shangyitong: integrate Nacos, hospital list, drop-down list query, hospital online function, hospital details query
Cluster chat server: creation of project directory
Cluster chat server: network module chatServer