【Python学习】Numpy测试题(1)

题目原文

1.Import numpy as np and see the version?

import numpy as np
print(np.__version__)

__version__就像是一种内置的宏。


2.How to create a 1D array?

import numpy as np
array = np.array([i for i in range(10)])
array1 = np.arange(10)
array2 = np.arange(0,10,1)
print(array, '\n', array1, '\n', array2)

一般来说array是指定数组元素的初始化方式,但是arange是一种自动初始化一个有规律的序列。arange(start, stop, step)在一个左闭右开的区间上按照步长进行初始化。


3.How to create a boolean array?

Method-1:

import numpy as np
bool_arr = np.full((3,3), True, dtype=bool)
print(bool_arr)

函数numpy.full(shape, value, dtype)是对一个指定shape的numpy数组按照value进行填充。

Method-2:

import numpy as np
arr = np.ones((3,3),dtype=bool)
print(arr)

函数numpy.ones(shape,dtype)是按照shape指定的numpy数组使用dtype指定类型的默认初始值进行构造。


4.How to extract items that satisfy a given condition from 1D array?

import numpy as np
arr = np.arange(10)
arr_ = arr[arr %2 == 1]
print(arr_)

numpy[condition]可以在numpy数组中根据condition进行筛选。


5.How to replace items that satisfy a condition with another value in numpy array?

import numpy as np
arr = np.arange(10)
arr[arr%2==1] = -1
print(arr)

6.How to replace items that satisfy a condition without affecting the original array?

import numpy as np
arr = np.arange(10)
arr_ = np.where(arr % 2 == 1, -1, arr)
print(arr, '\n', arr_)

函数numpy.where(condition,[,x,y])类似于条件表达式,如果condition是满足的(True),则执行x,否则执行y。


7.How to reshape an array?

import numpy as np
arr = np.arange(10)
arr_ = np.reshape(arr, (2,-1))
# arr_ = arr.reshape(2, -1)
print(arr_)

参数-1表示自动计算维度。


8.How to stack two arrays vertically?


9.How to stack two array horizontally?

10.How to generate custom sequences in numpy without hardcoding?

11.How to get the common items between two python numpy arrays?

12.How to remove from one array those items that exist in another?

13.How to get the positions where elements of two arrays match?

14.How to extract all numbers between a given range from a numpy array?

15.How to make a python function that handles scalars to work on numpy arrays?

16.How to swap two columns in a 2d numpy array?

17.How to swap rows in a 2d numpy array?

18.How to reverse the rows of a 2D array?

19.How to reverse the columns of 2 2D array?

20.How to create a 2D array containing random floats between 5 and 10?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值