Logout3. Indexing, Slicing and Iterating Last Checkpoint: Yesterday at 3:05 PM (autosaved)
Python 3
Not Trusted
File
Edit
View
Insert
Cell
Kernel
Widgets
Help
In [1]:
# 这一小节我们来讲 np array 的选取,我们先来看看一维array的选取,然后我们进行二维以及多维 array的选取
One-dimensional arrays.
In [ ]:
In [2]:
import numpy as np #
In [3]:
a = np.arange(10)**2 #0~9对应位置求平方
a
Out[3]:
array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81], dtype=int32)
In [4]:
# 我们从0开始数 0,1,2,3,4,5 第5个位置 对应的元素是 25
In [5]:
a[2] #切片
Out[5]:
4
In [6]:
# [2:6] 的意思是 选取位置是 2,3,4, 5 这四个元素,第六个元素不取
In [7]:
a[2:6] #取一段,定义开始和结束,取不到结尾
Out[7]:
array([ 4, 9, 16, 25], dtype=int32)
In [8]:
a[0:3] = 100 #取某一段进行赋值
a
Out[8]:
array([100, 100, 100, 9, 16, 25, 36, 49, 64, 81], dtype=int32)
In [9]:
a_slice = a[0:5] #切片,a_slice只是变量的名称
a_slice
Out[9]:
array([100, 100, 100, 9, 16], dtype=int32)
In [10]:
# 注意: 当我们选取部分 np array 的时候,我们创建了一个view。
# 换句话说,我们没有copy以前的值,而是直接改了原来的 array
a_slice[0] = 233
print(a_slice)
print(a) #原值被改变,母体被样本影响
[233 100 100 9 16]
[233 100 100 9 16 25 36 49 64 81]
In [11]:
# 如果不想对原来的 array 进行修改,我们可以把选取的部分 copy 出来
a_slice_2 = a[0:5].copy() #
print(a_slice_2)
[233 100 100 9 16]
In [12]:
a_slice_2[0] = 500
print(a_slice_2) #对母体不会产生影响
print(a)
[500 100 100 9 16]
[233 100 100 9 16 25 36 49 64 81]
Multidimensional arrays.
In [13]:
def f(x,y):
return 4 * x + y #4个空格,或者使用tab键 x y
b = np.fromfunction(f,(3,2),dtype=int) #fromfunction 前面定义为函数,(3,2)表示三行两列, 0 0 01
b # 1 0 11, 00:4x+y=0,11:4x+y=5
# derive from locations of the matrix
#[[00],[01],[10],[11],[20],[21]]
Out[13]:
array([[0, 1],
[4, 5],
[8, 9]])
In [14]:
# def f(x,y):
# return 4 * x + 2*y #4个空格,或者使用tab键 x y
# b = np.fromfunction(f,(3,3),dtype=int) #fromfunction 前面定义为函数,(3,2)表示三行两列, 0 0 01
# b # 1 0 11, 00:4x+y=0,11:4x+y=5
# # derive from locations of the matrix
# #[[00],[01],[10],[11],[20],[21]]
In [15]:
b[1]
Out[15]:
array([4, 5])
In [16]:
b[1][0]
Out[16]:
4
In [17]:
b[1,0]
Out[17]:
4
In [18]:
b[:, 1] #不写行,表示每一行都取,1表示取列
Out[18]:
array([1, 5, 9])
In [19]:
c = np.zeros ([5,5]) #全0方阵
c.shape #(5,5)
# c
Out[19]:
(5, 5)
In [20]:
column_length = c.shape[1] # c.shape是(5,5),只能取到下标索引1
column_length
Out[20]:
5
In [38]:
for i in range(column_length): # c是全0矩阵 column_length=5
c[i] = i
# print(i)
print(c[i])
[0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1.]
[2. 2. 2. 2. 2.]
[3. 3. 3. 3. 3.]
[4. 4. 4. 4. 4.]
In [36]:
for i in range(5): #for循环
print(i)
0
1
2
3
4
In [39]:
for i in range(5):
c[i]=i+1
c
Out[39]:
array([[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4.],
[5., 5., 5., 5., 5.]])
In [40]:
c
Out[40]:
array([[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4.],
[5., 5., 5., 5., 5.]])
Boolean Indexing
In [41]:
week_days=np.array(['Monday','Tuesday','Wednesday','Thursday','Friday'])
In [46]:
# print(np.random.randn(5,5)+8.0)
In [42]:
work_time=np.round(np.random.randn(5,5)+8.0, 2) #内部的random创建五行五列,2表示round是两位小数
In [43]:
work_time #竖着看是从周一到周五,横着看是对应的每一个人
Out[43]:
array([[9.36, 8.87, 6.97, 6.83, 7.5 ],
[6.78, 7.6 , 8.21, 7.58, 8.3 ],
[7.87, 6.55, 7.3 , 7.61, 8.53],
[8.24, 7.66, 9.29, 8.75, 8.38],
[8.09, 9.23, 6.61, 7.7 , 9.14]])
In [48]:
week_days == 'Tuesday'
Out[48]:
array([False, True, False, False, False])
In [52]:
work_time.T[week_days == 'Tuesday']
Out[52]:
array([[8.87, 7.6 , 6.55, 7.66, 9.23]])
In [55]:
work_time[week_days=="Friday"]
Out[55]:
array([[8.09, 9.23, 6.61, 7.7 , 9.14]])
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Logout4. Matrix Operations II Last Checkpoint: 6 hours ago (autosaved)
Python 3
Not Trusted
File
Edit
View
Insert
Cell
Kernel
Widgets
Help
In [5]:
# 在这一讲里,我们重点看看 numpy 对于矩阵有哪些操作
In [1]:
import numpy as np
In [2]:
a = np.array([[1,2,3],[3,4,5]],dtype='float')
a
Out[2]:
array([[1., 2., 3.],
[3., 4., 5.]])
In [3]:
a.shape
Out[3]:
(2, 3)
In [4]:
a.T # a.T 的意思是 把所有的维度直接反过来: 比如原来的形状是 [1,2,4] 直接反过来变成 [4,2,1]
Out[4]:
array([[1., 3.],
[2., 4.],
[3., 5.]])
In [5]:
a = np.array([[[1,2,3,0],[3,4,5,2]]])
a
Out[5]:
array([[[1, 2, 3, 0],
[3, 4, 5, 2]]])
In [6]:
a.shape # 1表示是堆,在堆里面分析行与列
Out[6]:
(1, 2, 4)
In [13]:
a.transpose([0,1,2]) #0是堆,1是行,2是列
Out[13]:
array([[[1, 2, 3, 0],
[3, 4, 5, 2]]])
In [16]:
a.transpose([0,2,1])
Out[16]:
array([[[1, 3],
[2, 4],
[3, 5],
[0, 2]]])
In [17]:
a.T
Out[17]:
array([[[1],
[3]],
[[2],
[4]],
[[3],
[5]],
[[0],
[2]]])
In [18]:
a.T.shape #(4,2,1)全部都交换,一共是4堆,对于第一堆中一共两行一列
Out[18]:
(4, 2, 1)
In [19]:
a.transpose() #a.transpose可以指定到底要怎么变换:比如原来是 [1,2,4],
#可以指定转变的方式 [0,2,1], 这个 [0,2,1] 的意思是 第一个维度不变,后两个维度交换
# 那么形状就变成了 [1,4,2]
#也是转置,比T更灵活
Out[19]:
array([[[1],
[3]],
[[2],
[4]],
[[3],
[5]],
[[0],
[2]]])
In [24]:
q=np.array([[1,2],[3,4]],dtype="int")
q
Out[24]:
array([[1, 2],
[3, 4]])
In [25]:
np.dot(q.T,q)
Out[25]:
array([[10, 14],
[14, 20]])
In [33]:
# 求逆矩阵 相乘等于1
# This calculates the inverse of a matrix
np.linalg.inv(np.dot(q.T,q))
Out[33]:
array([[ 5. , -3.5],
[-3.5, 2.5]])
In [28]:
# 求是否相同
# Returns True if two arrays are element-wise equal within a tolerance : default 1e-08
np.allclose? #无限接近
Out[28]:
<function numpy.allclose>
In [30]:
np.linalg.inv(q)
Out[30]:
array([[-2. , 1. ],
[ 1.5, -0.5]])
In [34]:
import numpy as np
from numpy.linalg import inv
In [35]:
from numpy.linalg import inv
a = np.array([[1., 2.], [3., 4.]])
ainv = inv(a)
# here is to check if np.dot(a, ainv) equals to I matrix
np.allclose(np.dot(a, ainv), np.eye(2)) #逆矩阵写在前面与后面是一样的
np.allclose(np.dot(ainv, a), np.eye(2)) #eye() 几行几列
Out[35]:
True
In [36]:
from numpy.linalg import *
In [7]:
# return eigenvalues and normalized eigenvectors
np.linalg.eig(np.dot(a.T,a)) #特征值
Out[7]:
(array([ 6.36227766e+01, 3.77223398e-01, -3.19802545e-15]),
array([[-0.39133557, -0.8247362 , 0.40824829],
[-0.5605708 , -0.13817999, -0.81649658],
[-0.72980603, 0.54837623, 0.40824829]]))
In [8]:
a = np.array([[7., 2.], [3., 4.]])
In [12]:
# np.trace? # 算对角的和
np.trace? # 算对角的和
Out[12]:
<function numpy.trace>
In [37]:
np.trace(a)
Out[37]:
5.0
In [32]:
a3d = np.arange(50).reshape([5,5,2])
a3d
Out[32]:
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9]],
[[10, 11],
[12, 13],
[14, 15],
[16, 17],
[18, 19]],
[[20, 21],
[22, 23],
[24, 25],
[26, 27],
[28, 29]],
[[30, 31],
[32, 33],
[34, 35],
[36, 37],
[38, 39]],
[[40, 41],
[42, 43],
[44, 45],
[46, 47],
[48, 49]]])
In [10]:
a3d.transpose([0,2,1]) # .reshape([5,2,5]) 变形装
Out[10]:
array([[[ 0, 2, 4, 6, 8],
[ 1, 3, 5, 7, 9]],
[[10, 12, 14, 16, 18],
[11, 13, 15, 17, 19]],
[[20, 22, 24, 26, 28],
[21, 23, 25, 27, 29]],
[[30, 32, 34, 36, 38],
[31, 33, 35, 37, 39]],
[[40, 42, 44, 46, 48],
[41, 43, 45, 47, 49]]])
In [38]:
a
Out[38]:
array([[1., 2.],
[3., 4.]])
In [25]:
a.swapaxes(0,1) #????????????????
Out[25]:
array([[ 1., 3.],
[ 2., 4.]])
In [9]:
a
Out[9]:
array([[ 1., 2., 3.],
[ 3., 4., 5.]])
In [2]:
np.bincount(np.array([0, 1, 1, 3, 2, 1, 7])) #数格子,第一个0的位置中0只出现一次,第二个位置是 1出现了三次,第三个位置是2出现了一次
#后面的第五六都没有出现0次,所以写0
Out[2]:
array([1, 3, 1, 1, 0, 0, 0, 1])
In [27]:
np.bincount(np.array([1, 1, 2, 10, 2, 4, 7]))
Out[27]:
array([0, 2, 2, 0, 1, 0, 0, 1, 0, 0, 1], dtype=int64)
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Logout5. Universal Functions Last Checkpoint: Yesterday at 4:03 PM (autosaved)
Python 3
Trusted
File
Edit
View
Insert
Cell
Kernel
Widgets
Help
In [2]:
import numpy as np
In [3]:
a = np.arange(10)
a
Out[3]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [4]:
np.exp(a) #指数
Out[4]:
array([1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01,
5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03,
2.98095799e+03, 8.10308393e+03])
In [5]:
np.sqrt(a) #开平方
Out[5]:
array([0. , 1. , 1.41421356, 1.73205081, 2. ,
2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])
In [6]:
# b = np.random.randn(10)
b = np.random.randn(10).reshape(2,5)
b
Out[6]:
array([[-0.52465291, -1.49418972, 0.51837126, 0.29193168, -0.90667532],
[-0.77537628, 1.57323198, -0.0245697 , 0.24121091, -0.86146164]])
In [7]:
c = np.random.randn(10).reshape(2,5)
c
Out[7]:
array([[-0.468604 , 0.45807946, -0.87776052, -0.31589996, -2.34399103],
[-1.37172141, -1.75014072, -1.08978736, -0.21885972, -1.33139784]])
In [8]:
# Binary Functions
np.add(b,c) # 行列不一样
Out[8]:
array([[-0.99325692, -1.03611026, -0.35938926, -0.02396828, -3.25066635],
[-2.14709769, -0.17690874, -1.11435706, 0.02235119, -2.19285949]])
2222222222
最新推荐文章于 2024-08-22 22:01:15 发布