numpy的学习

本文深入探讨了Python中NumPy库的应用,包括数组操作、数学运算、数据处理技巧及图表生成方法。通过丰富的实例,展示了如何高效地进行数据处理与可视化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#Python的学习:numpy学习

import numpy as np

import matplotlib as mpl

from mpl_toolkits.mplot3d import Axes3D

from matplotlib import cm

import time

from scipy.optimize import leastsq

from scipy import stats

import scipy.optimize as opt

import matplotlib.pyplot as plt

from scipy.stats import norm, poisson

from scipy.interpolate import BarycentricInterpolator

from scipy.interpolate import CubicSpline

import scipy as sp

import math  

L=[1,2,3,4,5]

print "L= ",L

b=np.array(L)  通过narry函数传递数组对象

print "b= ",b

c=np.array([[12,3,4,],[3,5,6,7],[6,7,8,9]])  narry函数传递多维数组

print'c= ',c

print c

print c.shape   输出数字的形状(几维)

c.shape=( 4,3)  强制转化数组维数 (将以前的3行4列转化成4行3列)

print c

d=c.reshape((2,6))  软转换维为2行6列

print "d= \n",d

d[1][3]=20     修改数组中某一具体的值(0行0列为起始)

print d

print d.dtype   输出d类型

c=np.array([[1,2,3,4],[3,5,6,7],[6,7,8,9]],dtype=np.complex)   输出额的数组中,元素要以复数 的形式展现(float是浮点数的形式输出)

print c

v=np.arange(1,10,0.5)    arange函数是通过指定起始值,终止值,步数创建数组

print v

g=np.linspace(1,10,10,endpoint=False)   linspace函数通过起始值 ,终止值和元素个数创建数组,endpoint=False不包括终止值等差数列

print g

np.set_printoptions(suppress=True,linewidth=150) 设置窗口大小,为使输出在一行展现

f=np.logspace(0,10,11,endpoint=True,base=2) 创建起始值为2**0,终止值2**10包括终止值输出11个值的等比数列



print f

x=[1,2,3,4,5,6,7,8,9]

print x[1:5:2]            输出从位置1搜索到位置5结束,步数为2

print x[::-1]             倒转过来

np.set_printoptions(linewidth=200)

a=np.random.rand(10)  随机产生10个数

print a

print a>0.5    随机产生的数与0.5作比较生成布尔值数组

print a[a>0.5]   将大于0.5的数搜索出来存储在a中

a[a>0.5]=0.5   将大于0.5的数全部修改成0.5

print a

 # 3.3 二维数组的切片

    #  输出 [[ 0  1  2  3  4  5]

    #  [10 11 12 13 14 15]

    #  [20 21 22 23 24 25]

    #  [30 31 32 33 34 35]

    #  [40 41 42 43 44 45]

    #  [50 51 52 53 54 55]]

    # a = np.arange(0, 60, 10)    # 行向量

    # print 'a = ', a

    # b = a.reshape((-1, 1))      # 转换成列向量

    # print b

    # c = np.arange(6)  行向量

    # print c

    # f = b + c   # 行 + 列

    # print f

    # # 合并上述代码:

    # a = np.arange(0, 60, 10).reshape((-1, 1)) + np.arange(6)

    # print a

a=np.arange(0,60,10).reshape((-1,1))+np.arange(6)

print a

print a[[0,1,2],[2,3,4]] 筛选0行2元素1行3元素2行,4元素

print a[4,[3,4,5]]      4行3,4,5元素

print a[4:,[3,4,5]] 4行及以下行3,4,5元素

 i = np.array([True, False, True, False, False, True]) 保留0,2,5行元素

print a[i]         输出0,2,5行元素

print a[i, 3] 输出0,2,5行元素的第3列



4.1 numpy与Python数学库的时间比较

  for j in np.logspace(0, 7, 8):    logspace()默认为10为指数0次幂,7次幂取8个等比数列

的值,j依次取10的0次幂,10的1次幂。。。。。一直到10的7次幂

  x = np.linspace(0, 10, j)

 start = time.clock()            计起始时间

 y = np.sin(x)

 t1 = time.clock() - start         time.clock()为现在的时间,

 x = x.tolist()                    将X由numpy 转化为Python中

 start = time.clock()

 for i, t in enumerate(x): python中计算sin(X)

 x[i] = math.sin(t)

t2 = time.clock() - start

 print j, ": ", t1, t2, t2/t1            花费的时间





 4.2 元素去重

    # 4.2.1直接使用库函数

    # a = np.array((1, 2, 3, 4, 5, 5, 7, 3, 2, 2, 8, 8))

    # print '原始数组:', a

    # # # 使用库函数unique去重

    # b = np.unique(a)

    # print '去重后:', b

    # # 4.2.2 二维数组的去重,结果会是预期的么?

    # c = np.array(((1, 2), (3, 4), (5, 6), (1, 3), (3, 4), (7, 6)))

    # print '二维数组:\n', c

# print '去重后:', np.unique(c)   不是 输出为[1,2,3,4,5,6,7]

 # # 4.2.3 方案1:转换为虚数

    # # r, i = np.split(c, (1, ), axis=1)

    # # x = r + i * 1j

    # x = c[:, 0] + c[:, 1] * 1j

    # print '转换成虚数:', x

    # print '虚数去重后:', np.unique(x)

    # print np.unique(x, return_index=True)   #   return_index 指示位置

    # idx = np.unique(x, return_index=True)[1]

    # print '二维数组去重:\n', c[idx]

  # # 4.2.3 方案2:利用set

    # print '去重方案2:\n', np.array(list(set([tuple(t) for t in c])))



# 4.3 stack and axis a = np.arange(1, 7).reshape((2, 3))

 b = np.arange(11, 17).reshape((2, 3))

    c = np.arange(21, 27).reshape((2, 3))

    d = np.arange(31, 37).reshape((2, 3))

    print 'a = \n', a

    print 'b = \n', b

    print 'c = \n', c

    print 'd = \n', d

    s = np.stack((a, b, c, d), axis=0)

    print 'axis = 0 ', s.shape, '\n', s

    s = np.stack((a, b, c, d), axis=1)

    print 'axis = 1 ', s.shape, '\n', s

    s = np.stack((a, b, c, d), axis=2)

    print 'axis = 2 ', s.shape, '\n', s



a = np.arange(1, 10).reshape(3,3)

    # print a

    # b = a + 10   给a数组中每个元素加10

    # print b

    # print np.dot(a, b)  数组a和数组b按矩阵乘法规则相乘

    # print a * b        数组a和数组b按对应元素相乘



    # a = np.arange(1, 10)

    # print a

    # b = np.arange(20,25)

    # print b

    # print np.concatenate((a, b))   将数组a和数组b连接起来,构成一个数组

结果:[1,2,3,4,5,6,7,8,9,20,21,22,23,24 ]   (不能用append()函数,主要原因是numpy是用C语言写的)



 5.绘图

    # 5.1 绘制正态分布概率密度函数

    mpl.rcParams['font.sans-serif'] = [u'SimHei']  #FangSong/黑体 FangSong/KaiTi  字体

    mpl.rcParams['axes.unicode_minus'] = False     图像上的负数的负号

    # mu = 0     取值  

    # sigma = 1

    # x = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 51)   x取的数组

    # y = np.exp(-(x - mu) ** 2 / (2 * sigma ** 2)) / (math.sqrt(2 * math.pi) * sigma)   正态分 布方程式

    # print x.shape   

    # print 'x = \n', x

    # print y.shape

    # print 'y = \n', y

    # plt.figure(facecolor='w')        背景设置成白色

    # plt.plot(x, y, 'ro-', linewidth=2)   “r”表示红色,‘o’表示XY值用圆圈表示,”-”表示用实线表示  线宽为2(上面个下面这句代码随便用一个)

    # # plt.plot(x, y, 'r-', x, y, 'go', linewidth=2, markersize=8)   ‘r-’为红色的实线,‘go’为

绿色的圈 g 为绿色也可以用r红色b蓝色  o表示圈 *表示五角星(XY之间的点)

    # plt.xlabel('X', fontsize=15)     横轴表示为X

    # plt.ylabel('Y', fontsize=15)    纵轴表示为Y

    # plt.title(u'高斯分布函数', fontsize=18)    #标题

    # plt.grid(True)   单网格

    # plt.show()      图像





5.2 损失函数:Logistic损失(-1,1)/SVM Hinge损失/ 0/1损失

    # plt.figure(figsize=(10,8),facecolor=’w’)     背景为白色

    # x = np.linspace(start=-2, stop=3, num=1001, dtype=np.float)   X数组

    # y_logit = np.log(1 + np.exp(-x)) / math.log(2)   Y数组

    # y_boost = np.exp(-x)                      提升函数

    # y_01 = x < 0               

    # y_hinge = 1.0 - x

    # y_hinge[y_hinge < 0] = 0

    # plt.plot(x, y_logit, 'r-', label='Logistic Loss', linewidth=2)   label为做标记表明哪种线 线宽为2

    # plt.plot(x, y_01, 'g-', label='0/1 Loss', linewidth=2)

    # plt.plot(x, y_hinge, 'b-', label='Hinge Loss', linewidth=2)

    # plt.plot(x, y_boost, 'm--', label='Adaboost Loss', linewidth=2)

    # plt.grid()                         单网格

    # plt.legend(loc='upper right')          将解释窗口放到右上角 (left左上角)   

    # plt.savefig('1.png')          将该图保存到‘1.png’路径中

    # plt.show()                  展现该图

5.4 胸型线

    # x = np.arange(1, 0, -0.001)

    # y = (-3 * x * np.log(x) + np.exp(-(40 * (x - 1 / np.e)) ** 4) / 25) / 2

    # plt.figure(figsize=(5,7), facecolor='w')

    # plt.plot(y, x, 'r-', linewidth=2)

    # plt.grid(True)

    # plt.title(u'胸型线', fontsize=20)

    # # plt.savefig('breast.png')

    # plt.show()



# 6. 概率分布

    # 6.1 均匀分布

    # x = np.random.rand(10000)   随机产生10000个随机数

    # t = np.arange(len(x))

    # # plt.hist(x, 30, color='m', alpha=0.5, label=u'均匀分布')

    # plt.plot(t, x, 'g.', label=u'均匀分布')      ‘g’为绿色 label显示图像的名字

    # plt.legend(loc='upper left')              绘制直方图

    # plt.grid()                             单网格

    # plt.show()





7. 绘制三维图像

    # x, y = np.mgrid[-3:3:7j, -3:3:7j]

    # print x

    # print y

    # u = np.linspace(-3, 3, 101)

    # x, y = np.meshgrid(u, u)     最重要:例子

X取[-3,-2,-1,0,1,2,3]Y等于-3,X取[-3,-2,-1,0,1,2,3]Y 等于-2,一次下去,X取[-3,-2,-1,0,1,2,3]y等于3,

# print x

    # print y

    # z = x*y*np.exp(-(x**2 + y**2)/2) / math.sqrt(2*math.pi)

    # # z = x*y*np.exp(-(x**2 + y**2)/2) / math.sqrt(2*math.pi)

    # fig = plt.figure()

    # ax = fig.add_subplot(111, projection='3d')

   







 # # ax.plot_surface(x, y, z, rstride=5, cstride=5, cmap=cm.coolwarm, linewidth=0.1)   rstride=5, cstride=5, 行、列的采样步数越密越好 cmap=cm.coolwarm    涂色

    # ax.plot_surface(x, y, z, rstride=3, cstride=3, cmap=cm.gist_heat, linewidth=0.5)

    # plt.show()

    # # cmaps = [('Perceptually Uniform Sequential', (老师给的各种涂色颜色)

    # #           ['viridis', 'inferno', 'plasma', 'magma']),

    # #          ('Sequential', ['Blues', 'BuGn', 'BuPu',

    # #                          'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd',

    # #                          'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu',

    # #                          'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),

    # #          ('Sequential (2)', ['afmhot', 'autumn', 'bone', 'cool',

    # #                              'copper', 'gist_heat', 'gray', 'hot',

    # #                              'pink', 'spring', 'summer', 'winter']),

    # #          ('Diverging', ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr',

    # #                         'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral',

    # #                         'seismic']),

    # #          ('Qualitative', ['Accent', 'Dark2', 'Paired', 'Pastel1',

    # #                           'Pastel2', 'Set1', 'Set2', 'Set3']),

    # #          ('Miscellaneous', ['gist_earth', 'terrain', 'ocean', 'gist_stern',

    # #                             'brg', 'CMRmap', 'cubehelix',

    # #                             'gnuplot', 'gnuplot2', 'gist_ncar',

    # #                             'nipy_spectral', 'jet', 'rainbow',

    # #                             'gist_rainbow', 'hsv', 'flag', 'prism'])]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值