import numpy as np
'''
下面是numpy的基本语法
'''
a=np.array([1,2,3,4]);
b=np.array([5,6,7,8]);
c=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]);
c.dtype;
c.shape;
c.shape=2,6;
c.shape=4,-1;
d=c.reshape((4,3));
c[0][0]=100;
e=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]],dtype=np.float);
f=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]],dtype=np.complex);
g = np.arange(0,1,0.1);
g=np.arange(10); 等价于g=np.arange(0,10,1);
h=np.linspace(0,1,10);
l=np.linspace(0,1,10,endpoint=False);
m=np.logspace(0,2,20);
n=np.ones((2,3),np.int);
o=np.zeros((2,3),np.float);
p=np.empty((2,3),np.int);
q=np.zeros_like(a);
q=np.ones_like(a);
q=np.empty_like(a);
def func(i,j):
return (i+1)*(j+1);
a=np.fromfunction(func,(9,9));
print(a)
a=np.array([0,1,100,101,4,5,6,7,8,9]);
print(a[2]);
print(a[1:3]);
print(a[:3]);
print(a[:-1]);
a[1:3]=100,101;
print(a);
print(a[1:-1:2]);
print(a[::-1]);
print(a[5:1:-2]);
x=np.arange(10,1,-1);
y=x[[3,3,-1,8]];
y[2]=100;
print(y);
print(x);
x[[3,5,-1]]=-1,-2,-3;
print(x);
x=np.arange(1,6,1);
print(x);
print(x[np.array([True,False,True,False,False])]);
print(x[[True,False,True,False,False]]);
x[np.array([True,False,True,False,False])]=-1,-2;
print(x);
a=np.array([[1,2,3,4],[1,2,3,4],[1,2,3,4]])
print(a[:,1]);
print(a[0,:]);