数据科学包numpy-初识numpy

1、numpy & pandas 有什么用

为什么用numpy & pandas?
答:numpy & pandas是科学计算包,转换为矩阵计算,计算速度会快很多倍.

2、numpy & pandas 安装

anoconda 中自带了很多科学数据包。

安装 Anaconda 以及numpy、 Scikit-learn 等必备库,参考以下链接
https://blog.csdn.net/tz_zs/article/details/73459800?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4

anaconda建造新的环境,参考以下链接
https://blog.csdn.net/x_iesheng/article/details/79725415?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1

3、 numpy 属性

import numpy as np #导入numpay模块,并且简写为np
#定义一个一维数组,会包含特定属性属性,如长,宽,size,形状,维度
array = np.array([[1,2,3],
                  [4,5,6]])
print(array)
print('number of dimension:',array.ndim)#dimension维度
print('shape:',array.shape)#shape形状 ,显示行数和列数(2,3)
print('size:',array.size)#size大小,显示总共有多少元素,2乘3=6个

4、numpy 的创建 array

初识array arange linspace reshape

'''
如果想要精度,可以定义成64位,如果想要更多空间储存数据,可以定义成16位
'''
import numpy as np #导入numpay模块,并且简写为np
#定义时可以设置数值类型,使用dtype
a = np.array([1,2,3],dtype = np.int)
#定义成整型,默认是64位,如果需要设置可以在int后加8、16、32等
print(a.dtype) #int64


#定义一个矩阵,需要在在外面再加一层列表
a =np.array([[1,2,3],
             [4,5,6]])

#定义一个3行4列的全部为0的矩阵
b = np.zeros((3,4)) #c = np.ones(shape,dtype)也可以生成全部为1或者空的矩阵
print(b)
'''
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
'''

#生成一个有序的举证,类似于python中的range
d = np.arange(10,20,2)
#arange([start,]stop[,step],[dtype] = None)
print(d)
#使用0-11,生成一个3行4列的矩阵
e = np.arange(12).reshape((3,4))
print(e)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
'''

#生成一个‘数据线段’,从1开始,到10结束,分成20份,并生成4行5列的矩阵
f = np.linspace(1,10,20).reshape((4,5))
print(f)
'''
[[ 1.          1.47368421  1.94736842  2.42105263  2.89473684]
 [ 3.36842105  3.84210526  4.31578947  4.78947368  5.26315789]
 [ 5.73684211  6.21052632  6.68421053  7.15789474  7.63157895]
 [ 8.10526316  8.57894737  9.05263158  9.52631579 10.        ]]
'''

5、numpy的基础运算1

数列运算

加减乘除、比较大小、sin()

import numpy as np 

a = np.array([10,20,30,40])
b = np.arange(4)#0-3的一个arry [0,1,2,3]

c = a-b  # a+b和a-b运算规则相同
d = a*2  #乘法
e = b**2 #平方
f =10*np.sin(a) #计算sin
print(c,d,e,f,b<3,b==3,sep='\n')#sep:以某个字符连接,b<3返回布尔值
'''
运算结果:
[10 19 28 37]
[20 40 60 80]
[0 1 4 9]
[-5.44021111  9.12945251 -9.88031624  7.4511316 ]
[ True  True  True False]
[False False False  True]
'''

矩阵运算

乘法

import numpy as np 
a = np.array([[0,1],
             [1,1]])
b = np.arange(4).reshape((2,2))#定义一个2行2列的矩阵
c = a*b #逐个普通相乘
c_dot= np.dot(a,b) #矩阵乘法,和线性代数中矩阵乘法一致
c_dot_2 = a.dot(b) #上面定义了a是一个np的矩阵,可以直接使用dot(),和上一行是一样的
print(c,c_dot,c_dot_2,sep = '\n')
'''
运行结果:
[[0 1]
 [2 3]]
[[2 3]
 [2 4]]
[[2 3]
 [2 4]]
'''

max、min、sum

import numpy as np
a = np.random.random((2,4))#随机生成一个2行4列的数组,每个元素是0-1之间的随机数
b = np.sum(a,axis = 1) #axis为0时是计算每行元素和,为1时是计算每列元素和,不加时是计算所有元素和
c = np.max(a,axis = 0) #求每行的最大值
d = np.min(a,axis = 1) #求每列最小值
print(a,b,c,d,sep = '\n')
'''
运行结果:
[[0.90747645 0.82152021 0.82446934 0.77333446]
 [0.59677933 0.68739983 0.16096833 0.05427701]]
[3.32680047 1.4994245 ]
[0.90747645 0.82152021 0.82446934 0.77333446]
[0.77333446 0.05427701]
'''

最大、最小值的索引位置:argmax、argmin

import numpy as np

a = np.arange(2,14).reshape((3,4))#定义一个元素值为2-13的3行4列的数组
b = np.argmax(a)#返回矩阵a最大值所在的位置索引
c = np.argmin(a)
print(b,c,sep='\n')
'''
运行结果:
11
0
'''

平均数(mean、average)、中位数(median)

a = np.arange(2,14).reshape((3,4))
b = np.mean(a) #求平均值,也可增加axis标识
c = a.mean() #求平均值
d = np.average(a) #求平均值
e = np.median(a)#求中位数
f = np.cumsum(a)#累加:新数列元素n=前矩阵n位的和
g = np.diff(a)#累差:相邻两位的差
print(a,b,c,d,e,f,g,sep='\n')
'''
运行结果:
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
7.5
7.5
7.5
7.5
[ 2  5  9 14 20 27 35 44 54 65 77 90]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
'''

nonzero()、矩阵转置(transpose()、object.T)、clip()

import numpy as np 
a = np.arange(14,2,-1).reshape((3,4))
b = np.nonzero(a)
#(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]))
#按顺序每个array取数,0行0列、0行1列的位置。。。。。为0
c = np.sort(a) #每行升序
d = np.transpose(a) #线代中矩阵的转置:行列互换
e = a.T #线代中矩阵的转置:行列互换
f = np.clip(a,5,9)#矩阵a小于5的元素全部变成5,大于9的元素全部变成9
print(a,b,c,d,e,f,sep='\n')
'''
运算结果:
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]))
[[11 12 13 14]
 [ 7  8  9 10]
 [ 3  4  5  6]]
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
[[9 9 9 9]
 [9 9 8 7]
 [6 5 5 5]]
'''

7、numpy的索引

行、列、行列区间、某元素的索引

import numpy as np 

a = np.arange(3,15).reshape(3,4)
b = a[2]#矩阵a的2行得值
c = a[0][0]#矩阵a的0行0列所在的元素的值
d = a[0,0]#和a[0][0]相同
e = a[:,1]#1列所有元素的值,a[1,:]--->第一行所有元素的值
f = a[1,1:4]#1行元素1-3的值
print(a,b,c,d,e,f,sep='\n')
'''
运行结果:
[[ 3  4  5  6]
 [ 7  8  9 10]
 [11 12 13 14]]
[11 12 13 14]
3
3
[ 4  8 12]
[ 8  9 10]
'''

行列的迭代

import numpy as np 

a = np.arange(3,15).reshape(3,4)
print(a)
for row in a: #默认按行迭代,迭代列可以把矩阵先转置
    print(row)
    
for column in a.T:#按列迭代
    print(column)
'''
运行结果:
[[ 3  4  5  6]
 [ 7  8  9 10]
 [11 12 13 14]]
[3 4 5 6]
[ 7  8  9 10]
[11 12 13 14]
[ 3  7 11]
[ 4  8 12]
[ 5  9 13]
[ 6 10 14]
'''

flatten()和flat取出矩阵的元素

import numpy as np 
print(a.flatten())#flatten是一个迭代器,把a的每个元素放到一个列表中   
for item in a.flat:#取出矩阵a中每个元素
    print(item)
'''
运行结果:
[ 3  4  5  6  7  8  9 10 11 12 13 14]
3
4
5
6
7
8
9
10
11
12
13
14
'''

8、numpy的 array 合并 (教学教程)

vstack、hstack(垂直合并、水平合并)

import numpy as np 

A = np.array([1,1,1])
B = np.array([2,2,2])
C = np.vstack((A,B)) #vertical stack 垂直合并
print(C,C.shape,sep = '\n')
D = np.hstack((A,B)) #horizontal stack 水平合并
print(D,D.shape,sep = '\n')  
'''
输出结果:
[[1 1 1]
 [2 2 2]]
(2, 3)#一个2行3列的矩阵
[1 1 1 2 2 2]
(6,)#一个6个元素的序列
'''

一维矩阵的转置(横向数列变竖向数列)

import numpy as np 

print(A.T)#序列无法转置,仍然输出【1,1,1】
print(A[:,np.newaxis])#利用np.newaxis转置序列
print(A[:,np.newaxis].shape)#(3,1)

print(A[np.newaxis,:])#利用np.newaxis
print(A[np.newaxis,:].shape)#(1,3)
'''
[1 1 1]
[[1]
 [1]
 [1]]
(3, 1)
[[1 1 1]]
(1, 3)
'''

多个矩阵连接,使用concatenate

#多个矩阵连接(是序列时会出错)
A = np.array([1,1,1])[:,np.newaxis]
B = np.array([2,2,2])[:,np.newaxis]
C = np.concatenate((A,B,B),axis = 0) #axis为0时,是纵向合并
D = np.concatenate((A,B,B),axis = 1) #axis为1时,是横向向合并
print(C,D,sep ='\n')
'''
[[1]
 [1]
 [1]
 [2]
 [2]
 [2]
 [2]
 [2]
 [2]]
[[1 2 2]
 [1 2 2]
 [1 2 2]]
'''

9、numpy的 array分割

均等分割

import numpy as np 

#均等分割
A = np.arange(12).reshape(3,4)
print(A)
print(np.split(A,2,axis = 1))#按列分割成两块,axis为0时代表按行分割
#print(np.split(A,3,axis = 1))#不均等分割时候会报错
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
'''

不均等分割

A = np.arange(12).reshape(3,4)
print(A)
print(np.array_split(A,2,axis = 1))
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
'''

水平和垂直分割的另一种形式

A = np.arange(12).reshape(3,4)
print(A)
print(np.vsplit(A,3))#vertical水平分割成3块
print(np.hsplit(A,2))#horizontal垂直分割2块
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
'''

10、numpy的 copy & deep copy

直接赋值是浅拷贝,使用copy方法是深拷贝

numpy直接赋值是浅拷贝(目的:把a和b关联起来)

import numpy as np 

a = np.arange(4)
b = a
c = a
d = b 
print(a,b,c,d,sep='\n')#b、c、d都是a,修改了a、b、c、d中的任何一个都修改了所有
a[0] = 10
print('b:',b)
'''
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
b: [10  1  2  3]
'''

numpy中copy()是深拷贝(目的:把a的值给b)

a = np.arange(4)
b = a.copy()#deep copy
print('a:',a)
print('b:',b)
a[0] = 10
print('a:',a)
print('b:',b)
'''
a: [0 1 2 3]
b: [0 1 2 3]
a: [10  1  2  3]
b: [0 1 2 3]
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值