第一个是学习一些python的库。首先学习的是numpy的库。
import numpy
vector =numpy.array([5,10,15,20])
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(vector)
print(matrix)
结果:
vector = numpy.array([1,2,3,4])
print(vector.shape)
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print(matrix.shape)结果:
(4,) (3, 3)shape是可以用来查询向量的基本情况,几行几列
numbers = numpy.array([1,2,3,4])
numbers.dtype
结果:
dtype('int32')dtype用来查询类型
vector =numpy.array([5,10,15,20])
print(vector[0:3])结果:
[ 5 10 15]
前包后不包
matrix = numpy.array([[5,10,15],
[20,25,30],
[35,40,45]])
print(matrix[:,1])
print(matrix[:,0:2])
结果:
[10 25 40] [[ 5 10] [20 25] [35 40]]第一个位置的:表示所有的行,第二个位置表示列。
vector =numpy.array([5,10,15,20])
vector ==10
结果:
array([False, True, False, False], dtype=bool)
查找向量中有没有10,有的话返回true,没有的话返回false,后面的是类型
vector =numpy.array([5,10,15,20])
equal_to_ten = (vector ==10)
print (equal_to_ten)
print(vector[equal_to_ten])
结果:
[False True False False] [10]与前面一样,然后在true的地方返回了10
matrix = numpy.array([[5,10,15],
[20,25,30],
[35,40,45]])
second_column_25 =(matrix[:,1]==25)
print(second_column_25)
print(matrix[second_column_25])
结果:
这是上面的方法的应用,由列找出了行(包含true的那一行)
vector = numpy.array(["1","2","3"])
print(vector.dtype)
print(vector)
vector = vector.astype(float)
print(vector.dtype)
print(vector)结果:
<U1 ['1' '2' '3'] float64 [ 1. 2. 3.]这是类型的转换,由String类型转变为float
matrix = numpy.array([[5,10,15],
[20,25,30],
[35,40,45]])
matrix.sum(axis = 1)结果:
array([ 30, 75, 120])sum是求和,axis=1表示的是行,axis=0,表示的是列
import numpy as np
a=np.arange(15).reshape(3,5)
a 结果:
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]) arange是一个数组,reshape是可以转化为一个向量,但是要确保可以被切割np.arange(10,30,5)结果:
array([10, 15, 20, 25]) 当不是一个变量时表示:从10开始每次加5,但是要小于30
np.random.random((2,3))
结果:
array([[ 0.84933299, 0.10524939, 0.85124946], [ 0.75002188, 0.27174962, 0.67449709]])随机生成0~1之前的数,不包含1from numpy import pi
np.linspace( 0, 2*pi, 100 )
结果:
array([ 0. , 0.06346652, 0.12693304, 0.19039955, 0.25386607, 0.31733259, 0.38079911, 0.44426563, 0.50773215, 0.57119866, 0.63466518, 0.6981317 , 0.76159822, 0.82506474, 0.88853126, 0.95199777, 1.01546429, 1.07893081, 1.14239733, 1.20586385, 1.26933037, 1.33279688, 1.3962634 , 1.45972992, 1.52319644, 1.58666296, 1.65012947, 1.71359599, 1.77706251, 1.84052903, 1.90399555, 1.96746207, 2.03092858, 2.0943951 , 2.15786162, 2.22132814, 2.28479466, 2.34826118, 2.41172769, 2.47519421, 2.53866073, 2.60212725, 2.66559377, 2.72906028, 2.7925268 , 2.85599332, 2.91945984, 2.98292636, 3.04639288, 3.10985939, 3.17332591, 3.23679243, 3.30025895, 3.36372547, 3.42719199, 3.4906585 , 3.55412502, 3.61759154, 3.68105806, 3.74452458, 3.8079911 , 3.87145761, 3.93492413, 3.99839065, 4.06185717, 4.12532369, 4.1887902 , 4.25225672, 4.31572324, 4.37918976, 4.44265628, 4.5061228 , 4.56958931, 4.63305583, 4.69652235, 4.75998887, 4.82345539, 4.88692191, 4.95038842, 5.01385494, 5.07732146, 5.14078798, 5.2042545 , 5.26772102, 5.33118753, 5.39465405, 5.45812057, 5.52158709, 5.58505361, 5.64852012, 5.71198664, 5.77545316, 5.83891968, 5.9023862 , 5.96585272, 6.02931923, 6.09278575, 6.15625227, 6.21971879, 6.28318531])0~2pi之间安装100均分A = np.array( [[1,1],
[0,1]] )
B = np.array( [[2,0],
[3,4]] )
print (A)
print (B)
#print A*B
print(A.dot(B))
print (np.dot(A, B))结果:
a = np.floor(10*np.random.random((3,4)))
print (a)
a.shape
## flatten the array
print (a.ravel())
#a.shape = (6, 2)
#print a
#print a.T
print (a.resize((2,6)))
print (a)
#If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
a.reshape(3,-1)
结果:
b = np.floor(10*np.random.random((2,2)))
print (a)
print ('---')
print (b)
print ('---')
print (np.hstack((a,b)))
#np.hstack((a,b))
结果:
[[ 0. 7.] [ 3. 1.]] --- [[ 9. 8.] [ 1. 4.]] --- [[ 0. 7. 9. 8.] [ 3. 1. 1. 4.]] hstack两个向量拼接a = np.floor(10*np.random.random((2,12)))
#print a
#print np.hsplit(a,3)
#print np.hsplit(a,(3,4)) # Split a after the third and the fourth column
a = np.floor(10*np.random.random((12,2)))
print (a)
np.vsplit(a,3)
结果:
[array([[ 5., 9.], [ 8., 0.], [ 2., 7.], [ 2., 2.]]), array([[ 3., 2.], [ 9., 1.], [ 4., 8.], [ 0., 2.]]), array([[ 8., 2.], [ 2., 3.], [ 9., 0.], [ 0., 1.]])]vsplit是安装行切分成3分#The copy method makes a complete copy of the array and its data.
d = a.copy()
d is a
d[0,0] = 9999
print (d)
print (a)
用了copy之后,两个向量就不会同时指向一个地址了,改变就没有事了
a = np.arange(0, 40, 10)
b = np.tile(a, (3, 5))
print (b)结果:
[[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]]生成一个3行5列的向量a = np.array([[4, 3, 5], [1, 2, 1]])
#print a
#b = np.sort(a, axis=1)
#print b
#b
#a.sort(axis=1)
#print a
a = np.array([4, 3, 1, 2])
j = np.argsort(a)
print (j)
print (a[j])结果:
[2 3 1 0] [1 2 3 4] argsort是指安装排序后的序号:例如:1最小,1的index为2.。。