机器学习—numpy、matplotlib和sklearn

2.3 numpy库

numpy是高性能的科学计算库。

在numpy中,引入了”ndarray”多维数组的概念,其一维数组表示向量;二维数组表示矩阵,numpy的优势就是包含了矩阵相关的运算。

2.3.1 numpy.ndarray基本属性

#程序2-3

import numpy as np

 

print(np.__version__)

 

a = np.array([0,1,2,3,4,5,6,7,8,9])

print(a)

print(type(a))

 

print('向量a的维数:',a.ndim)

print('向量a的元素(行、列)数:',a.shape)

print('向量a中元素的个数:',a.size)

print('向量a中元素的数据类型:',a.dtype)

print('向量a中每个元素的字节数:',a.itemsize)

 

b = np.array([[0,1,2,4],[4,5,6,7],[8,9,10,11.0]])

print(b)

print(type(b))

 

print('矩阵b的维数:',b.ndim)

print('矩阵b的行、列数:',b.shape)

print('矩阵b中元素的个数:',b.size)

print('矩阵b中元素的数据类型:',b.dtype)

print('矩阵b中每个元素的字节数:',b.itemsize)

运行结果:

1.16.0

[0 1 2 3 4 5 6 7 8 9]

<class 'numpy.ndarray'>

向量a的维数: 1

向量a的元素(行、列)数: (10,)

向量a中元素的个数: 10

向量a中元素的数据类型: int32

向量a中每个元素的字节数: 4

[[ 0.  1.  2.  4.]

 [ 4.  5.  6.  7.]

 [ 8.  9. 10. 11.]]

<class 'numpy.ndarray'>

矩阵b的维数: 2

矩阵b的行、列数: (3, 4)

矩阵b中元素的个数: 12

矩阵b中元素的数据类型: float64

矩阵b中每个元素的字节数: 8

在array对象中,ndim表示多维数组的维数,若是向量则为1,矩阵为2。

在array对象中,shape返回一个元组。若是向量,则元组索引0表示向量中元素的个数;若是矩阵,则元素返回矩阵的行数、列数。

在array对象中,size表示多维数组中元素的个数。

在array对象中,dtype表示多维数组中元素的数据类型。

在array对象中,itemsize表示多维数组中每个元素所占的字节数。

2.3.2创建numpy.ndarray

方法1:使用array

#程序2-4

import numpy as np

 

a = np.array([[1,2,3],[4,5,6]],dtype=int)

print(a)

print(a.dtype)

 

b = np.array([[1.0,2,3],[4,5,6]],dtype=float)

print(b)

print(b.dtype)

 

c = np.array(range(10))

print(c)

print(c.dtype)

 

d = np.array([x*2 for x in range(10)],dtype=float)

print(d)

print(d.dtype)

运行结果:

[[1 2 3]

 [4 5 6]]

int32

[[1. 2. 3.]

 [4. 5. 6.]]

float64

[0 1 2 3 4 5 6 7 8 9]

int32

[ 0.  2.  4.  6.  8. 10. 12. 14. 16. 18.]

float64

函数numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0),其返回值的数据类型并不是列表,而是numpy.ndarray,数据之间使用空格隔开。

参数

描述

object

列表或元组

dtype

数组元素的数据类型,可选

copy

对象是否被复制,默认为true,可选

order

C(按行)、F(按列)、A(任意,默认)

subok

默认情况下,返回的数组被强制为基类数组;如果为true,则返回子类

ndimin

指定返回数组的最小维数

方法2:使用zeros、ones、full

#程序2-5

import numpy as np

 

a = np.zeros(shape=(2,4))

print(a)

 

b = np.ones(shape=(3,4),dtype=int)

print(b)

 

c = np.full(shape=(3,5),fill_value=12,dtype=float)

print(c)

运行结果:

[[0. 0. 0. 0.]

 [0. 0. 0. 0.]]

[[1 1 1 1]

 [1 1 1 1]

 [1 1 1 1]]

[[12. 12. 12. 12. 12.]

 [12. 12. 12. 12. 12.]

 [12. 12. 12. 12. 12.]]

函数def zeros(shape, dtype=float, order='C')的作用是创建numpy.ndarray数组,其元素填充0;函数def ones(shape, dtype=None, order='C')在数组中填充1;函数def full(shape, fill_value, dtype=None, order='C')在数组中填充fill_value值。

方法3:使用arange

在Python3中可以使用内置的range函数和列表推导式来创建列表,但是range的参数只能是整数,而不能是浮点数。

在numpy中提供了arange来创建数组,def arange(start, stop, step, dtype)表示生成一个从[start,start+step,start+2*step,...,stop)的数字序列,起步长为 step,其中start、stop和step都可以是浮点数。

#程序2-6

import numpy as np

 

a = [x for x in range(1,10,2)]

print(a)

 

b = np.arange(0,1,0.2)

print(b)

运行结果:

[1, 3, 5, 7, 9]

[0.  0.2 0.4 0.6 0.8]

代码中a是列表,b是数据类型为numpy.ndarray的数组。

方法4:使用linspace

#程序2-7

import numpy as np

 

a = np.linspace(1,10,20)

print(a)

 

b = np.linspace(start=0,stop=1,num=10)

print(b)

运行结果:

[ 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.        ]

[0.         0.11111111 0.22222222 0.33333333 0.44444444 0.55555556

 0.66666667 0.77777778 0.88888889 1.        ]

在numpy中提供linspace函数,def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)表示在[start,stop]中等分num份,以数组形式返回。

方法5:使用random

#程序2-8

import random

 

a = random.random()

print(a)

 

b = random.randint(0,10)

print(b)

运行结果:

0.4582899552517844

9

在random模块中,random函数表示从0-1之间取随机数,randint(a,b)表示从a-b之间取随机整数。

在numpy中,也提供了属于numpy的random模块。

#程序2-9

import numpy as np

 

a = np.random.random()

print('a:\n',a)

 

b = np.random.random(size=4)

print('b:\n',b)

 

b1 = np.random.random(size=(2,4))

print('b1:\n',b1)

 

b2 = np.random.rand(2,4)

print('b2:\n',b2)

 

c = np.random.randint(0,10)

print('c:\n',c)

 

d = np.random.randint(0,10,size=6)

print('d:\n',d)

 

e = np.random.randint(0,10,size=(3,5))

print('e:\n',e)

运行结果:

a:

 0.9207344322941126

b:

 [0.02066962 0.64988011 0.05265515 0.60719892]

b1:

 [[0.10445075 0.22480481 0.24241775 0.42798398]

 [0.24675379 0.84126946 0.43283146 0.73175104]]

b2:

 [[0.73849037 0.51166791 0.69993536 0.94138962]

 [0.86137873 0.75368827 0.27708881 0.01204831]]

c:

 9

d:

 [0 9 6 1 3 1]

e:

 [[4 0 5 7 1]

 [7 5 9 5 3]

 [6 9 5 9 4]]

在np.random中,函数def random(size)表示创建范围[0,1)大小为size的numpy.ndarray数组;若不指定size,则表示创建一个在[0,1]之间的随机浮点数。

在np.random中,函数def rand(d0, d1, ..., dn)表示创建一个在[0,1)之间的向量或矩阵。

在np.random中,函数def randint(low, high, size, dtype)表示创建范围是[low,high]、大小为size的numpy.ndarray数组;若不指定size,则表示创建一个在[low,high]之间的随机整数。注:dtype只能是int。

#程序2-10

import numpy as np

 

#生成特定的随机数与seed(1)相关

np.random.seed(1)

a = np.random.randint(0,10,size=(2,4))

print(a)

 

#生成特定的随机数与seed(1)相关

np.random.seed(1)

b = np.random.randint(0,10,size=(2,4))

print(b)

 

#生成特定的随机数与seed(1)相关

np.random.seed(1)

c = np.random.randint(0,10,size=(2,5))

print(c)

运行结果:

[[5 8 9 5]

1. pandas:pandas是一个数据处理和分析库,提供了快速、灵活、可扩展的数据结构和数据分析工具。pandas最常用的数据结构是Series和DataFrame,可以对数据进行清洗、分析、转换和可视化等操作。 2. numpynumpy是一个基于Python的科学计算库,提供了高效的多维数组对象和数学函数库。numpy的核心是ndarray对象,可以进行快速的数值计算和数据操作,是进行科学计算和数据分析的基础库。 3. matplotlibmatplotlib是一个Python的数据可视化库,提供了丰富的图表和绘图工具,可以用于生成各种静态图表和动态图表。matplotlib最常用的绘图函数是plot函数,可以绘制线图、散点图、条形图、饼图等。 4. sklearnsklearn是一个Python的机器学习库,提供了各种常用的机器学习算法和工具,包括分类、回归、聚类、降维等算法。sklearn的核心是Estimator对象,提供了统一的API接口和模型训练、评估、预测等方法。 5. statsmodels:statsmodels是一个Python的统计分析库,提供了各种统计模型和方法,包括回归分析、时间序列分析、假设检验等。statsmodels的核心是Model对象,提供了统一的API接口和模型拟合、预测、诊断等方法。 6. itertools:itertools是Python的一个迭代器处理库,提供了各种迭代器生成函数和迭代器操作函数,可以简化迭代器处理的复杂度。itertools的常用函数包括permutations、combinations、product、chain等。 7. seaborn:seaborn是一个Python的数据可视化库,基于matplotlib库,提供了更高级的统计图表和美观的图表样式。seaborn最常用的绘图函数包括scatterplot、heatmap、barplot、boxplot等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值