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

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

   
🔗
 楼主| ZYYYZ 2021-2-7 03:54:41 | 只看该作者
全局:
16. Intersection of Two Arrays (Leetcode 349).google  и

Given two arrays, write a function to compute their intersection.. check 1point3acres for more.

Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
-baidu 1point3acres
解法1: T: O(nlogn), S: O(n)

  1. class Solution(object):
  2.     def intersection_1(self, nums1, nums2):
  3.         if not nums1 or len(nums1) == 0 or \
  4.            not nums2 or len(nums2) == 0:
  5.             return []
  6.         
  7.         my_set = set()
  8.         nums1.sort()
  9.         nums2.sort()
  10.         i, j = 0, 0. .и
  11.         while i < len(nums1) and j < len(nums2):.1point3acres
  12.             if nums1[i] < nums2[j]:
  13.                 i += 1
  14.             elif nums1[i] > nums2[j]:
  15.                 j += 1
  16.             else:
  17.                 my_set.add(nums1[i])
  18.                 i += 1
  19.                 j += 1. 1point 3 acres
  20.                
  21.         k = 0
  22.         result = [0]*len(my_set)
  23.         for num in my_set:
  24.             result[k] = num
  25.             k += 1
  26.             
  27.         return result ..
  28.     . 1point 3 acres
  29. if __name__ == '__main__':
  30.     assert Solution().intersection_1([1,2,2,1], [2,2]) == [2]
复制代码
. 1point3acres.com
.

. ----
解法2: T: O(logn), S: O(n)

  1. class Solution(object):
  2.     def intersection_2(self, nums1, nums2):
  3.         if not nums1 or len(nums1) == 0 or \
  4.            not nums2 or len(nums2) == 0:
  5.             return [].--
  6.         
  7.         my_set = set()
  8.         ret = set(). Χ
  9.         for num in nums1:
  10.             my_set.add(num)
  11.             
  12.         for num in nums2:
  13.             if num in my_set:
  14.                 ret.add(num). 1point 3acres
  15.         
  16.         k = 0
  17.         result = [0]*len(ret)
  18.         for num in ret:. From 1point 3acres bbs
  19.             result[k] = num. .и
  20.             k += 1
  21.              ..
  22.         return result

  23. . ----
  24. if __name__ == '__main__':
  25.     assert Solution().intersection_2([4,9,5], [9,4,9,8,4]) == [9, 4]
复制代码




补充内容 (2021-2-11 06:39):
解法2的Time complexity应该是T: O(n),有typo
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-7 04:34:42 | 只看该作者
全局:
17. Binary Search (Leetcode 704)

Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1

有名的Binary Search,非常重要的基本题。

左右指针有三种写法:1. left <= right, 2. left < right, 3. left + 1 < right。我个人prefer第一种写法。

T: O(logn), S: O(1)

  1. class Solution:
  2.     def bin_search(self, nums, target):
  3.         left, right = 0, len(nums) - 1
  4.         . From 1point 3acres bbs
  5.         while left <= right:
    . 1point3acres
  6.             mid = left + (right - left)//2
  7.             if nums[mid] > target:
  8.                 right = mid - 1. Waral dи,
  9.             elif nums[mid] < target:
  10.                 left = mid + 1
  11.             else:. check 1point3acres for more.
  12.                 return mid
  13.             
  14.         return -1
  15.    
  16.     ..
  17. if __name__ == '__main__':
  18.     assert Solution().bin_search([-1,0,3,5,9,12], 9) == 4
  19.     assert Solution().bin_search([-1,0,3,5,9,12], 2) == -1
复制代码


回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-7 06:56:34 | 只看该作者
全局:
18. Implement the K-means algorithm
.1point3acres
一些比较简单但是重要的机器学习算法需要会描述算法并且写出代码,K-means算法是常考题. check 1point3acres for more.

K-means算法简介:. 1point3acres.com
https://jakevdp.github.io/Python ... /05.11-k-means.html

Elbow method:
https://en.wikipedia.org/wiki/Elbow_method_(clustering)

Silhouette analysis:
https://scikit-learn.org/stable/ ... uette_analysis.html. Waral dи,

K-means ++:
https://en.wikipedia.org/wiki/K-means%2B%2B

.google  и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):-baidu 1point3acres
  5.     """
  6.     Args:. .и
  7.     - X - data matrix
  8.     - k - number of clusters
  9.     - max_iter - maximum iteratations
  10. . From 1point 3acres bbs
  11.     Returns:
  12.     - clusters - dict mapping cluster centers (tuples) to
  13.                  observations (list of datapoints, each datapoint is a numpy array)
  14.     """
  15.     # initialize cluster centers by random sample k rows of X
  16.     # random.seed(42)
  17.     centers = [tuple(pt) for pt in random.sample(list(X), k)]
  18.    
  19.     # begin iterations
  20.     for i in range(max_iter):
  21.         clusters = defaultdict(list)
  22.         . Χ
  23.         # E-Step: assign points to the nearest cluster center
  24.         for datapoint in X:
  25.             dists = [np.linalg.norm(datapoint - center) for center in centers]
  26.             center = centers[np.argmin(dists)]
  27.             clusters[center].append(datapoint)
  28.         
  29.         # M-Step: average the cluster datapoints to calculate the new centers. ----
  30.         new_centers = []
  31.         for center, datapoints in clusters.items():
  32.             new_center = np.mean(datapoints, axis=0). 1point 3acres
  33.             new_centers.append(tuple(new_center))
  34.         . 1point3acres
  35.         # stop iteration if the centers stop changing
  36.         if set(new_centers) == set(centers):
  37.             break
  38. . 1point 3acres
  39.         # update centers.google  и
  40.         centers = new_centers

  41.     return clusters.
  42. . 1point 3acres

  43. # use the iris dataset as example
  44. from sklearn import datasets
  45. iris = datasets.load_iris()
  46. X = iris.data. .и

  47. X_clustered = k_means(X, k=5, max_iter=1000)
  48. X_clustered


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

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-7 07:54:18 | 只看该作者
全局:
19. Find median in an unsorted array

Method 1: By definition
T: O(nlogn), S: O(1)

  1. def find_median(arr):. 1point3acres
  2.     result = None. ----
  3.     arr.sort()
  4.     n = len(arr)

  5.     if n%2 != 0:
  6.         result = arr[n//2]
  7.     else:
  8.         result = (arr[(n - 1)//2] + arr[n//2])*0.5.
  9.         
  10.     return result


  11. if __name__ == '__main__':
  12.     assert find_median([1, 4, 1, 2, 7, 1, 2, 5, 3, 6]) == 2.5
  13.     assert find_median([1, 1, 1, 2, 7, 1]) == 1.0
复制代码




Method2: Use counting sort

Counting Sort 简介: https://www.youtube.com/watch?v=OKd534EWcdk&ab_channel=CSDojo

T: O(n + k) <=  n is the length of input array and k is the range of input array
S: O(n + k)

  1. def find_median_counting_sort(arr):
  2.     result = None
  3.    
  4.     # the following code performs counting sort
  5.     n = len(arr)
  6.     arr_sorted = [0]*n
  7.    
  8.     arr_max = max(arr)
  9.     t = arr_max + 1. check 1point3acres for more.
  10.     count = [0]*t
  11.    
  12.     for i in range(n):
  13.         count[arr[i]] += 1
  14.    
  15.     for i in range(1, t):
  16.         count[i] += count[i - 1]. 1point 3acres
  17.         
  18.     for i in range(n):
  19.         arr_sorted[count[arr[i]] - 1] = arr[i]. 1point3acres
  20.         count[arr[i]] -= 1
  21.         
  22.     # find median by definition
  23.     if n%2 != 0:
  24.         result = arr_sorted[n//2]
  25.     else:
  26.         result = (arr_sorted[(n - 1)//2] + arr_sorted[n//2])*0.5
  27.         
  28.     return result

  29. . ----
  30. if __name__ == '__main__':
  31.     assert find_median_counting_sort([1, 4, 1, 2, 7, 1, 2, 5, 3, 6]) == 2.5
  32.     assert find_median_counting_sort([1, 1, 1, 2, 7, 1]) == 1.0
复制代码


回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-7 08:15:51 | 只看该作者
全局:
20. Estimate Pi by using Monte Carlo simulation

  1. import random

  2. def estimate_pi(max_iters):
  3.     result = 0
  4.     circle_points_count = 0. check 1point3acres for more.
  5.     square_points_count = 0
  6.    
  7.     for i in range(max_iters):
  8.         x_rand = random.uniform(-1, 1)
  9.         y_rand = random.uniform(-1, 1)
  10.         
  11.         dist = x_rand**2 + y_rand**2
  12.         
  13.         if dist <= 1: ..
  14.             circle_points_count += 1
  15.             
  16.         square_points_count += 1
  17.         
  18.     result = 4*circle_points_count/square_points_count
  19.     return result



  20. estimate_pi(10000000)
复制代码
. check 1point3acres for more.


Output: 3.141472.1point3acres
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-7 12:29:27 | 只看该作者
全局:
21.  Simulating a Fair Coin with a Biased Coin

Toss a coin twice.
If you get HT call it Heads.. ----
If you get TH call it Tails.
If you get HH or TT ignore and repeat your coin toss.

We use 1, 0 to encode success and failure, which are Head and Tail, respectively.

P(X = 1) = P(H) = p
P(X = 0) = P(T) = 1 - p

  1. from random import random.google  и

  2. # Simulate a biased coin with P(H) = p
  3. def biased_coin(p):
  4.     if p <= 0 or p >= 1:
  5.         raise Exception("p cannot lie outside (0,1)!")
  6.     return int(random() < p)


  7. # Verify that we have simulated a biased coin with p = 0.2.1point3acres
  8. print(sum(biased_coin(0.2) for i in range(100000))/100000)
  9. # We get 0.19915
  10. . 1point3acres.com
  11. .--
  12. # Simulate a fair coin by tossing this biased coin twice
  13. def fair_coin(p):
  14.     coin1, coin2 = 0,0
  15.    
  16.     # if TT, HH, toss again
  17.     while coin1 == coin2:
  18.         coin1, coin2 = biased_coin(p), biased_coin(p)
  19.     return coin1

  20. # We indeed get a fair coin!.
  21. print(sum(fair_coin(0.2) for i in range(100000))/100000). .и
  22. # We get 0.50026
复制代码

. From 1point 3acres bbs

评分

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

查看全部评分

回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-8 08:27:40 | 只看该作者
全局:
22. Implement the k-nearest neighbors algorithm .google  и
Feature scaling:
https://stats.stackexchange.com/ ... o-scale-data-in-knn

Overfitting issues:
https://www.quora.com/Would-usin ... smooth-out-the-data

T: O(nd + kn) <=  where n is the number of the rows in the training set and d is the number of features.

  1. import numpy as np
  2. . 1point3acres.com
  3. class KNN:. check 1point3acres for more.
  4.    
  5.     def __init__(self, k=3):
  6.         self.k = k

  7.     def fit(self, X_train, y_train):
  8.         self.X_train = X_train ..
  9.         self.y_train = y_train

  10. ..
  11.     def predict(self, X_test):.--
  12.             predictions = []

  13.             # loop over all rows in the test set
  14.             for i in range(len(X_test)):            

  15.                 # calculate the distance between the test point and all other points in the training set.
  16.                 dist = np.array([np.linalg.norm(X_test[i] - x_train) for x_train in self.X_train])

  17.                 # sort the distances and return the indices of k neighbors
  18.                 dist_sorted = dist.argsort()[:self.k]

  19.                 # get the neighbors
  20.                 neighor_count = {}
  21. . check 1point3acres for more.
  22.                 # for each neighbor, find the class
  23.                 for idx in dist_sorted:
  24.                     neighor_count[y_train[idx]] = neighor_count.get(y_train[idx], 0) + 1

  25.                 sorted_neighor_count = sorted(neighor_count.items(),  key=lambda item: item[1], reverse=True).google  и
  26.    
  27.                 # append the class label to the list. check 1point3acres for more.
  28.                 predictions.append(sorted_neighor_count[0][0])
  29.             
  30.             return predictions
  31.         
  32.         
  33.         
  34. import numpy as np
  35. from sklearn.datasets import make_classification
  36. from sklearn.model_selection import train_test_split
  37. from sklearn.metrics import accuracy_score

  38. np.random.seed(123)
  39. X, y = make_classification(n_samples=1000, n_classes=2).google  и
  40. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)

  41. clf = KNN(k=5)-baidu 1point3acres
  42. clf.fit(X_train, y_train).1point3acres
  43. y_pred = clf.predict(X_test)

  44. print('Accuracy:', accuracy_score(y_test, y_pred))

  45. # Output
  46. # 0.95
复制代码


.1point3acres
回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-8 14:30:14 | 只看该作者
全局:
23. Given x coordinate and y coordinate, calculate area under the curve.. 1point 3 acres

可以用Trapezoidal rule计算:
https://en.wikipedia.org/wiki/Trapezoidal_rule
. check 1point3acres for more.
  1. def area_under_curve(x, y):
  2.     result = 0
  3.    
  4.     for i in range(1, len(x)):
  5.         d = x[i] - x[i - 1]
  6.         result += (d/2)*(y[i - 1] + y[i])
  7.    
  8.     return result. check 1point3acres for more.


  9. x_coord = [0, 1, 2, 3, 4, 4, 5]. 1point 3 acres
  10. y_coord = [0, 1, 3, 3, 5, 6, 7]
  11. .--
  12. area_under_curve(x_coord, y_axis)
  13. # Output: 16.0
复制代码


[/i][/i]. ----
回复

使用道具 举报

全局:
mark 感谢感谢
回复

使用道具 举报

全局:
很有用的帖子!
回复

使用道具 举报

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

本版积分规则

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