本文主要是介绍【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?
这篇关于【Python学习】Numpy测试题(1)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!