数据分析介绍之七——单变量数据观察之汇总统计和箱线图

115人阅读 评论(0)

一、NumPy in Action

1. 转换Python列表
2. 使用函数返回一个密集的载体
3. 从文件直接进入NumPy对象读取数据

# Five different ways to create a vector...
import numpy as np
# From a Python list
vec1 = np.array( [ 0., 1., 2., 3., 4. ] )
# arange( start inclusive, stop exclusive, step size )
vec2 = np.arange( 0, 5, 1, dtype=float )
# linspace( start inclusive, stop inclusive, number of elements )
vec3 = np.linspace( 0, 4, 5 )
# zeros( n ) returns a vector filled with n zeros
vec4 = np.zeros( 5 )
for i in range( 5 ):
vec4[i] = i
# read from a text file, one number per row
vec5 = np.loadtxt( "data" )

# ... continuation from previous listing
# Add a vector to another
v1 = vec1 + vec2
# Unnecessary: adding two vectors using an explicit loop
v2 = np.zeros( 5 )
for i in range( 5 ):
v2[i] = vec1[i] + vec2[i]
# Adding a vector to another in place
vec1 += vec2
# Broadcasting: combining scalars and vectors
v3 = 2*vec3
v4 = vec4 + 3
# Ufuncs: applying a function to a vector, element by element
v5 = np.sin(vec5)
# Converting to Python list object again
lst = v5.tolist()

>>> import numpy as np
>>> # Create a 12-element vector and reshape into 3x4 matrix
>>> d = np.linspace( 0, 11, 12 )
>>> d.shape = ( 3,4 )
>>> print d
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]]
>>> # Slicing...
>>> # First row
>>> print d[0,:]
[ 0. 1. 2. 3.]
>>> # Second col
>>> print d[:,1]
[ 1. 5. 9.]
>>> # Individual element: scalar
>>> print d[0,1]
1.0
>>> # Subvector of shape 1
>>> print d[0:1,1]
[ 1.]
>>> # Subarray of shape 1x1
>>> print d[0:1,1:2]
[[ 1.]]
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：130559次
• 积分：2006
• 等级：
• 排名：千里之外
• 原创：100篇
• 转载：1篇
• 译文：0篇
• 评论：3条
文章分类
阅读排行
评论排行
最新评论