1. Numpy学习笔记

import numpy
# import numpy as np

# 从txt中读取数据,delimiter表示每一行的分隔符
score_info = numpy.genfromtxt('score.txt',delimiter='\t',dtype=str)
print(score_info)
# 查看指定函数的帮助文档
print(help(numpy.genfromtxt))

# 创建一维数组
vector1 = numpy.array([3, 6, 9, 12, 15])
print(vector1)
# 打印数组的shape
print(vector1.shape)
# 创建二维数组
vector2 = numpy.array([["00", "01", "02", "03"], ["10", "11", "12", "13"], ["20", "21", "22", "23"], ["30", "31", "32", "33"]])
print(vector2)
# 打印数组的shape
print(vector2.shape)
# numpy数组中所有数据是相同类型,自动隐性转换
numbers = numpy.array([1, 2, 3, 4])
print(numbers)
# 自动转换成浮点型
numbers = numpy.array([1, 2, 3, 4.0])
print(numbers)
# 自动转换成字符型
numbers = numpy.array(['1', 2, 3, 4])
print(numbers)
# 数据索引
# 常规索引数据
name = score_info[3, 0]
print(name)
score = score_info[3, 5]
print(score)
# 数据切片
vector = numpy.array([3, 6, 9, 12, 15])
# 左闭右开区间的数据
print(vector[0:4])
# 取矩阵的第一列
name = score_info[:, 0]
print(name)
# 取矩阵的第二列
job = score_info[:, 1]
print(job)
# 取矩阵的第一行
line1 = score_info[0, :]
print(line1)
# 取矩阵的第二行
line2 = score_info[1, :]
print(line2)
# 获取第2行-4行,2列-4列的子矩阵
data = score_info[1:4, 1:4]
print(data)

# 矩阵的拼接
a = numpy.array([[1, 3, 5], [2, 4, 6]])
b = numpy.array([[1, 2, 3], [4, 5, 6]])
# 列数相同,列不变,按列拼接
print(numpy.vstack((a, b)))
# 行数相同,行不变,按行拼接,水平拼接
print(numpy.hstack((a, b)))

# 矩阵的切分
matrix = numpy.floor(10 * numpy.random.random((8, 10)))
print(matrix)
# 水平均匀切分数值,切分列
print(numpy.hsplit(matrix, 2))
# 竖直均匀切分,切分行
print(numpy.vsplit(matrix, 2))
# 指定水平切分,切分列,在第3列和第4列后面切分
print(numpy.hsplit(matrix, (3, 4)))
# 指定竖直切分,切分行,在第2行和第5行后面切分
print(numpy.vsplit(matrix, (2, 5)))

# 判断一个数与数组是否相等,将返回布尔数组
# 一维数组
vector = numpy.array([3, 6, 9, 12, 15])
result = (vector == 9)
print(result)
# 二维数组
matrix = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
result = (matrix == 6)
print(result)
# 可以根据布尔数组进行打印相应的行和列,只有为true的值才会被打印
print(matrix[result])
# 从matrix的第3列中,找出值为7那一行的所有列值
result = (matrix[:, 2] == 7)
print(matrix[result, :])
# 与和或
vector = numpy.array([5, 10, 15, 20])
equal_to_five_and_ten = (vector == 5)&(vector == 10)
print(equal_to_five_and_ten)
equal_to_five_or_ten = (vector == 5)|(vector == 10)
print(equal_to_five_or_ten)
# 对数组整体进行值转换
vector = numpy.array(["1", "2", "3"])
print(vector.dtype)
print(vector)
#开始转换
vector = vector.astype(float)
print(vector.dtype)
print(vector)
# 求数组最小值、最大值与和
min_value = vector.min()
print(min_value)
max_value = vector.max()
print(max_value)
sum_value = vector.sum()
print(sum_value)

# 二维数组指定条件求和
matrix = numpy.array([[1, 2, 3, 4],
                      [5, 6, 7, 8],
                      [9, 10, 11, 12]
                      ])
# 对每一行求和,得到每一行的和数组
print(matrix.sum(axis=1))
# 对每一列求和,得到每一列的和数组
print(matrix.sum(axis=0))
# 矩阵变换
# 生成矩阵
a = numpy.arange(15)
print(a)
# 重新构造矩阵维度
a = a.reshape(3, 5)
print(a.shape)
a.shape
# 将矩阵还原成一维
a = a.ravel()
print(a)
# 指定维度为负数时,就是让机器自己算维度
a = a.reshape(5, -1)
print(a)
# 求矩阵的转置
print(a.T)
# 打印矩阵维度a.ndim
print(a.ndim)
# 打印矩阵数据类型a.dtype.name
print(a.dtype.name)
# 打印矩阵大小a.size
print(a.size)
# 生成零矩阵
matrix = numpy.zeros((3, 4))
print(matrix)
# 生成一矩阵
matrix = numpy.ones((2, 3, 4), dtype=numpy.int32)
print(matrix)

# 生成一组序列,从10~30,每一次加5,只能小于30
matrix = numpy.arange(10, 30, 5).reshape(2, 2)
print(matrix)
# 产生2行3列的随机数矩阵
matrix = numpy.random.random((2, 3))
print(matrix)
# 从0~2*pi 区间,等距生成200个数的矩阵
matrix = numpy.linspace(0, 2*pi, 200).reshape(40, 5)
print(matrix)
# 矩阵之间的运算
a = numpy.array([20, 30, 40, 50]).reshape(2,2)
b = numpy.arange(4).reshape(2,2)
print(a)
print(b)
# 加减法:每一个元素对应相加减
print(a + b)
print(a - b)
# 求乘法
print(b*2)
# 求平方
print(b**2)
# 求大小,返回布尔数组
print(b < 3)
# 矩阵相乘,对应位置相乘
print(a * b)
# 数学矩阵乘法,两种写法A*B=A.dot(B) 或 A*B = numpy.dot(A,B)
print(a.dot(b))
print(numpy.dot(a, b))
# 矩阵的复制
a = numpy.arange(12)
print(a)
# id相同,指向同一区域,相当于多了一个名字
b = a
print(b is a)
b.shape = 3, 4
print(a.shape)
print(id(a))
print(id(b))
# 浅拷贝,c和a虽然名字、id、shape不同,但最终指向的内存值是共用的
a = numpy.arange(12)
print(a)
c = a.view()
print(c is a)
c.shape = 3, 4
print(a.shape)
c[1, 1] = 0
print(a) # a里面值已经发生改变
# 深拷贝,完全独立的两个变量
a = numpy.arange(12)
print(a)
d = a.copy()
print(d is a)
d[0] = 9999
print(d)
print(a)

 

你可能感兴趣的:(机器学习)