Numpy练习100题--难度★☆☆

1.Import the numpy package under the name np (★☆☆)

#导入numpy模块
import numpy as np

2.Print the numpy version and the configuration (★☆☆)

#打印numpy的版本信息
print np.version.version
print np.__version__
np.show_config()

3.Create a null vector of size 10 (★☆☆)

#新建全为0的数组
vector=np.zeros(10)#一维
vector_2=np.zeros((10,10))#二维

4.How to find the memory size of any array (★☆☆)

#数组所占内存大小=元素个数*每个元素的大小
print vector.size*vector.itemsize

5.How to get the documentation of the numpy add function from the command line? (★☆☆)

#命令行获取numpy的add函数的文档信息
python -c "import numpy;numpy.info(numpy.add)"

6.Create a null vector of size 10 but the fifth value which is 1 (★☆☆)

#创建非空的大小为10的数组,第5个元素为1
vector=np.zeros(10)
vector[4]=1

7.Create a vector with values ranging from 10 to 49 (★☆☆)

#创建一个数组,元素值从10-49
vector=np.arange(10,50)

8.Reverse a vector (first element becomes last) (★☆☆)

#翻转一个数组
vector[::-1]

9.Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)

#创建一个3*3的矩阵,元素值为0-8
vector=np.arange(0,9).reshape(3,3)

10.Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)

#查找数组的所有非0元素的下标
indices=np.nonzero([1,2,0,0,4,0])

11.Create a 3x3 identity matrix (★☆☆)

#创建3*3单位矩阵
vector=np.eye(3)

12.Create a 3x3x3 array with random values (★☆☆)

#创建3*3*3的随机数组
vector=np.random.random([3,3,3])

13.Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)

#创建10*10的随机数组,并找出最大值和最小值
vector=np.random.random([10,10])
v_max=vector.max()
v_min=vector.min()

14.Create a random vector of size 30 and find the mean value (★☆☆)

#创建大小为30的随机数组,求数组的平均值
vector=np.random.random(30)
v_mean=vector.mean()

15.Create a 2d array with 1 on the border and 0 inside (★☆☆)

#创建二维数组,边界元素为1,内部元素为0
vector_2d=np.zeros((5,5))
vector_2d[:,[0,-1]]=1#左右为 1
vector_2d[[0,-1],:]=1#上下为 1
#或者
vector_2d=np.ones((5,5))
vector_2d[1:-1,1:-1]=0

16.How to add a border (filled with 0’s) around an existing array? (★☆☆)

#给一个数组的外围填充0
vector=np.pad(vector,pad_width=1,mode='constant',constant_values=0)

17.What is the result of the following expression? (★☆☆)

0 * np.nan#np.nan
np.nan == np.nan#False
np.inf > np.nan#False
np.nan - np.nan#np.nan
0.3 == 3 * 0.1#False

18.Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)

#生成5*5矩阵,值1,2,3,4刚好在对角线下面
#k=0对角线,k>0对角线上面,k<0对角线下面
vector=np.diag(np.arange(1,5),k=-1)

19.Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)

#生成8*8棋盘模式
vector=np.zeros((8,8))
vector[::2,1::2]=1
vector[1::2,::2]=1

20.Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?

#一个6*7*8的三维数组,第100个元素的下标是多少
z=100%8
y=(100/8)%7
x=100/(8*7)
#unravel_index()将一维数组下标转化为
print np.unravel_index(100,(6,7,8))

21.Create a checkerboard 8x8 matrix using the tile function (★☆☆)

#使用tile函数创建8*8棋盘,tile函数表示将某一模式在行、列重复多少次
vector=np.tile([[1,0],[0,1]],(4,4))#[[1,0],[0,1]]在行列均重复4

22.Normalize a 5x5 random matrix (★☆☆)

#5*5随机矩阵归一化
vector=np.random.random((5,5))
#极值归一化
vector=(vector-vector.min())/(vector.max()-vector.min())
#标准化
vector=(vector-vector.mean())/vector.std()

23.Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)

#自定义类型,用4个无符号字节来表示RGBA
color=np.dtype([('R',np.ubyte),('G',np.ubyte),('B',np.ubyte),('A',np.ubyte)])

24.Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)

#实数矩阵5*3,3*2的乘法
A=np.ones((5,3))
B=np.ones((3,2))
vector=np.dot(A,B)#numpy中*是逐元素计算,dot是矩阵乘法

25.Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)

#把一维数组中3-8之间的元素取反
vector=np.arange(10)
vector[vector>3&vector<8]*=-1

26.What is the output of the following script? (★☆☆)

# Author: Jake VanderPlas
sum(range(5),-1)#9,sum是python内建函数,sum(sequence[,start]),相当于-1+1+2+3+4=9
from numpy import *
#numpy.sum(a, axis=None),axis表示沿着哪个轴求和,因为数组是一维的,所以axis的大小没关系
sum(range(5),-1)

27.Consider an integer vector Z, which of these expressions are legal? (★☆☆)

#legal
Z**Z#**是乘方运算,element-wise
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z

28.What are the result of the following expressions?

np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)
29.How to round away from zero a float array ? (★☆☆)

30.How to find common values between two arrays? (★☆☆)

31.How to ignore all numpy warnings (not recommended)? (★☆☆)

32.Is the following expressions true? (★☆☆)

np.sqrt(-1) == np.emath.sqrt(-1)
33.How to get the dates of yesterday, today and tomorrow? (★☆☆)

  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Twitter Digg Facebook Del.icio.us Reddit Stumbleupon Newsvine Technorati Mr. Wong Yahoo! Google Windows Live Send as Email Add to your CodeProject bookmarks Discuss this article 85 Print Article Database » Database » Other databasesLicence CPOL First Posted 19 Jan 2012 Views 24,219 Downloads 992 Bookmarked 74 times RaptorDB - The Key Value Store V2 By Mehdi Gholam | 8 Mar 2012 | Unedited contribution C#.NETDBABeginnerIntermediateAdvanceddatabase Even faster Key/Value store nosql embedded database engine utilizing the new MGIndex data structure with MurMur2 Hashing and WAH Bitmap indexes for duplicates. See Also More like this More by this author Article Browse Code Stats Revisions (8) Alternatives 4.95 (56 votes) 1 2 3 4 5 4.95/5 - 56 votes μ 4.95, σa 1.05 [?] Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions. Add your own alternative version Introduction What is RaptorDB? Features Why another data structure? The problem with a b+tree Requirements of a good index structure The MGIndex Page Splits Interesting side effects of MGIndex The road not taken / the road taken and doubled back! Performance Tests Comparing B+tree and MGIndex Really big data sets! Index parameter tuning Performance Tests - v2.3 Using the Code Differences to v1 Using RaptorDBString and RaptorDBGuid Global parameters RaptorDB interface Non-clean shutdowns Removing Keys Unit tests File Formats File Format : *.mgdat File Format : *.mgbmp File Format : *.mgidx File Format : *.mgbmr , *.mgrec History Download RaptorDB_v2.0.zip - 38.7 KB Download RaptorDB_v2.1.zip - 39 KB Download RaptorDB_v2.2.zip - 39 KB Download RaptorDB_v2.3.zip - 39.6 KB D

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值