numpy 是否为零_python数据分析入门必刷!100道题掌握numpy

a806ad4dbc0e518adacd6ffde57ab84d.png

numpy-100 可以说是炒鸡经典的numpy学习资料了~原项目地址:numpy-100 各路大神提供的中文译版也有很多,不过为了巩固自己的学习成果,我还是自己进行了翻译+刷题+总结。对于每道题,均给出了原题翻译、正确答案,以及我的个人心得笔记。如下~

(1) 使用别名“np“调用numpy库。(★☆☆)(import … as …)

import 

(2) 显示当前numpy库的版本号和配置信息。(★☆☆)(np.__version__, np.show_config)

print

拓展阅读:Python中带双下划线的那些方法和属性

(3) 创建一个大小为10的零向量。(★☆☆)(np.zeros)

np

(4) 如何获取任意一个数组占用的内存大小?(★☆☆)(size, itemsize)

a 

(5) 如何在命令行中获取numpy的add函数的文档?(★☆☆)(http://np.info)

#win

(6) 创建一个大小为10的零向量,使之第5个元素为1。(★☆☆)(array[4])

a 

(7) 创建一个向量,使它的值为10到49。(★☆☆)(np.arange)

np

(8) 使一个向量逆序(第1个元素变成倒数第1个元素)。(★☆☆)(array[::-1])

a 

(9) 创建一个 3x3 矩阵,其值为0到8。(★☆☆)(reshape)

np

(10) 列出 [1,2,0,0,4,0] 该数组中非零元素的索引。(★☆☆)(np.nonzero)

np

(11) 创建一个 3x3 的单位矩阵。(★☆☆)(np.eye)

np

(12) 利用随机数创建一个 3x3x3 的数组。 (★☆☆)(np.random.random)

np

(13) 利用随机数创建一个 10x10 的数组,并获取数组的最小元素和最大元素。(★☆☆)(min, max)

a 

(14) 创建一个大小为30的随机向量,并求取均值。(★☆☆)(mean)

np

(15) 创建一个二维数组,使其边界全部为1,内部全部为0。(★☆☆)(array[1:-1, 1:-1])

#参考答案

(16) 如何给一个已有的数组加上一个边界(值为0)?(★☆☆)(np.pad)

a 

(17) 以下的表达式的运行结果为?(★☆☆)(NaN = not a number, Inf = infinity)

print

拓展阅读:关于Python中Inf与Nan的判断问题详解

(18) 创建一个 5x5 矩阵,使得数字 1,2,3,4 恰好处在对角线下方。(★☆☆)(np.diag)

#参考答案

(19) 创建一个8x8数组,使之形成棋盘格图案。 (★☆☆)(array[::2])

#参考答案

(20) 对于一个形为 (6,7,8) 的数组,如何用形似 (x,y,z) 的索引取出其第100个元素?(★☆☆)(np.unravel_index)

np

(21) 使用 tile 函数创建一个 8x8 棋盘格矩阵。(★☆☆)(np.tile)

np

(22) 对一个 5x5 矩阵进行标准化。(★☆☆)((x - mean) / std)

a 

(23) 创建一个自定义类型,用四个无符号字节存放颜色信息(RGBA)。 (★☆☆)(np.dtype)

color 

(24) 矩阵乘法(点乘):5x3矩阵 乘 3x2矩阵。(★☆☆)(np.dot | @)

#参考答案1

(25) 给定一维数组,对值落在 [3,8] 区间内的元素取负。(★☆☆)(>, <=)

#参考答案

(26) 这段代码的运行结果是什么? (★☆☆)(np.sum)

print

(27) 对于一个整数型向量Z,以下哪些表达式是合法的?(★☆☆)

Z

(28) 以下表达式的运行结果是?(★☆☆)

np

(29) 浮点型数组如何四舍五入? (★☆☆)(np.uniform, np.copysign, np.ceil, np.abs)

#参考答案

(30) 如何获取两个数组中相同的值(取交集)?(★☆☆)(np.intersect1d)

np

(31) 如何忽略numpy警告(不建议)? (★☆☆)(np.seterr, np.errstate)

# 开启自杀模式

拓展阅读:numpy.seterr()numpy.errstat()

(32) 下列表达式正确吗?(★☆☆)(imaginary number)

np

(33) 如何获取昨天、今天和明天的日期?(★☆☆)(np.datetime64, np.timedelta64)

yesterday 

拓展阅读:Datetimes and Timedeltas

(34) 如何获取2016年7月的每一天的日期?(★★☆)(np.arange(dtype=datetime64['D']))

np

(35) 如何正确地计算((A+B)*(-A/2))(不经过复制)? (★★☆)(np.add(out=), np.negative(out=), np.multiply(out=), np.divide(out=))

A 

(36) 用5种不同的方法,截取一个随机数组的整数部分。(★★☆)(%, np.floor, np.ceil, astype, np.trunc)

a 

(37) 创建一个 5x5 矩阵,每行的值为0到4。(★★☆)(np.arange)

#我愚蠢的答案……

(38) 写一个能够生成10个整数generate函数,并用它生成一个数组。(★☆☆)(np.fromiter)

#参考答案

(39) 生成一个大小为10的向量,其值的取值范围为(0,1)。(★★☆)(np.linspace)

np

(40) 创建一个随机向量并对其排序。(★★☆)(sort)

np

(41) 对于小型数组,如何求和比np.sum更快?(★★☆)(np.add.reduce)

np

(42) 对于随机数组A和B,比较它们是否相等。(★★☆)(np.allclose, np.array_equal)

A 

(43) 将一个数组设为只读。(★★☆)(flags.writeable)

Z

(44) 将一个 10x2 矩阵,由笛卡尔坐标系的值转换为极坐标系的值。(★★☆)(np.sqrt, np.arctan2)

#参考答案

(45) 随机生成一个大小为10的向量,将其最大值置换为0。(★★☆)(argmax)

a 

(46) 创建一个结构化数组,使得与之对应的表示坐标系中的点(x,y)能覆盖[0,1]x[0,1]的区域。(★★☆)(np.meshgrid)

Z 

拓展阅读:numpy.meshgrid()

(47) 给定两个数组X和Y,矩阵C的柯西行列式 (Cij =1/(xi - yj))。(★★☆)(np.subtract.outer)

X 

(48) 获取每种数字类型可表示的最小值和最大值。 (★★☆)(np.iinfo, np.finfo, eps)

for 

(49) 如何打印一个数组的所有值?(★★☆)(np.set_printoptions)

#参考答案

(50) 给定一个数和一个数组,如何在数组中找到与该数最接近的元素? (★★☆)(argmin)

a 

(51) 创建一个结构化数组,用来表示位置信息(x,y)和颜色信息(r,g,b)。(★★☆)(dtype)

Z 

(52) 对于一个形为(100,2)的数组表示的坐标点,求点与点之间的距离。(★★☆)(np.atleast_2d, T, np.sqrt)

a 

(53) 如何将浮点型数组(32位)转换成整型数组(32位)?(★★☆)(astype(copy=False))

Z 

(54) 如何读取以下文件? (★★☆)(np.genfromtxt)

83add4669dd8a852edd5a565b5f535e1.png
from 

拓展阅读:StringIO: Read and write strings as files numpy.genfromtxt()

(55) 数组的枚举形式?(★★☆)(np.ndenumerate, np.ndindex)

Z 

拓展阅读:numpy.ndenumerate() numpy.ndindex()

(56) 生成一个满足高斯分布的二维数组。(★★☆)(np.meshgrid, np.exp)

X

拓展阅读:numpy.exp()

(57) 如何将p个元素随机地置入一个二维数组中?(★★☆)(np.put, np.random.choice)

n 

拓展阅读:numpy.put() numpy.random.choice

(58) 使矩阵每行的数据减去该行的均值。(★★☆)(mean(axis=,keepdims=))

X 

(59) 如何根据第n列对数组进行排序?(★★☆)(argsort)

a

拓展阅读:numpy.argsort()

(60) 如何找出给定二维数组中的空列?(★★☆)(any, ~)

(

拓展阅读:numpy.any() ndarray.any()

(61) 在一个数组中,找出与给定数值大小最接近的元素。(★★☆)(np.abs, argmin, flat)

Z 

(62) 如何使用迭代器计算形如(1,3)和(3,1)的数组的和?(★★☆)(np.nditer)

A 

拓展阅读:numpy.nditer()

(63) 创建一个带有名称属性的数组类。(★★☆)(class method)

class 

(64) 对于数组Z中每一个被向量I索引到的值全部加1(注意重复索引)。(★★★)(np.bincount | np.add.at)

Z 

拓展阅读:numpy.bincount() numpy.ufunc.at()

(65) 如何基于索引列表(I)将向量(X)的元素累积到数组(F)中?(★★★)(np.bincount)

X 

(66) Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★★)(np.unique)

w

(67) 基于一个四维数组的最后两轴,对其进行求和。(★★★)(sum(axis=(-2,-1)))

A 

(68) 对于一个给定但一维数组D,另有与之大小一样的数组S存放了其子集的索引信息(即:将数组D划分为n个组,分组的具体信息存放于数组S),如何利用数组S求数组D这些子集的均值?(★★★)(np.bincount)

D 

(69) 获取矩阵内积运算后的对角线。 (★★★)(np.diag)

A 

(70) 如何根据向量[1, 2, 3, 4, 5]创建一个新向量,使得新向量每个值之间插入3个连续的零?(★★★)(array[::4])

Z 

(71) 如何将一个维度为(5,5,3)的数组,与维度为(5,5)的数组相乘?(★★★)(array[:, :, None])

A 

(72) 如何交换数组中的两行?(★★★)(array[[]] = array[[]])

A 

(73) Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★)(repeat, np.roll, np.sort, view, np.unique)

faces 

(74) 数组C是一个bincount(描述频数的数组),如何生成一个数组A,使得np.bincount(A)==C?(★★★)(np.repeat)

# 对整数型数组计数的逆运算

(75) 如何使用sliding window来计算一个数组的平均值?(★★★)(np.cumsum)

def 

(76) 给定一维数组Z,根据数组Z生成一个二维数组,使得该数组的第一行为:(Z[0],Z[1],Z[2]),随后的每一行都移动一位。(即:第二行为:(Z[1],Z[2],Z[3])……最后一行为(Z[-3],Z[-2],Z[-1]))(★★★)(from numpy.lib import stride_tricks)

from 

(77) 如何对数组中的布尔值取反?如何对数组中浮点数的符号取反?(★★★)(np.logical_not, np.negative)

Z 

拓展阅读:numpy.logical_not() numpy.negative()

救命⁄(⁄ ⁄ ⁄ω⁄ ⁄ ⁄)⁄……这100道题真的越到后面越难……容我缓缓,下次再整理后面的~前面的很多题也理解得不够透彻,容后一并消化~( ´▽`)

(78) 现有两个点集P0和P1,其中存放的点分别一一对应一条直线(二维)。(第一条直线过点P0[1]和点P1[1]。)另有一点p,求点p到每条直线i (P0[i],P1[i])的距离。(★★★)

def 

(79) 现有两个点集P0和P1,其中存放的点分别一一对应一条直线(二维)。(第一条直线过点P0[1]和点P1[1]。)另有一点集P,求点集P中的每一点j (P[j])到每条直线i (P0[i],P1[i])的距离。(★★★)

# based on distance function from previous question

(80) 给定一个随机数组,构造一个函数来提取该数组的一个固定形状的、以某元素为中心的子部(必要时用填充值填补空缺)。(★★★)(minimum, maximum)

(81) 给定数组Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14],如何生成数组R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]?(★★★)(stride_tricks.as_strided)

(82) 计算矩阵的秩。(★★★)(np.linalg.svd) (suggestion: np.linalg.svd)

(83) 求数组的众数?(★★★)(np.bincount, argmax)

(84) 从一个随机10x10矩阵中,提取所有的3x3子部。(★★★)(stride_tricks.as_strided)

(85) 构造一个二维数组子类使得元素关于主对角线对称(Z[i,j] == Z[j,i])。 (★★★)(class method)

(86) 给定一个含p个nxn矩阵的集合,和一个含p个nx1向量的集合,如何一次性计算p个矩阵乘法之和?(结果为nx1) (★★★)(np.tensordot)

(87) 给定一个16x16数组,如何获得子块的和?(子块大小为4x4,共16个子块)(★★★)(np.add.reduceat)

(88) 如何使用numpy数组实现the Game of Life? (★★★)

(89) 如何获得一个数组中第n大的数?(★★★)(np.argsort | np.argpartition)

(90) 给定任意数量的向量,构建笛卡尔积(每一项的所有组合)(★★★)(np.indices)

(91) How to create a record array from a regular array? (★★★) (np.core.records.fromarrays)

92) Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★) (np.power, *, np.einsum)

(93) Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B? (★★★) (np.where)

(94) Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)

(95) Convert a vector of ints into a matrix binary representation (★★★) (np.unpackbits)

(96) Given a two dimensional array, how to extract unique rows? (★★★) (np.ascontiguousarray | np.unique)

(97) Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★) (np.einsum)

(98) Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)? (np.cumsum, np.interp)

(99) Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n. (★★★) (np.logical_and.reduce, np.mod)

(100) Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means). (★★★) (np.percentile)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值