目录
numpy.argmax(a, axis=None, out=None)函数
np.load(path, allow_pickle=True)函数 and np.save()函数
sklearn.model_selection packages
sklearn.model_selection.train_test_split()函数
sklearn.metrics.roc_curve(y_true, y_score, pos_label=None)函数
sklearn.metrics.f1_score(y_true, y_pred)函数
sklearn.metrics.normalized_mutual_info_score(labels_true, labels_pred)
sklearn.metrics.adjusted_mutual_info_score(labels_true, labels_pred)
sklearn.metircs.adjusted_rand_score(labels_true, labels_pred)
sklearn.neighbors.KNeighborsClassifier()函数
sklearn.linear_model.LinearRegression()函数
scipy.io.loadmat(‘filepath’)函数
csr_matrix构造方式(一): 二维数组或矩阵(其他矩阵转化)
csr_matrix构造方式(二): 三元组方式(密集矩阵构建)
csr_matrix构造方式(三): 空csr_matrix + 赋值
scipy.sparse.todense()函数 -> numpy矩阵
scipy.sparse.isspmatrix_coo(x)函数
scipy.sparse.linalg.eigsh(A, k)函数
scipy.sparse.save_npz()和load_npz()存读.npz文件
networkx.to_numpy_matrix(G)函数返回图邻接矩阵A
error(1): nx.adjacency_matrix()计算邻接矩阵与真实结果不一致
networkx.get_node_attributes(G, ‘tweet_id’)函数
json.dump(dict, file, indent=2)函数
random.randit(low, high=None, size=None, dtype=’l’)函数
python fundamental functions
enumerate()函数
它用于将一个可遍历的数据对象(如list)组合为一个索引序列,同时列出数据和数据下标。
os package
- 获取当前目录
- 获取上级目录
- 获取上上级目录
获取当前目录
os.getcwd()
获取上级目录
os.path.abspath(os.path.dirname(os.getcwd()))
获取上上级目录
os.path.abspath(os.path.join(os.getcwd(), "../.."))
numpy package
numpy 创建ndarray数组
numpy.zeros()函数
zeros(shape, dtype=float, order='C')
根据形状和类型返回一个全是0的数组
np.zeros(5)
array([ 0., 0., 0., 0., 0.])
np.zeros((2, 1))
array([[ 0.],
[ 0.]])
numpy.empty()函数
numpy.empty(shape, dtype=float, order='C')
np.empty()函数,根据给定的 维度和 数值类型,返回一个ndarray数组,其元素不进行初始化。
import numpy as np
np.empty([2, 2])
[[9.90263869e+067, 8.01304531e+262],
[2.60799828e-310, 0.00000000e+000]]
numpy.eye()函数
返回一个单位矩阵。
numpy.eye(N,M=None,k=0,dtype=,order='C)
paras:
N:int型,表示的是输出的行数
M:int型,可选项,输出的列数,如果没有就默认为N
k:int型,可选项,对角线的下标,默认为0表示的是主对角线,负数表示的是低对角,正数表示的是高对角。
返回的是一个二维的ndarray数组(N,M),对角线的地方为1,其余的地方为0.
ar3 = np.eye(3,4,k=2)
ar4 = np.eye(3)
print('ar3:')
print(ar3)
print('ar4')
print(ar4)
np.random.permutation(x)函数
randomly permute a sequence, or returen a permuted range.
x: int or array_like
输入一个数或者数组,生成一个随机序列,对多维数组来说是多维随机打乱而不是1维
>>np.random.permutation([1, 4, 9, 12, 15])
array([15, 1, 9, 4, 12])
>>arr = np.arange(9).reshape((3, 3))
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>np.random.permutation(arr)
array([[6, 7, 8],
[0, 1, 2],
[3, 4, 5]])
>>permutation = list(np.random.permutation(10))
[5, 1, 7, 6, 8, 9, 4, 0, 2, 3]
>>Y = np.array([[1,1,1,1,0,0,0,0,0,0]])
>>Y_new = Y[:, permutation]
array([[0, 1, 0, 0, 0, 0, 0, 1, 1, 1]])
numpy.matrix()函数
x = np.matrix([[1, 3, 5], [2, 4, 6]])
numpy.arange()函数
b = np.arange(12).reshape([4, 3])
numpy数组 calculation
加:numpy.num()函数
np.sum(,axis=0),按行求和,结果行数为1,列数不变
- sum(1),数组按行求和,保留列形式。
乘: numpy.matmul()函数
numpy.matmul(a, b, out=None)
表示两个numpy 数组的矩阵相乘
import numpy as np
a = [[1, 0], [0, 1]]
b = [[4, 1], [2, 2]]
np.matmul(a, b)
array([[4, 1],
[2, 2]])
乘:numpy.dot()函数
- 如果a和b都是一维数组,那么它返回的就是向量的内积。
import numpy as np
c = np.arange(0,9)
d = c[::-1]
np.dot(c,d)
Out[35]: 84
- 如果a和b都是二维数组,那么它返回的就是矩阵乘法。
import numpy as np
a = [[1, 0], [0, 1]]
b = [[4, 1], [2, 2]]
np.dot(a, b)
Out[1]:
array([[4, 1],
[2, 2]])
除:numpy.divide()函数
数组对应位置元素做除法
平均:numpy.mean(data, axis=0)函数
表示numpy矩阵以行为基准求平均
import numpy as np
X = np.array([[1, 2], [4, 5], [7, 8]])
print np.mean(X, axis=0, keepdims=True)
print np.mean(X, axis=1, keepdims=True)
b = np.squeeze(a)
b.shape
'''
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
(10,)
'''
numpy.where()函数
- np.where(condition, x, y),满足条件condition,输出x,不满足输出y
>>> aa = np.arange(10)
>>> np.where(aa,1,-1)
array([-1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) # 0为False,所以第一个输出-1
>>> np.where(aa > 5,1,-1)
array([-1, -1, -1, -1, -1, -1, 1, 1, 1, 1])
>>> np.where([[True,False], [True,True]], # 官网上的例子
[[1,2], [3,4]],
[[9,8], [7,6]])
array([[1, 8],
[3, 4]])
- np.where(condition),只有条件condition,输出满足条件元素的索index(==numpy.nonzero)。这里的索引以 tuple形式给出。
>>> a = np.array([2,4,6,8,10])
>>> np.where(a > 5) # 返回索引index,元组tuple形式
'''(array([2, 3, 4]),) '''
>>> np.where(a > 5)[0] # 加[0],提取相应数组
'''array([2, 3, 4])'''
>>> a[np.where(a > 5)] # 等价于 a[a>5]
array([ 6, 8, 10])
>>> np.where([[0, 1], [1, 0]])
(array([0, 1]), array([1, 0]))
numpy.stack() 函数
沿着axis连接数组的序列。
import numpy as np
a = [[0,-1,1],[2,1,3]]
print(a)
c = np.stack(a, axis=0)
print(c)
'''
[[0, -1, 1], [2, 1, 3]]
[[ 0 -1 1]
[ 2 1 3]]
'''
numpy.newaxis()函数
np.newaxis()函数的功能是增加新的维度
- x[:, np.newaxis] ,放在后面,会给列上增加1个维度
- x[np.newaxis, :] ,放在前面,会给行上增加1个维度,可简化为x[np.newaxis]
x = np.array([1, 2, 3, 4])
print(x.shape) # (4,)
x_row = x[newaxis] # 行增加一个维度,(1,4)
x_add = x[:, np.newaxis] # 列增加一个维度, (4,1)
print(x_add.shape)
print(x_add)
>>>
(4,)
(4, 1)
[[1]
[2]
[3]
[4]]
np.array()函数、np.asarray()函数
- np.array,复制对象,创建一个新的内存空间
- np.asarray,不复制,实际上是个指针,只是指向对象内存空间。
np.asarray将结构数据转化为ndarray,比如将list列表转换为nd.array数组
import numpy as np
a = np.array([0,1,2,-1,-2])
'''
array([ 0, 1, 2, -1, -2])
'''
np.array(a>0)
'''
array([False, True, True, False, False])
'''
- np.array()函数, 将列表list转换成array,才能进行切片slice操作。
anchor_positives = [(0, 2), (0, 3), (2, 3)]
anchor_positives[:,0]
TypeError: list indices must be integers or slices, not tuple
需要将list转换成np.array()数组
anchor_positives = np.array([(0, 2), (0, 3), (2, 3)])
anchor_positives[:,0]
{'a': '1111', 'c': '3333', 'b': '2222', 'd': '4444'}
{"a": "1111", "c": "3333", "b": "2222", "d": "4444"}
json.loads()函数
json.loads()函数用于将str类型的数据转换成dict。
import json
name_emb = {'a':'1111','b':'2222','c':'3333','d':'4444'}
jsDumps = json.dumps(name_emb)
jsLoads = json.loads(jsDumps)
print(name_emb)
print(jsDumps)
print(jsLoads)
print(type(name_emb))
print(type(jsDumps))
print(type(jsLoads))
{u'a': u'1111', u'c': u'3333', u'b': u'2222', u'd': u'4444'}
key: a value: 1111
key: c value: 3333
key: b value: 2222
key: d value: 4444
random package
random.randint; random;uniform; choice; randrange
random.randint(low, high=None, size=None, )函数要设定范围从low(包括)到high(不包括)
返回[low, high)之间一个随机整型数.
import random
print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数
print( random.random() ) # 产生 0 到 1 之间的随机浮点数
print( random.uniform(1.1,5.4) ) # 产生 1.1 到 5.4 之间的随机浮点数,区间可以不是整数
print( random.choice('tomorrow') ) # 从序列中随机选取一个元素
print( random.randrange(1,100,2) ) # 生成从1到100的间隔为2的随机整数
a=[1,3,5,6,7] # 将序列a中的元素顺序打乱
random.shuffle(a)
print(a)
random.sample(list, length)函数
多用于截取列表的指定长度的随机数,但是不会改变列表本身的排序
`python
list = [0,1,2,3,4]
rs = random.sample(list, 2)
print(rs)
Original: https://blog.csdn.net/qq_33419476/article/details/127510370
Author: 天狼啸月1990
Title: GNN python packages
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/761064/
转载文章受原作者版权保护。转载请注明原作者出处!