开始使用免费开始使用

随机行选择

在本练习中,您将比较两种用于在 pandas DataFrame 中进行有放回随机行(条目)选择的方法:

  • 内置的 pandas 函数 .random()
  • NumPy 的随机整数生成器 np.random.randint()

在统计和机器学习领域中,训练算法时通常会使用 75% 的可用数据进行训练,然后用剩余的 25% 数据评估性能。

在本练习中,您将分别使用以上两种方法,随机抽取所有已发扑克牌局的 75%,并比较哪种方法在速度上更高效。

本练习是课程的一部分

使用 pandas 编写高效代码

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Extract number of rows in dataset
N=poker_hands.shape[0]

# Select and time the selection of the 75% of the dataset's rows
rand_start_time = time.time()
poker_hands.iloc[np.random.randint(____=0, high=____, ____=int(0.75 * N))]
print("Time using Numpy: {} sec".format(time.time() - rand_start_time))
编辑并运行代码