机器学习——数学基础(1)

1.采用两种方法求取以下方程的解,并采用绘图的方式比较两种方法的结果。

import matplotlib.pyplot as plt
import numpy as np
import numpy.linalg as nl
#
#方法一
a = np.array([[3,-2,1],[2,-1,1],[1,1,1]])
b = np.array([-1,-1,2])
m_sol1 = nl.solve(a,b)
print(m_sol1)

#方法二
a = np.array([[3,-2,1],[2,-1,1],[1,1,1]])
b = np.array([-1,-1,2])
a_inv = nl.inv(a)
m_sol2 = np.dot(a_inv,b)
print(m_sol2)

#比较差异
plt.figure()
plt.subplot(131)
plt.plot(m_sol1[0],m_sol1[1],marker='o',color='r',linestyle='')
plt.plot(m_sol2[0],m_sol2[1],marker='d',color='b',linestyle='')
labels = ['X_Solve','Y_Dot']
plt.legend(labels,loc='upper right')
plt.grid(True)

plt.subplot(132)
plt.plot(m_sol1[1],m_sol1[2],marker='o',color='r',linestyle='')
plt.plot(m_sol2[1],m_sol2[2],marker='d',color='b',linestyle='')
labels = ['Y_solve','Z_Dot']
plt.legend(labels,loc='upper right')
plt.grid(True)

plt.subplot(133)
plt.plot(m_sol1[0],m_sol1[2],marker='o',color='r',linestyle='')
plt.plot(m_sol2[0],m_sol2[2],marker='d',color='b',linestyle='')
labels=['X_solve','Z_Dot']
plt.legend(labels,loc='upper right')
plt.grid(True)
plt.show()

2.利用面向对象编程方法设计画圆类,实例化该类后可绘制指定半径的圆。

import matplotlib.pyplot as plt
import numpy as np

#方法一(参数方程)
class DrawCircle:
    r = 0
    a,b = (0,0)
    def __init__(self,R,A,B):
        self.r = R
        self.a = A
        self.b = B
    def Circle(self):
        theta = np.arange(0,2*np.pi,0.01)
        x = self.a + self.r * np.cos(theta)
        y = self.b + self.r * np.sin(theta)
        plt.figure()
        plt.grid(alpha=0.6,linestyle='-')
        plt.plot(x,y,color='r',linestyle='-')
        plt.scatter(self.a,self.b,color='b',marker='o')
        plt.xlabel('X')
        plt.ylabel('Y')
        plt.axis('equal')
        plt.title('Circle')
        plt.show()
        
r = 3
a,b = (0,0)
cir = DrawCircle(r,a,b)
cir.Circle()

#方法二
class DrawCircle:
    r = 0
    a,b = (0,0)
    def __init__(self,R,A,B):
        self.r = R
        self.a = A
        self.b = B
    def Circle(self):
        x = np.linspace(self.a-self.r,self.a+self.r,200)
        y1 = self.b+np.sqrt(np.power(self.r,2)-np.power(x-self.a,2))
        y2 = self.b-np.sqrt(np.power(self.r,2)-np.power(x-self.a,2))
        plt.figure()
        plt.plot(x,y1,linestyle='-',color='r')
        plt.plot(x,y2,linestyle='-',color='r')
        plt.plot(self.a,self.b,marker='o',color='b')
        plt.xlabel('X')
        plt.ylabel('Y')
        plt.title('Circle')
        plt.axis('equal')
        plt.grid(True)
        plt.text(self.a,self.b,(self.a,self.b))
        plt.show()
        
A = DrawCircle(2,0,0)
A.Circle()

 3.画出Logistic函数曲线()及过点(0,0.5)的水平线(观察其与f(x)函数之间的关系)。

import matplotlib.pyplot as plt
import numpy as np
 
#方法一
x = np.linspace(-5,5,100)
y = 1.0 / (1.0 + np.exp(-x))
plt.figure()
plt.grid(alpha=0.6,linestyle='-')
plt.plot(x,y,color='b',linestyle='-')
plt.axhline(y=0.5,color='r',linestyle='--')
plt.plot(0,0.5,marker='o',color='r')
plt.text(0.3,0.53,(0,0.5))
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Logistic')
plt.show()

 


#方法二
x = np.linspace(-6,6,1000)
y = 1/(1+np.exp(-x))
plt.figure()
plt.xlim(-6,6)
plt.ylim(0,1)
plt.yticks(np.linspace(0,1,5,endpoint=True))
plt.plot(x,y,linestyle='-',color='r')
plt.plot(x,np.ones(len(x))*0.5,linestyle='--',color='b')
plt.plot(0,0.5,marker='o',color='g')
plt.text(0.3,0.53,(0,0.5))
plt.grid(True)
plt.show()

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值