当前位置: 代码迷 >> 综合 >> 优达(Udacity)_outlier_mini_project
  详细解决方案

优达(Udacity)_outlier_mini_project

热度:37   发布时间:2023-12-16 21:29:32.0

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

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

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

def outlierCleaner(predictions, ages, net_worths):"""Clean away the 10% of points that have the largestresidual errors (difference between the predictionand 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 hereerrors = (predictions - net_worths) ** 2triplets = 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函数:点击打开链接

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