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)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章