Numpy是一个用python实现的科学计算的扩展程序库,包括:
1、一个强大的N维数组对象Array;
2、比较成熟的(广播)函数库;
3、用于整合C/C++和Fortran代码的工具包;
4、实用的线性代数、傅里叶变换和随机数生成函数。numpy和稀疏矩阵运算包scipy配合使用更加方便。
NumPy(Numeric Python)提供了许多高级的数值编程工具,如:矩阵数据类型、矢量处理,以及精密的运算库。专为进行严格的数字处理而产生。多为很多大型金融公司使用,以及核心的科学计算组织如:Lawrence Livermore,NASA用其处理一些本来使用C++,Fortran或Matlab等所做的任务。
本文整理了一个Numpy的练习题,总结了Numpy的常用操作,可以测试下自己对Numpy的掌握程度,有答案哦。
下载地址:
https://github.com/fengdu78/Data-Science-Notes/tree/master/2.numpy/numpy_exercises
Array creation routines(数组创建)
Array manipulation routines(数组操作)
String operations(字符串操作)
Numpy-specific help functions(Numpy特定帮助函数)
Input and output(输入和输出)
Linear algebra(线性代数)
Discrete Fourier Transform(离散傅里叶变换)
Logic functions(逻辑函数)
Mathematical functions(数学函数)
Random sampling (numpy.random)(随机抽样)
Set routines(集合操作)
Sorting, searching, and counting(排序、搜索和计数)
Statistics(统计)
试题分为13个练习,每个练习分为两个ipynb文件,文件名带_Solutions
的是带答案的文件,建议初学者先练习下不带答案的文件,做不出来再看看答案。
import numpy as np
np.__version__
'1.11.2'
Q1. Predict the results of the following code.
x = [1,2]
y = [[4, 1], [2, 2]]
print np.dot(x, y)
print np.dot(y, x)
print np.matmul(x, y)
print np.inner(x, y)
print np.inner(y, x)
[8 5]
[6 6]
[8 5]
[6 6]
[6 6]
Q2. Predict the results of the following code.
x = [[1, 0], [0, 1]]
y = [[4, 1], [2, 2], [1, 1]]
print np.dot(y, x)
print np.matmul(y, x)
[[4 1]
[2 2]
[1 1]]
[[4 1]
[2 2]
[1 1]]
Q3. Predict the results of the following code.
x = np.array([[1, 4], [5, 6]])
y = np.array([[4, 1], [2, 2]])
print np.vdot(x, y)
print np.vdot(y, x)
print np.dot(x.flatten(), y.flatten())
print np.inner(x.flatten(), y.flatten())
print (x*y).sum()
30
30
30
30
30
Q4. Predict the results of the following code.
x = np.array(['a', 'b'], dtype=object)
y = np.array([1, 2])
print np.inner(x, y)
print np.inner(y, x)
print np.outer(x, y)
print np.outer(y, x)
abb
abb
[['a' 'aa']
['b' 'bb']]
[['a' 'b']
['aa' 'bb']]
Q5. Get the lower-trianglular L
in the Cholesky decomposition of x and verify it.
x = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]], dtype=np.int32)
L = np.linalg.cholesky(x)
print L
assert np.array_equal(np.dot(L, L.T.conjugate()), x)
[[ 2. 0. 0.]
[ 6. 1. 0.]
[-8. 5. 3.]]
Q6. Compute the qr factorization of x and verify it.
x = np.array([[12, -51, 4], [6, 167, -68], [-4, 24, -41]], dtype=np.float32)
q, r = np.linalg.qr(x)
print "q=\n", q, "\nr=\n", r
assert np.allclose(np.dot(q, r), x)
q=
[[-0.85714287 0.39428571 0.33142856]
[-0.42857143 -0.90285712 -0.03428571]
[ 0.2857143 -0.17142858 0.94285715]]
r=
[[ -14. -21. 14.]
[ 0. -175. 70.]
[ 0. 0. -35.]]
Q7. Factor x by Singular Value Decomposition and verify it.
x = np.array([[1, 0, 0, 0, 2], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0]], dtype=np.float32)
U, s, V = np.linalg.svd(x, full_matrices=False)
print "U=\n", U, "\ns=\n", s, "\nV=\n", v
assert np.allclose(np.dot(U, np.dot(np.diag(s), V)), x)
U=
[[ 0. 1. 0. 0.]
[ 1. 0. 0. 0.]
[ 0. 0. 0. -1.]
[ 0. 0. 1. 0.]]
s=
[ 3. 2.23606801 2. 0. ]
V=
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
Q8. Compute the eigenvalues and right eigenvectors of x. (Name them eigenvals and eigenvecs, respectively)
x = np.diag((1, 2, 3))
eigenvals = np.linalg.eig(x)[0]
eigenvals_ = np.linalg.eigvals(x)
assert np.array_equal(eigenvals, eigenvals_)
print "eigenvalues are\n", eigenvals
eigenvecs = np.linalg.eig(x)[1]
print "eigenvectors are\n", eigenvecs
eigenvalues are
[ 1. 2. 3.]
eigenvectors are
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
Q9. Predict the results of the following code.
print np.array_equal(np.dot(x, eigenvecs), eigenvals * eigenvecs)
True
Q10. Calculate the Frobenius norm and the condition number of x.
x = np.arange(1, 10).reshape((3, 3))
print np.linalg.norm(x, 'fro')
print np.linalg.cond(x, 'fro')
16.8819430161
4.56177073661e+17
Q11. Calculate the determinant of x.
x = np.arange(1, 5).reshape((2, 2))
out1 = np.linalg.det(x)
out2 = x[0, 0] * x[1, 1] - x[0, 1] * x[1, 0]
assert np.allclose(out1, out2)
print out1
-2.0
Q12. Calculate the rank of x.
x = np.eye(4)
out1 = np.linalg.matrix_rank(x)
out2 = np.linalg.svd(x)[1].size
assert out1 == out2
print out1
4
Q13. Compute the sign and natural logarithm of the determinant of x.
x = np.arange(1, 5).reshape((2, 2))
sign, logdet = np.linalg.slogdet(x)
det = np.linalg.det(x)
assert sign == np.sign(det)
assert logdet == np.log(np.abs(det))
print sign, logdet
-1.0 0.69314718056
Q14. Return the sum along the diagonal of x.
x = np.eye(4)
out1 = np.trace(x)
out2 = x.diagonal().sum()
assert out1 == out2
print out1
4.0
Q15. Compute the inverse of x.
x = np.array([[1., 2.], [3., 4.]])
out1 = np.linalg.inv(x)
assert np.allclose(np.dot(x, out1), np.eye(2))
print out1
[[-2. 1. ]
[ 1.5 -0.5]]
参考
作者:Kyubyong
来源:https://github.com/Kyubyong/numpy_exercises
下载地址:
https://github.com/fengdu78/Data-Science-Notes/tree/master/2.numpy/numpy_exercises
文末福利
各位猿们,还在为记不住API发愁吗,哈哈哈,最近发现了国外大师整理了一份Python代码速查表和Pycharm快捷键sheet,火爆国外,这里分享给大家。
这个是一份Python代码速查表
下面的宝藏图片是2张(windows && Mac)高清的PyCharm快捷键一览图
怎样获取呢?可以添加我们的AI派团队的程序媛姐姐
一定要备注【高清图】哦
????????????????????
➕我们的程序媛小姐姐微信要记得备注【高清图】哦
来都来了,喜欢的话就请分享、点赞、在看三连再走吧~~~