Python数据分析与展示第0&1周学习笔记(北理工 嵩天)

一前奏

1..Python语言开发工具选择

IDLE:自带默认常用入门级

PyCharm:简单、集成度高

Anaconda:awesome

IDE较为简单,不做详细记录。

二.表示

1.numpy库入门

数据的维度

列表内的数据类型可以不同。

高维数据:键值对组成。

数组对象

Numpy是一个开源的Python科学计算库

*一个强大的N维数组对象ndarray

*广播功能函数

*线性代数、傅里叶变换、随机数生成等功能

N维数组对象:ndarray

*实际的数据

*元数据,下标从0开始(数据维度,数据类型deng)

.ndim  秩,即轴的数量或维度的数量

.shape ndarray对象的尺度,对于矩阵,n行m列

.size  ndarray对象元素的个数

.dtype ndarray对象的元素类型

.itemsize ndarray对象中每个元素的大小,以字节为单位

数组的创建和变换

从Python中的列表、元组等类型创建ndarray数组

x = np.array(list/tuple)

x = np.array([0,1,2,3])


x
Out[4]: array([0, 1, 2, 3])


print(x)
[0 1 2 3]


x = np.array((4,5,6,7))


x
Out[7]: array([4, 5, 6, 7])


print(x)
[4 5 6 7]


x = np.array([[1,2],[9,8],(0.1,0.2)])


print(x)
[[ 1. 2. ]
[ 9. 8. ]
[ 0.1 0.2]]

x = np.array(list/tuple,dtype=np.float32)

np.arange(n)类似range()函数,返回ndarray类型,元素从0到n-1

np.ones(shape) 根据shape生成一个全1数组,shape是元组类型

x = np.ones((2,3))

x
Out[13]: 
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

np.zeros(shape)根据shape生成一个全0数组,shape是元组类型

np.full(shape,val)根据shape生成一个数组,每个元素值都是val

np.eye(n)创建一个正方的n*n单位矩阵,对角线为1,其余为0

x = np.eye(3)

print(x)
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

np.ones_like(a)根据数组a的形状生成一个全1数组

x = np.eye(3)

print(x)
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

x = np.ones_like(x)

print(x)
[[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]


np.zeros_like(a)根据数组a的形状生成一个全0数组

np.full_like(a,val)根据数组a的形状生成一个数组,每个元素值都是val

np.linspace()根据起止数据等间距地填充数据,形成数组

a = np.linspace(1,10,4)

a
Out[20]: array([  1.,   4.,   7.,  10.])

b = np.linspace(1,10,4,endpoint=False)

b
Out[22]: array([ 1.  ,  3.25,  5.5 ,  7.75])

np.concatenate()将两个或多个数组合并成一个新的数组

c = np.concatenate((a,b))

c
Out[24]: array([  1.  ,   4.  ,   7.  ,  10.  ,   1.  ,   3.25,   5.5 ,   7.75])

.reshape(shape)不改变数组元素,返回一个shape形状的数组,原数组不变

a = np.ones((2,3,4),dtype=np.int32)

a
Out[26]: 
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]])

a.reshape((3,8))
Out[27]: 
array([[1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1]])

a
Out[28]: 
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]])

.resize(shape)与.shape()功能一致,但改变原数组

a.resize((3,8))

a
Out[30]: 
array([[1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1]])

.swapaxes(ax1,ax2)将数组n个维度中两个维度进行调换

.flatten()对数组进行降维,返回折叠后的一维数组,原数组不变

.astype(new_type)创建一个新类型数组,原数组不变

.tolist()数组向列表转换

数组的操作

索引:获取数组中特定位置元素的过程

a = np.array([9,8,7,6,5])

a[2]
Out[3]: 7

a = np.arange(24).reshape((2,3,4))

a
Out[6]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],

[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])

a[1,2,3]
Out[7]: 23

切片:获取数组元素子集的过程

a[1:4:2]   起始编号:终止编号(不含):步长
Out[4]: array([8, 6])

n [8]: a[:,1,-3]
Out[8]: array([ 5, 17])

a[:,1:3,:]
Out[9]:
array([[[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],

[[16, 17, 18, 19],
[20, 21, 22, 23]]])

a[:,:,::2]
Out[10]:
array([[[ 0, 2],
[ 4, 6],
[ 8, 10]],

[[12, 14],
[16, 18],
[20, 22]]])

数组的运算

import numpy as np

a = np.arange(24).reshape((2,3,4))

a
Out[3]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

a.mean()平均值
Out[4]: 11.5
与标量之间的运算
a = a/a.mean()

a
Out[6]: 
array([[[ 0.        ,  0.08695652,  0.17391304,  0.26086957],
        [ 0.34782609,  0.43478261,  0.52173913,  0.60869565],
        [ 0.69565217,  0.7826087 ,  0.86956522,  0.95652174]],

       [[ 1.04347826,  1.13043478,  1.2173913 ,  1.30434783],
        [ 1.39130435,  1.47826087,  1.56521739,  1.65217391],
        [ 1.73913043,  1.82608696,  1.91304348,  2.        ]]])

numpy一元函数

np.abs(x) np.fabs(x)计算数组各元素的绝对值

np.sqrt(x) 计算数组各元素的平方根

np.square(x) 计算数组各元素的平方

np.log(x) np.log10(x) np.log2(x) 计算数组各元素的自然对数、10底对数和2底对数

np.ceil(x) np.floor(x) 计算数组各元素的ceiling值或floor值

np.rint(x) 计算数组各元素的四舍五入值

np.modf(x) 将数组各元素的小数和整数部分以两个独立数组形式返回

a = np.arange(24).reshape((2,3,4))

np.square(a)
Out[8]: 
array([[[  0,   1,   4,   9],
        [ 16,  25,  36,  49],
        [ 64,  81, 100, 121]],

       [[144, 169, 196, 225],
        [256, 289, 324, 361],
        [400, 441, 484, 529]]], dtype=int32)

a = np.sqrt(a)

a
Out[10]: 
array([[[ 0.        ,  1.        ,  1.41421356,  1.73205081],
        [ 2.        ,  2.23606798,  2.44948974,  2.64575131],
        [ 2.82842712,  3.        ,  3.16227766,  3.31662479]],

       [[ 3.46410162,  3.60555128,  3.74165739,  3.87298335],
        [ 4.        ,  4.12310563,  4.24264069,  4.35889894],
        [ 4.47213595,  4.58257569,  4.69041576,  4.79583152]]])

np.modf(a)
Out[11]: 
(array([[[ 0.        ,  0.        ,  0.41421356,  0.73205081],
         [ 0.        ,  0.23606798,  0.44948974,  0.64575131],
         [ 0.82842712,  0.        ,  0.16227766,  0.31662479]],
 
        [[ 0.46410162,  0.60555128,  0.74165739,  0.87298335],
         [ 0.        ,  0.12310563,  0.24264069,  0.35889894],
         [ 0.47213595,  0.58257569,  0.69041576,  0.79583152]]]),
 array([[[ 0.,  1.,  1.,  1.],
         [ 2.,  2.,  2.,  2.],
         [ 2.,  3.,  3.,  3.]],
 
        [[ 3.,  3.,  3.,  3.],
         [ 4.,  4.,  4.,  4.],
         [ 4.,  4.,  4.,  4.]]]))

np.cos(x) np.cosh(x) np.sin(x) np.sinh(x) np.tan(x) np.tanh(x)  计算数组各元素的普通型和双曲型三角函数

np.exp(x) 计算数组各元素的指数值

np.sign(x) 计算数组各元素的符号值

 二元函数

+-*/**两个数组各元素进行对应运算

np.maximum(x,y) np.fmax()

np.minimum(x,y) np.fmin()元素级的最大值最小值运算

np.mod(x,y)元素级的模运算

np.copysign(x,y)讲数组y中各元素的符号赋值给数组x对应元素

><>= <= == != 算术比较,产生布尔类型数组

a = np.arange(24).reshape((2,3,4))

b = np.sqrt(a)

a
Out[14]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

b
Out[15]: 
array([[[ 0.        ,  1.        ,  1.41421356,  1.73205081],
        [ 2.        ,  2.23606798,  2.44948974,  2.64575131],
        [ 2.82842712,  3.        ,  3.16227766,  3.31662479]],

       [[ 3.46410162,  3.60555128,  3.74165739,  3.87298335],
        [ 4.        ,  4.12310563,  4.24264069,  4.35889894],
        [ 4.47213595,  4.58257569,  4.69041576,  4.79583152]]])

np.maximum(a,b)
Out[16]: 
array([[[  0.,   1.,   2.,   3.],
        [  4.,   5.,   6.,   7.],
        [  8.,   9.,  10.,  11.]],

       [[ 12.,  13.,  14.,  15.],
        [ 16.,  17.,  18.,  19.],
        [ 20.,  21.,  22.,  23.]]])

a>b
Out[17]: 
array([[[False, False,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]],

       [[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]]], dtype=bool)

2.numpy数据存取与函数

 a.tofile(frame,sep='',format='%s')

frame:文件、字符串

sep:数据分割字符串,如果是空串,写入文件为二进制

np.fromfile(frame,dtype=float,count=-1,sep='')

count:读入元素个数,-1表示读入整个文件

import numpy as np

a = np.arange(100).reshape(5,10,2)

a.tofile("b.dat",sep=",",format='%d')

c=np.fromfile("d.bat",dtype=np.int,sep=",")
Traceback (most recent call last):

  File "", line 1, in 
    c=np.fromfile("d.bat",dtype=np.int,sep=",")

FileNotFoundError: [Errno 2] No such file or directory: 'd.bat'


c=np.fromfile("b.dat",dtype=np.int,sep=",")

c
Out[6]: array([ 0,  1,  2, ..., 97, 98, 99])

c=np.fromfile("b.dat",dtype=np.int,sep=",").reshape(5,10,2)

c
Out[8]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        ..., 
        [14, 15],
        [16, 17],
        [18, 19]],

       [[20, 21],
        [22, 23],
        [24, 25],
        ..., 
        [34, 35],
        [36, 37],
        [38, 39]],

       [[40, 41],
        [42, 43],
        [44, 45],
        ..., 
        [54, 55],
        [56, 57],
        [58, 59]],

       [[60, 61],
        [62, 63],
        [64, 65],
        ..., 
        [74, 75],
        [76, 77],
        [78, 79]],

       [[80, 81],
        [82, 83],
        [84, 85],
        ..., 
        [94, 95],
        [96, 97],
        [98, 99]]])

 NumPy的便捷文件存取

np.save(frame,array)或np.savez(frame,array)

frame:文件名,以.npy为扩展名,压缩扩展名为.npz

array:数组变量

np.load(frame)

NumPy的随机数函数

 np.random的随机数函数

rand(d0,d1,...dn)根据d0-dn创建随机数数组,浮点数,[0,1),均匀分布

randn(d0,d1,...dn)根据d0-dn创建随机数数组,标准正态分布

randint(low[,high,shape])根据shape创建随机整数或整数数组,范围是[low,high)

seed(s)随机数种子,s是给定的种子值

import numpy as np

np.random.seed(10)

np.random.randint(100,200,(3,4))
Out[3]: 
array([[109, 115, 164, 128],
       [189, 193, 129, 108],
       [173, 100, 140, 136]])

np.random.randint(100,200,(3,4))
Out[4]: 
array([[116, 111, 154, 188],
       [162, 133, 172, 178],
       [149, 151, 154, 177]])

np.random.seed(10)

np.random.randint(100,200,(3,4))
Out[6]: 
array([[109, 115, 164, 128],
       [189, 193, 129, 108],
       [173, 100, 140, 136]])
View Code

 shuffe(a) 根据数组a的第1轴进行随机排列,改变数组x

permutation(a)根据数组a的第1轴产生一个新的乱序数组,不改变数组x

choice(a[,size,replace,p])从一维数组a中以概率p抽取元素,形成size形状新数组,replace表示是否可以重用元素,默认为False

b = np.random.randint(100,200,(8,))

b
Out[9]: array([116, 111, 154, 188, 162, 133, 172, 178])

np.random.choice(n,(3,2))
Traceback (most recent call last):

  File "", line 1, in 
    np.random.choice(n,(3,2))

NameError: name 'n' is not defined


np.random.choice(b,(3,2))
Out[11]: 
array([[111, 188],
       [172, 133],
       [133, 133]])

np.random.choice(b,(3,2),replace=False)
Out[12]: 
array([[172, 116],
       [178, 154],
       [162, 133]])

np.random.choice(b,(3,2),p=b/np.sum(b))
Out[13]: 
array([[172, 111],
       [154, 116],
       [116, 172]])
View Code

Numpy的统计函数

sum(a,axis=None)根据给定轴axis计算数组a相关元素之和,axis整数或元组

mean(a,axis=None)期望

average(a,axis=None,weights=None)加权平均值

std(a,axis=None)标准差

var(a,axis=None)方差

Numpy的梯度函数

 np.gradient(f)计算数组f中元素的梯度,当f为多维时,返回每个维度梯度

a = np.random.randint(0,20,(5))

a
Out[15]: array([11, 10,  9, 15, 18])

np.gradient(a)
Out[16]: array([-1. , -1. ,  2.5,  4.5,  3. ])

 

转载于:https://www.cnblogs.com/kmxojer/p/10799375.html

你可能感兴趣的:(Python数据分析与展示第0&1周学习笔记(北理工 嵩天))