Numpy大数据基础类库,重要性不言而喻。
环境::python3.7
编译器:Anaconda中的spyder.
代码块:
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import numpy as np
lst = [[1,2.1,3],[2,4,6]]
np_lst = np.array(lst, dtype=np.float)
np_lst.astype(int)
print(np_lst.dtype)
print(np_lst.shape)
print(np_lst.ndim)
print(np_lst.itemsize) #每个元素的大小
# some array
print(np.zeros([2,3]))
print(np.ones([2,3]))
print(np.random.rand(2,3)) #2*3
print(np.random.choice([10,20,30]))#从中随机选取一个
# array distribution.
print(np.random.rand(2,1,4)) #2*1 *4
#创建指定范围内的一个数
np_uniform = np.random.uniform(0,100,size=5)
print(np_uniform)
#Numpy.arange(start, stop, step) 的均匀分布
np_arange = np.arange(2,10,2)
print('arange:',np_arange)
#Numpy.linspace(start, stop, N) 的均匀分布
np_linspace = np.linspace(1,5,3)
print('linspace:',np_linspace)
#Numpy.random.normal(loc, scale, size) 正态分布 生成4行5列的二位数组
np_normal = np.random.normal(loc=1.75,scale=0.1,size=[4,5])
print('normal:',np_normal)
#beta分布.
np_beta = np.random.beta(1,2,12)
print('beta:',np_beta)
#numpy operation
#numpy copy
after_array = np_normal[1:3,2:4].copy()
print('array_copy:',after_array)
#numpy sort
np_sort = np.sort(np_beta)
print('sort:',np_sort) #整体排序
sort_col = np.sort(np_beta, axis=0)#行排序
print('col-sort:',sort_col)
sort_row = np.sort(np_normal, axis=1)#列排序
print('row-sort:',sort_row)
#numpy unique
print('提取唯一元素:',np.unique(np_arange, return_index=True, return_counts=True))
print('提取唯一元素:',np.unique(np_arange, return_inverse=True ))
#转置
print(np_sort.T)
#reshape():
c = np.array([[[0,1,2],[10,12,24]],[[100,101,12],[101,112,113]]])
c.reshape(3,2,2)
#resize()
a = np.array([[[0, 1, 2],
[10, 12, 13]],
[[100, 101, 102],
[110, 112, 113]]])
b = np.array([[[0, 1, 2],
[10, 12, 13]],
[[100, 101, 102],
[110, 112, 113]]])
a.resize(3,1)
b.resize(3,5)
print(a)
print(b)
#条件运算 - where
re_score = np.where(b < 80, 0,90)
print(re_score)
#整个矩阵的最大值
result = np.amax(b)
print(result)
#求每一列的最大值
resultrow = np.amax(b, axis=0)
print(resultrow)
# 求每一行的最小(1表示行)
resultcol = np.amin(b, axis=1)
print(resultcol)
#平均值
score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
result_mean = np.mean(score, dtype=np.int)
print('mean:',result_mean)
col_mean = np.mean(score, axis=0)
print('row_mean:',col_mean)
row_mean = np.mean(score, axis=1)
print('row_mean:',row_mean)
#方差
result_std = np.std(score)
print(result_std)
std_row = np.std(score,axis=0)
print(std_row)
std_col = np.std(score,axis=1)
print(std_col)
#数组的运算(+-*/,//,%)
score[:, :] = score[:, :]+5 #循环数组行和列,每一个数值都加5
print(score)
score[0:1, 0:1] = score[0:1, 0:1]*5
print(score)
score[0:1, 0:1] = score[0:1, 0:1]//5
print(score)
score[:, :] = score[:, :] % 5
#数组间的运算,数组的shape要一样。
#+ = np.add(a,b) ,- = np.substract(a,b)
#* = np.multiply(a,b),/ = np.divide(a,b)
sum = score + score
sum1 = np.add(a,b)
print(sum)
ints = np.intersect1d(a,b) #查找两个数组中的相同元素
print(ints)
setdiff = np.setdiff1d(a,b) #查找在数值a中不在b中的元素
print(setdiff)
unionld = np.union1d(a,b) #查找在数值a中不在b中的元素
print(unionld)
#矩阵的计算
#(M行,N列)*(N行,Z列)=(M行,Z列)
st_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
radio = np.array([[0.4],[0.6]])
result_dot = np.dot(st_score, radio)
print(result_dot)
#矩阵拼接
v1 = [[0, 1, 2, 3, 4, 5],
[1, 1, 2, 3, 4, 5]]
v2 = [[2, 4, 5, 3, 4, 5],
[4, 1, 2, 3, 4, 5]]
np_vstack = np.vstack((v1,v2)) #vstack(参数:tuple)
print(np_vstack)
#矩阵水平拼接
np_hstack = np.hstack((v1, v2)) #参数:tuple
print(np_hstack)
#矩阵删除:
#Numpy.delete(参数 1:a,数组;参数 2:elements,删除的对象;参数 3:axis=0/1)
OriginalY = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print('delete:',np.delete(OriginalY, [0, 2]))
print(np.delete(OriginalY, [0, 2], axis=0))
print(np.delete(OriginalY, [0, 2], axis=1))
#矩阵的添加
array1 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(np.append(array1,[0,2]))
print(np.append(array1,[[1,2,3]],axis=0))#追加一行
print(np.append(array1,[[1],[2],[3]],axis=1))#追加一列
#矩阵插入
arrat_insert = np.insert(array1,1,[11,12,13],axis=1)
print('insert:',arrat_insert)
#文件加载
file = np.loadtxt(fname=r'C:\Users\Lee\Desktop\aa.txt',dtype=str,
comments='#',delimiter='!',skiprows=0,
usecols=None)
print(file)