优达(Udacity)_outlier_mini_project

专门开了博客记录Udacity学习记录,进入正题。

因为对Python不熟悉,项目过程中遇到了很多问题,边学边完成,对自己找到的代码进行注释以及理解。

代码来源:点击打开链接(优达学城论坛,可能需要学院身份登陆)

def outlierCleaner(predictions, ages, net_worths):
    """
        Clean away the 10% of points that have the largest
        residual errors (difference between the prediction
        and the actual net worth).

        Return a list of tuples named cleaned_data where 
        each tuple is of the form (age, net_worth, error).
    """
    
    cleaned_data = []

    ### your code goes here
    errors = (predictions - net_worths) ** 2

    triplets = sorted(zip(ages, net_worths, errors),
                      key=lambda triplet: triplet[2])

    num_retain = int(len(predictions) * .9)
    cleaned_data = triplets[:num_retain]

    return cleaned_data
先定义一个新变量errors,课程中讲过为平方差

定义新的列表triplets,

sorted函数:点击打开链接

zip函数:点击打开链接

之后定义想要的元素个数以及对列表完成切片,返回列表函数结束


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值