SMOTE算法原理及Python代码实现

SMOTE算法原理及Python代码实现

预备知识

  1. 向量代数的知识:对于点 x 1 \mathbf{x}_1 x1 x 2 \mathbf{x}_2 x2,如果 λ ∈ [ 0 , 1 ] \lambda \in [0,1] λ[0,1] λ x 1 + ( 1 − λ ) x 2 \lambda \mathbf{x}_1 + (1-\lambda) \mathbf{x}_2 λx1+(1λ)x2肯定在点 x 1 \mathbf{x}_1 x1 x 2 \mathbf{x}_2 x2的连线上。

在这里插入图片描述
2. 面向对象的设计思想就是抽象出一个类(Class),用的时候对类具体化成实例(Instance)。

  • 类和实例

    首先,尝试创建一个Student类,代码如下:

    class Student(object):
        def __init__(self, name, score):
            self.name = name
            self.score = score
    

    说明:

    class:定义类的关键字,创建类必须先写上;

    Student:类名,根据需要填写,类名首字母通常大写;

    (object):表示该类是从哪个类继承来的,如果没有合适的继承类,就使用object类,这是所有类最终都会继承的类。

    __init__:类的一个特殊方法,创建实例时,会强制填写我们定义的属性,这里定义的属性是namescore

    注意__init__前后分别有两个下划线。

    另外,__init__的第一个参数永远是self,表示创建的实例本身。因此,在__init__方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。

    定义好了Student类,就可以根据Student类创建出Student的实例,创建实例通过类名+()实现:

    bart = Student('Bart Simpson', 59)
    print(bart.name)
    print(bart.score)
    

    结果如下:

    Bart Simpson
    59
    
  • 访问限制

    Class内部有属性(数据)和方法(函数),外部代码可以直接调用实例变量的方法来操作数据,这样就隐藏了内部的复杂逻辑。

    class Student(object):
    
        def __init__(self, name, score):
            self.name = name
            self.score = score
            
        def lost_points(self):
            return 100 - self.score
    
        def print_score(self):
            print(' name : %s \n score : %s \n lost points : %s ' % 
                  (self.name, self.score, self.lost_points()))
    

    上面的代码创建了lost_points方法用于计算失分、print_score方法来输出结果,用以下代码测试效果:

    bart = Student('Bart Simpson', 59)
    bart.score = 99
    print(bart.lost_points())
    bart.print_score()
    

    结果如下:

    1
     name : Bart Simpson 
     score : 99 
     lost points : 1 
    

    但是,外部代码可以自由地修改实例的属性namescore,为了不让外部访问内部属性,可以在属性名称前加上两个下划线__,使其变成私有变量(private),只有内部可以访问,外部无法访问。

    class Student(object):
    
        def __init__(self, name, score):
            self.__name = name
            self.__score = score
        
        def lost_points(self):
            return 100 - self.__score
    
        def print_score(self):
            print('name : %s \nscore : %s \nlost points : %s ' % 
                  (self.__name, self.__score, self.lost_points()))
    
    bart = Student('Bart Simpson', 59)
    print(bart.__score)
    

    结果报错:

    AttributeError: 'Student' object has no attribute '__score'
    

    两个下划线__设置私有对方法(函数)同样有效,将lost_points设置为私有:

    class Student(object):
    
        def __init__(self, name, score):
            self.__name = name
            self.__score = score
        
        def __lost_points(self):
            return 100 - self.__score
    
        def print_score(self):
            print('name : %s \nscore : %s \nlost points : %s ' % 
                  (self.__name, self.__score, self.__lost_points()))
    

    测试代码:

    bart = Student('Ba
  • 99
    点赞
  • 430
    收藏
    觉得还不错? 一键收藏
  • 34
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值