python数据分析学习numpy库






numpy:对数组进行计算
介绍:中文手册:https://www.numpy.org.cn/
  • 功能强大的N维数组对象。
  • 精密广播功能函数。
  • 集成 C/C+和Fortran 代码的工具。
  • 强大的线性代数、傅立叶变换和随机数功能。

 

 
import numpy

# numpy的版本号

numpy.__version__

'1.18.1'

numpy的核心是ndaray对象,n维数组

 
#创建列表

list=[1,3,5,8,3,5,9]

print(type(list))

# 转为ndaray,ndaray的方法很多

np_list=numpy.array(list)

print(type(np_list))

<class 'list'> <class 'numpy.ndarray'>

ndaray中多种多样的计算方法,运行速度快底层是C,C++

 
# 计算np_list中数的方差

np_fc=np_list.var()

print("np_list的方差:{}".format(np_fc))

np_list的方差:6.979591836734693

ndaray创建方法

 
# 创建全为1的二阶等阶矩阵

np_list1=numpy.ones(shape=(4,4), dtype=numpy.int8)

np_list1

array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], dtype=int8)
 
# 创建全为0的三阶等阶矩阵

np_list2=numpy.zeros(shape=(4,3,3),dtype=numpy.float16)

np_list2

array([[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]], [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]], [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]], [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]], dtype=float16)
 
#创建值为任意值的二维数组

np_list3=numpy.full(shape=(2,2), fill_value=3.2, dtype=numpy.float)

np_list3

array([[3.2, 3.2], [3.2, 3.2]])
 
#创建单位矩阵(对角线全为1,其他位置为0)

np_list4=numpy.eye(N=4,dtype=numpy.int8)

np_list4

array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], dtype=int8)
 
#创建等差数列(从0-100,共20个数)

np_list5=numpy.linspace(start=0, stop=100, num=20)

np_list5

array([ 0. , 5.26315789, 10.52631579, 15.78947368, 21.05263158, 26.31578947, 31.57894737, 36.84210526, 42.10526316, 47.36842105, 52.63157895, 57.89473684, 63.15789474, 68.42105263, 73.68421053, 78.94736842, 84.21052632, 89.47368421, 94.73684211, 100. ])
 
#创建标准正态分布数组,平均值是0,方差为1

np_list6=numpy.random.randn(3,3)

np_list6

array([[-0.57610895, 0.30709804, -1.81125909], [-0.71255554, 0.82455712, 1.20334221], [-0.91395139, -0.80929708, -0.71178201]])
 
#创建一般正态分布数组,平均值170.00,方差为6,共20个数

np_list7=numpy.random.normal(loc=170, scale=6, size=20000).round(2)

np_list7

array([171.76, 177.42, 170.97, ..., 174.43, 180.85, 169.53])
 
np_list7_mean=np_list7.mean().round(2)

np_list7_var=np_list7.var().round(2)

np_list7_std=np_list7.std().round(2)

print("身高的平均值:{};方差:{};标准差:{}".format(np_list7_mean,np_list7_var,np_list7_std))

身高的平均值:169.99;方差:36.23;标准差:6.02

ndarray的属性

 
np_list1_pro={}

np_list1_pro["元素个数"]=np_list1.size

np_list1_pro["行列"]=np_list1.shape

np_list1_pro["维度"]=np_list1.ndim

np_list1_pro["元素类型"]=np_list1.dtype

print(np_list1_pro)

{'元素个数': 16, '行列': (4, 4), '维度': 2, '元素类型': dtype('int8')}

基本操作

 
#根据索引找数据第三行第三列

#根据切片找第二行到第四行,第一列到第三列的所有数据

np_list8=numpy.random.randint(1,20,size=(4,5))

print(np_list8)

print("第三行,第三列:{}".format(np_list8[2,2]))

print("从第二行到第四行,第一列到第三列:\n{}".format(np_list8[1:4,0:3]))

[[15 16 13 7 5] [ 2 9 7 19 8] [12 8 13 6 10] [12 19 2 9 14]] 第三行,第三列:13 从第二行到第四行,第一列到第三列: [[ 2 9 7] [12 8 13] [12 19 2]]
 
#将数组变形

np_list8_2=np_list8.reshape(2,10)

#将数组转置,宽0,高1

np_list8_3=numpy.transpose(np_list8,axes=(1,0))

print(np_list8)

print(np_list1.shape)

print("变换矩阵:\n{}".format(np_list8_2))

print("转置矩阵:\n{}".format(np_list8_3))

[[15 16 13 7 5] [ 2 9 7 19 8] [12 8 13 6 10] [12 19 2 9 14]] (4, 4) 变换矩阵: [[15 16 13 7 5 2 9 7 19 8] [12 8 13 6 10 12 19 2 9 14]] 转置矩阵: [[15 2 12 12] [16 9 8 19] [13 7 13 2] [ 7 19 6 9] [ 5 8 10 14]]
 
# 拼接两个数组

numpy.concatenate([np_list8,np_list8])

array([[15, 16, 13, 7, 5], [ 2, 9, 7, 19, 8], [12, 8, 13, 6, 10], [12, 19, 2, 9, 14], [15, 16, 13, 7, 5], [ 2, 9, 7, 19, 8], [12, 8, 13, 6, 10], [12, 19, 2, 9, 14]])
 
from PIL import Image

import numpy

img=Image.open(r"D:\Python\代码\Data-Analysis\timg.jfif")

img
 
 
 
 
img_ndarry=numpy.array(img)

img_ndarry_pro={}

#图片三个维度:高,宽,像素(3个表示一种颜色)

img_ndarry_pro["行列"]=img_ndarry.shape

img_ndarry_pro["高度"]=str(img_ndarry_pro["行列"][0])+"px"

img_ndarry_pro["宽度"]=str(img_ndarry_pro["行列"][1])+"px"

#uint8只能表示256个数据,只有正数

img_ndarry_pro["元素数据类型"]=img_ndarry.dtype

print(img_ndarry_pro)

{'行列': (281, 500, 3), '高度': '281px', '宽度': '500px', '元素数据类型': dtype('uint8')}
 
#修改图片颜色,将颜色翻转

#通过数组读取图片

Image.fromarray(img_ndarry[:,:,-1])
 
 
 
import matplotlib.pyplot as plt

plt.imshow(img_ndarry[::8,::8])

<matplotlib.image.AxesImage at 0x241e58fc488>
 
plt.imshow(img_ndarry)

<matplotlib.image.AxesImage at 0x241e6955308>
 
img_ndarry_1=img_ndarry[50:,:280]

img_ndarry_2=img_ndarry[50:,390:501]

#拼接两个图片按照宽度1

img_ndarry_new=numpy.concatenate([img_ndarry_1,img_ndarry_2],axis=1)

plt.imshow(img_ndarry_new)

<matplotlib.image.AxesImage at 0x241e6e34a88>
 
 

 

numpy.concatenate()

posted on 2020-03-19 22:50  不愧下学  阅读(277)  评论(0编辑  收藏  举报

导航