2020-12-09

# -*- 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')

? ??

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 隨機數(shù)# 隨機產生一個長度為 7 數(shù)值在 1 到 10 (不包含10)之間的數(shù)組random_array = np...
    lei_charles閱讀 233評論 0 0
  • Numpy是用Python做數(shù)據(jù)分析所必須要掌握的基礎庫之一,它可以用來存儲和處理大型矩陣,并且Numpy提供了許...
    91160e77b9d6閱讀 1,013評論 0 0
  • import numpy as np 創(chuàng)建ndarray data1 = [6,7.5, 8, 0, 1]arr1...
    陸文斌閱讀 828評論 0 1
  • 本教程是基于Numpy1.14官方網(wǎng)站的文檔 原文地址:點我呀 為本人在備考期間利用課余時間進行翻譯的,預計在一周...
    劉點石閱讀 14,784評論 1 31
  • numpy.random.randint Return random integers fromlow(inclu...
    onepedalo閱讀 1,312評論 0 1

友情鏈接更多精彩內容