机器学习入门:准备知识笔记(numpy)

Azure机器学习实验

通过站点:https://studio.azureml.net/ 申请免费试用

体验机器学习工作流程

1.获取数据
2.数据基本处理
3.特征工程
4.机器学习(模型训练)
5.模型评估


ananconda安装后启动运行,出现错误

.continuum/anaconda-client: Permission denied

办法:查看 ls -al .continuum/,没内容,直接删除,重启anaconda,问题解决。


import matplotlib.pyplot as plt
# 1.创建画布
plt.figure(figsize=(10, 10), dpi=100)
# 2.绘制折线图
plt.plot([1, 2, 3, 4, 5, 6 ,7], [17,17,18,15,11,11,13])
# 3.显示图像
plt.show()


N维数组-ndarray

ndarray的属性
数组属性反映了数组本身固有的信息。
属性名字                         属性解释
ndarray.shape               数组维数
ndarray.size                 数组中的元素数量
ndarray.itemsize         一个数组元素的长度( 字节)
ndarray.dtype                 数组元素的类

ndarray的类型
>>> type(score.dtype)

 创建数组的时候指定类型
>>> a = np.array([[1, 2, 3],[4, 5, 6]], dtype=np.float32)
>>> a.dtype
dtype('float32')
>>> arr = np.array(['python', 'tensorflow', 'scikit-learn', 'numpy'], dtype = np.string_)
>>> arr
array([b'python', b'tensorflow', b'scikit-learn', b'numpy'], dtype='|S12')
注意: 若不指定, 整数默认int64, 小数默认float64

基本操作

生成0和1的数组
np.ones(shape, dtype)
np.ones_like(a, dtype)
np.zeros(shape, dtype)
np.zeros_like(a, dtype)
ones = np.ones([4,8])
ones
返回结果:
array([[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1.]])
np.zeros_like(ones)
返回结果:
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.]])

从现有数组生成
生成方式
np.array(object, dtype)
np.asarray(a, dtype)
a = np.array([[1,2,3],[4,5,6]])
# 从现有的数组当中创建
a1 = np.array(a)
# 相当于索引的形式, 并没有真正的创建一个新的
a2 = np.asarray(a)
关于array和asarray的不同

 生成固定范围的数组
 np.linspace (start, stop, num, endpoint)
创建等差数组 — 指定数量
参数:
start:序列的起始值
stop:序列的终止值
num:要生成的等间隔样例数量, 默认为50
endpoint:序列中是否包含stop值, 默认为True
# 生成等间隔的数组
np.linspace(0, 100, 11)
返回结果:
array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])

np.arange(start,stop, step, dtype)
创建等差数组 — 指定步长,不包含stop
参数
step:步长,默认值为1
np.arange(10, 50, 2)

返回结果:
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,
44, 46, 48]) 

np.logspace(start,stop, num,endpoint)
创建等比数列
参数:
num:要生成的等比数列数量, 默认为50
# 生成10^x
np.logspace(0, 2, 3)
返回结果:
array([ 1., 10., 100.])

生成随机数组
使用模块:        np.random模块

正态分布

什么是正态分布
正态分布是一种概率分布。 正态分布是具有两个参数μ和σ的连续型随机变量的分布, 第一参数μ是服从正态分布的随机变量的均值, 第二个参数σ是此随机变量的标准差, 所以正态分布记作N(μ, σ )。

正态分布特点
μ决定了其位置, 其标准差σ决定了分布的幅度。 当μ = 0,σ = 1时的正态分布是标准正态分布。
标准差如何来

正态分布创建方式
np.random.randn(d0, d1, …, dn)
功能: 从标准正态分布中返回一个或多个样本值


np.random.normal(loc=0.0, scale=1.0, size=None)

loc: float
此概率分布的均值( 对应着整个分布的中心centre)
scale: float
此概率分布的标准差( 对应于分布的宽度, scale越大越矮胖, scale越小, 越瘦高)
size: int or tuple of ints
输出的shape, 默认为None, 只输出一个值


np.random.standard_normal(size=None)
返回指定形状的标准正态分布的数组。

举例1: 生成均值为1.75, 标准差为1的正态分布数据, 100000000个
x1 = np.random.normal(1.75, 1, 100000000)
返回结果:
array([2.90646763, 1.46737886, 2.21799024, ..., 1.56047411, 1.87969135,
0.9028096 ])
# 生成均匀分布的随机数
x1 = np.random.normal(1.75, 1, 100000000)
# 画图看分布状况
# 1) 创建画布
plt.figure(figsize=(20, 10), dpi=100)
# 2) 绘制直方图
plt.hist(x1, 1000)
# 3) 显示图像
plt.show()

例如: 我们可以模拟生成一组股票的涨跌幅的数据
举例2: 随机生成4支股票1周的交易日涨幅数据4支股票, 一周(5天)的涨跌幅数据, 如何获取?
随机生成涨跌幅在某个正态分布内, 比如均值0, 方差1
股票涨跌幅数据的创建
# 创建符合正态分布的4只股票5天的涨跌幅数据

stock_change = np.random.normal(0, 1, (4, 5))
stock_change
返回结果:
array([[ 0.0476585 , 0.32421568, 1.50062162, 0.48230497, -0.59998822],
[-1.92160851, 2.20430374, -0.56996263, -1.44236548, 0.0165062 ],
[-0.55710486, -0.18726488, -0.39972172, 0.08580347, -1.82842225],
[-1.22384505, -0.33199305, 0.23308845, -1.20473702, -0.31753223]])

均匀分布
np.random.rand(d0, d1, ..., dn)
返回[0.0, 1.0)内的一组均匀分布的数。
np.random.uniform(low=0.0, high=1.0, size=None)
功能: 从一个均匀分布[low,high)中随机采样, 注意定义域是左闭右开, 即包含low, 不包含high.
参数介绍:
low: 采样下界, float类型, 默认值为0;
high: 采样上界, float类型, 默认值为1;
size: 输出样本数目, 为int或元组(tuple)类型, 例如, size=(m,n,k), 则输出mnk个样本, 缺省时输出1个值。
返回值: ndarray类型, 其形状和参数size中描述一致。
np.random.randint(low, high=None, size=None, dtype='l')
从一个均匀分布中随机采样, 生成一个整数或N维整数数组,
取数范围: 若high不为None时, 取[low,high)之间随机整数, 否则取值[0,low)之间随机整数。
# 生成均匀分布的随机数
x2 = np.random.uniform(-1, 1, 100000000)
返回结果:
array([ 0.22411206, 0.31414671, 0.85655613, ..., -0.92972446,
0.95985223, 0.23197723])
画图看分布状况:
import matplotlib.pyplot as plt
# 生成均匀分布的随机数
x2 = np.random.uniform(-1, 1, 100000000)
# 画图看分布状况
# 1) 创建画布
plt.figure(figsize=(10, 10), dpi=100)
# 2) 绘制直方图
plt.hist(x=x2, bins=1000) # x代表要使用的数据, bins表示要划分区间数
# 3) 显示图像
plt.show()

数组的索引、 切片
一维、 二维、 三维的数组如何索引?
直接进行索引,切片
对象[:, :] -- 先行后列

形状修改

ndarray.reshape(shape, order)
返回一个具有相同数据域, 但shape不一样的视图
行、 列不进行互换
# 在转换形状的时候, 一定要注意数组的元素匹配
stock_change.reshape([5, 4])
stock_change.reshape([-1,10]) # 数组的形状被修改为: (2, 10), -1: 表示通过待计算

ndarray.resize(new_shape)
修改数组本身的形状( 需要保持元素个数前后相同)
行、 列不进行互换
stock_change.resize([5, 4])
# 查看修改后结果
stock_change.shape
(5, 4)

ndarray.T
数组的转置
将数组的行、 列进行互换
stock_change.T.shape
(4, 5)

类型修改
ndarray.astype(type)
返回修改了类型之后的数组
stock_change.astype(np.int32)


ndarray.tostring([order])或者ndarray.tobytes([order])
构造包含数组中原始数据字节的Python字节
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[12, 3, 34], [5, 6, 7]]])
arr.tostring()

数组的去重
np.unique()
temp = np.array([[1, 2, 3, 4],[3, 4, 5, 6]])
>>> np.unique(temp)
array([1, 2, 3, 4, 5, 6])


ndarray运算

逻辑运算
# 生成10名同学, 5门功课的数据
>>> score = np.random.randint(40, 100, (10, 5))
# 取出最后4名同学的成绩, 用于逻辑判断
>>> test_score = score[6:, 0:5]
# 逻辑判断, 如果成绩大于60就标记为True 否则为False
>>> test_score > 60
array([[ True, True, True, False, True],
[ True, True, True, False, True],
[ True, True, False, False, True],
[False, True, True, True, True]])
# BOOL赋值, 将满足条件的设置为指定的值-布尔索引
>>> test_score[test_score > 60] = 1
>>> test_score
array([[ 1, 1, 1, 52, 1],
[ 1, 1, 1, 59, 1],
[ 1, 1, 44, 44, 1],
[59, 1, 1, 1, 1]])

通用判断函数
np.all()
# 判断前两名同学的成绩[0:2, :]是否全及格
>>> np.all(score[0:2, :] > 60)
False
np.any()
# 判断前两名同学的成绩[0:2, :]是否有大于90分的
>>> np.any(score[0:2, :] > 90)
True

np.where( 三元运算符)
通过使用np.where能够进行更加复杂的运算
np.where()
# 判断前四名学生,前四门课程中, 成绩中大于60的置为1, 否则为0
temp = score[:4, :4]
np.where(temp > 60, 1, 0)


复合逻辑需要结合np.logical_and和np.logical_or使用

# 判断前四名学生,前四门课程中, 成绩中大于60且小于90的换为1, 否则为0
np.where(np.logical_and(temp > 60, temp < 90), 1, 0)
# 判断前四名学生,前四门课程中, 成绩中大于90或小于60的换为1, 否则为0
np.where(np.logical_or(temp > 90, temp < 60), 1, 0)

统计运算

常用的指标如下:
min(a, axis)
Return the minimum of an array or minimum along an axis.
max(a, axis])
Return the maximum of an array or maximum along an axis.
median(a, axis)
Compute the median along the specified axis.
mean(a, axis, dtype)
Compute the arithmetic mean along the specified axis.
std(a, axis, dtype)
Compute the standard deviation along the specified axis.
var(a, axis, dtype)
Compute the variance along the specified axis

进行统计的时候, axis 轴的取值并不一定, Numpy中不同的API轴的值都不一样, 在这里, axis 0代表列, axis 1代表行去进行统计
# 接下来对于前四名学生,进行一些统计运算
# 指定列 去统计
temp = score[:4, 0:5]
print("前四名学生,各科成绩的最大分: {}".format(np.max(temp, axis=0)))
print("前四名学生,各科成绩的最小分: {}".format(np.min(temp, axis=0)))
print("前四名学生,各科成绩波动情况: {}".format(np.std(temp, axis=0)))
print("前四名学生,各科成绩的平均分: {}".format(np.mean(temp, axis=0)))

结果:
前四名学生,各科成绩的最大分: [96 97 72 98 89]
前四名学生,各科成绩的最小分: [55 57 45 76 77]
前四名学生,各科成绩波动情况: [16.25576821 14.92271758 10.40432602 8.0311892 4.32290412]
前四名学生,各科成绩的平均分: [78.5 75.75 62.5 85. 82.25]

如果需要统计出某科最高分对应的是哪个同学?
np.argmax(temp, axis=)
np.argmin(temp, axis=)
print("前四名学生, 各科成绩最高分对应的学生下标: {}".format(np.argmax(temp, axis=0)))
结果:
前四名学生, 各科成绩最高分对应的学生下标: [0 2 0 0 1]


数组与数的运算
arr = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])
arr + 1
arr / 2
# 可以对比python列表的运算, 看出区别
a = [1, 2, 3, 4, 5]
a * 3

广播机制
数组在进行矢量化运算时, 要求数组的形状是相等的。 当形状不相等的数组执行算术运算的时候, 就会出现广播机制, 该机制会对数组进行扩展, 使数组的shape属性值一样, 这样, 就可以进行矢量化运算了。 下面通过一个例子进行说明:
arr1 = np.array([[0],[1],[2],[3]])
arr1.shape
# (4, 1)
arr2 = np.array([1,2,3])
arr2.shape
# (3,)
arr1+arr2
# 结果是:
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])
上述代码中, 数组arr1是4行1列, arr2是1行3列。 这两个数组要进行相加, 按照广播机制会对数组arr1和arr2都进行扩展, 使得数组arr1和arr2都变成4行3列。

广播主要发生在两种情况, 一种是两个数组的维数不相等, 但是它们的后缘维度的轴长相符, 另外一种是有一方的长度为1。
广播机制实现了时两个或两个以上数组的运算, 即使这些数组的shape不是完全相同的, 只需要满足如下任意一个条件即可。
如果两个数组的后缘维度( trailing dimension, 即从末尾开始算起的维度) 的轴长度相符,
或其中的一方的长度为1。

广播会在缺失和( 或) 长度为1的维度上进行。
广播机制需要扩展维度小的数组, 使得它与维度最大的数组的shape值相同, 以便使用元素级函数或者运算符进行运算。

矩阵乘法api:
np.matmul
np.dot

>>> a = np.array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
>>> b = np.array([[0.7], [0.3]])
>>> np.matmul(a, b)
array([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])
>>> np.dot(a,b)
array([[81.8],
[81.4],
[82.9],
[90. ],
[84.8],
[84.4],
[78.6],
[92.6]])

np.matmul和np.dot的区别:
二者都是矩阵乘法。 np.matmul中禁止矩阵与标量的乘法。 在矢量乘矢量的內积运算中, np.matmul与np.dot没有区别。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值