Numpy练习
>>> import numpy as np
>>> import pandas as pd
>>> a=np.array([4,5,6])
>>> type(a)
>>> a.shape
(3,)
>>> a[0]
4
>>> b=np.array([[4,5,6],[1,2,3]])
>>> b.shape
(2, 3)
>>> b[0,0],b[0][0]
(4, 4)
>>> b[0,1]
5
>>> b[0,1],b[0][1]
(5, 5)
>>> b[1,1],b[1][1]
(2, 2)
>>> a=np.zeros((3,3),dtype=np.int);a
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
>>> a=np.zeros((3,3),dtype=np.int)
>>> a=np.zeros((3,3),dtype=np.int);a
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
>>> b=np.ones((4,5));b
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
>>> cl=np.identity(4);cl
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
>>> c2=np.diag([1]*4,);c2
array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
>>> d=np.random.random((3,2));d
array([[0.69696963, 0.2557797 ],
[0.04526759, 0.79868102],
[0.07012425, 0.20989641]])
>>> a=np.reshape(np.linspace(1,12,12,dtype=np.int),(3,4));a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> a[2,3],a[0,0]
(12, 1)
>>> b=a[0:2,2:4]
>>> b
array([[3, 4],
[7, 8]])
>>> b[0,0]
3
>>> c=a[1:][:];c
array([[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> c[0,-1]
8
>>> a=np.array([1,2][3,4],[5,6])
Traceback (most recent call last):
File "", line 1, in
a=np.array([1,2][3,4],[5,6])
TypeError: list indices must be integers or slices, not tuple
>>> a=np.array([[1,2][3,4],[5,6]])
Traceback (most recent call last):
File "", line 1, in
a=np.array([[1,2][3,4],[5,6]])
TypeError: list indices must be integers or slices, not tuple
>>> a=np.array([[1,2],[3,4],[5,6]])
>>> print(a[0,0],a[1,1],a[2,0])
1 4 5
>>> print(a[[0,1,2],[0,1,0]])
[1 4 5]
>>> a=np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
>>> b=np.array([0,2,0,1])
>>> print(a[np.arange(4),b])
[ 1 6 7 11]
>>> a[np.arange(4),b]+=10;a
array([[11, 2, 3],
[ 4, 5, 16],
[17, 8, 9],
[10, 21, 12]])
>>> x=np.array([1,2]);x.dtype
dtype('int64')
>>> x=np.array([1.0,2.0]);x.dtype
dtype('float64')
>>> x=np.array([[1,2],[3,4]],dtype=np.float64);y=np.array([[5,6],[7,8]],dtype=np.float64)
>>> x+y
array([[ 6., 8.],
[10., 12.]])
>>> np.add(x,y)
array([[ 6., 8.],
[10., 12.]])
>>> x-y
array([[-4., -4.],
[-4., -4.]])
>>> np.subtract(x,y)
array([[-4., -4.],
[-4., -4.]])
>>> x*y
array([[ 5., 12.],
[21., 32.]])
>>> np.multiply(x,y)
array([[ 5., 12.],
[21., 32.]])
>>> np.dot(x,y)
array([[19., 22.],
[43., 50.]])
>>> x1=np.reshape(np.arange(1,7),(2,3))
>>> y1=np.reshape(np.arange(1,7),(3,2))
>>> x1,y1
(array([[1, 2, 3],
[4, 5, 6]]), array([[1, 2],
[3, 4],
[5, 6]]))
>>> x1*y1
Traceback (most recent call last):
File "", line 1, in
x1*y1
ValueError: operands could not be broadcast together with shapes (2,3) (3,2)
>>> np.multiply(x,y)
array([[ 5., 12.],
[21., 32.]])
>>> np.multiply(x1,y1)
Traceback (most recent call last):
File "", line 1, in
np.multiply(x1,y1)
ValueError: operands could not be broadcast together with shapes (2,3) (3,2)
>>> np.dot(x1,y1)
array([[22, 28],
[49, 64]])
>>> x/y
array([[0.2 , 0.33333333],
[0.42857143, 0.5 ]])
>>> np.divide(x,y)
array([[0.2 , 0.33333333],
[0.42857143, 0.5 ]])
>>> x**(1/2)
array([[1. , 1.41421356],
[1.73205081, 2. ]])
>>> np.sqrt(x)
array([[1. , 1.41421356],
[1.73205081, 2. ]])
>>> print(x.dot(y))
[[19. 22.]
[43. 50.]]
>>> print(np.dot(x,y))
[[19. 22.]
[43. 50.]]
>>> print(np.sum(x))
10.0
>>> print(np.sum(x,axis=0))
[4. 6.]
>>> print(np.sum(x,axis=1))
[3. 7.]
>>> print(np.mean(x))
2.5
>>> print(np.mean(x,axis=0))
[2. 3.]
>>> print(np.mean(x,axis=1))
[1.5 3.5]
>>> x.T
array([[1., 3.],
[2., 4.]])
>>> np.exp(x)
array([[ 2.71828183, 7.3890561 ],
[20.08553692, 54.59815003]])
>>> print(np.argmax(x))
3
>>> print(np.argmax(x,axis=0))
[1 1]
>>> print(np.argmax(x,axis=1))
[1 1]
>>> import matplotlib.pyplot as plt
>>> x=np.arrange(0,100,0.1)
Traceback (most recent call last):
File "", line 1, in
x=np.arrange(0,100,0.1)
AttributeError: module 'numpy' has no attribute 'arrange'
>>> x=np.arange(0,100,0.1)
>>> y=x*x
>>> plt.plot(x,y)
[]
>>> plt.show()
>>> x=np.arange(1,3*np.pi,0.1)
>>> plt.plot(x,np.sin(x))
[]
>>> plt.plot(x,np.cos(x))
[]
>>> plt.show()
>>> print(0*np.nan)
nan
>>> print(np.nan==np.nan)
False
>>> print(np.inf>np.nan)
False
>>> print(np.nan-np.nan)
nan
>>> print(0.3==3***0.1)
SyntaxError: invalid syntax
>>> print(0.3==3**0.1)
False
>>>