numpy的100个练习解析(持续更新)

numpy100个练习解析

先放链接https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises.md

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

可以下载Anaconda,里面带有很多python库。

以下默认已经引入numpy

print(np.__version__)
np.show_config()
3. Create a null vector of size 10 (★☆☆)
A=np.zeros(10)
print(A)

创建一个大小为10的零向量

4. How to find the memory size of any array (★☆☆)
A=np.zeros(10)
print(A.size * A.itemsize,"bytes")

size元素个数,itemsize每个元素的大小

5. How to get the documentation of the numpy add function from the command line? (★☆☆)
np.info(np.add)
x1 = np.arange(9.0).reshape((3, 3))
x2 = np.arange(3.0)
print(np.add(x1, x2))
'''
值得注意的是,若两个矩阵维度不相同也可以利用add相加,结果如下所示:
[[ 0.  2.  4.]
 [ 3.  5.  7.]
 [ 6.  8. 10.]]
'''
6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
A=np.zeros(10)
A[4]=1

注意第五个元素应该是A[4]

7. Create a vector with values ranging from 10 to 49 (★☆☆)
A=np.arange(10,50)

arange(start,end)表示从startend-1

8. Reverse a vector (first element becomes last) (★☆☆)
A=np.arange(0,5)
A=A[::-1]
9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
A= np.arange(9).reshape(3,3)
10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
index=np.nonzero([1,2,0,0,4,0])
print(index)
11. Create a 3x3 identity matrix (★☆☆)
A=np.eye(3)
12. Create a 3x3x3 array with random values (★☆☆)
A=np.random.random((3,3,3))
13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
A = np.random.random((10, 10))
B = A.max()
C = A.min()
14. Create a random vector of size 30 and find the mean value (★☆☆)
A = np.random.random(30)
print(A.mean())
15. Create a 2d array with 1 on the border and 0 inside (★☆☆)
A=np.ones((5,5))
A[1:-1,1:-1]=0
print(A)

转载于:https://www.cnblogs.com/wyb6231266/p/11273486.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值