numpy 练习
numpy学习,为后续机器学习铺垫
#!/usr/bin/python
#coding=utf-8
#__author__='dahu'
#
from numpy import *
import numpy as np a = np.arange(12).reshape(2, 2, 3) # 2个2行3列的数组
print a, type(a) # ndarray
print a.shape ,'3行5列'
print a.ndim ,'轴的个数'
print a.size ,'总个数'
print a.dtype ,'每个元素的类型'
a = array(range(5)) # 创建数组
print a, type(a), 'array是工厂函数,生成数组'
b = array(tuple(range(5)))
# print b, type(b)
print linspace(1, 2, 11) ,'[start,stop],还有一个是数量'
print arange(1, 2, 0.1) , '类似range,不过可以搞浮点数'
fl = array(linspace(1, 2, 11))
print fl.dtype ,'用linspace生成浮点数组,查看每个元素类型,正确。'
c = array(zip(range(5), range(10, 15), range(20, 25)))
print c,'配合zip生成数组,纵向的'
d = array((range(5), range(10, 15), range(20, 25)))
print d,'横向的生成数组'
# print zeros((3,4)) #全0数组
# print ones((3,4)) #全1数组
print empty((3, 4)) # 函数 empty 创建一个内容随机并且依赖与内存状态的数组,这个没怎么明白 #打印数组
''' 打印规则:
最后的轴从左到右打印
次后的轴从顶向下打印
剩下的轴从顶向下打印,每个切片通过一个空行与下一个隔开
'''
print np.arange(6),'1维'
print np.arange(12).reshape(4,3),'2维'
print np.arange(24).reshape(2,3,4),'3维' # np.set_printoptions(threshold='nan') #强制打印整个数组
print arange(10000).reshape(100,100),'数组太大,省略中间部分只打印角落' #基本运算
print np.arange(10,15)-np.arange(5),'数组减法,按元素运算'
print np.arange(5)**2
print np.arange(5)*np.arange(10,15),'数组相乘,对应元素相乘'
a=np.arange(12).reshape(3,4)
a+=1
print a,'操作+=,*=也是针对每个元素来操作的'
print np.fromfunction(lambda x,y:x*y,(3,4)),'也算是构造数组,由函数生成'
# print a,a.shape
# a=a.reshape(2,2,3)
# print a
for ele in a.flat:
print ele, #对每个数组元素进行迭代,多维也可以
c=[ele for ele in a.flat]
print np.array(c).reshape(3,4) ,'迭代完了再转换成数组,不耽误'
e= np.floor(10*np.random.random((2,12))) #floor取整数位
print e
print np.hsplit(e,4),'纵向切'
print np.vsplit(e,2),'横向切'
/usr/bin/python2. /home/dahu/Homework/GMM的EM算法实现/numpy练习.py
[[[ ]
[ ]] [[ ]
[ ]]] <type 'numpy.ndarray'>
(, , ) 3行5列
轴的个数
总个数
int64 每个元素的类型
[ ] <type 'numpy.ndarray'> array是工厂函数,生成数组
[ . 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 . ] [start,stop],还有一个是数量
[ . 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9] 类似range,不过可以搞浮点数
float64 用linspace生成浮点数组,查看每个元素类型,正确。
[[ ]
[ ]
[ ]
[ ]
[ ]] 配合zip生成数组,纵向的
[[ ]
[ ]
[ ]] 横向的生成数组
[[ 0.00000000e+000 4.94065646e-324 9.88131292e-324 1.48219694e-323]
[ 1.97626258e-323 2.47032823e-323 2.96439388e-323 3.45845952e-323]
[ 3.95252517e-323 4.44659081e-323 4.94065646e-323 5.43472210e-323]]
[ ] 1维
[[ ]
[ ]
[ ]
[ ]] 2维
[[[ ]
[ ]
[ ]] [[ ]
[ ]
[ ]]] 3维
[[ ..., ]
[ ..., ]
[ ..., ]
...,
[ ..., ]
[ ..., ]
[ ..., ]] 数组太大,省略中间部分只打印角落
[ ] 数组减法,按元素运算
[ ]
[ ] 数组相乘,对应元素相乘
[[ ]
[ ]
[ ]] 操作+=,*=也是针对每个元素来操作的
[[ . . . .]
[ . . . .]
[ . . . .]] 也算是构造数组,由函数生成
[[ ]
[ ]
[ ]] 迭代完了再转换成数组,不耽误
[[ . . . . . . . . . . . .]
[ . . . . . . . . . . . .]]
[array([[ ., ., .],
[ ., ., .]]), array([[ ., ., .],
[ ., ., .]]), array([[ ., ., .],
[ ., ., .]]), array([[ ., ., .],
[ ., ., .]])] 纵向切
[array([[ ., ., ., ., ., ., ., ., ., ., ., .]]), array([[ ., ., ., ., ., ., ., ., ., ., ., .]])] 横向切 Process finished with exit code
numpy 练习的更多相关文章
- 利用Python进行数据分析(5) NumPy基础: ndarray索引和切片
概念理解 索引即通过一个无符号整数值获取数组里的值. 切片即对数组里某个片段的描述. 一维数组 一维数组的索引 一维数组的索引和Python列表的功能类似: 一维数组的切片 一维数组的切片语法格式为a ...
- 利用Python进行数据分析(4) NumPy基础: ndarray简单介绍
一.NumPy 是什么 NumPy 是 Python 科学计算的基础包,它专为进行严格的数字处理而产生.在之前的随笔里已有更加详细的介绍,这里不再赘述. 利用 Python 进行数据分析(一)简单介绍 ...
- 利用Python进行数据分析(6) NumPy基础: 矢量计算
矢量化指的是用数组表达式代替循环来操作数组里的每个元素. NumPy提供的通用函数(既ufunc函数)是一种对ndarray中的数据进行元素级别运算的函数. 例如,square函数计算各元素的平方,r ...
- python安装numpy、scipy和matplotlib等whl包的方法
最近装了python和PyCharm开发环境,但是在安装numpy和matplotlib等包时出现了问题,现总结一下在windows平台下的安装方法. 由于现在找不到了工具包新版本的exe文件,所以采 ...
- 深入理解numpy
一.为啥需要numpy python虽然说注重优雅简洁,但它终究是需要考虑效率的.别说运行速度不是瓶颈,在科学计算中运行速度就是瓶颈. python的列表,跟java一样,其实只是一维列表.一维列表相 ...
- Python Numpy,Pandas基础笔记
Numpy Numpy是python的一个库.支持维度数组与矩阵计算并提供大量的数学函数库. arr = np.array([[1.2,1.3,1.4],[1.5,1.6,1.7]])#创建ndarr ...
- broadcasting Theano vs. Numpy
broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...
- python之numpy
一.矩阵的拼接合并 列拼接:np.column_stack() >>> import numpy as np >>> a = np.arange(9).reshap ...
- win7系统下python安装numpy,matplotlib,scipy和scikit-learn
1.安装numpy,matplotlib,scipy和scikit-learn win7系统下直接采用pip或者下载源文件进行安装numpy,matplotlib,scipy时会遇到各种问题,这是因为 ...
- 给numpy矩阵添加一列
问题的定义: 首先我们有一个数据是一个mn的numpy矩阵现在我们希望能够进行给他加上一列变成一个m(n+1)的矩阵 import numpy as np a = np.array([[1,2,3], ...
随机推荐
- 找圆算法((HoughCircles)总结与优化
http://www.opencv.org.cn/forum.php?mod=viewthread&tid=34096 Opencv内部提供了一个基于Hough变换理论的找圆算法,Hough ...
- Codeforces 932.F Escape Through Leaf
F. Escape Through Leaf time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- 线程函数对比 win/linux
原文
- u3d图片转视频
c#代码://将截图生成视频public static void createVideo(){ ProcessStartInfo psi = new ProcessStartInfo(); psi.F ...
- Codeforces 221 E. Little Elephant and Shifts
E. Little Elephant and Shifts time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Productivity tips, tricks and hacks for academics (2015 edition)
Productivity tips, tricks and hacks for academics (2015 edition) Contents Jump to: My philosophy: Op ...
- R2—《R in Nutshell》 读书笔记(连载)
R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshe ...
- 数组B - 我想我需要一艘船屋
[题目大意]弗雷德先生正在考虑在路易斯安娜州买一块地造房子,在土地调查中,他了解到由于密西西比河的侵蚀,路易斯安那州正以每年50平方英里的速度变小.弗雷德先生想知道他买的那块地是否会被侵蚀掉,经过进一 ...
- 【leetcode 简单】第四十九题 颠倒二进制位
颠倒给定的 32 位无符号整数的二进制位. 示例: 输入: 43261596 输出: 964176192 解释: 43261596 的二进制表示形式为 000000101001010000011110 ...
- 天梯赛L2-008 最长对称子串 (字符串处理)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...