Shinkai005

V1

2023/02/21阅读:16主题:极简黑

Numpy内容

ESC 进入 命令模式 Enter 进入编辑模式 命令模式下 M 进入 markdown Y 进入code

开始进入正题

  • 是 Numpy内容
import numpy as np
c = np.array([[1,2,3],[4,5,6]]) # np.array 接受 列表
c
array([[1, 2, 3],
       [4, 5, 6]])
d = np.array(((1,2,3),(4,5,6))) # np.array 接受 元素 tuples
d
array([[1, 2, 3],
       [4, 5, 6]])
g = np.array([['a','b'],['c','d']])
g
array([['a', 'b'],
       ['c', 'd']], dtype='<U1')
  • 打印出来的dtype ='<u1' 意思是 utf-8 一个字符
f = np.array([[1,2,3],[4,5,6]],dtype=complex) # 用 dtype来操作类型
f
array([[1.+0.j, 2.+0.j, 3.+0.j],
       [4.+0.j, 5.+0.j, 6.+0.j]])
np.zeros((3,3))
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
a= np.ones(3)
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Input In [128], in <cell line: 1>()
----> 1 a= np.ones(3)


TypeError: 'tuple' object is not callable
np.arange(0,10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,12,3)
array([0, 3, 6, 9])
np.arange(0,12).reshape(3,4# reshape改变数组维度
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
np.linspace(0,11,5# 前两个和arrange一样, 第三个是想要的元素个数. 结果会被平分
array([ 0.  ,  2.75,  5.5 ,  8.25, 11.  ])
a = np.random.random((3,3)) # 这个是np内置的, 专门用来生成随机数
a
array([[0.72131936, 0.95123781, 0.27715533],
       [0.67257842, 0.29871138, 0.78577955],
       [0.26620622, 0.13776566, 0.3184214 ]])
# 都可以修改类型
# a.astype(int)
# a.dtype="float16"
array([[ 1.068e+01, -1.721e+03, -6.508e+03,  1.974e+00, -1.978e+01,
         1.149e-01, -1.350e-03,  1.968e+00,  4.546e-01,  7.014e-04,
         1.447e-04,  1.981e+00],
       [-7.075e-01,  1.519e-01,  9.685e-04,  1.970e+00,  2.179e+04,
         4.488e-05,  2.898e+02,  1.962e+00,  7.117e-02, -3.964e-05,
               nan,  1.953e+00],
       [ 1.639e+00,  8.306e-01,  9.787e-05,  1.963e+00, -1.850e+02,
        -6.145e+00,  4.834e-05,  1.935e+00, -2.383e+01, -1.457e+03,
        -3.754e+04,  1.970e+00]], dtype=float16)

Arithmetic 算法

a = np.arange(4)
a
array([0, 1, 2, 3])
a+4 
# 每个位置都加了4
array([4, 5, 6, 7])
a*2
# 每个位置都乘2
array([0, 2, 4, 6])
b = np.arange(4,8)
b
array([4, 5, 6, 7])
a+b
# 每个位置相加
array([ 4,  6,  8, 10])
a-b
array([-4, -4, -4, -4])
a*b
array([ 0,  5, 12, 21])
a*np.sin(b)
array([-0.        , -0.95892427, -0.558831  ,  1.9709598 ])
a*np.sqrt(b)
array([0.        , 2.23606798, 4.89897949, 7.93725393])
A=np.arange(0,9).reshape(3,3)
A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
B=np.ones((3,3))
B
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
A*B
array([[0., 1., 2.],
       [3., 4., 5.],
       [6., 7., 8.]])

Matrix 矩阵 matrices. 矩阵复数 matrix product 矩阵积 This operation is not element-wise 不是按元素排列的

np.dot(A,B)
array([[ 3.,  3.,  3.],
       [12., 12., 12.],
       [21., 21., 21.]])

Transpose - Trace - Inverse 转置,迹, 取逆

# 转置transpose是切换行和列实现的
A  = np.array([[15,35,45],[60,59,67],[50,78,99]])
print(A)
print()
print(A.T)
print()
print(A.transpose())
print()
print(A)
# 两种方式 A.T 和 A.transpose() 都不改变原数组
[[15 35 45]
 [60 59 67]
 [50 78 99]]

[[15 60 50]
 [35 59 78]
 [45 67 99]]

[[15 60 50]
 [35 59 78]
 [45 67 99]]

[[15 35 45]
 [60 59 67]
 [50 78 99]]
# 迹trace是斜对角元素的和
print(np.trace(A))
173
# 逆, 一个矩阵和他的逆矩阵相乘等于单位矩阵.
print(np.linalg.inv(A))
[[-0.17202797 -0.01258741  0.08671329]
 [ 0.72447552  0.21398601 -0.47412587]
 [-0.48391608 -0.16223776  0.33986014]]

Increment and Decrement Operators 递增递减操作符

  • python 没有++/--运算符
  • python 自增自减需要使用 += -=
# shape 函数 可以改变形状, 维度
a = np.random.random(12)
a
array([0.46561932, 0.60702979, 0.68140217, 0.04024   , 0.52607732,
       0.11197173, 0.79977183, 0.06455059, 0.18500467, 0.22661201,
       0.97044627, 0.04624839])
# A = a.reshape(3,5) # 会报错不能把size=12的元素放到shape(3,5)里
A = a.reshape(3,4)
print(A.size)
A
12





array([[0.46561932, 0.60702979, 0.68140217, 0.04024   ],
       [0.52607732, 0.11197173, 0.79977183, 0.06455059],
       [0.18500467, 0.22661201, 0.97044627, 0.04624839]])
a.shape=(3,4)
a
array([[0.46561932, 0.60702979, 0.68140217, 0.04024   ],
       [0.52607732, 0.11197173, 0.79977183, 0.06455059],
       [0.18500467, 0.22661201, 0.97044627, 0.04624839]])
# reshape 是返回一个新矩阵, shape改变原矩阵
a = a.ravel()
a
# ravel 解开. 变成一维
array([0.46561932, 0.60702979, 0.68140217, 0.04024   , 0.52607732,
       0.11197173, 0.79977183, 0.06455059, 0.18500467, 0.22661201,
       0.97044627, 0.04624839])
# np.zeros((2, 1))
a= np.ones((2,1))

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Input In [130], in <cell line: 2>()
      1 # np.zeros((2, 1))
----> 2 a= np.ones((2,1))


TypeError: 'tuple' object is not callable
# Stacking的意思是堆叠, 通常是把多个数组拼成一个新数组
A = np.ones((3,3))
B = np.zeros((3,3))
np.vstack((A,B))
np.hstack((A,B))
array([[1., 1., 1., 0., 0., 0.],
       [1., 1., 1., 0., 0., 0.],
       [1., 1., 1., 0., 0., 0.]])
# column_stack 和 row_stack 一般是拼接一维数组
a = np.array([0,1,2])
b = np.array([3,4,5])
c = np.array([6,7,8])
np.column_stack((a,b,c))
np.row_stack((a,b,c))
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
# split来分割数组
A = np.arange(16).reshape((4,4))
A
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
[B,C] = np.hsplit(A,2)
B
array([[ 0,  1],
       [ 4,  5],
       [ 8,  9],
       [12, 13]])
C
array([[ 2,  3],
       [ 6,  7],
       [10, 11],
       [14, 15]])
[B,C]=np.vsplit(A,2)
B
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
C
array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]])
# 这种方式可以多个返回多个元素. 这也是切片的原理.
a= np.arange(16).reshape([4,4])
print(a)
print(a[[1,2],[2,3]])
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
[ 6 11]
A = np.arange(20,29).reshape((3,3))
A
array([[20, 21, 22],
       [23, 24, 25],
       [26, 27, 28]])
print(A[0,:3])
# 第一行意思是[0,0:3]
# 进一步意思是[[0,0],[0,1],[0,3]]
A[[0,0],[0,1],[0,3]]
[20 21 22]



---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

Input In [57], in <cell line: 4>()
      1 print(A[0,:3])
      2 # 第一行意思是[0,0:3]
      3 # 进一步意思是[[0,0],[0,1],[0,3]]
----> 4 A[[0,0],[0,1],[0,3]]


IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed
print(A[0:])
# 意思是 [0:3] 
# 分开是[[0,1,2]]
[[20 21 22]
 [23 24 25]
 [26 27 28]]
A[0:2,0:2]
# 继续分开 A[[0,0],[0,1],[1,0],[1,1]]
array([[20, 21],
       [23, 24]])
A[[0,2],0:2]
# 继续分开 A[[0,2],0]
# A[[0,2],0] => [[0,0],[2,0]]
# A[[0,2],1] => [[0,1],[2,1]]

array([22, 20])
A[[0,0],[2,0]]
array([22, 20])
A[:,0]
array([20, 23, 26])
# Iterating 迭代
A = np.arange(20,29).reshape((3,3))
print()
for row in A:
    for column in row:
        print(column)
20
21
22
23
24
25
26
27
28
# 第二种方式,扁平化矩阵再遍历
for item in A.flat:
    print(item)
20
21
22
23
24
25
26
27
28
print(np.apply_along_axis(np.mean, axis=0, arr=A))
[23. 24. 25.]
print(np.apply_along_axis(np.mean, axis=1, arr=A))
# 这个第一个参数可以自定义, 就是每个元素都需要执行一下.
[21. 24. 27.]
print(np.__version__) 
1.21.5
x = np.array([10,2,30,45])
print("original array:")
print(x)
print(np.all(x))# 测试是否全部元素非0
print(np.any(x))# 只要有一个元素非0即为true
original array:
[10  2 30 45]
True
True
# 生成单位矩阵
x = np.eye(4)
print(x)
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
# array和list相互转换
myarr=[[10,25],[40,44]]
print(type(myarr))
print(id(myarr))

x = np.array(myarr)
print(type(myarr))#不改变原数据
print(x)
myarr2=x.tolist()
print(id(myarr2))
print(myarr == myarr2)
print(myarr is myarr2)
# == 只判断值, 不判断id 
#  如果判断完全相等 使用is
<class 'list'>
140390757086144
<class 'list'>
[[10 25]
 [40 44]]
140390756832896
True
False
import matplotlib.pyplot as plt
x = np.arange(05*np.pi, 0.2)
y = np.sin(x)
print("plot")
plt.plot(x,y)
plt.show()
plot
png
png
x = np.array([45,67,23])
y = np.array([56,23,89])
print(x)
print(y)
print(np.greater(x,y))
print(np.greater_equal(x,y))
print(np.less(x,y))
print(np.less_equal(x,y))
[45 67 23]
[56 23 89]
[False  True False]
[False  True False]
[ True False  True]
[ True False  True]
import os
x = np.arange(9).reshape(3,3)
print(x)
header = 'col1 col2 col3 col4'
np.savetxt('temp.txt', x, fmt = "%d", header = header)
print(...)
result = np.loadtxt('temp.txt')
print(result)
[[0 1 2]
 [3 4 5]
 [6 7 8]]
Ellipsis
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]
# array 之间赋值 是引用传递, 也就是说如果修改其中之一两个都会修改
arr = np.array([1,2,3,4])
arr1 = arr
print(arr1 is arr) # true
arr1[0]=0
print(arr1)
print(arr)

# 通过copy来深度复制一个值一样, 但是互不影响的array
arr = np.array([11,22,33,44])
arr1 =arr.copy()
print(id(arr)==id(arr1))
arr1[0]=0
print(arr)
print(arr1)
True
[0 2 3 4]
[0 2 3 4]
False
[11 22 33 44]
[ 0 22 33 44]

Pandas!!!!!!!!!!!!

import numpy as np
import pandas as pd
s = pd.Series([12,-4,7,9])
s
0    12
1    -4
2     7
3     9
dtype: int64
s[2]
7
s[0:3]
0    12
1    -4
2     7
dtype: int64
s[1]=0
s
0    12
1     0
2     7
3     9
dtype: int64
s = pd.Series([12,-4,7,9],index = ['a','b','c','d'])
s
a    12
b    -4
c     7
d     9
dtype: int64
s['b']=0
s
a    12
b     0
c     7
d     9
dtype: int64
s[s>8]
a    12
d     9
dtype: int64
s/2
a    6.0
b    0.0
c    3.5
d    4.5
dtype: float64
serd = pd.Series([1,0,2,1,2,3],index=['white','white','blue','green','green','yellow'])
serd
white     1
white     0
blue      2
green     1
green     2
yellow    3
dtype: int64
serd.unique() #这个函数会告诉我们所有的存在的数
array([1, 0, 2, 3])
serd.value_counts() # 这个函数会给每个出现的数(这个数和unique返回的一样)进行计数
1    2
2    2
0    1
3    1
dtype: int64
serd.isin([0,3])# 判断值是否在范围内
white     False
white      True
blue      False
green     False
green     False
yellow     True
dtype: bool
# NaN
# 一般用在pandas里表示, 空字段 或者 非数值元素
# np.NaN
s2 = pd.Series([5,-3,np.NaN, 20])
s2
0     5.0
1    -3.0
2     NaN
3    20.0
dtype: float64
s2.isnull()
0    False
1    False
2     True
3    False
dtype: bool
s2.notnull()
0     True
1     True
2    False
3     True
dtype: bool
s2[s2.notnull()] # 里面的值为true才返回
0     5.0
1    -3.0
3    20.0
dtype: float64
s2[s2.isnull()]
2   NaN
dtype: float64
# 可以将字典转变成Series
mydict = {'red':250,'blue':560,'green':700,"white":1456}
mydict
{'red': 250, 'blue': 560, 'green': 700, 'white': 1456}
myseries = pd.Series(mydict)
myseries
red       250
blue      560
green     700
white    1456
dtype: int64
# 自定义也行
colors = ['red','blue','green','white','purple']
myseries = pd.Series(mydict, index =colors)
myseries
red        250.0
blue       560.0
green      700.0
white     1456.0
purple       NaN
dtype: float64
mydict2 = {'red':900,"black":800,'white':500}
myseries2 = pd.Series(mydict2)
myseries+myseries2
black        NaN
blue         NaN
green        NaN
purple       NaN
red       1150.0
white     1956.0
dtype: float64
# DataFrame
- END -

分类:

后端

标签:

后端

作者介绍

Shinkai005
V1

公众号:深海笔记Shinkai