Numpy常见的语法

Numpy 学习的一些常见语法,然后进行不断的扩充

  • 学习环境是Pycharm, 所需要的资料可以私信我
world_alcohol = np.genfromtxt('world_alcohol.txt', delimiter=',', dtype=str)
print(type(world_alcohol))
print(world_alcohol)
# print(help(np.genfromtxt))
# 可以直接打印文档
# The numpy.array()
vector = np.array([5, 10, 15, 20])
# when we input a list of lists
matrix = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
print(vector)
print(matrix)

print(vector.shape)
print(matrix.shape)
# array 里面必须是相同的东西
numbers = np.array([1, 2, 3, 4.0])
print(numbers)
print(numbers.dtype)

# 下面是numpy实战
# 取某一个特殊的值
uruguay_other_1986 = world_alcohol[2,4]
third_country = world_alcohol[3, 2]
print(uruguay_other_1986)
print(third_country)

# 切片
vector = np.array([5, 10, 15, 20])
print(vector[0:3])
matrix = np.array([[5, 10, 15],
                   [20, 25, 30],
                   [35, 40, 45]])
print(matrix[:, 1])
print(matrix[:, 0:2])

#进行计算- 对里面的每一个元素进行操作
vector = np.array([5, 10, 15, 20])
print(vector == 10)

matrix = np.array([[5, 10, 15],
                   [20, 25, 30],
                   [35, 40, 45]])
print(matrix == 25)

# 得到结果有什么用?
# 把正确的结果的数字会返回出来
vector = np.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print(equal_to_ten)
print(vector[equal_to_ten])

matrix = np.array([[5, 10, 15],
                   [20, 25, 30],
                   [35, 40, 45]])
second_column_25 = (matrix[:, 1] == 25)
print(second_column_25)
print(matrix[second_column_25, :])

# 取判断的操作
vector = np.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
print(equal_to_ten_and_five)

equal_to_ten_or_five = (vector == 10) | (vector == 5)
print(equal_to_ten_or_five)
# 可以修改赋值
vector[equal_to_ten_or_five] = 50
print(vector)

matrix = np.array([[5, 10, 15],
                   [20, 25, 30],
                   [35, 40, 45]])
second_column_25 = (matrix[:, 1] == 25)
matrix[second_column_25, 1] = 10
print(matrix)

# 对整体的Numpy.array 进行修改类型
vector = np.array(['1', '2', '3'])
print(vector.dtype)
print(vector)

vector = vector.astype(float)
print(vector.dtype)
print(vector)

# 常见的函数
vector = np.array([5, 10, 15, 20])
print(vector.min())
print(vector.max())

# 求和, 按列和行进行求和
matrix = np.array([[5, 10, 15],
                   [20, 25, 30],
                   [35, 40, 45]])
# axis = 1 是按行进行求和
print(matrix.sum(axis=1))
# axis = 0 按列进行求和
print(matrix.sum(axis=0))

# 对矩阵进行变换
print(np.arange(15))
a = np.arange(15).reshape(3, 5)
print(a)
print(a.shape)
# 得到维度
print(a.ndim)
print(a.dtype.name)
# 得到元素个数
print(a.size)
# 初始化矩阵全为0
print(np.zeros((3, 4)))
print(np.ones((2, 3, 4), dtype=np.int32))

# 初始化序列
print(np.arange(10, 30, 5))

# 随机初始化序列
print(np.random.random((2, 3)))

# 在XX之间指定固定的数
from numpy import pi
print(np.linspace(0, 2*pi, 100))

# 对两个array 进行计算
a = np.array([20, 30, 40, 50])
b = np.arange(4)
print(a)
print(b)
# -
c = a - b
print(c)
c = c - 1
print(c)
print(b ** 2)
print(a < 35)

# 内积
A = np.array([[1, 1],
              [0, 1]])
B = np.array([[2, 0],
              [3, 4]])
print(A)
print('---------')
print(B)
print('---------')
print(A*B)
print('---------')
print(A.dot(B))
print('---------')
print(np.dot(A, B))

# 常见的性质
B = np.arange(3)
print(B)
print(np.exp(B))
print(np.sqrt(B))

# 返回整形 floor, 向下取整
a = np.floor(10 * np.random.random((3, 4)))
print(a)
print('---------')

# flatten the array, 拉平一个矩阵
print(a.ravel())
a.shape = (6, 2)
print(a)
print(a.T)

# 如果一个维度确定, 另外一个维度可以写成-1, 自动计算
print(a.reshape(3, -1))

# 按行拼接hstack, 按列拼接 vstack
a = np.floor(10 * np.random.random((2, 2)))
b = np.floor(10 * np.random.random((2, 2)))
print(a)
print('------')
print(b)
print('------')
print(np.hstack((a, b)))
print('------')
print(np.vstack((a, b)))

# 进行切分, 根据列切分hsplit, vsplit
a = np.floor(10 * np.random.random((2, 12)))
print(a)
print('------')
print(np.hsplit(a, 3))
print('-------')
print(np.hsplit(a, (3, 4)))
a.reshape(12, 2)
a.shape = (12, 2)
print(a)
print(np.vsplit(a, 3))


# 等于,这两个变量名字不一样,但是指向同一个位置,改变一个也会改变另外一个
a = np.arange(12)
b = a
print(b is a)
b.shape = 3, 4
print(a.shape)
print(id(a))
print(id(b))

# 浅复制, 改变一个不会改变另外一个, 指向不同的位置, 但是指向相同的值, 改变里面的值都会改变
print('--------')
a = np.arange(12)
c = a.view()
print(c is a)
c.shape = 2, 6
print(a.shape)
c[0, 4] = 1234
print(a)
print(id(a))
print(id(b))
# 深复制, 各自是各自的, 没有任何关系
d = a.copy()
print(d is a)
d[0] = 99999
print(d)
print(a)

# 找到每一列的最大值 axis=0 是每一列的最大值的行
data = np.sin(np.arange(20).reshape(5, 4))
print(data)

ind = data.argmax(axis=0)
print(ind)

data_max = data[ind,range(data.shape[1])]
print(data_max)

# 扩展 行列是原来的几倍
a = np.arange(0, 40, 10)
print(a)
b = np.tile(a, (3, 5))
print(b)

# 排序 按行排序 是axis=1
a = np.array([[4, 3, 5], [1, 2, 1]])
print(a)
b = np.sort(a, axis=1)
print(b)
print(a.sort(axis=1))

a = np.array([4, 3, 1, 2])
j = np.argsort(a)
print('-----')
print(j)
print('-----')
print(a[j])```
原文地址:https://www.cnblogs.com/jly1/p/12964156.html