Python数据分析基础(一)

1、numpy

keyword:开源 数据计算扩展
功能: ndarray,多维操作,线性代数
官网:https://www.numpy.org/
NumPy教程:https://www.numpy.org/devdocs/user/quickstart.html

Eg1. list中类型任意
#encoding=utf-8
import numpy as np

def main():
	lst=[[1,3,5],[2,4,6]]
	print(type(lst))

if __name__=="__main__":
	main()

output:


Eg2. list中最基础的数据结构numpy.ndarray

numpy的数据结构中只能有一种数据类型,这种数据类型可以由程序员自行定义

#encoding=utf-8
import numpy as np

def main():
	lst=[[1,3,5],[2,4,6]]
	print(type(lst))
	np_lst=np.array(lst)
	print(type(np_lst))

if __name__=="__main__":
	main()

output:



Eg3. 定义numpy数据结构中的数据类型
#encoding=utf-8
import numpy as np

def main():
	lst=[[1,3,5],[2,4,6]]
	print(type(lst))
	np_lst=np.array(lst)
	print(type(np_lst))
	np_lst=np.array(lst,dtype=np.float) #此处定义为float64
	
	# 涉及到的数据类型dtype
	# bool
	# int8/16/32/64/128
	# uint8/16/32/64/128
	# float16/32/64
	# complex64/128
	
	# np.array是numpy中最为基础的数据结构,接下来查看其属性
	print(np_lst,shape)
	print(np_lst,ndim)
	print(np_lst,dtype)
	print(np_lst,itemsize)
	print(np_lst,size)
	

if __name__=="__main__":
	main()

output:



(2L,3L)
2
float64     # 8字节
8           # np.array当中每个元素的大小
6           # 大小:6个元素
            #一共有6 * 8 = 48 个字节

EG

#encoding=utf-8
import numpy as np

def main():
	lst=[[1,3,5],[2,4,6]]
	print(type(lst))
	np_lst=np.array(lst)
	print(type(np_lst))
	np_lst=np.array(lst,dtype=np.float) #此处定义为float64
	
	# 涉及到的数据类型dtype
	# bool
	# int8/16/32/64/128
	# uint8/16/32/64/128
	# float16/32/64
	# complex64/128
	
	# np.array是numpy中最为基础的数据结构,接下来查看其属性
	print(np_lst,shape)
	print(np_lst,ndim)
	print(np_lst,dtype)
	print(np_lst,itemsize)
	print(np_lst,size)
	
	#2 Some Arrays
	print(np.zeros([2,4])) 
	print(np.ones([3,5])) 
	## 随机数-均匀分布
	print(np.random.rand(2, 4)) 
	print(np.random.rand())
	print(np.random.randint(1,10)) # 随机生成1~10之间的整数
	print(np.random.randint(1,10,3)) # 随机生成3个范围在1~10之间的整数
	## 随机数-正态分布
	print(np.random.randn())
	print(np.random.randn(2,4))
	## 随机数-生成指定范围内的随机数
	print(np.random.choice([10,20,30]))
	
	
if __name__=="__main__":
	main()

OutPut:

(2L,3L)
2
float64 # 8字节
8 # np.array当中每个元素的大小
6 # 大小:6个元素
#一共有6 * 8 = 48 个字节
#
# 2
# zeros
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]]
# ones
[[1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]
##### 随机数
# (1) 均匀分布的随机数
# rand 均匀分布在0~1之间的
[[0.26114721 0.9941431  0.03464749 0.32010473]
 [0.46422949 0.00506082 0.21373707 0.94724498]]
# rand 只有一个
0.39973189225137684
# RandInt:1~10之间的整数
1
[2 3 7]
# (2) 正态分布的随机数
-0.9697129425598122
# 2 行 4 列
[[-0.07009294 -0.56377026  0.17159821 -1.05876506]
 [-0.9442131   0.91248175 -0.01410148 -1.35599969]]
# (3) 指定随机数
10

你可能感兴趣的:(Python)