numpy1

1、NumPy包含的內容

  1、ndarrray,高效的多維數組,提供了基于數組的便捷算術操作以及靈活的廣播功能;

  2、對所有數組對象進行快速的矩陣計算,而無需編寫循環;

  3、提供對硬盤中的數據的讀寫工具,并對內存映射文件進行操作;

  4、可實現線性變換、隨機數生成以及傅里葉變換功能;

  特點:NumPy在內部將數據存儲在連續的內存塊上,這與其他的Python內建序列是不同的,這使得NumPy數組對象的計算速度比其他同內容同操作的對象快(快10—100倍)。

?

2、每個ndarray對象具備的屬性

  1、shape屬性用來描述數組的維數;

  2、dtype屬性描述數組的數據類型,它包含了ndarray需要為某一種類型數據所申明的內存塊信息,是NumPy能夠與其他系統數據靈活交互的原因;

1 import numpy as np
2 data = np.random.randn(2,3)
3 data.shape
4 Out[14]: 
5 》(2, 3)
6 data.dtype
7 Out[15]: 
8 》dtype('float64')

?

3、生成ndarray

  1、array函數,array函數接受任意的序列型對象(包括數組),生成一個包含傳遞數據的NumPy數組;接受嵌套序列時,自動轉換成多維數組。

 1 data1 = [1,2,7.9,0,1]
 2 arr1 = np.array(data1)
 3 arr1
 4 Out[18]: 
 5 array([1. , 2. , 7.9, 0. , 1. ])
 6 data2 = [[1,2,3],[1,2,3]]
 7 np.array(data2)                 # 接受嵌套序列,自動轉成二維數組
 8 Out[20]: 
 9 》array([[1, 2, 3],
10        [1, 2, 3]])

  2、zeros()、ones()、empty()、full()等函數,注意zeros()、ones()、empty()等函數接受的參數只有一個,如果要生成多維數組,則需要為shape傳遞一個元組指定維數;

  3、ones_like()根據所給的數據數組生成一個形狀一摸一樣的全1數組;zeros_like(),empty_like()、full_like()也類似;

  4、arange()生成python內建函數range的數組版,返回一個數組。

?

4、ndarray數據類型

  1、python數據數據類型眾多,可以使用astype方法顯示地轉換數組地數據類型,注意:浮點型轉整型時,小數點后部分將被消除。使用astype時,返回一個新的數組,而原數組不變。

1 arr = np.array([1, 2, 3, 4, 5])
2 print(arr.dtype)
3 float_arr = arr.astype(np.float64)
4 print(float_arr.dtype)
5 》》int32
6 》》float64

  2、如果一個數組里面地元素都是表達數字含義地字符串,也可以將字符串轉換成數字;

1 numeric_strings = np.array(['1.25', '-9.6', '42'], dtype=np.string_)
2 numeric_strings
3 Out[30]: 
4 >>array([b'1.25', b'-9.6', b'42'], dtype='|S4')
5 numeric_strings.astype(np.float)
6 Out[31]: 
7 >>array([ 1.25, -9.6 , 42.  ])

?

5、NumPy數組算術

  數組之間可以直接進行批量操作而無需任何for循環;

  任何在兩個等尺寸數組之間的算術操作都運用了逐元素操作的方式;

  帶有標量的算術操作,會把計算參數傳遞給每一個元素;

  同尺寸數組之間的比較,會產生一個布爾類型的數組;

?

6、數組索引與切片

  1、數組的索引與列表的索引類似,但不相同。區別于python數組的內建列表,數組的切片是原數組的視圖,即數組并不是被復制了,任何對于視圖的修改都會反應到原數組上。

 1 arr = np.arange(10)
 2 arr[5:8] = 12
 3 arr_slice = arr[5:8]
 4 print(arr_slice)
 5 arr_slice[1] = 12345    # 對視圖的修改后,原數組也會被改變
 6 arr
 7 [12 12 12]
 8 Out[38]: 
 9 array([    0,     1,     2,     3,     4,    12, 12345,    12,     8,
10            9])

  2、如果相對數組進行復制而不是得到一份視圖,應該使用copy方法,eg:arr[5:8].copy()

  3、多維數組的索引方法,以二維數組為例,數組名[index][index]或者數組名[index,index]的方式。

?

7、布爾索引

  1、在索引數組時,可以傳入布爾值數組,返回的是True對應的內容。

names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
data = np.random.randn(7, 4)
names == 'Bob'
Out[41]: 
array([ True, False, False,  True, False, False, False])
data[names == 'Bob']
Out[42]: 
array([[-0.71491699,  0.40102409, -0.42140722, -1.50136196],[ 0.21920979, -0.13960939,  1.60586575,  0.0712131 ]])

  2、基于常識來設置布爾數組的值也是可行的。

 1 data = np.random.randn(7, 4)
 2 data
 3 Out[44]: 
 4 >>array([[-0.83434737, -0.67205305,  0.17626815, -0.60448911],
 5        [ 0.30011278, -0.98530314, -0.58816207,  2.40943742],
 6        [-0.94236761,  1.12991724, -0.4361433 ,  0.75806253],
 7        [-0.7228912 ,  1.1955933 , -0.75127874, -0.73905711],
 8        [-0.4128283 , -0.15726642,  0.86381129, -1.2467569 ],
 9        [-0.3290692 , -1.03838623,  0.68320058, -0.58237692],
10        [ 1.40461917,  0.55720836,  0.39822819,  0.64182056]])
11 data[data < 0] = 0
12 data
13 Out[46]: 
14 >>array([[0.        , 0.        , 0.17626815, 0.        ],
15        [0.30011278, 0.        , 0.        , 2.40943742],
16        [0.        , 1.12991724, 0.        , 0.75806253],
17        [0.        , 1.1955933 , 0.        , 0.        ],
18        [0.        , 0.        , 0.86381129, 0.        ],
19        [0.        , 0.        , 0.68320058, 0.        ],
20        [1.40461917, 0.55720836, 0.39822819, 0.64182056]])

8、reshape函數,格式 reshape(a, newshape, order='C')

Parameters
----------
a : array_like
Array to be reshaped.
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If
an integer, then the result will be a 1-D array of that length.
One shape dimension can be -1. In this case, the value is
inferred from the length of the array and remaining dimensions.
order : {'C', 'F', 'A'}, optional
1 a = np.arange(6) 2 a 3 Out[49]: 4 array([0, 1, 2, 3, 4, 5]) 5 a.reshape(3,2) 6 Out[50]: 7 array([[0, 1], 8 [2, 3], 9 [4, 5]])

?

9、數組轉置和換軸

  1、轉置是一種特殊的數據重組形式,也可以返回底層數據的視圖而不需要復制任何內容,數組擁有transpose方法,也擁有T屬性。當數組是一維或者二維時,數組名.T的形式直接返回轉置后的數組(原來的數組不會修改),

 1 arr = np.arange(15).reshape((3, 5))2 arr3 Out[54]: 4 array([[ 0,  1,  2,  3,  4],5        [ 5,  6,  7,  8,  9],6        [10, 11, 12, 13, 14]]) 7 arr.T 8 Out[55]: 9 array([[ 0, 5, 10], 10 [ 1, 6, 11], 11 [ 2, 7, 12], 12 [ 3, 8, 13], 13 [ 4, 9, 14]])

  2、對于更高維的數組,轉置使用transpose和swapxes方法。

轉載于:https://www.cnblogs.com/Chris-01/p/11430904.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391485.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391485.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391485.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

我如何預測10場英超聯賽的確切結果

Is there a way to predict the outcome of any soccer game with 100% accuracy? The honest and simplest answer is…. no. Regardless of what your fantasy football friends say, there is absolutely no way to be 100% certain, but there is a proven, mathematical …

多迪技術總監揭秘:PHP為什么是世界上最好的語言?

PHP這么一個腳本語言&#xff0c;雖然他是web開發中&#xff0c;使用者最多的語言&#xff0c;最快最簡單的語言&#xff0c;生態環境和社區積累最深厚的語言&#xff0c;作為最好的編程語言&#xff0c;多迪技術總監為你介紹&#xff1a;PHP為什么是世界上最好的語言&#xff…

aws數據庫同步區別_了解如何通過使用AWS AppSync構建具有實時數據同步的應用程序

aws數據庫同步區別AWS AppSync automatically updates the data in web and mobile applications in real time, and updates data for offline users as soon as they reconnect. AWS AppSync會自動實時更新Web和移動應用程序中的數據&#xff0c;并在離線用戶重新連接后立即為…

leetcode 153. 尋找旋轉排序數組中的最小值(二分查找)

已知一個長度為 n 的數組&#xff0c;預先按照升序排列&#xff0c;經由 1 到 n 次 旋轉 后&#xff0c;得到輸入數組。例如&#xff0c;原數組 nums [0,1,2,4,5,6,7] 在變化后可能得到&#xff1a; 若旋轉 4 次&#xff0c;則可以得到 [4,5,6,7,0,1,2] 若旋轉 4 次&#xff0…

test1

test1 轉載于:https://www.cnblogs.com/Forever77/p/11434403.html

打印風車旋轉效果

1 while True: 2 for i in["/","-","\\","|"]: 3 print "%s\r" %i, 轉載于:https://www.cnblogs.com/feifei-cyj/p/7469333.html

深度學習數據自動編碼器_如何學習數據科學編碼

深度學習數據自動編碼器意見 (Opinion) When I first wanted to learn programming, I coded along to a 4 hour long YouTube tutorial.剛開始學習編程時&#xff0c;我編寫了長達4個小時的YouTube教程。 “Great,” I thought after finishing the course. “I know how to …

Angular 5.0 學習2:Angular 5.0 開發環境的搭建和新建第一個ng5項目

1.安裝Node.js 在開始工作之前&#xff0c;我們必須設置好開發環境。如果你的機器上還沒有Node.js和npm&#xff0c;請先安裝它們。去Node.js的官網&#xff0c;https://nodejs.org/en/&#xff0c;點擊下載按鈕&#xff0c;下載最新版本&#xff0c;直接下一步下一步安裝即可&…

leetcode 154. 尋找旋轉排序數組中的最小值 II(二分查找)

已知一個長度為 n 的數組&#xff0c;預先按照升序排列&#xff0c;經由 1 到 n 次 旋轉 后&#xff0c;得到輸入數組。例如&#xff0c;原數組 nums [0,1,4,4,5,6,7] 在變化后可能得到&#xff1a; 若旋轉 4 次&#xff0c;則可以得到 [4,5,6,7,0,1,4] 若旋轉 7 次&#xff0…

robot:根據條件主動判定用例失敗或者通過

場景&#xff1a; 當用例中的斷言部分需要滿足特定條件時才會執行&#xff0c;如果不滿足條件時&#xff0c;可以主動判定該用例為passed狀態&#xff0c;忽略下面的斷言語句。 如上圖場景&#xff0c;當每月1號時&#xff0c;表中才會生成上月數據&#xff0c;生成后數據不會再…

golang go語言_在7小時內學習快速簡單的Go編程語言(Golang)

golang go語言The Go programming language (also called Golang) was developed by Google to improve programming productivity. It has seen explosive growth in usage in recent years. In this free course from Micheal Van Sickle, you will learn how to use Go step…

使用MUI框架,模擬手機端的下拉刷新,上拉加載操作。

套用mui官方文檔的一句話&#xff1a;“開發者只需關心業務邏輯&#xff0c;實現加載更多數據即可”。真的是不錯的框架。 想更多的了解這個框架&#xff1a;http://dev.dcloud.net.cn/mui/ 那么如何實現下拉刷新&#xff0c;上拉加載的功能呢&#xff1f; 首先需要一個容器&am…

圖深度學習-第1部分

有關深層學習的FAU講義 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as much as the videos. Of cou…

Git上傳項目到github

2019獨角獸企業重金招聘Python工程師標準>>> Git入門 個人理解git就是一個上傳工具&#xff0c;同時兼具和svn一樣的版本控制功能&#xff08;此解釋純屬本人個人觀點&#xff09; Github是什么 github就是一個分布式版本管理系統&#xff08;反正我就是這么認為的…

ionic4 打包ios_學習Ionic 4并開始創建iOS / Android應用

ionic4 打包iosLearn how to use Ionic 4 in this full course for beginners from Awais Mirza. Ionic Framework is the free, open source mobile UI toolkit for developing high-quality cross-platform apps for native iOS, Android, and the web—all from a single Ja…

robot:當用例失敗時執行關鍵字(發送短信)

使用場景&#xff1a; 當用例失敗時需要通知對應人員&#xff0c;則需要在Teardown中&#xff0c;使用關鍵字Run Keyword If Test Failed Send Message關鍵字為自定義關鍵字&#xff0c;${content}為短信內容&#xff0c;${msg_receiver}為短信接收者列表。 當然執行成功時需要…

leetcode 263. 丑數

給你一個整數 n &#xff0c;請你判斷 n 是否為 丑數 。如果是&#xff0c;返回 true &#xff1b;否則&#xff0c;返回 false 。 丑數 就是只包含質因數 2、3 和/或 5 的正整數。 示例 1&#xff1a; 輸入&#xff1a;n 6 輸出&#xff1a;true 解釋&#xff1a;6 2 3 …

NTP同步

RedHat Linux NTP實施步驟1、 查看本系統與NTP服務器的時間偏差 ntpdate -d 192.168.142.114 [rootzabbix-proxy ~]# ntpdate -d 192.168.142.114 24 Aug 17:26:45 ntpdate[3355]: ntpdate 4.2.6p51.2349-o Fri Apr 13 12:52:28 UTC 2018 (1) Looking for host 192.168.142.…

項目經濟規模的估算方法_估算英國退歐的經濟影響

項目經濟規模的估算方法On June 23 2016, the United Kingdom narrowly voted in a country-wide referendum to leave the European Union (EU). Economists at the time warned of economic losses; the Bank of England produced estimates that that GDP could be as much …

Oracle宣布新的Java Champions

\看新聞很累&#xff1f;看技術新聞更累&#xff1f;試試下載InfoQ手機客戶端&#xff0c;每天上下班路上聽新聞&#xff0c;有趣還有料&#xff01;\\\Oracle宣布了2017年新接納的Java Champion的綜述。這次宣布了40位新的成員&#xff0c;包括InfoQ的貢獻者Monica Beckwith。…