机器学习实战------第一天(numpy)

机器学习:根据经验E,完成任务T,达到性能P,不断从经验E中去学习,下次完成任务T,能够通过性能P的考验。

机器学习算法分类:有监督学习,分为分类和回归。无监督学习,主要是聚类。强化学习,策略学习。

numpy实践:

# 利用numpy中的array函数生成一维向量和矩阵

import numpy                                     # 导入numpy
vector = numpy.array([1,2,3,4])          #生成一维向量
print(vector)
matrix = numpy.array([[1,2,3,4],[4,5,6,7],[8,9,10,11]])     #生成矩阵
print(matrix)

输出结果[1 2 3 4]   [[ 1 2 3 4] [ 4 5 6 7] [ 8 9 10 11]]

# 利用shape属性测量向量元素
 

import numpy
vector = numpy.array([1,2,3,4])
print(vector)
matrix = numpy.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(matrix)
print(vector.shape)
print(matrix.shape)

输出结果:[1 2 3 4] [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] (4,) (3, 4)

# 利用dtype属性可以返回数据类型
import numpy
vector = numpy.array([1,2,3,4])
print(vector.dtype)

输出结果:int32

# 利用索引取值,包头不包尾
import numpy
vector = numpy.array([5,10,15,20])
print(vector[0:3])
matrix = numpy.array([[4,3,7,9],[8,6,2,7],[10,12,19,17]])
print(matrix[1][2])
print(matrix[:,1])
print(matrix[:,0:3])

输出结果:[ 5 10 15] 2 [ 3 6 12] [[ 4 3 7] [ 8 6 2] [10 12 19]]

# 比较运算符,整体比较
import numpy
vector = numpy.array([10,15,20,25])
matrix = numpy.array([[10,15,20,25],[12,16,18,19],[4,5,9,15]])
print(vector)
print(matrix)
a = (vector==15)
b = (matrix==15)
print(a)
print(a.shape)
print(a.dtype)
print(b)

计算结果:[10 15 20 25] [[10 15 20 25] [12 16 18 19] [ 4 5 9 15]] [False True False False] (4,) bool [[False True False False] [False False False False] [False False False True]]

# 返回正确索引值所在的行
import numpy
matrix = numpy.array([[10,12,16,18],[20,22,24,26],[28,30,32,34]])
print(matrix)
equal_22_colum = (matrix[:,1]==22)
print(equal_22_colum)
print(matrix[equal_22_colum,:])
计算结果:[[10 12 16 18] [20 22 24 26] [28 30 32 34]] [False True False] [[20 22 24 26]]

# 做复杂逻辑运算
import numpy
vector = numpy.array([5,10,15,20])
print(vector)
equal_to_ten_and_five = (vector==10) & (vector==5)
print(equal_to_ten_and_five)
print("/**************************/")
equal_to_ten_or_five = (vector==10) | (vector==5)
print(equal_to_ten_or_five)
运算结果:[ 5 10 15 20] [False False False False] /**************************/ [ True True False False]

# 用复杂运算结果做索引值,进行取值或者修改原数组内容
import numpy
vector = numpy.array([5,10,25,30])
print(vector)
equal_to_ten_or_five = ((vector==5) | (vector==10))
print(equal_to_ten_or_five)
vector[equal_to_ten_or_five] = 50
print(vector)

输出结果:[ 5 10 25 30] [ True True False False] [50 50 25 30]

 对数组进行强制类型转换,调用ndarray.astype() method.注意:数组里面所有元素类型应该一致
import numpy
vector = numpy.array(["1","2","3","4"])
print(vector,vector.dtype)
vector = vector.astype(float)
print(vector,vector.dtype)

输出结果:['1' '2' '3' '4'] <U1 [1. 2. 3. 4.] float64

# 对数据组进行按行或者按列进行求和
import numpy
matrix = numpy.array([[10,20,30,40],[11,21,31,41],[12,22,32,42],[13,23,33,43]])
print(matrix)
print(matrix.sum(axis=1))      #按行求和
print(matrix.sum(axis=0))      #按列求和

计算结果:

[[10 20 30 40] [11 21 31 41] [12 22 32 42] [13 23 33 43]]

[100 104 108 112]

[ 46 86 126 166]

# 对数组进行重新整合
import numpy as np
a = np.arange(15)
print(a,a.shape)
b = a.reshape(3,5)
print(b,b.shape)
print(b.dtype)

计算结果:

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] (15,) [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] (3, 5) int32

# 生成特殊数组
from numpy import pi
import numpy as np
a = np.zeros((3,4))    # 注意里面应该传输元组
print(a)
b = np.ones((4,4))
print(b)
c = np.random.random((3,4))
print(c)
d = np.linspace(0,2*pi,100)
print(d)

# 矩阵运算
import numpy
matrix1 = numpy.array([[1,2],[3,4]])
matrix2 = numpy.array([[5,6],[7,8]])
print(matrix1)
print(matrix2)
print(matrix1*matrix2)
a = matrix1.dot(matrix2)
b = np.dot(matrix1,matrix2)
print(a)
print(b)

计算结果:[[1 2] [3 4]] [[5 6] [7 8]] [[ 5 12] [21 32]] [[19 22] [43 50]] [[19 22] [43 50]]

科学计算库中的flatten()和ravel()

相同点:两者功能一致,将多维数组转化为一维数组

不同点:在于是返回(copy)拷贝还是返回视图(view)

numpy.flatten()返回一份拷贝,对拷贝所做的修改不会影响原始矩阵

而numpy.ravel()返回的是视图,会影响原始矩阵

# 将多维数组转化为一维数组
import numpy
x = numpy.array([[1,2],[3,4]])
print(x)
a = x.flatten()      
print(a)
b = x.ravel()
print(b)                    # 两者都默认行序优先
c = x.flatten("F")          # 按列重组
print(c)

运算结果:

[[1 2] [3 4]] [1 2 3 4] [1 2 3 4] [1 3 2 4]

# ravel()的两种用法

# numpy.ravel()
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
y1 = np.ravel(x)                    # 默认按行
print(y1)

y2 = np.ravel(x,order="F")         # 按列
print(y2)

# ndarray.ravel()
a = np.arange(12).reshape(3,4)
print(a)
b = a.ravel(order="C")
c = a.ravel(order="F")
print(b)
print(c)

运算结果:

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

# 注意两者不同点
import numpy
x = numpy.array([[1,2],[3,4]])
x.flatten()[1] = 100
print(x)
x.ravel()[1] = 100
print(x)

运算结果:[[1 2] [3 4]] [[ 1 100] [ 3 4]]

# 浅复制(传引用)
import numpy as np
x = np.arange(3)
y = x.view()
print(x)
print(y)
print(x is y)
x[0] = 100
print(x)
print(y)

运算结果:[0 1 2] [0 1 2] False [100 1 2] [100 1 2]

# 深复制(传值)
import numpy as np
x = np.arange(3)
y = x.copy()
print(x)
print(y)
x[0] = 100
print(x)
print(y)
print(x is y)

运算结果:[0 1 2] [0 1 2] [100 1 2] [0 1 2] False

# 两种调用方法,方法一:numpy.argmax(a,axis=0)   方法二:adarray.argmax(axis=0)
import numpy as np
data = np.arange(20).reshape(5,4)
print(data)
ind = data.argmax(axis=0)          # 返回列当中元素值最大的索引值
print(ind)
a = data.argmax(axis=1)            # 返回行当中元素值最大的索引值
print(a)

运算结果:[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15] [16 17 18 19]]

                  [4 4 4 4]

                  [3 3 3 3 3]

# 生成相同多数数组
x = np.arange(0,40,10)
print(x)
y = np.tile(x,(3,4))
print(y)

运算结果:

[ 0 10 20 30]

[[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]]

# 对数组元素进行排序
a = np.array([[4,3,5],[1,2,1]])
print(a)
b = np.sort(a,axis=1)               # 按行默认升序排列
print(b)
c = np.argsort(a,axis=1)            # 返回的是数组值从小到大的索引值
print(c)

运算结果:

[[4 3 5] [1 2 1]] [[3 4 5] [1 1 2]] [[1 0 2] [0 2 1]]

发布了3 篇原创文章 · 获赞 1 · 访问量 65

猜你喜欢

转载自blog.csdn.net/qq_38975852/article/details/100587168