码迷,mamicode.com
首页 > 编程语言 > 详细

python数据分析Numpy(二)

时间:2017-03-11 23:43:49      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:imp   pre   import   oat   nes   bash   cti   port   font   

Numpy (Numerical Python)

  • 高性能科学计算和数据分析的基础包;
  • ndarray,多维数组(矩阵),具有矢量运算能力,快速、节省空间;
  • 矩阵运算,无需循环,可以完成类似Matlab中的矢量运算;
  • 线性代数、随机送生成;

 

ndarray ,N维数组对象(矩阵)

  • 所有元素必须是相同类型
  • ndim属性,维度个数
  • shape属性,各维度大小
  • dtype属性,数据类型

代码示例:

import numpy
# 生成指定维度的随机多维数据(两行三列)
data = numpy.random.rand(2, 3)
print data
print type(data)
  • 执行结果:
[[ 0.49458614  0.14245674  0.26883084]
 [ 0.87402248  0.71089966  0.29023523]]
<type ‘numpy.ndarray‘>

 

print ‘维度个数‘, data.ndim
print ‘各维度大小: ‘, data.shape
print ‘数据类型: ‘, data.dtype
  • 执行结果:
维度个数 2
各维度大小:  (2L, 3L)
数据类型:  float64

 

1、创建ndarray

nd.array(collection),collection为序列型对象(list),嵌套序列(list of list)

# list 转换为 ndarray(一维数组)
l = range(10)
data = np.array(l)
print data
print data.shape
print data.ndim
  • 执行结果
[0 1 2 3 4 5 6 7 8 9]
(10L,)
1

 

# 嵌套序列转换为ndarray
l2 = [range(10), range(10)]
data = np.array(l2)
print data
print data.shape
  • 执行结果
[[0 1 2 3 4 5 6 7 8 9]
 [0 1 2 3 4 5 6 7 8 9]]
(2L, 10L)

 

np.zeros,np.ones,np.empty 指定大小的全0或全1数组

  • 第一个参数是元祖,用来指定大小,如(3,4)
  • empty不是总是返回全0,有事返回的是未初始的随机值

 

python数据分析Numpy(二)

标签:imp   pre   import   oat   nes   bash   cti   port   font   

原文地址:http://www.cnblogs.com/shhnwangjian/p/6536479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!