1.将 NumPy 导入为 np,并查看版本
难度:L1
solution:
import numpy as np
print(np.__version__)
#1.14.2
2.如何创建一个元素为0-9的一维数组?
难度:L1
期望输出:
#> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
solution:
arr = np.arange(10)
arr
#> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
3.怎样创建布尔数组?
难度:L1
问题:创建一个3x3的numpy数组,所有元素为True
solution:
np.full((3, 3), True, dtype=bool)
#> array([[ True, True, True],
#> [ True, True, True],
#> [ True, True, True]], dtype=bool)
# Alternate method:
np.ones((3,3), dtype=bool)
4.从一维数组中提取满足给定条件的项
难度:L1
问题:从arr中提取所有奇数
输入:
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
期望输出:
#> array([1, 3, 5, 7, 9])
solution:
# Input
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# Solution
arr[arr % 2 == 1]
#> array([1, 3, 5, 7, 9])
5.将数组中满足给定条件的元素替换成另一个值
难度:L1
问题:将数组arr中所有的奇数替换为-1
输入:
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
期望输出:
#> array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])
solution:
arr[arr % 2 == 1] = -1
arr
#> array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])
6.将数组中满足条件的元素替换为另一个值,并且不影响原来的数组
难度:L2
问题:将数组arr中所有奇数都替换为-1,并且不改变arr。
输入:
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
期望输出:
out
#> array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])
arr
#> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
solution:
arr = np.arange(10)
out = np.where(arr % 2 == 1, -1, arr)
print(arr)
out
#> [0 1 2 3 4 5 6 7 8 9]
array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])
7.怎样reshape数组?
难度:L1
问题:将1D数组转变为2D
输入:
np.arange(10)
#> array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
期望输出:
#> array([[0, 1, 2, 3, 4],
#> [5, 6, 7, 8, 9]])
solution:
arr = np.arange(10)
arr.reshape(2, -1) # Setting to -1 automatically decides the number of cols
#> array([[0, 1, 2, 3, 4],
#> [5, 6, 7, 8, 9]])
8.如何垂直堆叠两个数组
难度:L2
问题:垂直堆叠两个数组a和b
输入:
a = np.arange(10).reshape(2,-1)
b = np.repeat(1, 10).reshape(2,-1)
#>>> a
#array([[0, 1, 2, 3, 4],
# [5, 6, 7, 8, 9]])
#>>> b
#array([[1, 1, 1, 1, 1],
# [1, 1, 1, 1, 1]])
期望输出:
#> array([[0, 1, 2, 3, 4],
#> [5, 6, 7, 8, 9],
#> [1, 1, 1, 1, 1],
#> [1, 1, 1, 1, 1]])
solution:
a = np.arange(10).reshape(2,-1)
b = np.repeat(1, 10).reshape(2,-1)
# Answers
# Method 1:
np.concatenate([a, b], axis=0)
# Method 2:
np.vstack([a, b])
# Method 3:
np.r_[a, b]
#> array([[0, 1, 2, 3, 4],
#> [5, 6, 7, 8, 9],
#> [1, 1, 1, 1, 1],
#> [1, 1, 1, 1, 1]])
np.concatenate
np.vstack
np.r_
9.如何水平堆叠两个数组?
难度:L2
问题:水平堆叠两个数组a和b
输入:
a = np.arange(10).reshape(2,-1)
b = np.repeat(1, 10).reshape(2,-1)
期望输出:
#> array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1],
#> [5, 6, 7, 8, 9, 1, 1, 1, 1, 1]])
solution:
a = np.arange(10).reshape(2,-1)
b = np.repeat(1, 10).reshape(2,-1)
# Answers
# Method 1:
np.concatenate([a, b], axis=1)
# Method 2:
np.hstack([a, b])
# Method 3:
np.c_[a, b]
#> array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1],
#> [5, 6, 7, 8, 9, 1, 1, 1, 1, 1]])
np.concatenate
np.hstack
np.c_
10.如何获取两个数组中相同的元素?
难度:L2
问题:获取数组a和b中的公共元素
输入:
a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
期望输出:
array([2, 4])
solution:
a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
np.intersect1d(a,b)
#> array([2, 4])
11.如何在一个数组中移除出现在另一个数组中的元素?
难度:L2
问题:移除数组a中所有在数组b中出现的元素
输入:
a = np.array([1,2,3,4,5])
b = np.array([5,6,7,8,9])
期望输出:
array([1,2,3,4])
solution:
a = np.array([1,2,3,4,5])
b = np.array([5,6,7,8,9])
# From 'a' remove all of 'b'
np.setdiff1d(a,b)
#> array([1, 2, 3, 4])
np.setdiff1d:类似集合的差集运算。
12.怎样得到两个数组元素相等的元素下标?
难度:L2
问题:得到数组a和数组b元素相等的位置(下标)
输入:
a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
期望输出:
#> (array([1, 3, 5, 7]),)
solution:
a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
np.where(a == b)
#> (array([1, 3, 5, 7]),)
np.where:当输入为二维,返回两个值,分别为横坐标和纵坐标
13.如何重数组中提取在给定范围内的元素?
难度:L2
问题:在数组a中提取所有数值在5到10之间的元素
输入:
a = np.array([2, 6, 1, 9, 10, 3, 27])
输出:
(array([6, 9, 10]),)
solution:
a = np.array([2, 6, 1, 9, 10, 3, 27])
# Method 1
index = np.where((a >= 5) & (a <= 10))
a[index]
# Method 2:
index = np.where(np.logical_and(a>=5, a<=10))
a[index]
#> (array([6, 9, 10]),)
# Method 3: (thanks loganzk!)
a[(a >= 5) & (a <= 10)]
14.如何将python中处理两个标量的函数转化为能够处理numpy中的数组?
难度:L2
问题:mmax函数能够求出两个数的最大值,转化该函数使得能够处理numpy中的数组
输入:
def maxx(x, y):
"""Get the maximum of two items"""
if x >= y:
return x
else:
return y
maxx(1, 5)
#> 5
期望输出:
a = np.array([5, 7, 9, 8, 6, 4, 5])
b = np.array([6, 3, 4, 8, 9, 7, 1])
pair_max(a, b)
#> array([ 6., 7., 9., 8., 9., 7., 5.])
solution:
def maxx(x, y):
"""Get the maximum of two items"""
if x >= y:
return x
else:
return y
pair_max = np.vectorize(maxx, otypes=[float])
a = np.array([5, 7, 9, 8, 6, 4, 5])
b = np.array([6, 3, 4, 8, 9, 7, 1])
pair_max(a, b)
#> array([ 6., 7., 9., 8., 9., 7., 5.])
15.怎样交换一个二维数组中的两列?
难度:L2
问题:交换数组arr中的第一列和第二列
输入:
arr = np.arange(9).reshape(3,3)
期望输出:
#> array([[1, 0, 2],
#> [4, 3, 5],
#> [7, 6, 8]])
solution:
# Input
arr = np.arange(9).reshape(3,3)
arr
# Solution
arr[:, [1,0,2]]
#> array([[1, 0, 2],
#> [4, 3, 5],
#> [7, 6, 8]])
交换行原理同上
未完待续…