numpy基礎教程--where函式的使用
在numpy中,where函式是一個三元運算子,函式原型為where(condition, x, y),意思是當條件成立的時候,將矩陣的值設定為x,否則設定為y
一個很簡單的應用就是,在一個矩陣當中,將大於或等於平均值的數設定為1,將小於平均值的數設定為0
1 import numpy as np 2 # 隨機生成一個3行四列的矩陣,範圍是1--16 3 np.random.seed(10) 4 t = np.random.randint(1, 16,(3, 4), dtype=int) 5 print(t) 6 print("*"*30) 7 t_mean = t.mean()8 print("t的平均數為{0}".format(t_mean)) 9 print("*"*30) 10 # 使用where函式可以快速將一個矩陣裡面,小於平均數的值設定為0,大於平均數的值設定為1 11 t1 = np.where(t<t_mean, 0, 1) 12 print(t1)
執行結果如下圖所示
相關推薦
numpy基礎教程--where函式的使用
在numpy中,where函式是一個三元運算子,函式原型為where(condition, x, y),意思是當條件成立的時候,將矩陣的值設定為x,否則設定為y 一個很簡單的應用就是,在一個矩陣當中,將大於或等於平均值的數設定為1,將小於平均值的數設定為0 1 import num
numpy基礎教程--clip函式的使用
在numpy中,clip函式的原型為clip(self, min=None, max=None, out=None),意思是把小於min的數全部置換為min,大於max的數全部置換為max,在[min,max]之間的數則不變。out返回的是一個數組,這個數值必須和原數值維度相同,不然會報錯。 呼叫clip函
Python基礎教程 type()函式用途及使用方法
python語言中的type()函式在python中是即簡單又實用的一種物件資料型別查詢方法,本文主要介紹type()函式用途及使用方法。 type()函式可以做什麼 在介紹資料型別的文章中提到過,要怎麼樣檢視對像的資料型別。type()就是一個最實用又簡單的檢視資料型別的方法。type()
NumPy基礎:通用函式
通用函式,也可以稱為ufunc,是一種在ndarray資料中進行逐元素操作的函式。某些簡單函式接收一個或多個標量數值,併產生一個或多個標量結果,通用函式就是對這些簡單函式的向量化封裝。 有很多ufunc是簡單的逐元素轉換,比如sqrt或exp函式: import numpy as np
numpy基礎教程--將二維陣列轉換為一維陣列
1、匯入相應的包,本文所有的np指的都是numpy這個包 1 # coding = utf-8 2 import numpy as np 3 import random 2、將二維陣列轉換為一維陣列的方法 (1)使用reshape()函式 (2)使用flatten()函式 # 把二維
numpy基礎教程--二維陣列的轉置
使用numpy庫可以快速將一個二維陣列進行轉置,方法有三種 1.使用numpy包裡面的transpose()可以快速將一個二維陣列轉置 2.使用.T屬性快速轉置 3.使用swapaxes(1, 0)方法 t5 = np.arange(12).reshape(3, 4) print(t5) pr
numpy基礎教程--淺拷貝和深拷貝
在numpy中,使用等號(=)直接賦值返回的是一個檢視,屬於淺拷貝;要完整的拷貝一個numpy.ndarray型別的資料的話,只能呼叫copy()函式 # coding = utf-8 import numpy as np t = np.zeros(24).reshape(4, 6) t1 = t t
numpy基礎教程--對數組進行水平拼接和豎直拼接
src 實用 code \n 接收 odin 快捷 拼接 進行 在處理數組的時候經常要用到拼接,numpy中有兩個非常實用的函數,可以快捷對數組進行拼接 1.hstack(tup)函數可以接收維度相同的數組,進行水平拼接。 2.vstack(tup)用來豎直拼接 演示代碼如
numpy基礎教程--對陣列進行水平拼接和豎直拼接
在處理陣列的時候經常要用到拼接,numpy中有兩個非常實用的函式,可以快捷對陣列進行拼接 1.hstack(tup)函式可以接收維度相同的陣列,進行水平拼接。 2.vstack(tup)用來豎直拼接 演示程式碼如下: # coding = utf-8 import numpy as np imp
基礎教程——python函式
定義一個函式: 1.使用def關鍵字, def + 函式名 +(引數列表) 2.函式的內容由‘ :’開始,由縮排結束 注意:由於python的變數沒有型別,則不像c和c++那麼樣有引數型別 雖然,沒有引數型別,但是在變數的運算過程中,如果變數所引用的值錯誤依舊會報錯
C語言基礎教程 printf( )函式中的回車‘\r’與回車換行'\n'
今天完成了初級階段的學習,其實以前在開發微控制器的時候用過C語言,但是沒有系統的學過,所以再次學習感覺能夠查缺補漏。 C語言中的printf( )函式是一個很重要的函式。printf( )中的字元包含了三種: (1)以%開頭的格式控制字元 (2)以 \ 開頭的轉義字元 (3)普通字元 在printf
Python Numpy-基礎教程
目錄 1. 為什麼要學習numpy? 2. Numpy基本用法 2.1. 建立np.ndarry 2.2. Indexing and Slicing Boolean Index 2.3. Universal Functions
python numpy 基礎教程
Numpy簡單介紹 1.Numpy是什麼 很簡單,Numpy是Python的一個科學計算的庫,提供了矩陣運算的功能,其一般與Scipy、matplotlib一起使用。其實,list已經提供了類似於矩陣的表示形式,不過numpy為我們提供了更多的函式。如果接觸過matlab、
numpy中的where()函式
test_data=np.where(df_test>120,1,0) where算是三目運算子,where(條件,真,假) Return elements, either from x or y, depending on condition. If only
Python Numpy基礎教程
Python Numpy基礎教程 本文是一個關於Python numpy的基礎學習教程,其中,Python版本為Python 3.x 什麼是Numpy Numpy = Numerical + Python,它是Python中科學計算的核心庫,可以高效的處理多維陣列的計算。並且,因為它的許多底層函式是用C語
Python資料分析基礎教程:NumPy學習指南 第二章 常用函式
目錄 第二章 常用函式 1 檔案讀寫示例 建立對角矩陣: np.eye(2) 儲存為txt檔案:np.savetxt("eye.txt", i2) 2 CSV檔案讀取: loadtxt() 3 &nb
數據分析基礎教程Numpy指南筆記
rgs sign font 功能 hang ptime indices import ges Numpy指南筆記 第2章:Numpy基礎 創建多維數組# coding:utf-8import numpy as npm=np.array([np.arange(2),np.ar
Python資料分析基礎教程:NumPy學習指南(第2版) pdf 下載
罕見的NumPy中文入門教程,Python資料分析優選從基礎的知識講起,手把手帶你進入大資料探勘領域囊括大量具有啟發性與實用價值的實戰案例。 內容簡介 《圖靈程式設計叢書;Python資料分析基礎教程:NumPy學習指南(第2版)》是NumPy的入門教程,主要介紹NumPy以及相關
numpy where()函式
原文連結:https://blog.csdn.net/qq_32005671/article/details/74011993 numpy.where()函式是三元表示式x if condition else y的向量化版本。假設我們有一個布林陣列和兩個值陣列: x = np.array([
分享《Python資料分析基礎教程:NumPy學習指南(第2版)》高清中文PDF+英文PDF+原始碼
下載:https://pan.baidu.com/s/1YSD97Gd3gmmPmNkvuG0eew更多資料分享:http://blog.51cto.com/3215120 《Python資料分析基礎教程:NumPy學習指南(第2版)》高清中文PDF+高清英文PDF+原始碼 高清中文版PDF,249頁,帶