讲解对象:【Colab系列】Colab中的numpy模块
作者:融水公子 rsgz
文章出处:360doc个人图书馆[其他平台均为盗版]
提醒:建议大家电脑浏览我的网页,因为手机浏览网页 代码会自动缩成一行,很不方便
1 首先 我们进入colab
2 先导入这个模块
import numpy as np
我们快捷键Ctrl+enter运行代码 是没有任何反应的 因为只是导入模块 并没有要求输出什么对象,所以说很正常
3 如果你不放心 觉得这可能是一个报错 或者说colab里面没有预先安装numpy模块 那么我换换一个numpy1导入 试试看
分析:里面是报错的 恰恰证明了 numpy模块其实 colab已经准备好了 直接拿来使用就行了 如果我们在windows cmd中 还需要使用下面的命令下载这个模块才能使用
pip install numpy
4 下面要讲解的内容之前 我先简介一下 numpy的一个子模块以及他的作用
numpy.linalg.eigvals(a):计算矩阵的特征值numpy.random.rand(d0, d1, ..., dn):定一个数组,并使用 [0, 1) 区间随机数据填充,这些数据均匀分布%timeit:行魔术 作用就是计算代码运行时间
5 好了 既然了解上上面的基础知识 我们开始运行代码试试
import numpy as np%timeit np.linalg.eigvals(np.random.rand(100,100))--------------------------运行结果:100 loops, best of 3: 9.15 ms per loop
6 大家可能还是不理解这一连串代码的含义,那么我分别 分开执行代码 你们就理解了
import numpy as npnp.random.rand(100,100)-------------------------------------------array([[0.16350277, 0.37400986, 0.10178194, ..., 0.32097701, 0.1236406 , 0.69012982], [0.67244973, 0.80989966, 0.69928216, ..., 0.08353696, 0.79816447, 0.43216955], [0.04322875, 0.91927785, 0.96039567, ..., 0.18260893, 0.65009984, 0.49157518], ..., [0.96179641, 0.15853575, 0.47757648, ..., 0.31463299, 0.92844948, 0.22672775], [0.84039929, 0.44765895, 0.18632183, ..., 0.42013944, 0.63246891, 0.19807404], [0.95109206, 0.28346964, 0.79318681, ..., 0.3488978 , 0.72919665, 0.52356033]])
计算矩阵特征值
import numpy as npnp.linalg.eigvals(np.random.rand(100,100))----------------------------------------------array([ 4.98688515e+01+0.j , 2.94358500e+00+0.39372138j, 2.94358500e+00-0.39372138j, 2.35359342e+00+1.66540043j, 2.35359342e+00-1.66540043j, -2.08735822e+00+2.02297922j, -2.08735822e+00-2.02297922j, 2.61822599e+00+0.48100139j, 2.61822599e+00-0.48100139j, 2.52007100e+00+0.82237737j, 2.52007100e+00-0.82237737j, 2.18774082e+00+1.5428337j , 2.18774082e+00-1.5428337j , -2.86232242e+00+0.j , 1.29284401e+00+2.40761747j, 1.29284401e+00-2.40761747j, -2.62728724e-01+2.74054182j, -2.62728724e-01-2.74054182j, 3.90579355e-01+2.64091009j, 3.90579355e-01-2.64091009j, -2.63565758e+00+0.10544969j, -2.63565758e+00-0.10544969j, -1.92677751e+00+1.80026535j, -1.92677751e+00-1.80026535j, -1.20257141e+00+2.26796126j, -1.20257141e+00-2.26796126j, 8.57605035e-01+2.34251948j, 8.57605035e-01-2.34251948j, -2.32258644e+00+0.73203228j, -2.32258644e+00-0.73203228j, 1.24517400e+00+2.10278497j, 1.24517400e+00-2.10278497j, -6.58807923e-01+2.37933184j, -6.58807923e-01-2.37933184j, -1.90068618e+00+1.28159438j, -1.90068618e+00-1.28159438j, -3.94619768e-01+2.28014446j, -3.94619768e-01-2.28014446j, 1.47024478e+00+1.65197049j, 1.47024478e+00-1.65197049j, -9.75808102e-01+2.01345677j, -9.75808102e-01-2.01345677j, 3.60730899e-02+2.20199655j, 3.60730899e-02-2.20199655j, -1.30087924e+00+1.58701533j, -1.30087924e+00-1.58701533j, -1.96926219e+00+0.33887089j, -1.96926219e+00-0.33887089j, 1.67697895e+00+1.06938718j, 1.67697895e+00-1.06938718j, -1.53689480e+00+1.03937219j, -1.53689480e+00-1.03937219j, -1.84884558e+00+0.j , 1.77648815e+00+0.59702772j, 1.77648815e+00-0.59702772j, -1.61232897e+00+0.76028212j, -1.61232897e+00-0.76028212j, 4.35460936e-01+1.72786858j, 4.35460936e-01-1.72786858j, 1.59237786e+00+0.j , 9.01864182e-01+1.44284542j, 9.01864182e-01-1.44284542j, -8.79438533e-01+1.50520595j, -8.79438533e-01-1.50520595j, 1.46895682e+00+0.68125497j, 1.46895682e+00-0.68125497j, -6.39873806e-01+1.62912958j, -6.39873806e-01-1.62912958j, -1.65966584e+00+0.11489318j, -1.65966584e+00-0.11489318j, 1.36418785e+00+0.j , 1.08037042e+00+0.97289752j, 1.08037042e+00-0.97289752j, 1.87854535e-01+1.53337093j, 1.87854535e-01-1.53337093j, 1.17840609e+00+0.j , -1.32999101e-01+1.41908441j, -1.32999101e-01-1.41908441j, -2.56589953e-01+1.34938522j, -2.56589953e-01-1.34938522j, 6.40772344e-01+1.04624058j, 6.40772344e-01-1.04624058j, 4.26505612e-01+1.08850977j, 4.26505612e-01-1.08850977j, -9.33501429e-01+0.8121111j , -9.33501429e-01-0.8121111j , -2.26618289e-01+0.86841114j, -2.26618289e-01-0.86841114j, 6.38558850e-01+0.39603606j, 6.38558850e-01-0.39603606j, 2.81993337e-01+0.24234895j, 2.81993337e-01-0.24234895j, 5.77502304e-01+0.49656662j, 5.77502304e-01-0.49656662j, -5.88368540e-01+0.22915341j, -5.88368540e-01-0.22915341j, -4.54324260e-01+0.49207133j, -4.54324260e-01-0.49207133j, -6.10408376e-01+0.05428372j, -6.10408376e-01-0.05428372j])
谢谢大家的支持!可以点击我的头像,进入我的空间浏览更多文章呢。建议大家360doc注册一个账号登录,里面真的有很多优秀的文章,欢迎大家的到来。
---