机器学习训练营第一次作业
发布于: 2021 年 04 月 27 日
# distutils: language=c++import numpy as npcimport numpy as np
def hello(): print("hello world")
cpdef target_mean_v3(data, y_name, x_name): cdef nrow = data.shape[0] cdef np.ndarray[double] result = np.asfortranarray(np.zeros(nrow), dtype=np.float64) cdef np.ndarray[long] y = np.asfortranarray(data[y_name], dtype=np.long) cdef np.ndarray[long] x = np.asfortranarray(data[x_name], dtype=np.long)
target_mean_v3_impl(result, y, x, nrow) return result
cdef void target_mean_v3_impl(double[:] result, long[:] y, long[:] x, const long nrow): cdef dict tmpDict = dict()
for i in range(nrow): if x[i] not in tmpDict.keys(): tmpDict[x[i]] = [y[i], 1] else: tmp = tmpDict[x[i]] tmp[0] += y[i] tmp[1] += 1
for i in range(nrow): tmp = tmpDict[x[i]] result[i] = (tmp[0] - y[i]) / (tmp[1]-1)复制代码
10 万条数据耗时情况对比
v1 1202.1009006500244
v2 5.086725473403931
老师 v3 0.026609182357788086
我的 v3 0.022057771682739258
划线
评论
复制
发布于: 2021 年 04 月 27 日阅读数: 41
威
关注
还未添加个人签名 2017.10.29 加入
还未添加个人简介











评论