numpy中文手册_快速掌握NumPy

0a517b8ca8dfcada37efce6fc032d035.png

NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。本文收集了numpy常用手册链接,速查表还有我写的入门实例代码。


手册和学习网站

Quickstart tutorial​numpy.org
2de1c37ce8e326d188342b32a65c9fcd.png
NumPy 参考手册 | NumPy 中文​www.numpy.org.cn NumPy 教程 | 菜鸟教程​www.runoob.com
07ff4bde3de89486ed43ba3da64897b7.png


NumPy Cheat Sheet

97cf8af939e02e1b590b38927ef43951.png


实例代码

import numpy as np
import time
import numpy.matlib
# import numpy.linalg
# a. create a comment

# b. create a 1*4 row vector
b = np.zeros(4)
# 	print(b, b.shape)
# c. create a 5*1 column vector
c = np.array([0, 1, 2, 3, 4]).reshape(5, 1)
# 	print(c, c.shape)
# d. create a zero matrix using a function provided by numpy
d = np.zeros((4, 4))
# 	print(d, d.shape)
# e. print the second row of an 4*3 array
e = np.arange(12).reshape(4, 3)
# 	print(e[1, :])
# f. print the third column of an 4*4 array.
f = np.arange(16).reshape(4, 4)
# 	print(f[:, 2])
# g. transpose an array.
g = np.arange(10, 25, 2).reshape(2, 4).T
# 	print(g)
# h. create two array of equal size m*m. multiply them once using conventional matrix
# multiplication and once using elementwise multiplication.
h1 = np.arange(9).reshape(3, 3)
h2 = np.arange(9, 18).reshape(3, 3)
# 	print(h1, h2)
h3 = h1*h2
# 	print(h3)
h4 = np.multiply(h1, h2)
# 	print(h4)
# i. Concate two arrays vertically, as well as horizontally
i1 = np.arange(4)
i2 = np.arange(4, 8)
i3 = np.append(i1, i2)
i4 = np.concatenate((i1, i2), axis=0)
i5 = np.vstack((i1, i2))
i6 = np.hstack((i1, i2))
# 	print(i4, i5, i6)
# j. print the size of an array
j = np.arange(10).reshape(2, 5)
# 	print(j.size)
# k. change the structure of a 8*7 array to 14*4.
k = np.arange(56).reshape(8, 7)
k1 = k.reshape(14, 4)
# 	print(k, k1)
# l. replicate a 3*1 vector to an array of size 3*1000o
l1 = np.arange(3).reshape(3, 1)
# 	print(l1)
l2 = l1.repeat(1000).reshape(3, 1000)
# 	print(l2)
# m. replace all matrix entries less than 0 by 0.
m = np.arange(-5, 5)
# 	print(m)
m1 = np.maximum(m, 0)
# 	print(m1)
# n. create a vector containing numbers from 1 to 100 with a gap of 7 between the numbers.
n = np.arange(1, 100, 7).T
# 	print(n)
# o. create a vector with 100 entries. set every second element to 0.
o = np.arange(1, 100).T
o[1::2] = 0
# 	print(o)
# p. create a vector with 100 entries. delete every second element.
p = np.arange(100).T
p1 = np.delete(p, p[1::2])
# 	print(p1)
# q. create 2 arrays a,b of size 1000*3 containing random numbers.
q1 = np.random.rand(1000, 3)
q2 = np.random.rand(1000, 3)

# r. You can interprete the rows of such a arrays as vectors of size 1 × 3.
# Compute the dot product of those vectors using loops.
# This means you have to iterate over the rows of the 1000 × 3 array and
# compute the dot product between the vectors represented by the current array row.
i = 0
r1 = np.zeros(1000).reshape(1000, 1)
r2 = np.zeros(1000).reshape(1000, 1)

start_time = time.time()
while i <= 999:
	r1[i] = np.dot(q1[i], q2[i])
	i = i+1
# print(r1)
end_time = time.time()
print(end_time-start_time)

# s. Now, try to compute the dot product without using loops.
# Compare the run times of your implementation (Hint: To measure the runtime,
# you can use the code snipped below or in ipython %timeit. Did you recognize something,
# while comparing the times (loops vs. no loops)?
start_time = time.time()
r2 = np.sum(q1*q2, axis=1).reshape(1000, 1)
# print(r2)
end_time = time.time()
print(end_time-start_time)

# t. The following scenario is given. We want to invert 1000 2 × 2 matrices.
# The 2 × 2 matrices are represented by a row (see Figure 1).
# The numbers written as subscripts represent the position of the original matrix entry of the 2 × 2 matrices.
# The superscript denotes the current array n ∈ [1 . . . 1000].
# Now, create an array with a size of 1000×4 using the command rand.
# Every row in that matrix represents a 2 × 2 matrix (compare Figure 1).
# Note, that the memory layout of the 2 × 2 matrices must not be changed.
# That means we don’t want to change the current structure of the 2 × 2 matrices represented as a row.
# Don’t change the 1 × 4 matrices to 2×2 matrices and don’t change the inv command.
# We can now use Cramer’s Rule to compute the inverse of our 1000, as row represented, 2 × 2 matrices.
# Compute the inverses of the created 2 × 2 matrices without using any loops.
T = np.matlib.rand(1000, 4)
T_inv = np.matlib.empty((1000, 4))
T_star = np.matlib.empty((1000, 4))
i = 0
while i <= 999:
	det = T[i, 0]*T[i, 3]-T[i, 1]*T[i, 2]
	T_star[i, :] = [T[i, 3], -T[i, 1], -T[i, 2], T[i, 0]]
	T_inv[i, :] = T_star[i, :] / det
	i = i+1
print('t. invert 1000 2*2 matrices :n', T_inv)


# u. Write a function, which accepts 2 m × m matrices.
# The function should be able to compute the sum as well as the product of both matrices.
def mat_sum_and_product(a, b):
	if (a.shape[0] == a.shape[1]) and (a.shape == b.shape):
		mat_sum = a+b
		mat_product = np.dot(a, b.T)
		print(' the sum and product of mat a and b is n {0} n and n {1}'.format(mat_sum, mat_product))
	else:
		print('input error')


A = np.arange(4).reshape(2, 2)
B = np.arange(4, 8).reshape(2, 2)
mat_sum_and_product(A, B)



本人博客:

Zkpeace​zkpeace.com
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: scipy和numpy都是Python中非常常用的科学计算库或模块。scipy包含了许多用于科学计算和数据分析的函数和工具,而numpy则是一个用于进行数值计算的库。 scipy和numpy参考手册是官方提供的用于介绍和提供使用指南的文档。这些参考手册以pdf格式提供,方便用户进行下载和打印阅读。 这些参考手册的语言通常简练明了,结构清晰,适合用作学习和查找函数或方法的参考。它们通常包含了对每个模块或子模块的详细描述,包括函数、类和其他对象的用法、参数、示例和文档等。这些参考手册还提供了一些实践中常见问题的解决方案,以及一些示例代码,帮助用户更好地理解如何使用这些库来解决实际问题。 对于想要深入学习和掌握scipy和numpy的用户来说,这些参考手册非常有用。不仅可以通过阅读这些手册来了解库的各种功能和用法,还可以作为用于解决具体问题时的参考工具。这些手册还经常更新,以跟随库的最新变化和新增功能,所以及时下载最新版本的手册也是非常有益的。 总之,scipy和numpy参考手册pdf是一份用于详细介绍和指导如何使用这两个库的文档。它们提供了丰富的信息和资源,帮助用户更好地理解和利用这些强大的科学计算库。 ### 回答2: Scipy和Numpy是Python中常用的科学计算库,它们提供了许多有用的函数和工具,用于处理和分析大规模的数据集。对于想要学习和使用这两个库的用户来说,参考手册是非常有用的资源。 Scipy和Numpy参考手册 pdf是一本详细介绍了Scipy和Numpy库中所有函数、类、方法及其参数和用法的电子书。该参考手册总结了库的主要功能和特性,并提供了实际应用示例和代码片段。 这本参考手册包含了Scipy和Numpy的绝大部分功能,例如线性代数、信号处理、数值优化、傅里叶变换等等。通过这本参考手册,用户可以快速查找到某个函数或方法的具体用法和参数设置,避免了大量的查找和试错过程。 由于Scipy和Numpy是Python的重要库,所以这本参考手册非常有价值。它可以帮助用户更好地理解和掌握这两个库的使用方法,提高数据处理和分析的效率。对于科研工作者、数据分析师、工程师等使用Python进行科学计算的用户来说,这本参考手册是不可或缺的工具。 总之,Scipy和Numpy参考手册 pdf对于学习和使用Scipy和Numpy库的用户来说是一本非常有用的资源,它提供了详细的文档和示例代码,帮助用户更好地理解和应用这两个库的功能。 ### 回答3: SciPy和NumPy是用于科学计算的两个重要的Python库。它们有广泛的功能和用途,并且为用户提供了大量的函数和工具来进行高效的数值计算、数据处理和科学建模等任务。 关于SciPy和NumPy的参考手册,PDF格式的文档是一个非常有用的资源。它们可以帮助用户深入了解这两个库的特性、用法和常见的问题解决方法。 这些PDF参考手册通常包含了针对每个函数的详细说明,包括函数的参数、返回值和用法示例等。它们还提供了库的整体结构和组件之间的关系,以便用户可以更好地理解它们的内部工作原理。 通过阅读这些参考手册,用户可以更容易地理解和掌握SciPy和NumPy的功能,并且可以更好地将它们应用于自己的科学计算项目中。用户可以根据需要在手册中进行搜索和查找,以找到特定函数或概念的详细信息。 总之,SciPy和NumPy的PDF参考手册为用户提供了方便和实用的资源,可以帮助他们更好地使用这两个库进行科学计算和数据处理。通过阅读这些手册,用户可以更好地掌握这些库的知识和技能,从而提高他们在科学计算领域的能力和效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值