📣 黑五网一特惠|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 56528| 回复: 121
跳转到指定楼层
上一主题 下一主题
收起左侧

Data Scientist Python Coding 题整理(持续更新

   
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 YZDH 于 2021-2-2 13:26 编辑

这几年看了不少DS相关复习资料(地里面筋和其他网上资料),也参加了一些面试,积累了一点General Data Scientist岗位的Python Coding题目。目前看来General DS岗位的Python Coding题目分为以下五类

1. Leetcode easy难度的题目,主要考察Array,String,Hashtable,Searching & Sorting,和简单DP。Tree,Linked List和Graph我从来没见过。

2. Simulation,比如抽样

3. Scripting,比如读取一个文件,存成dictionary或者list of list,再进行相关操作
.--
4. 一些机器学习算法实现
..
5. 统计计算类编程

注意如果是面MLE的话,对coding的要求就是SDE的要求了,General Data Scientist的Coding要求不适用

每一题会给出题目简单描述和相关代码(我不保证自己的代码一定对),此贴会一直慢慢更新,也欢迎其他同学提出意见和贡献题目。 ..
. check 1point3acres for more.


评分

参与人数 85大米 +123 收起 理由
rocxiao + 1 给你点个赞!
Zhaoshenqi + 1 赞一个
kellyiwork + 1 赞一个
crj02 + 1 很有用的信息!
adamdad + 2 很有用的信息!

查看全部评分


上一篇:[找小伙伴]DS/DA new grad互相mock interview
下一篇:没有测试集的label怎么估计MSE

本帖被以下淘专辑推荐:

推荐
 楼主| ZYYYZ 2021-2-2 13:12:06 | 只看该作者
全局:
1. Weighted Sampling

You are given n numbers as well as probabilities p_0, p_1, ... , p_{n - 1}, which sum up to 1, how would you generate one of the n numbers according to the specified probabilities?

For example, if the numbers are 3, 5, 7, 11, and the probabilities are 9/18, 6/18, 2/18, 1/18, then 100000 calls to your program, 3 should appear roughly 500000 times, . Waral dи,
5 should appear roughly 333333 times, 7 should appear roughly 111111 times, and 11 should appear roughly 55555 times.


  1. import itertools, bisect, random.google  и

  2. def weighted_sample(values, probs):. .и
  3.     cumulative_probs = [0.0] + list(itertools.accumulate(probs))
  4.     interval_idx = bisect.bisect(cumulative_probs, random.random()) - 1
  5.     return values[interval_idx]
复制代码
. From 1point 3acres bbs


  1. values = [3, 5, 7, 11]
  2. probs = [9/18, 6/18, 2/18, 1/18]

  3. result = {}

  4. for _ in range(1000000):. 1point 3acres
  5.     value = weighted_sample(values, probs)
  6.     result[value] = result.get(value, 0) + 1
  7.    
  8. print(result)

  9. # {3: 500175, 5: 333546, 7: 110936, 11: 55343}
复制代码

-baidu 1point3acres


补充内容 (2021-2-3 04:30):. 1point3acres.com
"then 100000 calls" 更正到 "then 1000000 calls", 感谢Jack2020u同学提醒

评分

参与人数 4大米 +5 收起 理由
tianqi1212 + 2 被这道题考过😂
Florence0909 + 1 赞一个
alicecsy02 + 1 欢迎分享你知道的情况,会给更多积分奖励!
ledantalianddd + 1 赞一个

查看全部评分

回复

使用道具 举报

推荐
 楼主| ZYYYZ 2021-2-7 06:56:34 | 只看该作者
全局:
18. Implement the K-means algorithm

一些比较简单但是重要的机器学习算法需要会描述算法并且写出代码,K-means算法是常考题 ..

K-means算法简介:. 1point 3 acres
https://jakevdp.github.io/Python ... /05.11-k-means.html

Elbow method:
https://en.wikipedia.org/wiki/Elbow_method_(clustering) ..
. 1point3acres
Silhouette analysis:
https://scikit-learn.org/stable/ ... uette_analysis.html
.
K-means ++:
https://en.wikipedia.org/wiki/K-means%2B%2B

Feature scaling:
https://datascience.stackexchang ... -stage#:~:text=Yes.,KGs%20before%20calculating%20the%20distance.

  1. import random
  2. import numpy as np
  3. from collections import defaultdict

  4. def k_means(X, k=5, max_iter=1000):.1point3acres
  5.     """
  6.     Args:. check 1point3acres for more.
  7.     - X - data matrix
  8.     - k - number of clusters
  9.     - max_iter - maximum iteratations

  10.     Returns:
  11.     - clusters - dict mapping cluster centers (tuples) to
  12.                  observations (list of datapoints, each datapoint is a numpy array)
  13.     """.1point3acres
  14.     # initialize cluster centers by random sample k rows of X
  15.     # random.seed(42)
  16.     centers = [tuple(pt) for pt in random.sample(list(X), k)]
  17.    
  18.     # begin iterations
  19.     for i in range(max_iter):
  20.         clusters = defaultdict(list).
  21.         
  22.         # E-Step: assign points to the nearest cluster center
  23.         for datapoint in X:.--
  24.             dists = [np.linalg.norm(datapoint - center) for center in centers].1point3acres
  25.             center = centers[np.argmin(dists)]
  26.             clusters[center].append(datapoint). Waral dи,
  27.         
  28.         # M-Step: average the cluster datapoints to calculate the new centers
  29.         new_centers = []. 1point3acres
  30.         for center, datapoints in clusters.items():. 1point3acres
  31.             new_center = np.mean(datapoints, axis=0)
  32.             new_centers.append(tuple(new_center))
  33.         
  34.         # stop iteration if the centers stop changing
  35.         if set(new_centers) == set(centers):
  36.             break

  37.         # update centers
  38.         centers = new_centers

  39.     return clusters


  40. # use the iris dataset as example
  41. from sklearn import datasets
  42. iris = datasets.load_iris()
  43. X = iris.data

  44. X_clustered = k_means(X, k=5, max_iter=1000)
    . check 1point3acres for more.
  45. X_clustered


  46. # Output:. .и
  47. # defaultdict(list,
  48. #             {(5.242857142857143,. check 1point3acres for more.
  49. #               3.6678571428571423,
  50. #               1.5,
  51. #               0.28214285714285714): [array([5.1, 3.5, 1.4, 0.2]),
  52. #               array([5. , 3.6, 1.4, 0.2]),
  53. #               array([5.4, 3.9, 1.7, 0.4]),
  54. #               array([5. , 3.4, 1.5, 0.2]),
  55. #               array([5.4, 3.7, 1.5, 0.2]),
  56. #               array([5.8, 4. , 1.2, 0.2]),
  57. #               array([5.7, 4.4, 1.5, 0.4]),
  58. #               array([5.4, 3.9, 1.3, 0.4]),
  59. #               array([5.1, 3.5, 1.4, 0.3]),
  60. #               array([5.7, 3.8, 1.7, 0.3]),
  61. #               array([5.1, 3.8, 1.5, 0.3]),. 1point 3 acres
  62. #               array([5.4, 3.4, 1.7, 0.2]),
  63. #               array([5.1, 3.7, 1.5, 0.4]),
  64. #               array([5.1, 3.3, 1.7, 0.5]),
  65. #               array([5. , 3.4, 1.6, 0.4]),
  66. #               array([5.2, 3.5, 1.5, 0.2]),
  67. #               array([5.2, 3.4, 1.4, 0.2]),. ----
  68. #               array([5.4, 3.4, 1.5, 0.4]),
  69. #               array([5.2, 4.1, 1.5, 0.1]),
  70. #               array([5.5, 4.2, 1.4, 0.2]),
  71. #               array([5.5, 3.5, 1.3, 0.2]),. 1point 3 acres
  72. #               array([4.9, 3.6, 1.4, 0.1]),
  73. #               array([5.1, 3.4, 1.5, 0.2]),
  74. #               array([5. , 3.5, 1.3, 0.3]),
  75. #               array([5. , 3.5, 1.6, 0.6]),.google  и
  76. #               array([5.1, 3.8, 1.9, 0.4]),. 1point3acres.com
  77. #               array([5.1, 3.8, 1.6, 0.2]),
    .--
  78. #               array([5.3, 3.7, 1.5, 0.2])],
  79. #              (4.704545454545454,
  80. #               3.122727272727273,
  81. #               1.4136363636363638,.
  82. #               0.2000000000000001): [array([4.9, 3. , 1.4, 0.2]),-baidu 1point3acres
  83. #               array([4.7, 3.2, 1.3, 0.2]),
  84. #               array([4.6, 3.1, 1.5, 0.2]),
  85. #               array([4.6, 3.4, 1.4, 0.3]),
  86. #               array([4.4, 2.9, 1.4, 0.2]),
  87. #               array([4.9, 3.1, 1.5, 0.1]),
  88. #               array([4.8, 3.4, 1.6, 0.2]),
  89. #               array([4.8, 3. , 1.4, 0.1]),. Waral dи,
  90. #               array([4.3, 3. , 1.1, 0.1]),. 1point3acres
  91. #               array([4.6, 3.6, 1. , 0.2]),
  92. #               array([4.8, 3.4, 1.9, 0.2]),
  93. #               array([5. , 3. , 1.6, 0.2]),
  94. #               array([4.7, 3.2, 1.6, 0.2]),
  95. #               array([4.8, 3.1, 1.6, 0.2]),. Χ
  96. #               array([4.9, 3.1, 1.5, 0.2]),
  97. #               array([5. , 3.2, 1.2, 0.2]),
  98. #               array([4.4, 3. , 1.3, 0.2]),
  99. #               array([4.5, 2.3, 1.3, 0.3]),
  100. #               array([4.4, 3.2, 1.3, 0.2]),
  101. #               array([4.8, 3. , 1.4, 0.3]),.google  и
  102. #               array([4.6, 3.2, 1.4, 0.2]),
  103. #               array([5. , 3.3, 1.4, 0.2])],
  104. #              (6.23658536585366,
  105. #               2.8585365853658535,
  106. #               4.807317073170731,
  107. #               1.6219512195121943): [array([7. , 3.2, 4.7, 1.4]),
  108. #               array([6.4, 3.2, 4.5, 1.5]),
  109. #               array([6.9, 3.1, 4.9, 1.5]),
  110. #               array([6.5, 2.8, 4.6, 1.5]),
  111. #               array([6.3, 3.3, 4.7, 1.6]),
  112. #               array([6.6, 2.9, 4.6, 1.3]),
  113. #               array([6.1, 2.9, 4.7, 1.4]),.--
  114. #               array([6.7, 3.1, 4.4, 1.4]),
  115. #               array([5.6, 3. , 4.5, 1.5]),
  116. #               array([6.2, 2.2, 4.5, 1.5]),. .и
  117. #               array([5.9, 3.2, 4.8, 1.8]),
  118. #               array([6.3, 2.5, 4.9, 1.5]),. From 1point 3acres bbs
  119. #               array([6.1, 2.8, 4.7, 1.2]),. Χ
  120. #               array([6.4, 2.9, 4.3, 1.3]),
  121. #               array([6.6, 3. , 4.4, 1.4]),
  122. #               array([6.8, 2.8, 4.8, 1.4]),-baidu 1point3acres
  123. #               array([6.7, 3. , 5. , 1.7]),. 1point3acres.com
  124. #               array([6. , 2.9, 4.5, 1.5]),
  125. #               array([6. , 2.7, 5.1, 1.6]),. From 1point 3acres bbs
  126. #               array([6. , 3.4, 4.5, 1.6]),
  127. #               array([6.7, 3.1, 4.7, 1.5]),
  128. #               array([6.3, 2.3, 4.4, 1.3]),. check 1point3acres for more.
  129. #               array([6.1, 3. , 4.6, 1.4]),
  130. #               array([6.2, 2.9, 4.3, 1.3]),. 1point 3 acres
  131. #               array([5.8, 2.7, 5.1, 1.9]),
  132. #               array([6.5, 3.2, 5.1, 2. ]),
  133. #               array([6.4, 2.7, 5.3, 1.9]),
    .--
  134. #               array([5.7, 2.5, 5. , 2. ]),
  135. #               array([5.8, 2.8, 5.1, 2.4]),
  136. #               array([6. , 2.2, 5. , 1.5]),
  137. #               array([5.6, 2.8, 4.9, 2. ]),
  138. #               array([6.3, 2.7, 4.9, 1.8]),
  139. #               array([6.2, 2.8, 4.8, 1.8]),.google  и
  140. #               array([6.1, 3. , 4.9, 1.8]),
  141. #               array([6.3, 2.8, 5.1, 1.5]),
  142. #               array([6.1, 2.6, 5.6, 1.4]),
  143. #               array([6. , 3. , 4.8, 1.8]),
  144. #               array([5.8, 2.7, 5.1, 1.9]),
  145. #               array([6.3, 2.5, 5. , 1.9]),
  146. #               array([6.5, 3. , 5.2, 2. ]),
  147. #               array([5.9, 3. , 5.1, 1.8])],. Waral dи,
  148. #              (5.529629629629629,
  149. #               2.6222222222222222,
  150. #               3.940740740740741,
  151. #               1.2185185185185188): [array([5.5, 2.3, 4. , 1.3]),
  152. #               array([5.7, 2.8, 4.5, 1.3]),
  153. #               array([4.9, 2.4, 3.3, 1. ]),
  154. #               array([5.2, 2.7, 3.9, 1.4]),
  155. #               array([5. , 2. , 3.5, 1. ]),
  156. #               array([5.9, 3. , 4.2, 1.5]),
  157. #               array([6. , 2.2, 4. , 1. ]),
  158. #               array([5.6, 2.9, 3.6, 1.3]),
  159. #               array([5.8, 2.7, 4.1, 1. ]),
  160. #               array([5.6, 2.5, 3.9, 1.1]),. 1point3acres.com
  161. #               array([6.1, 2.8, 4. , 1.3]),
  162. #               array([5.7, 2.6, 3.5, 1. ]),
  163. #               array([5.5, 2.4, 3.8, 1.1]),
  164. #               array([5.5, 2.4, 3.7, 1. ]),. 1point3acres
  165. #               array([5.8, 2.7, 3.9, 1.2]),. 1point3acres.com
  166. #               array([5.4, 3. , 4.5, 1.5]),
  167. #               array([5.6, 3. , 4.1, 1.3]),
  168. #               array([5.5, 2.5, 4. , 1.3]),
  169. #               array([5.5, 2.6, 4.4, 1.2]),. 1point3acres.com
  170. #               array([5.8, 2.6, 4. , 1.2]),
  171. #               array([5. , 2.3, 3.3, 1. ]),
    .google  и
  172. #               array([5.6, 2.7, 4.2, 1.3]),
  173. #               array([5.7, 3. , 4.2, 1.2]),
  174. #               array([5.7, 2.9, 4.2, 1.3]),. .и
  175. #               array([5.1, 2.5, 3. , 1.1]),
  176. #               array([5.7, 2.8, 4.1, 1.3]),
  177. #               array([4.9, 2.5, 4.5, 1.7])],
  178. #              (6.9125000000000005,
  179. #               3.099999999999999,
  180. #               5.846874999999999,
  181. #               2.1312499999999996): [array([6.3, 3.3, 6. , 2.5]),
  182. #               array([7.1, 3. , 5.9, 2.1]),
  183. #               array([6.3, 2.9, 5.6, 1.8]),. .и
  184. #               array([6.5, 3. , 5.8, 2.2]),
  185. #               array([7.6, 3. , 6.6, 2.1]),-baidu 1point3acres
  186. #               array([7.3, 2.9, 6.3, 1.8]),. 1point3acres
  187. #               array([6.7, 2.5, 5.8, 1.8]),
  188. #               array([7.2, 3.6, 6.1, 2.5]),
  189. #               array([6.8, 3. , 5.5, 2.1]),.google  и
  190. #               array([6.4, 3.2, 5.3, 2.3]),
  191. #               array([6.5, 3. , 5.5, 1.8]),
  192. #               array([7.7, 3.8, 6.7, 2.2]),
  193. #               array([7.7, 2.6, 6.9, 2.3]),. From 1point 3acres bbs
  194. #               array([6.9, 3.2, 5.7, 2.3]),
  195. #               array([7.7, 2.8, 6.7, 2. ]),
  196. #               array([6.7, 3.3, 5.7, 2.1]),
  197. #               array([7.2, 3.2, 6. , 1.8]),
  198. #               array([6.4, 2.8, 5.6, 2.1]),
  199. #               array([7.2, 3. , 5.8, 1.6]),. .и
  200. #               array([7.4, 2.8, 6.1, 1.9]),. Waral dи,
  201. #               array([7.9, 3.8, 6.4, 2. ]),
  202. #               array([6.4, 2.8, 5.6, 2.2]),-baidu 1point3acres
  203. #               array([7.7, 3. , 6.1, 2.3]),
  204. #               array([6.3, 3.4, 5.6, 2.4]),
  205. #               array([6.4, 3.1, 5.5, 1.8]),
  206. #               array([6.9, 3.1, 5.4, 2.1]),
  207. #               array([6.7, 3.1, 5.6, 2.4]),
  208. #               array([6.9, 3.1, 5.1, 2.3]),.google  и
  209. #               array([6.8, 3.2, 5.9, 2.3]),
  210. #               array([6.7, 3.3, 5.7, 2.5]),
  211. #               array([6.7, 3. , 5.2, 2.3]),
  212. #               array([6.2, 3.4, 5.4, 2.3])]})
复制代码
.

回复

使用道具 举报

推荐
 楼主| ZYYYZ 2021-2-25 18:49:18 | 只看该作者
全局:
本帖最后由 YZDH 于 2021-2-25 18:55 编辑
. Waral dи,
31. Merge k sorted arraysGiven k sorted arrays, create a combined list while maintaining sorted order.(这道题和leetcode 23其实是一道题,不过leetcod里面是list of linkedLists).--

Example:
Input: arrs = [[1, 4, 5], [1, 3, 4], [2, 6]]
Output: [1, 1, 2, 3, 4, 4, 5, 6]. check 1point3acres for more.

  1. # Use Python heapq module
  2. # T: O(nlogk)
  3. # S: O(n)

  4. import heapq
  5. . Χ
  6. class Solution:. Χ
  7.     def merge_k_arrays(self, arrs):
  8.         if not arrs or len(arrs) == 0:
  9.             return []

  10.         result = []
  11.         min_heap = []
  12. .1point3acres
  13.         for arr in arrs:
  14.             i = 0
  15.             while i < len(arr):
  16.                 heapq.heappush(min_heap, arr[i])
  17.                 i += 1

  18.         while min_heap:
  19.             result.append(heapq.heappop(min_heap)). 1point 3 acres

  20.         return result
  21.     . check 1point3acres for more.

  22. if __name__ == '__main__':
  23.     Solution().merge_k_arrays([[1, 4, 5], [1, 3, 4], [2, 6]]) == \
  24.                                [1, 1, 2, 3, 4, 4, 5, 6]
  25.     Solution().merge_k_arrays([[1, 3, 5, 7], [2, 4, 6, 8], [0, 9, 10, 11]]) == \
  26.                                [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
  27.     Solution().merge_k_arrays([]) == []
  28.     Solution().merge_k_arrays([[]]) == []
复制代码


[/i]-baidu 1point3acres

评分

参与人数 1大米 +2 收起 理由
wujiayikelly + 2 又来更新啦,点赞!

查看全部评分

回复

使用道具 举报

🔗
123小液泡 2021-2-2 13:12:32 | 只看该作者
全局:
感谢分享,
请问第一句话是这么断句么:
Leetcode easy难度的题目,主要考察Array,String,Hashtable,Searching和简单DP. Tree,Linked List和Graph我从来没见过。
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-2 13:27:06 | 只看该作者
全局:
123小液泡 发表于 2021-2-2 13:12. 1point3acres.com
感谢分享,
请问第一句话是这么断句么:
Leetcode easy难度的题目,主要考察Array,String,Hashtable,S ...

对的,sorry写得有点confusing,已经更正
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-2 13:42:50 | 只看该作者
全局:
2. String Compression ..

Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccddd would become a2b1c5d3. If the "compressed" string would not become smaller than the original string, your method should return the original string. -baidu 1point3acres

You can assume the string has only lowercase letters. (a-z)

  1. def string_compress(s):
  2.     result = ""
  3.     cnt = 1
  4.    
    . ----
  5.     for i in range(len(s) - 1):
  6.         if s[i] == s[i + 1]:
  7.             cnt += 1
  8.         else:
  9.             result += s[i] + str(cnt). check 1point3acres for more.
  10.             cnt = 1
    . Χ
  11.             . 1point3acres.com
  12.     result += s[i] + str(cnt). 1point3acres
  13.     . From 1point 3acres bbs
  14.     if len(result) >= len(s):
  15.         return s
  16.     return result
  17. .
  18. if __name__ == "__main__":
  19.     assert string_compress("aabcccccddd") == "a2b1c5d3"
  20.     assert string_compress("abcde") == "abcde"
复制代码

. Χ
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-2 14:12:35 | 只看该作者
全局:
3. Rotate matrix. 1point 3acres

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise and anticlockwise).

You have to rotate the image in-place.

  1. def rotate_clock(matrix):
  2.     n = len(matrix). check 1point3acres for more.
  3.     for i in range(n):
  4.         for j in range(i):
  5.             matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
  6.             
  7.     for i in range(n):
  8.         for j in range(n//2):
  9.             matrix[i][j], matrix[i][n - 1 - j] = matrix[i][n - 1 - j], matrix[i][j]
  10.     return matrix

  11. if __name__ == "__main__":
  12.     assert rotate_clock([[1, 2, 3],
  13.                          [4, 5, 6],
  14.                          [7, 8, 9]]) == [[7, 4, 1],
  15.                                          [8, 5, 2],
  16.                                          [9, 6, 3]]
复制代码



  1. def rotate_anticlock(matrix):
  2.     n = len(matrix)
  3.     for i in range(n):
  4.         for j in range(i):. From 1point 3acres bbs
  5.             matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
  6.             
  7.     for i in range(n//2):
  8.         for j in range(n):
  9.             matrix[i][j], matrix[n - 1 - i][j] = \
    .1point3acres
  10.             matrix[n - 1 - i][j], matrix[i][j]
  11.     return matrix. 1point3acres.com
  12. .--
  13. if __name__ == "__main__":
  14.     assert rotate_anticlock([[1, 2, 3],.1point3acres
  15.                              [4, 5, 6],
  16.                              [7, 8, 9]]) == [[3, 6, 9],
  17.                                              [2, 5, 8],
  18.                                              [1, 4, 7]]
复制代码


评分

参与人数 1大米 +1 收起 理由
Florence0909 + 1 赞一个

查看全部评分

回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-2 14:51:21 | 只看该作者
全局:
本帖最后由 YZDH 于 2021-2-2 15:22 编辑

4. Largest sum subarry

Given an array of integers, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
. 1point3acres
[i][i][i][i]
  1. def max_subarray(nums):
  2.     # T: O(n), S: O(n)
  3.     if len(nums) == 0:
  4.         return 0. 1point 3acres
  5.     . From 1point 3acres bbs
  6.     dp = [0]*len(nums)
  7.     dp[0] = nums[0]
  8.     result = dp[0]
  9.    
  10.     for i in range(1, len(nums)):. check 1point3acres for more.
  11.         if dp[i - 1] < 0:
  12.             dp[i] = nums[i]
  13.         else:
  14.             dp[i] = nums[i] + dp[i - 1]. Χ
  15.         result = max(result, dp[i])
  16.         . check 1point3acres for more.
  17.     return result
  18. . check 1point3acres for more.
  19. if __name__ == "__main__":
  20.     assert max_subarray([-2,1,-3,4,-1,2,1,-5,4]) == 6
复制代码

[/i][/i][/i][/i]
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-2 15:19:55 | 只看该作者
全局:
5. Reservoir sampling (有名的蓄水池抽样)

Reservoir sampling is a family of randomized algorithms for randomly choosing a sample of k items from a list S containing n items, where n is either a very large or unknown number. Typically, n is too large to fit the whole list into main memory.. 1point3acres

算法:. From 1point 3acres bbs


  1. '''  S has items to sample, R will contain the result. 1point 3 acres

  2. ReservoirSample(S[1..n], R[1..k])
  3.   // fill the reservoir array
  4.   for i = 1 to k
  5.       R[i] := S[i].

  6.   // replace elements with gradually decreasing probability
  7.   for i = k+1 to n
  8.     j := random(1, i) // important: inclusive range
  9.     if j <= k
  10.         R[j] := S[i]. 1point3acres
  11. '''
复制代码



Code:

  1. import random

  2. def reservoir_sample(iterable, k):
  3.     """.1point3acres
  4.     Returns @param n random items from @param iterable.. 1point 3 acres
  5.     """
  6.     reservoir = []
  7.     for i, item in enumerate(iterable):
  8.         if i < k:
  9.             reservoir.append(item). check 1point3acres for more.
  10.         else:
  11.             j = random.randint(0, i).--
  12.             if j < k:.google  и
  13.                 reservoir[j] = item
  14.     return reservoir. Waral dи,


  15. reservoir_sample(range(1000000), 10)
  16. # [973801, 423005, 360287, 932539, 372598, 139061, 147183, 44640, 686224, 165398]
复制代码


[/i][/i][/i]

评分

参与人数 3大米 +3 收起 理由
tong214 + 1 赞一个
wujiayikelly + 1 欢迎分享你知道的情况,会给更多积分奖励!
Florence0909 + 1 赞一个

查看全部评分

回复

使用道具 举报

全局:
谢谢分享🦀~~
回复

使用道具 举报

🔗
Jack2020u 2021-2-2 21:27:35 | 只看该作者
全局:
YZDH 发表于 2021-2-2 00:12. ----
1. Weighted Sampling
..
You are given n numbers as well as probabilities p_0, p_1, ... , p_{n - 1},  ...

多谢分享,前面的表述里面100000 call是不是应该是1 million call(少了一个0)?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表