的锂电池.为什么手机不能用两节镍氢充电电池和锂电池

电池技术 - 知乎暂无话题描述关注话题分享阅读全文8K705 条评论分享收藏感谢阅读全文2.9K429 条评论分享收藏感谢阅读全文2.3K387 条评论分享收藏感谢阅读全文1.7K287 条评论分享收藏阅读全文1.4K93 条评论分享收藏感谢<strong class="NumberBoard-itemValue" title="为什么手机都用锂电池而不是镍氢电池_百度知道
为什么手机都用锂电池而不是镍氢电池
写上自己见解即可,拒绝复制粘贴
我有更好的答案
首先,反复从充电不会对电池有损害,反复冲放电对电池基本上是没有损害的,因锂元素比较活跃并且无记忆效应,所以用锂元素制造的电池可以反复的冲放电.现在大多数人(90%以上)的思维还保留在几年前流行的镍氢电池的阴影中,因为镍元素本身不活跃,并且元素对电子具有记忆效果,即:记忆上次冲放电深度!所以如果不是把电池用完或者长时间充电(充分激发镍元素-因其不活跃),则不能达到最好的充电效果,所以一般充电时间都在一夜或者多少小时以上!现在用的是锂元素,该元素在自然界都不能独立存在,只能存在于化合物中,可以查阅元素周期表中,锂元素半衰期是很短的,并且无记忆效应,所以锂电池在使用过程中不可以长时间冲电和完全放电,因为长时间充电会使本身就很活跃的元素在电子饱和的状态下发生剧烈运动,同时拌有爆炸的产生,即:充电时间最好为2-4个小时,最好不要超过4个小时(元素比较活跃,充电激发很快,充电时间也相应的缩短,就算是第一次充电也是只要2个小时到4个小时,因锂元素已经不需要激活了,长时间充电反而对影响锂元素在充放电过程中的活跃程度),顺便说一下锂电池的充电过程,充电过程中我们会在1个半小时或者2个小时时发现已经满,此时再冲半个小时就好了,因为充电是先电压冲完毕,然后是电子充完毕,要慢大约半个小时的!用电时也最好不要完全用光再充电,因为用光了,锂元素所需要的反向反应所需要的化合物不足,无法完全充电,造成电池电容下降!所以,综合而言,手机锂电池是现代发明的人性化的电池,它和笔记本电脑\MP3等电子产品中的电池是一样的,基本上不用管它的充电和放电,要不现在科技发达的今天,整天为了电池充电头疼,也太和今天的科技电子发展不适应啊,创造者们也不会甘心的,所以放心用吧,说了这么多,就是要让大家了解,锂电池内容概括如下:1.充电时间2-4个小时(不可以太长,时间长了会严重影响电池寿命和发生危险)。2.第一次充电也不可以时间长,和平时充电一样即可(不要进入镍氢电池误区)。3.不可以完全用光电(完全用光,对电池寿命也有严重影响)一个锂电池能深度放电的次数也就那么几次,多了就不好了。4.使用过程中,看到快没有电了,觉的不够用了就冲,能充满最好,冲不满也不要紧!)5. 随用随充,随充随用!这可是锂电池发明者的口号,要是和镍氢电池一样充电如此麻烦。
采纳率:87%
来自团队:
锂电相比镍氢有很多优势。1.锂电的能量体积比高于镍氢电池,能量质量比也高于镍氢电池,这样采用锂电可有效降低手机质量和体积。2.锂电的形状多样化,可以按需求开发各种形状,镍氢电池则形状固定(基本都是圆柱形)。3.锂电使用温度范围比镍氢电池宽,镍氢电池在低温和高温条件下容量损失远大于锂电。主要就是以上几点原因,其实从成本的角度说,锂电还是相对成本较高的,安全性也差一些,但由于其优点突出,所以手机多数都采用锂电池,只是有少部分销往非洲等欠发达地区的手机有在用镍氢(飞利浦有机型采用镍氢电池)。
本回答被提问者采纳
原因有二。第一,镍氢电池的体积较大,影响手机的外观,也影响手机的重量。锂电池比镍氢电池轻便,同等电量体积小了很多。第二,锂电池可以随充随用了,不用像以前用完了才能冲。消除了记忆效应,电池方便多了。
锂电池的尺寸范围更大, 镍氢电池一般都是圆柱形的, 而且锂电单颗电压3.7v比镍氢的1.2V高很多,如果相同电压镍氢的体积更大,锂电池比镍氢电池环保
镍氢电池能量和体积之比没有锂电池高。
其他2条回答
为您推荐:
其他类似问题
您可能关注的内容
镍氢电池的相关知识
&#xe675;换一换
回答问题,赢新手礼包&#xe6b9;
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。镍氢电池充电器可以给锂电池充电吗_百度知道
镍氢电池充电器可以给锂电池充电吗
我有更好的答案
不可有,两者充电电压完全不一样,镍氢电池1.4V,锂电池4.2V。如有帮助请采纳,手机则点击右上角的满意,谢谢!!
如果你真的懂 那就继续回答我吧
我是把对讲机里的6节1.2V氢电池拆掉(注.7.2V) 然后在某宝买的两节手机用的锂电池芯 串联成7.4伏然后接上我买的7.4伏的保护板
单节氢电池充满应该是1.4伏 锂电池单个充满应该是4.2伏
我算了算差不多 我就是想知道氢电池和锂电池充电器的性质是不是一样!
如果你真的懂 那就继续回答我吧
我是把对讲机里的6节1.2V氢电池拆掉(注.7.2V) 然后在某宝买的两节手机用的锂电池芯 串联成7.4伏然后接上我买的7.4伏的保护板
单节氢电池充满应该是1.4伏 锂电池单个充满应该是4.2伏
我算了算差不多 我就是想知道氢电池和锂电池充电器的性质是不是一样!
两种电池性质不一样。你用2节锂电池代替6节镍氢电池的方法是可取的。但是充电器不太一样,最好是用锂电池专用充电器。用镍氢电池充电器充电会把锂电池充爆的。如有帮助请采纳,手机则点击右上角的满意,谢谢!!
不一样在那 ?
麻烦说清楚点吧 谢谢
锂电池的保护板到底是什么作用
我会加分的
充电机理不一样,充电电流不一样,充满电后的检测方法也不一样。锂电池保护板的作用是:防止电池大电流、过电压、过电流放电,以至于损坏电池。 如有帮助请采纳,手机请点击右上角的满意,谢谢!!
也就是说保护板只是保护放电 不保护充电?
采纳率:84%
来自团队:
为您推荐:
其他类似问题
您可能关注的内容
锂电池的相关知识
&#xe675;换一换
回答问题,赢新手礼包&#xe6b9;
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。3.2v铁锂电池充电器可以充2节镍氢电池吗?_百度知道
3.2v铁锂电池充电器可以充2节镍氢电池吗?
镍镉电池截止电压1.65v
我有更好的答案
不能直接充,电压高了,临时充可以用两个二极管降压后对两节镍氢充,但不建议长期这样使用!
采纳率:29%
没关系,可以直接冲,但锂铁充电器电压时3.6伏,所以注意时间不要太长。因为镍氢电池没有电压敏感性,不会过压损坏,而且充满会以热量形势消耗,所以一般此类电池充电器充满自停的设计不多。
不能,镍氢电池截止电压1.42v左右
为您推荐:
其他类似问题
您可能关注的内容
铁锂电池的相关知识
&#xe675;换一换
回答问题,赢新手礼包&#xe6b9;
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。&p&学习这种实践性很强的技术,只看书其实是一件信噪比非常低的事情。在 DataCastle 的另一个回答中,详细给出了一个快速入门数据分析师的学习路径,如果你有兴趣,可以看看:&a href=&https://www.zhihu.com/question//answer/& class=&internal&&如何快速成为数据分析师&/a&&/p&&p&当然,读书也有读书的学习方法,推荐的书不多,但保证每一本都值得看,更重要的是,给你一条合理的学习路径,并配合一些另外的学习资源,你能对核心的知识、实践的技巧有更好的理解。(以下推荐的书并非都是必读,也并非适合每一个人,你处于什么阶段,未来发展的方向是什么,需要自己来决定,对每本书详细说明是希望能购帮助你更好地取舍。)&/p&&p&&br&&/p&&h2&&b&一、数据分析入门&/b&&/h2&&p&&br&&/p&&ol&&li&&a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject//& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&深入浅出数据分析 (豆瓣)&/a&&/li&&/ol&&figure&&img src=&https://pic2.zhimg.com/v2-a1b749df64af612d4e57e028b009f409_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1376& data-rawheight=&540& class=&origin_image zh-lightbox-thumb& width=&1376& data-original=&https://pic2.zhimg.com/v2-a1b749df64af612d4e57e028b009f409_r.jpg&&&/figure&&p&把这本书放在第一顺序,是因为它真的很简单,但是能够让你对数据分析的一些基本概念有大致的了解。即便是你毫无数据分析经验,一两天也足够读完整本书了。这本书的实操性并不强,所以也不建议你去跟着去实践,了解作者传达出来的数据分析基本思想和原则就ok了,这对你建立宏观的视野,和接下来的学习很有帮助。&/p&&p&另外,书中提到的一些案例,比如提升化妆品销量、分析星巴克销量、生产线最优解、网站AB test、竞品分析、薪资预测等等,看起来很简单,但其实都是工作最常见的一些分析场景。这对数据思维的养成,非常有帮助。&/p&&p&个人觉得书中最有用的一些点:&/p&&ul&&li&统计学概念在数据分析中的作用:比如方差、标准差、相关系数、均方根误差等;&/li&&li&集中数据分析的基本方法:假设检验、回归分析、误差分析等;&/li&&li&统计图形分析:散点图、直方图等来探索数据中隐藏的规律;&/li&&li&数据库以及数据整理。&/li&&/ul&&p&这本书很难让你掌握数据分析技术,过一遍即可。但它会大大降低你之后学习的一些阻力,因为接下来推荐的这本,真的很厉害,但也有一定难度。&/p&&p&&br&&/p&&p&&br&&/p&&p&2. &a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject//& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&利用Python进行数据分析 (豆瓣)&/a&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-4b3dc1e462_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1370& data-rawheight=&494& class=&origin_image zh-lightbox-thumb& width=&1370& data-original=&https://pic3.zhimg.com/v2-4b3dc1e462_r.jpg&&&/figure&&p&《利用Python进行数据分析》应该是最经典的数据分析书之一了,作者是 pandas 库的作者 Wes McKinney。所以这本书对于 pandas 的理解,应该是非常深刻的,而利用梳理介绍的 pandas、numpy、matplotlib 等库,应对一般的数据分析,完全足够了。&/p&&p&书中应该重点掌握的一些点:&/p&&ul&&li&IPython Notebook 的使用:最适合小白的代码编写环境,非常容易上手;&/li&&li&科学计算库 Numpy:数组和矢量计算、学会利用数组进行数据处理;&/li&&li&数据处理及分析工具 pandas:数据查询,缺失值、重复值、异常值的处理,数据的合并与规整化,基本的描述性分析及可视化;&/li&&li&可视化工具 matplotlib:用这个库,基本的数据可视化问题皆可以解决了。&/li&&/ul&&p&看上去是不是很简单,这本书就是教会你如何开始使用 Python 进行数据分析,当然首推的就是 pandas,不仅可以做数据的预处理,还能够做基本的数据分析和可视化。这个库一定是你开始入门的时候需要重点学习的,其次用 Numpy 进行数组的计算、利用 matplotlib 进行可视化的描述性分析,也是同步需要掌握的东西。&/p&&p&但是,这个部分光看书是远远不够的,你可以尽量去找一些可以练手的数据集,来实际操作和调用这些库的功能,确保熟练数据分析中最常用的函数和模块。如果纠结去哪找练手的数据,推荐&a href=&//link.zhihu.com/?target=http%3A//archive.ics.uci.edu/ml/datasets.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&UCI经典数据集&/a&。&/p&&p&关于 pandas、Numpy、matplotlib 网上应该可以搜索到很多有用的资料、教程,可以看一看别人的使用技巧、应用场景,并通过联系转化成自己的经验。&/p&&p&因为 Python 库的更新迭代非常快,这本书里的一些内容其实已经“过时”了,这里也非常建议你去查看一些官方的文档,基本上你需要的都能查到。&/p&&p&&a href=&//link.zhihu.com/?target=http%3A//pandas.pydata.org/pandas-docs/stable/tutorials.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&pandas 官方教程文档&/a&&/p&&p&&a href=&//link.zhihu.com/?target=https%3A//docs.scipy.org/doc/numpy-dev/user/quickstart.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Numpy 官方教程文档&/a&&/p&&p&&a href=&//link.zhihu.com/?target=http%3A//matplotlib.org/resources/index.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&matplotlib 官方推荐教程文档&/a&&/p&&p&另外,在进行一些数据处理、数据分析的时候,你可能需要去了解一些更细节的 Python 的用法,这里就不推荐书了,因为你没必要去系统地学,按照这个菜鸟教程看看或者查询相关的用法就OK了。&/p&&p&&a href=&//link.zhihu.com/?target=http%3A//www.runoob.com/python3/python3-tutorial.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Python3 教程 | 菜鸟教程&/a&&/p&&p&&br&&/p&&p&&br&&/p&&p&3. &a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject/7056708/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&深入浅出统计学 (豆瓣)&/a&&/p&&figure&&img src=&https://pic2.zhimg.com/v2-4ff1df5d27e2d99679aa2d_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1344& data-rawheight=&438& class=&origin_image zh-lightbox-thumb& width=&1344& data-original=&https://pic2.zhimg.com/v2-4ff1df5d27e2d99679aa2d_r.jpg&&&/figure&&p&非常非常基础的统计书,适合任何一个没有基础的小白,文科生也能看懂。有人说这本书简直太简单了,但是对于数据分析来说,需要用到的恰好是这些最简单的东西。比如基本的统计量,基本上每个分析项目中都会用到。比如基本的概率分布,总体与样本的概念、置信区间、假设检验、回归分析,我去,都是为数据分析定制的统计学知识。&/p&&p&所以强烈推荐这本,其他的比较深度的书,并不建议在入门的阶段去啃,一方面是很多难以理解,二是即便你花大力气学习了,入门的阶段你也不怎么能在实践中使用。当然多学一些是没有坏处的,但你需要知道在什么时候学习哪些东西性价比最高。或者你自我感觉良好,誓要学最难的,从入门到放弃,得不偿失。&/p&&p&总结起来,需要重点了解的统计学知识如下:&/p&&ul&&li&基本的统计量:均值、中位数、众数、方差、标准差、百分位数等;&/li&&li&概率分布:几何分布、二项分布、泊松分布、正态分布等;&/li&&li&总体和样本:了解基本概念,抽样的概念;&/li&&li&置信区间与假设检验:如何进行验证分析;&/li&&li&相关性与回归分析:一般数据分析的基本模型。&/li&&/ul&&p&这本书非常的简单,但是基本的数据分析的一些方法都有了,你需要做的,是利用之前学过的 Python 的一些库(pandas、Numpy、matplotlib)来实现这些数据分析的方法、实现基本的可视化来进行图形化的分析。&/p&&p&&br&&/p&&p&&br&&/p&&p&4. &a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject//& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&商务与经济统计学 (豆瓣)&/a&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-1c982acb87be7162fdfef_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1368& data-rawheight=&396& class=&origin_image zh-lightbox-thumb& width=&1368& data-original=&https://pic4.zhimg.com/v2-1c982acb87be7162fdfef_r.jpg&&&/figure&&p&当然,如果在后续的数据分析中,你觉得统计学的知识掌握得不是扎实,或者你非常重视分析结果中蕴含的统计学规律和原理,那么你可以更深入地了解概率统计的知识,推荐这本《商务与经济统计学》,当然吴喜之的《统计学》也非常好,选择一本即可,不必纠结。&/p&&p&相比《深入浅出统计学》强调易于理解,本书更加强调统计学的思想养成,所以适合有一些经验之后进行提高。很多知识你前期学习可能并不好理解其本质,而当你实际做一些数据分析项目之后,再来看这些本质的思想和原理,会得到更多的启发:哦,原来如此!&/p&&p&不说重点了,整本书都是重点。如果你想跟其他的分析师拉开差距,那么你就需要在有基础之后或者遇到瓶颈的时候再来看看这些经典的思想,没准能给你不少的启发。&/p&&p&&br&&/p&&p&&br&&/p&&p&5. &a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject/3354490/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&MySQL必知必会 (豆瓣)&/a&&/p&&figure&&img src=&https://pic1.zhimg.com/v2-dc492b0eb5de0_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1350& data-rawheight=&510& class=&origin_image zh-lightbox-thumb& width=&1350& data-original=&https://pic1.zhimg.com/v2-dc492b0eb5de0_r.jpg&&&/figure&&p&这本书把SQL写的非常简单,没办法,SQL确实也很简单。其实 pandas 就已可以实现很多数据管理的工作,而了解 SQL 的意义在于融入到实际的数据使用的场景。比如企业的数据,多是以数据库的形式存储起来的,那么如果你需要去调用你需要的那部分数据,那么 SQL 就是必须的技能。如果你在最开始就想用公司的数据来练习,那么你可以把这本书的阅读放到最前面。(当然,如果你不会遇到数据提取的问题,SQL这部分也可以暂时不管,对具体的数据分析没有影响,等到你真正需要用到SQL的时候再学习。)&/p&&p&MySQL 本身比较简单,对于数据分析师来说,只需要掌握基本的语句和技巧,能够进行基本的数据提取和处理就能够应对一般的数据分析需求了。&/p&&p&书中重点掌握以下几个点:&/p&&ul&&li&SELECT语句:让你能够去提取你需要的那部分数据;&/li&&li&DELETE和UPDATE:知道怎么实现数据的增、删、改;&/li&&li&数据过滤:where、and、or、通配符等过滤方式;&/li&&li&数据的汇总和分组、数据库连接:应对更加复杂的数据和相关联的数据;&/li&&li&子查询:查询中的查询。&/li&&/ul&&p&当然还是希望你去公司的数据库找一些数据来进行练习,如果不方便的话,也可以直接用上述 UCI数据集中的数据来进行实践。&/p&&p&如果遇到问题,可以去菜鸟教程查询相关操作。&/p&&p&&a href=&//link.zhihu.com/?target=http%3A//www.runoob.com/mysql/mysql-tutorial.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&MySQL 教程 | 菜鸟教程&/a&&/p&&p&&br&&/p&&p&好了,恭喜,你已经基本入门了。到此,你就完全可以去进行一个完整的数据分析项目了,如果你没有头绪,可以去找一些行业的分析报告来看看,找一找分析的思路。能够独立完成一些项目,通过数据分析能够得出一些深刻的结论,能够给人以可视化的形式将结果描述出来,能够基于历史数据对未来的一些情况进行预测,那么一般的数据分析岗位,完全可以胜任了。&/p&&p&&br&&/p&&p&&br&&/p&&p&6. &a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject//& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&数据科学实战 (豆瓣)&/a&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-8cfae582bacdce5e4158056_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1356& data-rawheight=&482& class=&origin_image zh-lightbox-thumb& width=&1356& data-original=&https://pic3.zhimg.com/v2-8cfae582bacdce5e4158056_r.jpg&&&/figure&&p&这本书应该是数据分析和数据挖掘(机器学习)之间的桥梁。从探索性的数据分析开始,通过数据分析的思维,引出了机器学习的基本算法:回归分析、k近邻、k均值。接着通过不同的应场景分别介绍了最常见的机器学习算法,以及在真实场景中的应用。&/p&&p&对于做了一段时间数据分析工作的人,这无疑是进阶更高维度的好书,很难有一本书,能够让你从简单的数据分析平滑地过渡到机器学习和数据挖掘,这本书我认为是这方面做的最好的一本。&/p&&p&所以如果你在做一些探索性的分析遇到瓶颈之后,自然而然会进入数据挖掘和机器学习算法的坑,因为只有更高级的算法和模型,才能够支撑大规模的数据的预测。&/p&&p&下面列出一些书中有意思也比较有用的点:&/p&&ul&&li&了解探索性数据分析,为更高级的需求打好基础;&/li&&li&了解机器学习的基本算法,k近邻、k均值等;&/li&&li&用朴素贝叶斯方法做垃圾邮件的过滤;&/li&&li&线性回归和逻辑回归的分析方法;&/li&&li&如何从数据中获取结论,从数据挖掘竞赛开始;&/li&&li&构建自己的推荐系统;&/li&&li&数据泄漏与模型评价,如何筛选模型。&/li&&/ul&&p&&br&&/p&&p&&br&&/p&&h2&&b&二、R语言数据分析&/b&&/h2&&p&有同学是准备用R来做数据分析,也推荐两本非常不错的书,用 Python 的同学可忽略。&/p&&p&&br&&/p&&ol&&li&&a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject//& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&R语言实战 (豆瓣)&/a&&/li&&/ol&&figure&&img src=&https://pic2.zhimg.com/v2-1a423a743fb6bdf91cfe65_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1334& data-rawheight=&464& class=&origin_image zh-lightbox-thumb& width=&1334& data-original=&https://pic2.zhimg.com/v2-1a423a743fb6bdf91cfe65_r.jpg&&&/figure&&p&如果你是要用R语言来做数据分析,那么读完《深入浅出数据分析》之后,就可以开始读这本书了,深入浅出,可操作性极强。从工具的安装,到具体分析方法在R语言中的实现,可以说是非常的详细,是一本非常值得读的书。&/p&&p&知识点就不罗列了,这是一本学习路径设计非常合理的书,按照作者的思路跟着全部实现一遍,那么基本的数据分析,也就没问题了。在学会R分析的技巧之后,再看看统计学的相关知识,简直起飞。&/p&&p&&br&&/p&&p&&br&&/p&&p&2. &a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject//& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&ggplot2:数据分析与图形艺术 (豆瓣)&/a&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-eb6fff_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1352& data-rawheight=&560& class=&origin_image zh-lightbox-thumb& width=&1352& data-original=&https://pic4.zhimg.com/v2-eb6fff_r.jpg&&&/figure&&p&ggplot2 是 R 中一个非常强大的可视化包,书中有大量的例子,也可以下载源代码。这本书将 ggplot2 的基本原理和操作讲解的非常清楚和系统,可以说 ggplot2 是目前最优秀的数据分析作图工具之一了。其作者Hadley Wickham是学医出身,但在R数据分析及可视化领域的影响力,也非同小可。&/p&&p&因为本书是 ggplot2 作者(同时也是R语言很多好用的包的作者)Hadley大神自己写的,主要讲的是ggplot2绘图的整体语法思想,思路清晰,墙裂推荐。本书的英文版已经出版到第二版了,方法也从原来第一版的qplot更新为ggplot,建议如果英文过了四六级的同学可以直接学习英文版的教材。&/p&&p&所以对于学习R语言数据分析的同学来说,ggplot2 一定是一个不能错过的工具。&/p&&p&&br&&/p&&p&&br&&/p&&h2&&b&三、细分领域业务分析&/b&&/h2&&p&这个部分其实是针对具体的业务场景了,有一些细分领域的书籍,可以给你在具体方向的一些分析的技巧和经验,因为关注的指标、数据不同,所以分析的方法也不尽相同。&/p&&p&&br&&/p&&ol&&li&&a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject//& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&社交网站的数据挖掘与分析 (豆瓣)&/a&&/li&&/ol&&figure&&img src=&https://pic2.zhimg.com/v2-68963ebbfdbb7900fdb1c9_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1358& data-rawheight=&474& class=&origin_image zh-lightbox-thumb& width=&1358& data-original=&https://pic2.zhimg.com/v2-68963ebbfdbb7900fdb1c9_r.jpg&&&/figure&&p&书中介绍了很多社交网站的数据分析方法,如果你在参与社交产品的研发、运营,或者对社交网络的数据感兴趣,那么你可以看看。&/p&&p&书中涉及到 Twitter 等网站的分析会有一定的启发,但其中一些东西因为网站的更新可能不适用了,但是分析方法可以借鉴,你也可以尝试去分析国内的微博之类的社交网络。&/p&&p&&br&&/p&&p&2. &a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject//& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&精益数据分析 (豆瓣)&/a&&/p&&figure&&img src=&https://pic2.zhimg.com/v2-1e38b206fa42d6cb292b5d973eb15de9_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1354& data-rawheight=&520& class=&origin_image zh-lightbox-thumb& width=&1354& data-original=&https://pic2.zhimg.com/v2-1e38b206fa42d6cb292b5d973eb15de9_r.jpg&&&/figure&&p&书中主要讲到各种产品中用到的指标和模型,这是一本写给产品经理的书,其中并没有讲到具体的数据分析技术,涉及到的更多是数据驱动型产品的一些思路。&/p&&p&比如怎么将数据驱动的产品落地,怎么喂产品设计数据指标。哪些指标对于产品迭代优化更有效,如何依靠数据分析来驱动用户增长等等。&/p&&p&&br&&/p&&p&3. &a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject//& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&数据挖掘与数据化运营实战 (豆瓣)&/a&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-1f640edc514d057c8c24bc358bcce763_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1338& data-rawheight=&438& class=&origin_image zh-lightbox-thumb& width=&1338& data-original=&https://pic4.zhimg.com/v2-1f640edc514d057c8c24bc358bcce763_r.jpg&&&/figure&&p&这是一本数据化运营的书,但相比上面一本,涉及到的技术层面的东西要多很多。比一般的数据分析更高级一些数据挖掘算法都有涉及,比如决策树、神经网络、支持向量机、关联规则挖掘等等。&/p&&p&觉得这本书更好的一点是,它介绍了一些数据分析和运营密切相关的一些模型,比如交易模型、风险模型、推荐模型,这些其实跟电商、风控、推荐系统的自动化运营密切相关。&/p&&p&另外还讨论数据挖掘项目的落地、协作、有效性评价等实战,这就上升到产品的大规模数据挖掘了。前期看这本书会觉得特别的枯燥,当你在遇到大规模的真实数据分析、挖掘工作的时候,来看看这本书,或许就能得到一些启发。&/p&&p&&br&&/p&&p&4. &a href=&//link.zhihu.com/?target=https%3A//book.douban.com/subject//& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Python金融大数据分析 (豆瓣)&/a&&/p&&figure&&img src=&https://pic1.zhimg.com/v2-c416ec5b9aed418b3e09c790_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&1334& data-rawheight=&476& class=&origin_image zh-lightbox-thumb& width=&1334& data-original=&https://pic1.zhimg.com/v2-c416ec5b9aed418b3e09c790_r.jpg&&&/figure&&p&在互联网金融行业,或者向往这个方面发展的,可以看看这本,在金融分析方面,时间序列会用的比较多,也建议重点了解这一块。&/p&&p&同时需要去了解一些常见的金融数据分析模型,比如常见的估值框架、衍生品分析库等。&/p&&p&&br&&/p&&p&最后这几本书,不推荐在前期看,并不是没有帮助,而是在相应的领域你有一定的数据分析经验之后,再去看这些书,才能得到更大的收获。&/p&&p&&br&&/p&&hr&&p&如果你觉得看书实操性不强,而且效率比较低的话,正好DC学院也推出了一门数据分析的体系课。提取了目前数据分析类书中最有效的那部分信息,学习路径更加平滑,老师实时答疑。有兴趣可以看看:&a href=&//link.zhihu.com/?target=https%3A//www.dcxueyuan.com/%3Fslxydc%3D3c75bf%23/classDetail/classIntroduce/1& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&数据分析师(入门)-DC学院&/a&&/p&&p&关注公众号(datacastle2016),获取更多数据分析干货。&/p&
学习这种实践性很强的技术,只看书其实是一件信噪比非常低的事情。在 DataCastle 的另一个回答中,详细给出了一个快速入门数据分析师的学习路径,如果你有兴趣,可以看看:当然,读书也有读书的学习方法,推荐的书不多,但保证每一本…
&figure&&img src=&https://pic1.zhimg.com/v2-4d1ef93dd54b7f55aaca30_b.jpg& data-rawwidth=&1000& data-rawheight=&667& class=&origin_image zh-lightbox-thumb& width=&1000& data-original=&https://pic1.zhimg.com/v2-4d1ef93dd54b7f55aaca30_r.jpg&&&/figure&&p&先决条件&br&&/p&&p&在阅读这个教程之前,你多少需要知道点&a href=&http://link.zhihu.com/?target=http%3A//lib.csdn.net/base/11& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Python&/a&。如果你想从新回忆下,请看看&a href=&http://link.zhihu.com/?target=http%3A//docs.python.org/tut/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Python Tutorial&/a&.&/p&&p&如果你想要运行教程中的示例,你至少需要在你的电脑上安装了以下一些软件:&/p&&ul&&li&&a href=&http://link.zhihu.com/?target=http%3A//www.python.org/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Python&/a&&/li&&li&&a href=&http://link.zhihu.com/?target=http%3A//numpy.scipy.org/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&NumPy&/a&&/li&&/ul&&p&这些是可能对你有帮助的:&/p&&ul&&li&&a href=&http://link.zhihu.com/?target=http%3A//ipython.scipy.org/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&ipython&/a&是一个净强化的交互Python Shell,对探索NumPy的特性非常方便。&/li&&li&&a href=&http://link.zhihu.com/?target=http%3A//matplotlib.sourceforge.net/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&matplotlib&/a&将允许你绘图&/li&&li&&a href=&http://link.zhihu.com/?target=http%3A//scipy.org/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Scipy&/a&在NumPy的基础上提供了很多科学模块&/li&&/ul&&h2&基础篇&/h2&&p&NumPy的主要对象是同种元素的多维数组。这是一个所有的元素都是一种类型、通过一个正整数元组索引的元素表格(通常是元素是数字)。在NumPy中维度(dimensions)叫做轴(axes),轴的个数叫做秩(rank)。&/p&&p&例如,在3D空间一个点的坐标[1, 2, 3]是一个秩为1的数组,因为它只有一个轴。那个轴长度为3.又例如,在以下例子中,数组的秩为2(它有两个维度).第一个维度长度为2,第二个维度长度为3.&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&[[ 1., 0., 0.],
[ 0., 1., 2.]]
&/code&&/pre&&/div&&p&NumPy的数组类被称作ndarray。通常被称作数组。注意numpy.array和标准Python库类array.array并不相同,后者只处理一维数组和提供少量功能。更多重要ndarray对象属性有:&/p&&ul&&li&&p&ndarray.ndim&/p&&p&数组轴的个数,在python的世界中,轴的个数被称作秩&/p&&/li&&li&&p&ndarray.shape&/p&&p&数组的维度。这是一个指示数组在每个维度上大小的整数元组。例如一个n排m列的矩阵,它的shape属性将是(2,3),这个元组的长度显然是秩,即维度或者ndim属性&/p&&/li&&li&&p&ndarray.size&/p&&p&数组元素的总个数,等于shape属性中元组元素的乘积。&/p&&/li&&li&&p&ndarray.dtype&/p&&p&一个用来描述数组中元素类型的对象,可以通过创造或指定dtype使用标准Python类型。另外NumPy提供它自己的数据类型。&/p&&/li&&li&&p&ndarray.itemsize&/p&&p&数组中每个元素的字节大小。例如,一个元素类型为float64的数组itemsiz属性值为8(=64/8),又如,一个元素类型为complex32的数组item属性为4(=32/8).&/p&&/li&&li&&p&ndarray.data&/p&&p&包含实际数组元素的缓冲区,通常我们不需要使用这个属性,因为我们总是通过索引来使用数组中的元素。&/p&&/li&&/ul&&h3&一个例子&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A1& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&1&/a&&/h3&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& from numpy
&&& a = arange(15).reshape(3, 5)
array([[ 0,
[10, 11, 12, 13, 14]])
&&& a.shape
&&& a.ndim
&&& a.dtype.name
&&& a.itemsize
&&& a.size
&&& type(a)
numpy.ndarray
&&& b = array([6, 7, 8])
array([6, 7, 8])
&&& type(b)
numpy.ndarray
&/code&&/pre&&/div&&h3&创建数组&/h3&&p&有好几种创建数组的方法。&/p&&p&例如,你可以使用array函数从常规的Python列表和元组创造数组。所创建的数组类型由原序列中的元素类型推导而来。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& from numpy
&&& a = array( [2,3,4] )
array([2, 3, 4])
&&& a.dtype
dtype('int32')
&&& b = array([1.2, 3.5, 5.1])
&&& b.dtype
dtype('float64')
一个常见的错误包括用多个数值参数调用`array`而不是提供一个由数值组成的列表作为一个参数。
&&& a = array(1,2,3,4)
&&& a = array([1,2,3,4])
&/code&&/pre&&/div&&p&数组将序列包含序列转化成二维的数组,序列包含序列包含序列转化成三维数组等等。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& b = array( [ (1.5,2,3), (4,5,6) ] )
array([[ 1.5,
&/code&&/pre&&/div&&p&数组类型可以在创建时显示指定&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& c = array( [ [1,2], [3,4] ], dtype=complex )
array([[ 1.+0.j,
&/code&&/pre&&/div&&p&通常,数组的元素开始都是未知的,但是它的大小已知。因此,NumPy提供了一些使用占位符创建数组的函数。这最小化了扩展数组的需要和高昂的运算代价。&/p&&p&函数function创建一个全是0的数组,函数ones创建一个全1的数组,函数empty创建一个内容随机并且依赖与内存状态的数组。默认创建的数组类型(dtype)都是float64。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& zeros( (3,4) )
array([[0.,
&&& ones( (2,3,4), dtype=int16 )
# dtype can also be specified
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]]], dtype=int16)
&&& empty( (2,3) )
&/code&&/pre&&/div&&p&为了创建一个数列,NumPy提供一个类似arange的函数返回数组而不是列表:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& arange( 10, 30, 5 )
array([10, 15, 20, 25])
&&& arange( 0, 2, 0.3 )
# it accepts float arguments
array([ 0. ,
&/code&&/pre&&/div&&p&当arange使用浮点数参数时,由于有限的浮点数精度,通常无法预测获得的元素个数。因此,最好使用函数linspace去接收我们想要的元素个数来代替用range来指定步长。&/p&&p&其它函数array, zeros, zeros_like, ones, ones_like, empty, empty_like, arange, linspace, rand, randn, fromfunction, fromfile参考:&a href=&http://link.zhihu.com/?target=http%3A//scipy.org/Numpy_Example_List& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&NumPy示例&/a&&/p&&h3&打印数组&/h3&&p&当你打印一个数组,NumPy以类似嵌套列表的形式显示它,但是呈以下布局:&/p&&ul&&li&最后的轴从左到右打印&/li&&li&次后的轴从顶向下打印&/li&&li&剩下的轴从顶向下打印,每个切片通过一个空行与下一个隔开&/li&&/ul&&p&一维数组被打印成行,二维数组成矩阵,三维数组成矩阵列表。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(6)
# 1d array
&&& print a
[0 1 2 3 4 5]
&&& b = arange(12).reshape(4,3)
# 2d array
&&& print b
[ 9 10 11]]
&&& c = arange(24).reshape(2,3,4)
# 3d array
&&& print c
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
&/code&&/pre&&/div&&p&查看形状操作一节获得有关reshape的更多细节&/p&&p&如果一个数组用来打印太大了,NumPy自动省略中间部分而只打印角落&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& print arange(10000)
2 ..., 99]
&&& print arange(10000).reshape(100,100)
[02 ..., 99]
[02 ..., 99]
[02 ..., 99]]
&/code&&/pre&&/div&&p&禁用NumPy的这种行为并强制打印整个数组,你可以设置printoptions参数来更改打印选项。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& set_printoptions(threshold='nan')
&/code&&/pre&&/div&&h3&基本运算&/h3&&p&数组的算术运算是按元素的。新的数组被创建并且被结果填充。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = array( [20,30,40,50] )
&&& b = arange( 4 )
array([0, 1, 2, 3])
&&& c = a-b
array([20, 29, 38, 47])
array([0, 1, 4, 9])
&&& 10*sin(a)
array([ 9., -9.,
7.4511316 , -2.])
array([True, True, False, False], dtype=bool)
&/code&&/pre&&/div&&p&不像许多矩阵语言,NumPy中的乘法运算符*指示按元素计算,矩阵乘法可以使用dot函数或创建矩阵对象实现(参见教程中的矩阵章节)&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A = array( [[1,1],
&&& B = array( [[2,0],
# elementwise product
array([[2, 0],
&&& dot(A,B)
# matrix product
array([[5, 4],
&/code&&/pre&&/div&&p&有些操作符像+=和*=被用来更改已存在数组而不创建一个新的数组。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = ones((2,3), dtype=int)
&&& b = random.random((2,3))
&&& a *= 3
array([[3, 3, 3],
[3, 3, 3]])
&&& b += a
array([[ 3.,
3.8324276 ,
3.0114541 ],
3.3039349 ,
&&& a += b
# b is converted to integer type
array([[6, 6, 6],
[6, 6, 6]])
&/code&&/pre&&/div&&p&当运算的是不同类型的数组时,结果数组和更普遍和精确的已知(这种行为叫做upcast)。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = ones(3, dtype=int32)
&&& b = linspace(0,pi,3)
&&& b.dtype.name
&&& c = a+b
array([ 1.
&&& c.dtype.name
&&& d = exp(c*1j)
array([ 0..j, -0..j,
&&& d.dtype.name
'complex128' 许多非数组运算,如计算数组所有元素之和,被作为ndarray类的方法实现
&&& a = random.random((2,3))
array([[ 0.6903007 ,
&&& a.sum()
&&& a.min()
&&& a.max()
&/code&&/pre&&/div&&p&这些运算默认应用到数组好像它就是一个数字组成的列表,无关数组的形状。然而,指定axis参数你可以吧运算应用到数组指定的轴上:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& b = arange(12).reshape(3,4)
array([[ 0,
9, 10, 11]])
&&& b.sum(axis=0)
# sum of each column
array([12, 15, 18, 21])
&&& b.min(axis=1)
# min of each row
array([0, 4, 8])
&&& b.cumsum(axis=1)
# cumulative sum along each row
array([[ 0,
9, 15, 22],
[ 8, 17, 27, 38]])
&/code&&/pre&&/div&&h3&通用函数(ufunc)&/h3&&p&NumPy提供常见的数学函数如sin,cos和exp。在NumPy中,这些叫作“通用函数”(ufunc)。在NumPy里这些函数作用按数组的元素运算,产生一个数组作为输出。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& B = arange(3)
array([0, 1, 2])
&&& exp(B)
array([ 1.
7.3890561 ])
&&& sqrt(B)
array([ 0.
&&& C = array([2., -1., 4.])
&&& add(B, C)
array([ 2.,
&/code&&/pre&&/div&&p&更多函数all, alltrue, any, apply along axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, conjugate, corrcoef, cov, cross, cumprod, cumsum, diff, dot, floor, inner, inv, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re, round, sometrue, sort, std, sum, trace, transpose, var, vdot, vectorize, where 参见:&a href=&http://link.zhihu.com/?target=http%3A//scipy.org/Numpy_Example_List& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&NumPy示例&/a&&/p&&h3&索引,切片和迭代&/h3&&p&一维数组可以被索引、切片和迭代,就像&a href=&http://link.zhihu.com/?target=http%3A//docs.python.org/tutorial/introduction.html%23SECTION& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&列表&/a&和其它Python序列。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(10)**3
64, 125, 216, 343, 512, 729])
&&& a[2:5]
array([ 8, 27, 64])
&&& a[:6:2] = -1000
# equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
array([-1000,
27, -1000,
&&& a[ : :-1]
# reversed a
125, -1000,
27, -1000,
1, -1000])
&&& for i in a:
print i**(1/3.),
nan 1.0 nan 3.0 nan 5.0 6.0 7.0 8.0 9.0
&/code&&/pre&&/div&&p&多维数组可以每个轴有一个索引。这些索引由一个逗号分割的元组给出。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& def f(x,y):
return 10*x+y
&&& b = fromfunction(f,(5,4),dtype=int)
array([[ 0,
[10, 11, 12, 13],
[20, 21, 22, 23],
[30, 31, 32, 33],
[40, 41, 42, 43]])
&&& b[2,3]
&&& b[0:5, 1]
# each row in the second column of b
array([ 1, 11, 21, 31, 41])
&&& b[ : ,1]
# equivalent to the previous example
array([ 1, 11, 21, 31, 41])
&&& b[1:3, : ]
# each column in the second and third row of b
array([[10, 11, 12, 13],
[20, 21, 22, 23]])
&/code&&/pre&&/div&&p&当少于轴数的索引被提供时,确失的索引被认为是整个切片:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& b[-1]
# the last row. Equivalent to b[-1,:]
array([40, 41, 42, 43])
&/code&&/pre&&/div&&p&b[i]中括号中的表达式被当作i和一系列:,来代表剩下的轴。NumPy也允许你使用“点”像b[i,...]。&/p&&p&点(…)代表许多产生一个完整的索引元组必要的分号。如果x是秩为5的数组(即它有5个轴),那么:&/p&&ul&&li&x[1,2,…] 等同于 x[1,2,:,:,:],&/li&&li&x[…,3] 等同于 x[:,:,:,:,3]&/li&&li&x[4,…,5,:] 等同 x[4,:,:,5,:].&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& c = array( [ [[
# a 3D array (two stacked 2D arrays) ...
[ 10, 12, 13]], ... ...
[[100,101,102], ...
[110,112,113]] ] ) &&& c.shape (2, 2, 3) &&& c[1,...]
# same as c[1,:,:] or c[1] array([[100, 101, 102],
[110, 112, 113]]) &&& c[...,2]
# same as c[:,:,2] array([[
[102, 113]])
&/code&&/pre&&/div&&p&迭代多维数组是就第一个轴而言的:&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A2& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&2&/a&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& for row in b:
[10 11 12 13]
[20 21 22 23]
[30 31 32 33]
[40 41 42 43]
&/code&&/pre&&/div&&p&然而,如果一个人想对每个数组中元素进行运算,我们可以使用flat属性,该属性是数组元素的一个迭代器:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& for element in b.flat:
print element,
0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33 40 41 42 43
&/code&&/pre&&/div&&p&更多[], …, newaxis, ndenumerate, indices, index exp 参考&a href=&http://link.zhihu.com/?target=http%3A//scipy.org/Numpy_Example_List& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&NumPy示例&/a&&/p&&h2&形状操作&/h2&&h3&更改数组的形状&/h3&&p&一个数组的形状由它每个轴上的元素个数给出:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = floor(10*random.random((3,4)))
array([[ 7.,
&&& a.shape
&/code&&/pre&&/div&&p&一个数组的形状可以被多种命令修改:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a.ravel() # flatten the array
array([ 7.,
&&& a.shape = (6, 2)
&&& a.transpose()
array([[ 7.,
&/code&&/pre&&/div&&p&由ravel()展平的数组元素的顺序通常是“C风格”的,就是说,最右边的索引变化得最快,所以元素a[0,0]之后是a[0,1]。如果数组被改变形状(reshape)成其它形状,数组仍然是“C风格”的。NumPy通常创建一个以这个顺序保存数据的数组,所以ravel()将总是不需要复制它的参数&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A3& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&3&/a&。但是如果数组是通过切片其它数组或有不同寻常的选项时,它可能需要被复制。函数reshape()和ravel()还可以被同过一些可选参数构建成FORTRAN风格的数组,即最左边的索引变化最快。&/p&&p&reshape函数改变参数形状并返回它,而resize函数改变数组自身。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a
array([[ 7.,
&&& a.resize((2,6))
array([[ 7.,
&/code&&/pre&&/div&&p&如果在改变形状操作中一个维度被给做-1,其维度将自动被计算&/p&&p&更多 shape, reshape, resize, ravel 参考&a href=&http://link.zhihu.com/?target=http%3A//scipy.org/Numpy_Example_List& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&NumPy示例&/a&&/p&&h3&组合(stack)不同的数组&/h3&&p&几种方法可以沿不同轴将数组堆叠在一起:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = floor(10*random.random((2,2)))
array([[ 1.,
&&& b = floor(10*random.random((2,2)))
array([[ 3.,
&&& vstack((a,b))
array([[ 1.,
&&& hstack((a,b))
array([[ 1.,
&/code&&/pre&&/div&&p&函数column_stack以列将一维数组合成二维数组,它等同与vstack对一维数组。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& column_stack((a,b))
# With 2D arrays
array([[ 1.,
&&& a=array([4.,2.])
&&& b=array([2.,8.])
&&& a[:,newaxis]
# This allows to have a 2D columns vector
array([[ 4.],
&&& column_stack((a[:,newaxis],b[:,newaxis]))
array([[ 4.,
&&& vstack((a[:,newaxis],b[:,newaxis])) # The behavior of vstack is different
array([[ 4.],
&/code&&/pre&&/div&&p&row_stack函数,另一方面,将一维数组以行组合成二维数组。&/p&&p&对那些维度比二维更高的数组,hstack沿着第二个轴组合,vstack沿着第一个轴组合,concatenate允许可选参数给出组合时沿着的轴。&/p&&p&Note&/p&&p&在复杂情况下,r_[]和c_[]对创建沿着一个方向组合的数很有用,它们允许范围符号(“:”):&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& r_[1:4,0,4]
array([1, 2, 3, 0, 4])
&/code&&/pre&&/div&&p&当使用数组作为参数时,r_和c_的默认行为和vstack和hstack很像,但是允许可选的参数给出组合所沿着的轴的代号。&/p&&p&更多函数hstack , vstack, column_stack , row_stack , concatenate , c_ , r_ 参见&a href=&http://link.zhihu.com/?target=http%3A//scipy.org/Numpy_Example_List& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&NumPy示例&/a&.&/p&&h3&将一个数组分割(split)成几个小数组&/h3&&p&使用hsplit你能将数组沿着它的水平轴分割,或者指定返回相同形状数组的个数,或者指定在哪些列后发生分割:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = floor(10*random.random((2,12)))
array([[ 8.,
&&& hsplit(a,3)
# Split a into 3
[array([[ 8.,
9.]]), array([[ 0.,
5.]]), array([[ 0.,
&&& hsplit(a,(3,4))
# Split a after the third and the fourth column
[array([[ 8.,
2.]]), array([[ 9.],
[ 9.]]), array([[ 0.,
&/code&&/pre&&/div&&p&vsplit沿着纵向的轴分割,array split允许指定沿哪个轴分割。&/p&&h2&复制和视图&/h2&&p&当运算和处理数组时,它们的数据有时被拷贝到新的数组有时不是。这通常是新手的困惑之源。这有三种情况:&/p&&h3&完全不拷贝&/h3&&p&简单的赋值不拷贝数组对象或它们的数据。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(12)
# no new object is created
&&& b is a
# a and b are two names for the same ndarray object
&&& b.shape = 3,4
# changes the shape of a
&&& a.shape
&/code&&/pre&&/div&&p&Python 传递不定对象作为参考&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A4& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&4&/a&,所以函数调用不拷贝数组。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& def f(x):
print id(x)
# id is a unique identifier of an object
&/code&&/pre&&/div&&h3&视图(view)和浅复制&/h3&&p&不同的数组对象分享同一个数据。视图方法创造一个新的数组对象指向同一数据。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& c = a.view()
&&& c is a
&&& c.base is a
# c is a view of the data owned by a
&&& c.flags.owndata
&&& c.shape = 2,6
# a's shape doesn't change
&&& a.shape
&&& c[0,4] = 1234
# a's data changes
&/code&&/pre&&/div&&p&切片数组返回它的一个视图:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& s = a[ : , 1:3]
# spac could also be written &s = a[:,1:3]&
&&& s[:] = 10
# s[:] is a view of s. Note the difference between s=10 and s[:]=10
&/code&&/pre&&/div&&h2&深复制&/h2&&p&这个复制方法完全复制数组和它的数据。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& d = a.copy()
# a new array object with new data is created
&&& d is a
&&& d.base is a
# d doesn't share anything with a
&&& d[0,0] = 9999
&/code&&/pre&&/div&&h3&函数和方法(method)总览&/h3&&p&这是个NumPy函数和方法分类排列目录。这些名字链接到&a href=&http://link.zhihu.com/?target=http%3A//scipy.org/Numpy_Example_List& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&NumPy示例&/a&,你可以看到这些函数起作用。[^5]&/p&&p&创建数组&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&arange, array, copy, empty, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r , zeros, zeros_like
&/code&&/pre&&/div&&p&转化&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&astype, atleast 1d, atleast 2d, atleast 3d, mat
&/code&&/pre&&/div&&p&操作&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&array split, column stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack
&/code&&/pre&&/div&&p&询问&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&all, any, nonzero, where
&/code&&/pre&&/div&&p&排序&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&argmax, argmin, argsort, max, min, ptp, searchsorted, sort
&/code&&/pre&&/div&&p&运算&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&choose, compress, cumprod, cumsum, inner, fill, imag, prod, put, putmask, real, sum
&/code&&/pre&&/div&&p&基本统计&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&cov, mean, std, var
&/code&&/pre&&/div&&p&基本线性代数&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&cross, dot, outer, svd, vdot
&/code&&/pre&&/div&&h2&进阶&/h2&&h3&广播法则(rule)&/h3&&p&广播法则能使通用函数有意义地处理不具有相同形状的输入。&/p&&p&广播第一法则是,如果所有的输入数组维度不都相同,一个“1”将被重复地添加在维度较小的数组上直至所有的数组拥有一样的维度。&/p&&p&广播第二法则确定长度为1的数组沿着特殊的方向表现地好像它有沿着那个方向最大形状的大小。对数组来说,沿着那个维度的数组元素的值理应相同。&/p&&p&应用广播法则之后,所有数组的大小必须匹配。更多细节可以从这个&a href=&http://link.zhihu.com/?target=http%3A//scipy.org/EricsBroadcastingDoc& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&文档&/a&找到。&/p&&h2&花哨的索引和索引技巧&/h2&&p&NumPy比普通Python序列提供更多的索引功能。除了索引整数和切片,正如我们之前看到的,数组可以被整数数组和布尔数组索引。&/p&&h3&通过数组索引&/h3&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(12)**2
# the first 12 square numbers
&&& i = array( [ 1,1,3,8,5 ] )
# an array of indices
# the elements of a at the positions i
array([ 1,
9, 64, 25])
&&& j = array( [ [ 3, 4], [ 9, 7 ] ] )
# a bidimensional array of indices
# the same shape as j
array([[ 9, 16],
[81, 49]])
&/code&&/pre&&/div&&p&当被索引数组a是多维的时,每一个唯一的索引数列指向a的第一维&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A6& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&5&/a&。以下示例通过将图片标签用调色版转换成色彩图像展示了这种行为。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& palette = array( [ [0,0,0],
[255,0,0],
[0,255,0],
[0,0,255],
[255,255,255] ] )
&&& image = array( [ [ 0, 1, 2, 0 ],
# each value corresponds to a color in the palette
[ 0, 3, 4, 0 ]
&&& palette[image]
# the (2,4,3) color image
[255, 255, 255],
&/code&&/pre&&/div&&p&我们也可以给出不不止一维的索引,每一维的索引数组必须有相同的形状。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(12).reshape(3,4)
array([[ 0,
9, 10, 11]])
&&& i = array( [ [0,1],
# indices for the first dim of a
&&& j = array( [ [2,1],
# indices for the second dim
&&& a[i,j]
# i and j must have equal shape
array([[ 2,
[ 7, 11]])
&&& a[i,2]
array([[ 2,
[ 6, 10]])
&&& a[:,j]
# i.e., a[ : , j]
array([[[ 2,
[11, 11]]])
&/code&&/pre&&/div&&p&自然,我们可以把i和j放到序列中(比如说列表)然后通过list索引。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& l = [i,j]
# equivalent to a[i,j]
array([[ 2,
[ 7, 11]])
&/code&&/pre&&/div&&p&然而,我们不能把i和j放在一个数组中,因为这个数组将被解释成索引a的第一维。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& s = array( [i,j] )
# not what we want
---------------------------------------------------------------------------
IndexError
Traceback (most recent call last)
&ipython-input-100-b912f631cc75& in &module&()
----& 1 a[s]
IndexError: index (3) out of range (0&=index&2) in dimension 0
&&& a[tuple(s)]
# same as a[i,j]
array([[ 2,
[ 7, 11]])
&/code&&/pre&&/div&&p&另一个常用的数组索引用法是搜索时间序列最大值&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A7& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&6&/a&。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& time = linspace(20, 145, 5)
# time scale
&&& data = sin(arange(20)).reshape(5,4)
# 4 time-dependent series
array([[ 0.
[-0.7568025 , -0., -0.2794155 ,
0.6569866 ],
0., -0., -0.],
[-0., -0., -0.,
&&& ind = data.argmax(axis=0)
# index of the maxima for each series
array([2, 0, 3, 1])
&&& time_max = time[ ind]
# times corresponding to the maxima
&&& data_max = data[ind, xrange(data.shape[1])] # =& data[ind[0],0], data[ind[1],1]...
&&& time_max
&&& data_max
array([ 0.,
0.6569866 ])
&&& all(data_max == data.max(axis=0))
&/code&&/pre&&/div&&p&你也可以使用数组索引作为目标来赋值:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(5)
array([0, 1, 2, 3, 4])
&&& a[[1,3,4]] = 0
array([0, 0, 2, 0, 0])
&/code&&/pre&&/div&&p&然而,当一个索引列表包含重复时,赋值被多次完成,保留最后的值:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(5)
&&& a[[0,0,2]]=[1,2,3]
array([2, 1, 3, 3, 4])
&/code&&/pre&&/div&&p&这足够合理,但是小心如果你想用Python的+=结构,可能结果并非你所期望:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(5)
&&& a[[0,0,2]]+=1
array([1, 1, 3, 3, 4])
&/code&&/pre&&/div&&p&即使0在索引列表中出现两次,索引为0的元素仅仅增加一次。这是因为Python要求a+=1和a=a+1等同。&/p&&h3&通过布尔数组索引&/h3&&p&当我们使用整数数组索引数组时,我们提供一个索引列表去选择。通过布尔数组索引的方法是不同的我们显式地选择数组中我们想要和不想要的元素。&/p&&p&我们能想到的使用布尔数组的索引最自然方式就是使用和原数组一样形状的布尔数组。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(12).reshape(3,4)
&&& b = a & 4
# b is a boolean with a's shape
array([[False, False, False, False],
[False, True, True, True],
[True, True, True, True]], dtype=bool)
# 1d array with the selected elements
array([ 5,
9, 10, 11])
&/code&&/pre&&/div&&p&这个属性在赋值时非常有用:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a[b] = 0
# All elements of 'a' higher than 4 become 0
array([[0, 1, 2, 3],
[4, 0, 0, 0],
[0, 0, 0, 0]])
&/code&&/pre&&/div&&p&你可以参考&a href=&http://link.zhihu.com/?target=http%3A//scipy.org/Tentative_NumPy_Tutorial/Mandelbrot_Set_Example& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&曼德博集合示例&/a&看看如何使用布尔索引来生成曼德博集合的图像。&/p&&p&第二种通过布尔来索引的方法更近似于整数索引;对数组的每个维度我们给一个一维布尔数组来选择我们想要的切片。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(12).reshape(3,4)
&&& b1 = array([False,True,True])
# first dim selection
&&& b2 = array([True,False,True,False])
# second dim selection
&&& a[b1,:]
# selecting rows
array([[ 4,
9, 10, 11]])
# same thing
array([[ 4,
9, 10, 11]])
&&& a[:,b2]
# selecting columns
array([[ 0,
[ 8, 10]])
&&& a[b1,b2]
# a weird thing to do
array([ 4, 10])
&/code&&/pre&&/div&&p&注意一维数组的长度必须和你想要切片的维度或轴的长度一致,在之前的例子中,b1是一个秩为1长度为三的数组(a的行数),b2(长度为4)与a的第二秩(列)相一致。&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A8& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&7&/a&&/p&&h3&ix_()函数&/h3&&p&ix_函数可以为了获得&a href=&http://link.zhihu.com/?target=http%3A//zh.wikipedia.org/wiki/%25E5%25A4%259A%25E5%E7%25BB%2584& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&多元组&/a&的结果而用来结合不同向量。例如,如果你想要用所有向量a、b和c元素组成的三元组来计算a+b*c:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = array([2,3,4,5])
&&& b = array([8,5,4])
&&& c = array([5,4,6,8,3])
&&& ax,bx,cx = ix_(a,b,c)
array([[[2]],
array([[[8],
array([[[5, 4, 6, 8, 3]]])
&&& ax.shape, bx.shape, cx.shape
((4, 1, 1), (1, 3, 1), (1, 1, 5))
&&& result = ax+bx*cx
&&& result
array([[[42, 34, 50, 66, 26],
[27, 22, 32, 42, 17],
[22, 18, 26, 34, 14]],
[[43, 35, 51, 67, 27],
[28, 23, 33, 43, 18],
[23, 19, 27, 35, 15]],
[[44, 36, 52, 68, 28],
[29, 24, 34, 44, 19],
[24, 20, 28, 36, 16]],
[[45, 37, 53, 69, 29],
[30, 25, 35, 45, 20],
[25, 21, 29, 37, 17]]])
&&& result[3,2,4]
&&& a[3]+b[2]*c[4]
&/code&&/pre&&/div&&p&你也可以实行如下简化:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&def ufunc_reduce(ufct, *vectors):
vs = ix_(*vectors)
r = ufct.identity
for v in vs:
r = ufct(r,v)
&/code&&/pre&&/div&&p&然后这样使用它:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& ufunc_reduce(add,a,b,c)
array([[[15, 14, 16, 18, 13],
[12, 11, 13, 15, 10],
[11, 10, 12, 14,
[[16, 15, 17, 19, 14],
[13, 12, 14, 16, 11],
[12, 11, 13, 15, 10]],
[[17, 16, 18, 20, 15],
[14, 13, 15, 17, 12],
[13, 12, 14, 16, 11]],
[[18, 17, 19, 21, 16],
[15, 14, 16, 18, 13],
[14, 13, 15, 17, 12]]])
&/code&&/pre&&/div&&p&这个reduce与ufunc.reduce(比如说add.reduce)相比的优势在于它利用了广播法则,避免了创建一个输出大小乘以向量个数的参数数组。&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A9& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&8&/a&&/p&&h3&用字符串索引&/h3&&p&参见&a href=&http://link.zhihu.com/?target=http%3A//www.scipy.org/RecordArrays& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&RecordArray&/a&。&/p&&h2&线性代数&/h2&&p&继续前进,基本线性代数包含在这里。&/p&&h3&简单数组运算&/h3&&p&参考numpy文件夹中的linalg.py获得更多信息&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& from numpy import *
&&& from numpy.linalg import *
&&& a = array([[1.0, 2.0], [3.0, 4.0]])
&&& print a
&&& a.transpose()
array([[ 1.,
&&& inv(a)
array([[-2. ,
[ 1.5, -0.5]])
&&& u = eye(2) # unit 2x2 &eye& represents &I&
array([[ 1.,
&&& j = array([[0.0, -1.0], [1.0, 0.0]])
&&& dot (j, j) # matrix product
array([[-1.,
[ 0., -1.]])
&&& trace(u)
&&& y = array([[5.], [7.]])
&&& solve(a, y)
array([[-3.],
&&& eig(j)
(array([ 0.+1.j,
array([[ 0..j,
Parameters:
square matrix
The eigenvalues, each repeated according to its multiplicity.
The normalized (unit &length&) eigenvectors, such that the
column ``v[:,i]`` is the eigenvector corresponding to the
eigenvalue ``w[i]`` .
&/code&&/pre&&/div&&h3&矩阵类&/h3&&p&这是一个关于矩阵类的简短介绍。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A = matrix('1.0 2.0; 3.0 4.0')
&&& type(A)
# file where class is defined
&class 'numpy.matrixlib.defmatrix.matrix'&
# transpose
&&& X = matrix('5.0 7.0')
&&& Y = X.T
&&& print A*Y
# matrix multiplication
&&& print A.I
[ 1.5 -0.5]]
&&& solve(A, Y)
# solving linear equation
matrix([[-3.],
&/code&&/pre&&/div&&h3&索引:比较矩阵和二维数组&/h3&&p&注意NumPy中数组和矩阵有些重要的区别。NumPy提供了两个基本的对象:一个N维数组对象和一个通用函数对象。其它对象都是建构在它们之上 的。特别的,矩阵是继承自NumPy数组对象的二维数组对象。对数组和矩阵,索引都必须包含合适的一个或多个这些组合:整数标量、省略号 (ellipses)、整数列表;布尔值,整数或布尔值构成的元组,和一个一维整数或布尔值数组。矩阵可以被用作矩阵的索引,但是通常需要数组、列表或者 其它形式来完成这个任务。&/p&&p&像平常在Python中一样,索引是从0开始的。传统上我们用矩形的行和列表示一个二维数组或矩阵,其中沿着0轴的方向被穿过的称作行,沿着1轴的方向被穿过的是列。&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A10& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&9&/a&&/p&&p&让我们创建数组和矩阵用来切片:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A = arange(12)
array([ 0,
9, 10, 11])
&&& A.shape = (3,4)
&&& M = mat(A.copy())
&&& print type(A),&
&type 'numpy.ndarray'&
&class 'numpy.core.defmatrix.matrix'&
&&& print A
&&& print M
&/code&&/pre&&/div&&p&现在,让我们简单的切几片。基本的切片使用切片对象或整数。例如,A[:]和M[:]的求值将表现得和Python索引很相似。然而要注意很重要的一点就是NumPy切片数组不创建数据的副本;切片提供统一数据的视图。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& print A[:]; print A[:].shape
&&& print M[:]; print M[:].shape
&/code&&/pre&&/div&&p&现在有些和Python索引不同的了:你可以同时使用逗号分割索引来沿着多个轴索引。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& print A[:,1]; print A[:,1].shape
&&& print M[:,1]; print M[:,1].shape
&/code&&/pre&&/div&&p&注意最后两个结果的不同。对二维数组使用一个冒号产生一个一维数组,然而矩阵产生了一个二维矩阵。&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A11& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&10&/a&例如,一个M[2,:]切片产生了一个形状为(1,4)的矩阵,相比之下,一个数组的切片总是产生一个最低可能维度&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A12& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&11&/a&的数组。例如,如果C是一个三维数组,C[...,1]产生一个二维的数组而C[1,:,1]产生一个一维数组。从这时开始,如果相应的矩阵切片结果是相同的话,我们将只展示数组切片的结果。&/p&&p&假如我们想要一个数组的第一列和第三列,一种方法是使用列表切片:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A[:,[1,3]]
array([[ 1,
[ 9, 11]])
&/code&&/pre&&/div&&p&稍微复杂点的方法是使用take()方法(method):&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A[:,].take([1,3],axis=1)
array([[ 1,
[ 9, 11]])
&/code&&/pre&&/div&&p&如果我们想跳过第一行,我们可以这样:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A[1:,].take([1,3],axis=1)
array([[ 5,
[ 9, 11]])
&/code&&/pre&&/div&&p&或者我们仅仅使用A[1:,[1,3]]。还有一种方法是通过矩阵向量积(叉积)。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A[ix_((1,2),(1,3))]
array([[ 5,
[ 9, 11]])
&/code&&/pre&&/div&&p&为了读者的方便,在次写下之前的矩阵:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A[ix_((1,2),(1,3))]
array([[ 5,
[ 9, 11]])
&/code&&/pre&&/div&&p&现在让我们做些更复杂的。比如说我们想要保留第一行大于1的列。一种方法是创建布尔索引:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A[0,:]&1
array([False, False, True, True], dtype=bool)
&&& A[:,A[0,:]&1]
array([[ 2,
[10, 11]])
&/code&&/pre&&/div&&p&就是我们想要的!但是索引矩阵没这么方便。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& M[0,:]&1
matrix([[False, False, True, True]], dtype=bool)
&&& M[:,M[0,:]&1]
matrix([[2, 3]])
&/code&&/pre&&/div&&p&这个过程的问题是用“矩阵切片”来切片产生一个矩阵&a href=&http://link.zhihu.com/?target=http%3A//reverland.org/python//numpy/%23fn%3A13& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&12&/a&,但是矩阵有个方便的A属性,它的值是数组呈现的。所以我们仅仅做以下替代:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& M[:,M.A[0,:]&1]
matrix([[ 2,
[10, 11]])
&/code&&/pre&&/div&&p&如果我们想要在矩阵两个方向有条件地切片,我们必须稍微调整策略,代之以:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A[A[:,0]&2,A[0,:]&1]
array([ 6, 11])
&&& M[M.A[:,0]&2,M.A[0,:]&1]
matrix([[ 6, 11]])
&/code&&/pre&&/div&&p&我们需要使用向量积ix_:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& A[ix_(A[:,0]&2,A[0,:]&1)]
array([[ 6,
[10, 11]])
&&& M[ix_(M.A[:,0]&2,M.A[0,:]&1)]
matrix([[ 6,
[10, 11]])
&/code&&/pre&&/div&&h2&技巧和提示&/h2&&p&下面我们给出简短和有用的提示。&/p&&h3&“自动”改变形状&/h3&&p&更改数组的维度,你可以省略一个尺寸,它将被自动推导出来。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&& a = arange(30)
&&& a.shape = 2,-1,3
# -1 means &whatever is needed&
&&& a.shape
array([[[ 0,
[ 9, 10, 11],
[12, 13, 14]],
[[15, 16, 17],
[18, 19, 20],
[21, 22, 23],
[24, 25, 26],
[27, 28, 29]]])
&/code&&/pre&&/div&&h3&向量组合(stacking)&/h3&&p&我们如何用两个相同尺寸的行向量列表构建一个二维数组?在MATLAB中这非常简单:如果x和y是两个相同长度的向量,你仅仅需要做m=[x;y]。在NumPy中这个过程通过函数column_stack、dstack、hstack和vstack来完成,取决于你想要在那个维度上组合。例如:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&x = arange(0,10,2)
# x=([0,2,4,6,8])
y = arange(5)
# y=([0,1,2,3,4])
m = vstack([x,y])
# m=([[0,2,4,6,8],
[0,1,2,3,4]])
xy = hstack([x,y])
# xy =([0,2,4,6,8,0,1,2,3,4])
&/code&&/pre&&/div&&p&二维以上这些函数背后的逻辑会很奇怪。&/p&&p&参考&a href=&http://link.zhihu.com/?target=http%3A//scipy.org/NumPy_for_Matlab_Users& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&写个Matlab用户的NumPy指南&/a&并且在这里添加你的新发现: )&/p&&h3&直方图(histogram)&/h3&&p&NumPy中histogram函数应用到一个数组返回一对变量:直方图数组和箱式向量。注意:matplotlib也有一个用来建立直方图的函数(叫作hist,正如matlab中一样)与NumPy中的不同。主要的差别是pylab.hist自动绘制直方图,而numpy.histogram仅仅产生数据。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&import numpy
import pylab
# Build a vector of 10000 normal deviates with variance 0.5^2 and mean 2
mu, sigma = 2, 0.5
v = numpy.random.normal(mu,sigma,10000)
# Plot a normalized histogram with 50 bins
pylab.hist(v, bins=50, normed=1)
# matplotlib version (plot)
pylab.show()
# Compute the histogram with numpy and then plot it
(n, bins) = numpy.histogram(v, bins=50, normed=True)
# NumPy version (no plot)
pylab.plot(.5*(bins[1:]+bins[:-1]), n)
pylab.show()
&/code&&/pre&&/div&
先决条件 在阅读这个教程之前,你多少需要知道点。如果你想从新回忆下,请看看.如果你想要运行教程中的示例,你至少需要在你的电脑上安装了以下一些软件:这些是可能对你有帮助的:是一个净强化的交互Python Shell,…
&figure&&img src=&https://pic3.zhimg.com/v2-9b40ca88e9c5d2c4d09c4b52d9d63934_b.jpg& data-rawwidth=&615& data-rawheight=&250& class=&origin_image zh-lightbox-thumb& width=&615& data-original=&https://pic3.zhimg.com/v2-9b40ca88e9c5d2c4d09c4b52d9d63934_r.jpg&&&/figure&&p&如题,不废话,目前我整理的应该是全网最全的,呵呵...&/p&&p&&br&&/p&&figure&&img src=&https://pic4.zhimg.com/v2-e2ba5b72773_b.jpg& data-rawwidth=&1241& data-rawheight=&2598& class=&origin_image zh-lightbox-thumb& width=&1241& data-original=&https://pic4.zhimg.com/v2-e2ba5b72773_r.jpg&&&/figure&&div class=&highlight&&&pre&&code class=&language-python3&&&span&&/span&&span class=&n&&觉得还不错的话也不用打赏&/span&&span class=&err&&,&/span&
&span class=&n&&帮忙推荐一下我们的新作品&/span&&span class=&err&&:&/span&
&/code&&/pre&&/div&&p&&a href=&https://zhuanlan.zhihu.com/p/& class=&internal&&一个颇具创意的农业互联网+项目(微信小程序:沁香农) - 知乎专栏&/a&&/p&&figure&&img src=&https://pic3.zhimg.com/v2-25dc8f47a955a276ab49c177f46f4f36_b.jpg& data-rawwidth=&430& data-rawheight=&430& class=&origin_image zh-lightbox-thumb& width=&430& data-original=&https://pic3.zhimg.com/v2-25dc8f47a955a276ab49c177f46f4f36_r.jpg&&&/figure&&p&&/p&&p&&/p&
如题,不废话,目前我整理的应该是全网最全的,呵呵... 觉得还不错的话也不用打赏,
帮忙推荐一下我们的新作品:
&figure&&img src=&https://pic1.zhimg.com/v2-4c0a8b1d6ffe4cf31c127b_b.jpg& data-rawwidth=&490& data-rawheight=&187& class=&origin_image zh-lightbox-thumb& width=&490& data-original=&https://pic1.zhimg.com/v2-4c0a8b1d6ffe4cf31c127b_r.jpg&&&/figure&&p&&b&1.冒泡排序&/b&&/p&&p&冒泡排序重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。&br&这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故名。&br&&/p&&p&步骤:&/p&&p&&br&&/p&&ol&&li&比较相邻的元素。如果第一个比第二个大,就交换他们两个。&br&&/li&&li&对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。&br&&/li&&li&针对所有的元素重复以上的步骤,除了最后一个。&br&&/li&&li&持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。&br&&/li&&/ol&&p&&br&&/p&&p&代码实现:&/p&&p&&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&# -*- coding:utf-8 -*-
def bubble_sort(list):
length=len(list)
for index in range(length):
for i in range(1,length-index):
if list[i-1]&list[i]:
list[i],list[i-1]=list[i-1],list[i]
return list
#一下为测试其正确性:
list=[10,23,1,53,654,54,16,646,65,]
print bubble_sort(list)
&/code&&/pre&&/div&&p&&b&2.选择排序&/b&&/p&&p&从待排序的&a href=&https://link.zhihu.com/?target=http%3A//baike.baidu.com/view/38785.htm& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&数据元素&/a&中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&# -*- coding:utf-8 -*-
def select_sort(list):
length=len(list)
for index in range(length):
for i in range(index,length):
if list[index]&list[i]:
list[index],list[i]=list[i],list[index]
return list
list=[10,23,1,53,654,54,16,646,65,]
print select_sort(list)
&/code&&/pre&&/div&&p&&b&3.插入排序&/b&&/p&&p&每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。&/p&&p&步骤:&/p&&ol&&li&从第一个元素开始,该元素可以认为已经被排序&/li&&li&取出下一个元素,在已经排序的元素序列中从后向前扫描&/li&&li&如果该元素(已排序)大于新元素,将该元素移到下一位置&/li&&li&重复步骤3,直到找到已排序的元素小于或者等于新元素的位置&/li&&li&将新元素插入到该位置后&/li&&li&重复步骤2~5&/li&&/ol&&p&&br&&/p&&p&代码实现:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&# -*- coding:utf-8 -*-
def insert_sort(lists):
# 插入排序
count = len(lists)
for i in range(1, count):
key = lists[i]
while j &= 0:
if lists[j] & key:
lists[j + 1] = lists[j]
lists[j] = key
return lists
list=[10,23,1,53,654,54,16,646,65,]
print insert_sort(lists)
&/code&&/pre&&/div&&p&&br&&/p&&p&&b&4.希尔排序&/b&&/p&&p&先取一个小于n的整数d1作为第一个&a href=&https://link.zhihu.com/?target=http%3A//baike.baidu.com/view/422272.htm& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&增量&/a&,把文件的全部记录分组。所有距离为d1的倍数的记录放在同一个组中。先在各组内进行&a href=&https://link.zhihu.com/?target=http%3A//baike.baidu.com/view/1193395.htm& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&直接插入排序&/a&;然后,取第二个增量d2&d1重复上述的分组和排序,直至所取的增量&br&&/p&&figure&&img src=&https://pic4.zhimg.com/9c63f93b1df1b9ae5af7ac_b.jpg& class=&content_image&&&/figure&&p&即所有记录放在同一组中进行直接插入排序为止。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&# -*- coding:utf-8 -*-
def shell_sort(list):
length=len(list)
dist=length/2
while dist&0:
for i in range(dist,length):
temp=list[i]
while j&=dist and temp&list[j-dist]:
list[j]=list[j-dist]
list[j]=temp
return list
list=[10,23,1,53,654,54,16,646,65,]
print shell_sort(list)
&/code&&/pre&&/div&&p&&b&5.归并排序&/b&&/p&&p&归并排序,就是把两个已经排列好的序列合并为一个序列。&br&&/p&&p&代码实现:&/p&&p&&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&# -*- coding:utf-8 -*-
#算法逻辑比较简单,以第一个list为基准,第二个向第一个插空
def merge_sort(list1,list2):
length_list1=len(list1)
length_list2=len(list2)
for i in range(length_list1):
while list2[j]&list1[i] and j&length_list2:
list3.append(list2[j])
list3.append(list1[i])
if j&(lengt}

我要回帖

更多关于 镍氢电池和锂电池 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信