python高级应用与数据分析学习笔记 07
# -*- coding: utf-8 -*-
import numpy as np
"""
-------------------------------------------------
File Name: numpy1
Description :
Author : Lenovo
date: 2018/1/6
-------------------------------------------------
Change Activity:
2018/1/6:
-------------------------------------------------
"""
__author__ = 'Lenovo'
# 查看numpy的版本
print(np.__version__) #1.13.3
# ndarray创建数组的8种方式
# 1、array
a = np.array([1,2,3],dtype=np.float)
print(a) #[ 1. 2. 3.]
print(a.dtype) #float64
aa = np.array([[1,2,3],[4,5,6]],dtype=np.float)
print(aa) #[[ 1. 2. 3.]
#[ 4. 5. 6.]]
# 2、zeros #数值是0
b = np.zeros((2,3),dtype=np.int)
print(b) #[[0 0 0]
#[0 0 0]]
# 3、ones
c = np.ones((2,3)) #数值是1
print(c) #[[ 1. 1. 1.]
#[ 1. 1. 1.]]
# 4、empty
d = np.empty((2,2)) #数值是随机数
print(d) #[[ 3.13408242e-312 3.13408236e-312]
#[ 3.13408242e-312 3.13408236e-312]]
# 5、arange
e = np.arange(1,10,2) #效果等同于下面的一行代码
ee = np.array(range(1,10,2))
print(e) #[1 3 5 7 9]
print(ee) #[1 3 5 7 9]
# 6、linspace
f = np.linspace(1,10,5) #等差数列
print(f) #[ 1. 3.25 5.5 7.75 10. ]
ff = np.linspace(1,10,6,endpoint=False) #endpoint=False 不包括最后一个 默认为True
print(ff) #[ 1. 2.5 4. 5.5 7. 8.5]
fff = np.linspace(1,10,7)
print(fff) #[ 1. 2.5 4. 5.5 7. 8.5 10. ]
# 7、logspace 等积数列
h = np.logspace(1,10,5,endpoint=False)
print(h)
# [ 1.00000000e+01 6.30957344e+02 3.98107171e+04 2.51188643e+06
# 1.58489319e+08]
hh = np.logspace(1,10,6)
print(hh)
# [ 1.00000000e+01 6.30957344e+02 3.98107171e+04 2.51188643e+06
# 1.58489319e+08 1.00000000e+10]
# 8、logspace 等积数列
g = np.random.random((2,3,4))
print(g)
# [[[ 0.68460976 0.39046828 0.88349152 0.82786386]
# [ 0.41709914 0.04807389 0.25544836 0.03965502]
# [ 0.27632234 0.27974446 0.77059336 0.58248951]]
#
# [[ 0.0792647 0.88046797 0.02701309 0.97778673]
# [ 0.3007913 0.52267062 0.81059741 0.94046282]
# [ 0.15352565 0.02459086 0.30466552 0.19771866]]]
gg = np.random.randint(1,10,(2,3,4))
print(gg)
# [[[8 1 3 4]
# [8 1 1 7]
# [2 2 7 3]]
#
# [[4 5 9 7]
# [1 9 5 4]
# [8 3 5 1]]]
# 属性的访问
# dtype 数据类型
# shape 内容类型
# size 个数
# ndim 维度
print('属性的访问:',g.dtype,g.shape,g.size,g.ndim)