Skip to content

Commit a270e5f

Browse files
修复删除的内容
1 parent 71899ca commit a270e5f

File tree

2 files changed

+233
-0
lines changed

2 files changed

+233
-0
lines changed

run_example.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# import tutorials.keras.text_NER as ft
2+
# import tutorials.keras.brat_tag as ft
3+
import tutorials.RecommenderSystems.rs_rating_demo as ft
4+
# from middleware.utils import TimeStat, Chart
5+
# import matplotlib.pyplot as plt
6+
# import matplotlib.gridspec as gridspec
7+
# from matplotlib.font_manager import FontProperties
8+
# plt.rcParams['font.sans-serif'] = ['SimHei']
9+
# plt.rcParams['axes.unicode_minus'] = False
10+
11+
12+
def main():
13+
ft.main()
14+
# x=y=[1,2,3]
15+
# plt.plot(x, y, color='g', linestyle='-') # 绘制
16+
# plt.grid(True, ls = '--')
17+
# plt.show()
18+
19+
if __name__ == "__main__":
20+
main()

tutorials/AI常用函数说明.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
# AI常用函数说明
2+
3+
## numpy 相关
4+
5+
> from numpy import random, mat, eye
6+
7+
```py
8+
'''
9+
# NumPy 矩阵和数组的区别
10+
NumPy存在2中不同的数据类型:
11+
1. 矩阵 matrix
12+
2. 数组 array
13+
相似点:
14+
都可以处理行列表示的数字元素
15+
不同点:
16+
1. 2个数据类型上执行相同的数据运算可能得到不同的结果。
17+
2. NumPy函数库中的 matrix 与 MATLAB中 matrices 等价。
18+
'''
19+
from numpy import random, mat, eye
20+
21+
# 生成一个 4*4 的随机数组
22+
randArray = random.rand(4, 4)
23+
# 转化关系, 数组转化为矩阵
24+
randMat = mat(randArray)
25+
'''
26+
.I 表示对矩阵求逆(可以利用矩阵的初等变换)
27+
意义: 逆矩阵是一个判断相似性的工具。逆矩阵A与列向量p相乘后,将得到列向量q,q的第i个分量表示p与A的第i个列向量的相似度。
28+
参考案例链接:
29+
https://www.zhihu.com/question/33258489
30+
http://blog.csdn.net/vernice/article/details/48506027
31+
.T 表示对矩阵转置(行列颠倒)
32+
* 等同于: .transpose()
33+
.A 返回矩阵基于的数组
34+
参考案例链接:
35+
http://blog.csdn.net/qq403977698/article/details/47254539
36+
'''
37+
invRandMat = randMat.I
38+
TraRandMat = randMat.T
39+
ArrRandMat = randMat.A
40+
# 输出结果
41+
print('randArray=(%s) \n' % type(randArray), randArray)
42+
print('randMat=(%s) \n' % type(randMat), randMat)
43+
print('invRandMat=(%s) \n' % type(invRandMat), invRandMat)
44+
print('TraRandMat=(%s) \n' % type(TraRandMat), TraRandMat)
45+
print('ArrRandMat=(%s) \n' % type(ArrRandMat), ArrRandMat)
46+
# 矩阵和逆矩阵 进行求积 (单位矩阵,对角线都为1嘛,理论上4*4的矩阵其他的都为0)
47+
myEye = randMat*invRandMat
48+
# 误差
49+
print(myEye - eye(4))
50+
```
51+
52+
> np.dot
53+
54+
```py
55+
import numpy as np
56+
57+
a = np.array([2, 3])
58+
b = np.array([4, 5])
59+
np.dot(a, b, out=None) #该函数的作用是获取两个元素a,b的乘积
60+
61+
Out[1]: 23 = 2*4 + 3*5
62+
63+
a = np.array([2, 3, 4])
64+
b = np.array([5, 6, 7])
65+
np.dot(a, b, out=None) #该函数的作用是获取两个元素a,b的乘积
66+
67+
Out[2]: 56 = 2*5 + 3*6 + 4*7
68+
```
69+
70+
> array sum/mean
71+
72+
```py
73+
import numpy as np
74+
75+
# ---- sum ---- #
76+
a = np.array([[2, 3, 4], [2, 3, 4]])
77+
78+
# 纵向求和: 0 表示某一列所有的行求和
79+
a.sum(axis=0)
80+
Out[6]: array([4, 6, 8])
81+
82+
# 横向求和: 1 表示某一行所有的列求和
83+
a.sum(axis=1)
84+
Out[7]: array([9, 9])
85+
86+
87+
# ---- mean ---- #
88+
a = np.array([[2, 3, 4], [12, 13, 14]])
89+
90+
# 纵向求平均: 0 表示某一列所有的行求和
91+
a.mean(axis=0)
92+
Out[13]: array([7., 8., 9.])
93+
94+
# 横向求平均: 1 表示某一行所有的列求平均
95+
a.mean(axis=1)
96+
Out[14]: array([ 3., 13.])
97+
98+
```
99+
100+
> np.newaxis
101+
102+
* numpy 添加新的维度: newaxis(可以给原数组增加一个维度)
103+
104+
```py
105+
import numpy as np
106+
107+
In [59]: x = np.random.randint(1, 8, size=(2, 3, 4))
108+
In [60]: y = x[:, np.newaxis, :, :]
109+
In [61]: Z = x[ :, :, np.newaxis, :]
110+
111+
In [62]: x. shape
112+
Out[62]: (2, 3, 4)
113+
114+
In [63]: y. shape
115+
Out[63]: (2, 1, 3, 4)
116+
117+
In [64]: z. shape
118+
Out [64]: (2, 3, 1, 4)
119+
```
120+
121+
## pandas 相关
122+
123+
> df.shift()
124+
125+
```
126+
DataFrame.shift(periods=1, freq=None, axis=0)
127+
128+
* periods: 类型为int,表示移动的幅度,可以是正数,也可以是负数,默认值是1,
129+
1就表示移动一次,注意这里移动的都是数据,而索引是不移动的,移动之后没有对应值的,就赋值为NaN
130+
* freq: DateOffset, timedelta, or time rule string,可选参数,默认值为None,只适用于时间序列,如果这个参数存在,那么会按照参数值移动时间索引,而数据值没有发生变化。
131+
* axis: {0, 1, ‘index’, ‘columns’},表示移动的方向,如果是0或者’index’表示上下移动,如果是1或者’columns’,则会左右移动。
132+
```
133+
134+
```py
135+
136+
"""
137+
index value1
138+
A 0
139+
B 1
140+
C 2
141+
D 3
142+
"""
143+
144+
df.shift() # 或 df.shift(1)
145+
# 就会变成如下:
146+
147+
"""
148+
index value1
149+
A NaN
150+
B 0
151+
C 1
152+
D 2
153+
"""
154+
155+
df.shift(2)
156+
157+
"""
158+
159+
index value1
160+
A NaN
161+
B NaN
162+
C 0
163+
D 1
164+
"""
165+
166+
df.shift(-1)
167+
168+
"""
169+
index value1
170+
A 1
171+
B 2
172+
C 3
173+
D NaN
174+
"""
175+
176+
####################
177+
178+
"""
179+
index value1
180+
2016-06-01 0
181+
2016-06-02 1
182+
2016-06-03 2
183+
2016-06-04 3
184+
"""
185+
186+
# 如果 index 为时间
187+
df1.shift(periods=1,freq=datetime.timedelta(1))
188+
189+
"""
190+
index value1
191+
2016-06-02 0
192+
2016-06-03 1
193+
2016-06-04 2
194+
2016-06-05 3
195+
"""
196+
```
197+
198+
199+
200+
## keras 相关
201+
202+
> from keras.preprocessing.sequence import pad_sequences
203+
204+
```python
205+
from keras.preprocessing.sequence import pad_sequences
206+
207+
208+
# padding: pre(默认) 向前补充0 post 向后补充0
209+
# truncating: 文本超过 pad_num, pre(默认) 删除前面 post 删除后面
210+
x = [[1,2,3,4,5]]
211+
x_train = pad_sequences(x, maxlen=pad_num, value=0, padding='post', truncating="post")
212+
print("--- ", x_train[0][:20])
213+
```

0 commit comments

Comments
 (0)