数据分析-numpy相关操作1-2
目录
1.数组简介和数组的构造
2. 数组的取值和赋值
1.数组简介和数组的构造
#导入numpy模块
import numpy as np
Arrays
调用np.array()从list初始化数组
#构造数组
a = numpy.array([1,2,3])
print(a)
#out<<
[1,2,3]
#打印元素类型
print(type(a))
#out<<
<class 'numpy.ndarray'>
#对比
type([1,2,3])
#out<<
list
#构造矩阵
b = np.array([[1,2,3],[3,2,1]])
print(b)
#out<<
[[1 2 3]
[3 2 1]]
#shape用于输出矩阵维数
print(a,shape,b.shape)
#out<<
(3,) (2, 3)
#根据坐标,输出矩阵的元素
print(b[0,2])
#out<<
3
常用函数,方便用来创建数组(矩阵)
#创建全是0元素的数组的函数
#构造一个2行3列的0矩阵
a = np.zer0s((2,3))
print(a)
#out<<
[[0. 0. 0.]
[0. 0. 0.]]
*注意函数使用的括号,有两层,容易出错!
#创建全是1元素的数组的函数
b = np.ones((2,3))
print(b)
#out<<
[[1. 1. 1.]
[1. 1. 1.]]
#full(),以某元素充满,构造一个同一元素的数组
c = np.full((2,3),5)
print(c)
#out<<
[[5 5 5]
[5 5 5]]
#eye(),创建一个对角矩阵
d = np.eye(5)
print(d)
#out<<
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
#random()生成随机矩阵
#生成0到1的4行5列数组
e = np.random.random((4,5))
print(e)
#out<<
[[0.10741274 0.6000198 0.85585006 0.72483361 0.44521006]
[0.13794356 0.9610332 0.31433672 0.83437519 0.43897039]
[0.9306755 0.08639974 0.19172991 0.65244492 0.31036739]
[0.54068172 0.46926277 0.83960664 0.87333669 0.76868625]]
#empty()函数,随机生成多个数组,数组个数以及维度都是固定的
#2个3行4列
f = np.empty((2,3,4),dtype = float)
print(f)
#out<<
[[[4.41990004e+222 2.23119629e-046 2.21211602e+214 6.01346953e-154]
[6.01334637e-154 5.28595595e-085 8.88054495e+252 9.05437256e+271]
[5.42373773e-109 1.16304905e+214 6.01346953e-154 5.74381615e+169]]
[[8.30443188e-114 1.22829239e+184 6.01346953e-154 6.01334637e-154]
[5.28595595e-085 8.88054495e+252 3.13146950e-120 6.01334503e-154]
[2.46090679e-154 2.93573416e+222 1.36181966e+161 5.43185167e-096]]]
#arrange() 按要求构造数组
#左开右闭
e = np.arange(1,15,3)
print(e)
#out<<
[ 1 4 7 10 13]
print(e.shape)
#out<<
(5,)
#数组可以有不同的数据类型,注意区分数据类型dtype,数组类型type
arr = np.array([11,2,3,4,5])
print(arr.dtype)
#out<<
int32
#生成数组时可以指定数据类型,如果不指定numpy会自动匹配合适的类型
#使用astype()函数可以复制数组并转换数据类型
int_arr = np.array([1,2,3,4,5])
print(int_arr,int_arr.dtype)
#out<<
[1 2 3 4 5] int32
float_arr = np.array([3.4,4.5,2.3,-3.4])
print(float_arr)
#out<<
[ 3.4 4.5 2.3 -3.4]
#使用astype将float转换为int时小数部分被舍弃
int_arr = float_arr.astype(np.int64)
print(int_arr,int_arr.dtype)
#out<<
[ 3 4 2 -3] int64
#使用astype把字符串转换为数组,如果失败便抛出异常
str_arr = np.array(['1.24','2.2','5.8','assas'],dtype = np.string_)
str_arr
#out<<
array([b'1.24', b'2.2', b'5.8', b'assas'], dtype='|S5')
#astype使用其他数组的数据类型作为参数
int_arr = np.arrange(10)
float_arr = np.array([1.3,2.4,3.5,45.0,5.8])
print(float_arr.dtype,int_arr.dtype)
#out<<
float64 int32
int_arr.astype(dtype = float_arr.dtype)
#out<<
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
2.数组取值和赋值
#numpy含有多种取值的方式
a = np.array([[1,2,3],[1,2,3],[2,3,3]])
print(a)
print(a.shape)
print(a.dtype)
#out<<
[[1 2 3]
[1 2 3]
[2 3 3]]
(3, 3)
int32
#数组切片(同样用于多维数组)
b = a[0:2,0:2].copy()
print(b)
#out<<
[[1 2]
[1 2]]
#修改指定元素的值
b[0,0] = 11111
print(b)
print(a)
#out<<
[[11111 2]
[ 1 2]]
[[1 2 3]
[1 2 3]
[2 3 3]]
#创建3*4的二维矩阵/数组提取行元素
row_r1 = a[1,:]
print(row_r1,row_r1.shape)
row_r2 = a[1:2,:]
print(row_r2,row_r2.shape)
#out<<
[[1 2 3]] (1, 3)
#提取列元素
col_r1 = a[:,1]
print(col_r1)
#更自由的取值和组合
a = np.array([[12,2],[3,4],[4,5]])
print(a)
#out<<
[[12 2]
[ 3 4]
[ 4 5]]
#相当于提取a[0][0],a[1][1],a[2][0]
print(a[[0,1,2],[0,1,0]])
[0],a[1][1],a[2][0]
print(a[[0,1,2],[0,1,0]].shape)
#out<<
[12 4 4]
(3,)
#注意语法
print(np.array([a[0,0],a[1,1],a[2][0]]))
[12 4 4]
#创建二维数组
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
print(a)
#out<<
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
#用下标生成一个向量
b = np.array([0,2,0,1])
a[np.arange(4),b]
#out<<
array([ 1, 6, 7, 11])
a[np.arange(4),b] += 10#对指定元素进行相关操作
print(a)
#out<<
[[11 2 2]
[ 1 2 13]
[11 2 4]
[ 1 12 3]]
#根据条件判断取元素
a = np.array([[1,2],[3,4],[5,6]])
print(a)
#out<<
[[1 2]
[3 4]
[5 6]]
#根据条件对每个元素判断
bool_index = (a > 2)
print(bool_index)
#out<<
[[False False]
[ True True]
[ True True]]
#用刚才的布尔型数组作为下标就可以去除符合条件的元素
print(a[bool_index])
#out<<
[3 4 5 6]
print(a[a>2])
#out<<
[3,4,5,6]
2万+



被折叠的 条评论
为什么被折叠?



