1.
看faiss示例:
import faiss
import numpy as np
## 构建待检索向量和查询向量
d = 64 # 向量维度
nb = 100000 # 待索引向量size
nq = 10000 # 查询向量size
np.random.seed(1234) # 随机种子确定
xb = np.random.random((nb, d)).astype('float32') #生成nb行d列的随机数float32型
xb.shape # (100000, 64) #获取行数,列数
xb
"""
array([[0.19151945, 0.62210876, 0.43772775, ..., 0.62491673, 0.4780938 ,
0.19567518],
[0.38231745, 0.05387368, 0.4516484 , ..., 0.15139526, 0.33517465,
0.65755177],
[0.07334255, 0.0550064 , 0.3231948 , ..., 0.34441698, 0.6408804 ,
0.12620533],
...,
[0.81447345, 0.5902453 , 0.7988935 , ..., 0.339859 , 0.3019495 ,
0.85385454],
[0.6714615 , 0.9160688 , 0.9550788 , ..., 0.59536433, 0.03849181,
0.10563799],
[0.85663575, 0.59113413, 0.67890793, ..., 0.21897699, 0.06530159,
0.21753833]], dtype=float32)
"""
np.arange(nb)
# array([ 0, 1, 2, ..., 99997, 99998, 99999])
xb[:, 0] += np.arange(nb) / 1000. #为了使随机产生的向量有较大区别进行人工调整向量
xb
"""
array([[1.91519454e-01, 6.22108757e-01, 4.37727749e-01, ...,
6.24916732e-01, 4.78093803e-01, 1.95675179e-01],
[3.83317441e-01, 5.38736843e-02, 4.51648414e-01, ...,
1.51395261e-01, 3.35174650e-01, 6.57551765e-01],
[7.53425434e-02, 5.50063960e-02, 3.23194802e-01, ...,
3.44416976e-01, 6.40880406e-01, 1.26205325e-01],
...,
[1.00811470e+02, 5.90245306e-01, 7.98893511e-01, ...,
3.39859009e-01, 3.01949501e-01, 8.53854537e-01],
[1.00669464e+02, 9.16068792e-01, 9.55078781e-01, ...,
5.95364332e-01, 3.84918079e-02, 1.05637990e-01],
[1.00855637e+02, 5.91134131e-01, 6.78907931e-01, ...,
2.18976989e-01, 6.53015897e-02, 2.17538327e-01]], dtype=float32)
"""
xq = np.random.random((nq, d)).astype('float32')
xq[:, 0] += np.arange(nq) / 1000.
xq.shape # (10000, 64)
np.arange()
函数返回一个有终点和起点的固定步长的排列,如[1,2,3,4,5],起点是1,终点是6,步长为1。
参数个数情况: np.arange()函数分为一个参数,两个参数,三个参数三种情况
1)一个参数时,参数值为终点,起点取默认值0,步长取默认值1。
2)两个参数时,第一个参数为起点,第二个参数为终点,步长取默认值1。
3)三个参数时,第一个参数为起点,第二个参数为终点,第三个参数为步长。其中步长支持小数
xb[:, 0],取第一列数据
xb[1,:],取第二行数据;
2.
3 import matplotlib
4 import matplotlib.pyplot as plt
5 import numpy as np
6 from sklearn import linear_model
7
8 model = linear_model.LinearRegression()
9
10 x = np.linspace(-3, 3, 30)
11 y = 2 * x + 1
12
14 model.fit(x.reshape(-1, 1), y)
16 x_ = np.array(1)
17 print type(model.predict(x_.reshape(-1, 1)))
sklearn报错:
ValueError: Expected 2D array, got 1D array instead:
fit第一个参数,predict参数,都应该矩阵,需要使用reshape(1,-1)进行转换
reshape:
reshape(3, 2) 转换成3行2列
reshape(-1, m) 转换成(a*b/m)行,m列
3.
np.multiply
arr = numpy.array([1,2,3])
arr1 = numpy.array([[1,2,3],[3,4,5]])
#arr = arr**2 #平方
numpy.multiply(arr, arr1)
输出:
[1 2 3] //(3,)
[[1 2 3]
[3 4 5]] //(3,3)
res: [[ 1 4 9]
[ 3 8 15]]
每个对应元素相乘,shape必须满足每个对应元素相乘,比如(3,3)和(2,3)就会报错;
np.newaxis
print type(numpy.newaxis)
<type 'NoneType'>
//
arr2 = numpy.array([1,2,3])
print arr2
#arr3 = arr2[:,numpy.newaxis]
arr3 = arr2[:,None]
print arr3
#多维获取某行/列,转为列向量
#arr = numpy.array([[1,2,3],[4,5,6]])
#print arr[1,:][:,numpy.newaxis]
输出:
[2 3 4]
[[2]
[3]
[4]]
///
arr2 = numpy.array([2,3,4])
arr3 = arr2[:,numpy.newaxis]
print arr3
arr4=numpy.array([1,2,3])
print arr4
print numpy.multiply(arr4, arr3)
输出:
[[2]
[3]
[4]]
[1 2 3]
[[ 2 4 6]
[ 3 6 9]
[ 4 8 12]]
为了转换成列向量,并且发现和None效果一样;
np.sum
arr2=numpy.array([2,3])
arr4=numpy.array([[1,2],[3,4]])
print numpy.sum(arr4*arr2)
输出:
26
//
arr2=numpy.array([2,3,4])
arr3=numpy.array([[1,2,3],[3,4,5]])
print arr2*arr3
print numpy.sum(arr2*arr3)
print numpy.sum(arr2*arr3, axis=0)
print numpy.sum(arr2*arr3, axis=1)
输出:
[[ 2 6 12]
[ 6 12 20]]
58
[ 8 18 32]
[20 38]
所有元素相加,同时发现,*和multiply等价;
axis=0表示第一行为基础,纵向向下求和,二行三列退化为3元
axis=1表示第一列为基础,横向向右求和,二行三列退化为2元
多维处理,同样如此,以某维度为基础,进行退化;
4.读写文件
8 def rf():
9 with open(f1) as fp:
10 for s in fp:
11 ind=s.find(":feat:")
12 if ind == -1:
13 continue
14 s1 = s[ind:]
15 print s
16 exit(0)
17 m[s1]=""
5.
#通过设置随机种子返回随机生成器
np.random.RandomState(1338)
#高斯分布
loc:float
此概率分布的均值(对应着整个分布的中心centre)
scale:float
此概率分布的标准差(对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高)
size:int or tuple of ints
输出的shape,默认为None,只输出一个值
x = rs.normal(size=(n, d1))
#向量点积/矩阵乘积
x = np.dot(x, rs.rand(d1, d))