# -*- coding: utf-8 -*-
#2.3.2
import numpy as np
#生成一個向量
print(np.random.randint(0,10,10))
#size 向量大小
print(np.random.randint(0,5,size=5));
#生成矩陣
print(np.random.randint(4,9,size=(3,5)))
#seed 每次生成的隨機數(shù)是固定的,隨機種子用1記錄
#生成介于0~1之間的浮點數(shù)的向量或者矩陣
print(np.random.random(10))
print(np.random.random((3,4)))
#random.normal 正態(tài)分布
#loc正態(tài)分布均值 scale正態(tài)分布標準差 sieze大小
print(np.random.normal(loc=0,scale=5,size=(3,5)))
#2.3.3 獲取numpy屬性
# =============================================================================
# 生成數(shù)組
# =============================================================================
m_matrix = np.arange(15)
m_matrix=m_matrix.reshape(3,5)
print(m_matrix.shape)
#EX
x = np.arange(15)
print(x.ndim)
#轉換成3行5列的2維矩陣
X=x.reshape(5,3)
print(X.ndim)
#只關心有多少列或行
print(x.reshape(5,-1))
print(x.reshape(-1,15))
# =============================================================================
# 切片操作
# =============================================================================
print(X)
print(X[:1])
print(X[:,0:2])
print(X[1:3,:])
print(X[1:3,0:2])
# =============================================================================
# numpy矩陣運算
# =============================================================================
myones = np.ones([3,3])
#生成一個對角線值為1,其余值為0的三行三列矩陣
myeye = np.eye(3)
print(myeye)
print(np.sin(myones - myeye))
#矩陣點乘,滿足 n*m * m*l = n*l,矩陣乘法函數(shù)為dot
mymatrix = np.linspace(1,6,6,dtype=(int)).reshape(2,3)
a = np.linspace(1,6,6,dtype=int).reshape(3,2)
print(mymatrix.shape[1] == a.shape[0])
print(mymatrix.dot(a))
# =============================================================================
# 矩陣轉置,逆矩陣
# =============================================================================
import numpy.linalg as lg
A = np.array([[i for i in range(2)],[i+2 for i in range(2)]])
print(A)
invA = lg.inv(A)
print(invA)
print(A.dot(invA))
# =============================================================================
# 數(shù)據(jù)類型轉換
# =============================================================================
vector = np.array(['1','2','3'])
print("String to float = ",vector.astype(float),'\n\n\n')
# =============================================================================
# Numpy內置方法
# =============================================================================
#sum mean max? median 中位數(shù)
#類型必須為int或者float
vector = np.array([5,10,15,20])
print('the np.sum=',vector.sum(),'\n\n\n')
#矩陣例子
matrix = np.array([[5,10,15],[20,10,30],[35,40,45]])
print(matrix)
print(matrix.T)
masu1=matrix.sum(axis=1)
print('matrix.sum axis1 為1時為行和,輸出為列 =',masu1)
print(masu1.ndim)
print('matrix.sum axis0 =',matrix.sum(axis=0),'\n\n\n')
# =============================================================================
# np中的arg運算
# =============================================================================
index2 = np.argmax([1,2,6,3,2])
print('the max of this matrix = ',index2)
#例子
x = np.arange(15)
print(x)
np.random.shuffle(x)#隨機打亂順序
print(x)
sx = np.argsort(x)
print('the index of this arg vector=',sx)
# =============================================================================
# FancyIndexing
# =============================================================================
x = np.arange(15)
np.random.shuffle(x)
ind = [3,5,8]
print(x[ind],'\n\n')
ind = np.array([[0,2],[1,3]])
print(x,'\n')
print(x[ind],'\n')
#二維矩陣取數(shù)
x = np.arange(16)
X =x.reshape(4,-1)
row = np.array([0,1,2])
col = np.array([1,2,3])
print(X)
print(X[row,col])
print(X[1:3,col])
# =============================================================================
# np數(shù)組比較
# =============================================================================
matrix = np.array([[5,10,15],[20,25,30],[35,40,45]])
m = (matrix == 25)
print(m,'\n')
second_column_25 = (matrix[:,1] == 25)
print(second_column_25,'\n')
print(matrix[second_column_25,:],'\n')
#計算符合條件的總數(shù)np.count_nonzero
print(np.count_nonzero(matrix<=28))
#計算是否符合條件
print(np.any(matrix == 25))
#計算是否所有數(shù)據(jù)都符合條件
print(np.all(matrix==25),'\n\n')
? ??