python中 numpy_Python中的Numpy

python中 numpy

Python中的Numpy是什么? (What is Numpy in Python?)

Numpy is an array processing package which provides high-performance multidimensional array object and utilities to work with arrays. It is a basic package for scientific computation with python. It is a linear algebra library and is very important for data science with python since almost all of the libraries in the pyData ecosystem rely on Numpy as one of their main building blocks. It is incredibly fast, as it has bindings to C.

Numpy是一個數組處理程序包,它提供高性能的多維數組對象和實用程序來處理數組。 它是使用python進行科學計算的基本軟件包。 它是一個線性代數庫,對于使用python進行數據科學非常重要,因為pyData生態系統中的幾乎所有庫都依賴Numpy作為其主要構建模塊之一。 它非常快,因為它與C具有綁定。

Some of the many features, provided by numpy are as always,

numpy提供的許多功能一如既往,

  1. N-dimensional array object

    N維數組對象

  2. Broadcasting functions

    廣播功能

  3. Utilities for integrating with C / C++

    與C / C ++集成的實用程序

  4. Useful linear algebra and random number capabilities

    有用的線性代數和隨機數功能

安裝Numpy (Installing Numpy)

1) Using pip

1)使用點子

    pip install numpy

Installing output

安裝輸出

pip install numpy
Collecting numpy
Downloading https://files.pythonhosted.org/packages/60/9a/a6b3168f2194fb468dcc4cf54c8344d1f514935006c3347ede198e968cb0/numpy-1.17.4-cp37-cp37m-macosx_10_9_x86_64.whl (15.1MB)
100% |████████████████████████████████| 15.1MB 1.3MB/s 
Installing collected packages: numpy
Successfully installed numpy-1.17.4

2) Using Anaconda

2)使用水蟒

    conda install numpy

numpy中的數組 (Arrays in Numpy)

Numpy's main object is the homogeneous multidimensional array. Numpy arrays are two types: vectors and matrices. vectors are strictly 1-d arrays and matrices are 2-d.

Numpy的主要對象是齊次多維數組。 numpy數組有兩種類型: 向量和矩陣向量嚴格是一維數組, 矩陣是二維。

In Numpy dimensions are known as axes. The number of axes is rank. The below examples lists the most important attributes of a ndarray object.

在Numpy中,尺寸稱為軸。 軸數為等級。 以下示例列出了ndarray對象的最重要屬性。

Example:

例:

# importing package
import numpy as np
# creating array
arr = np.array([[11,12,13],[14,15,16]])
print("Array is of type {}".format(type(arr)))
print("No. of dimensions {}".format(arr.ndim))
print("shape of array:{}".format(arr.shape))
print("size of array:{}".format(arr.size))
print("type of elements in the array:{}".format(arr.dtype))

Output

輸出量

Array is of type <class 'numpy.ndarray'>
No. of dimensions 2
shape of array:(2, 3)
size of array:6
type of elements in the array:int64

創建一個numpy數組 (Creating a numpy array)

Creating a numpy array is possible in multiple ways. For instance, a list or a tuple can be cast to a numpy array using the. array() method (as explained in the above example). The array transforms a sequence of the sequence into 2-d arrays, sequences of sequences into a 3-d array and so on.

創建numpy數組的方式有多種。 例如,可以使用將列表或元組強制轉換為numpy數組。 array()方法 (如以上示例中所述)。 數組將序列的序列轉換為2維數組,將序列的序列轉換為3維數組,依此類推。

To create sequences of numbers, NumPy provides a function called arange that returns arrays instead of lists.

為了創建數字序列,NumPy提供了一個稱為arange的函數,該函數返回數組而不是列表。

Syntax:

句法:

    # returns evenly spaced values within a given interval. 
arange([start,] stop [,step], dtype=None) 

Example:

例:

x = np.arange(10,30,5)
print(x)
# Ouput: [10 15 20 25]

The function zeros create an array full of zeros, the function ones create an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.

函數零將創建一個由零組成的數組,函數一個將創建由零組成的數組,函數空將創建一個數組,其初始內容是隨機的,并且取決于內存的狀態。 默認情況下,創建的數組的dtype為float64。

Example:

例:

# importing package
import numpy as np
x = np.zeros((3,4))
print("np.zeros((3,4))...")
print(x)
x = np.ones((3,4))
print("np.ones((3,4))...")
print(x)
x = np.empty((3,4))
print("np.empty((3,4))...")
print(x)
x = np.empty((1,4))
print("np.empty((1,4))...")
print(x)

Output

輸出量

np.zeros((3,4))...
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
np.ones((3,4))...
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
np.empty((3,4))...
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
np.empty((1,4))...
[[1.63892563e-316 0.00000000e+000 2.11026305e-312 2.56761491e-312]]

numpy函數 (Numpy functions)

Some more function available with NumPy to create an array are,

NumPy提供了一些更多的函數來創建數組,

1) linspace()

1)linspace()

It returns an evenly spaced numbers over a specified interval.

它在指定的間隔內返回均勻間隔的數字。

Syntax:

句法:

    linspace(start, stop, num=50, endpoint=True, restep=False, dtype=None) 

Example:

例:

# importing package
import numpy as np
x = np.linspace(1,3,num=10)
print(x)

Output

輸出量

[1.         1.22222222 1.44444444 1.66666667 1.88888889 2.11111111
2.33333333 2.55555556 2.77777778 3.        ]

2) eye()

2)眼睛()

It returns a 2-D array with ones on the diagonal and zeros elsewhere.

它返回一個二維數組,對角線上有一個,其他位置為零。

Syntax:

句法:

    eye(N, M=None, k=0, dtype=<class 'float'>, order='C')

Example:

例:

# importing package
import numpy as np
x = np.eye(4)
print(x)

Output

輸出量

[[1. 0. 0. 0.] [0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]

3) random()

3)random()

It creates an array with random numbers

它創建一個帶有隨機數的數組

Example:

例:

# importing package
import numpy as np
x = np.random.rand(5)
print("np.random.rand(5)...")
print(x)
x = np.random.rand(5,1)
print("np.random.rand(5,1)...")
print(x)
x = np.random.rand(5,1,3)
print("np.random.rand(5,1,3)...")
print(x)
# returns a random number
x = np.random.randn() 
print("np.random.randn()...")
print(x)
# returns a 2-D array with random numbers
x = np.random.randn(2,3) 
print("np.random.randn(2,3)...")
print(x)
x = np.random.randint(3)
print("np.random.randint(3)...")
print(x)
# returns a random number in between low and high
x = np.random.randint(3,100) 
print("np.random.randint(3,100)...")
print(x)
# returns an array of random numbers of length 34
x = np.random.randint(3,100,34)
print("np.random.randint(3,100,34)...")
print(x)

Output

輸出量

np.random.rand(5)...[0.87417146 0.77399086 0.40012698 0.37192848 0.98260636]
np.random.rand(5,1)...
[[0.712829  ]
[0.65959462]
[0.41553044]
[0.30583293]
[0.83997539]]
np.random.rand(5,1,3)...
[[[0.75920149 0.54824968 0.0547891 ]]
[[0.70911911 0.16475541 0.5350475 ]]
[[0.74052103 0.4782701  0.2682752 ]]
[[0.76906319 0.02881364 0.83366651]]
[[0.79607073 0.91568043 0.7238144 ]]]
np.random.randn()...
-0.6793254693909823
np.random.randn(2,3)...
[[ 0.66683143  0.44936287 -0.41531392]
[ 1.86320357  0.76638331 -1.92146833]]
np.random.randint(3)...
1
np.random.randint(3,100)...
53
np.random.randint(3,100,34)...
[43 92 76 39 78 83 89 87 96 59 32 74 31 77 56 53 18 45 78 21 46 10 25 86
64 29 49  4 18 19 90 17 62 29]

4) Reshape method (shape manipulation)

4)整形方法(形狀處理)

An array has a shape given by the number of elements along each axis,

數組的形狀由沿每個軸的元素數確定,

# importing package
import numpy as np
x = np.floor(10*np.random.random((3,4)))
print(x)
print(x.shape)

Output

輸出量

[[0. 2. 9. 4.] 
[0. 4. 1. 7.]
[9. 7. 6. 2.]]
(3, 4)

The shape of an array can be changes with various commands. However, the shape commands return all modified arrays but do not change the original array.

數組的形狀可以通過各種命令進行更改。 但是,shape命令返回所有修改后的數組,但不更改原始數組。

# importing package
import numpy as np
x = np.floor(10*np.random.random((3,4)))
print(x)
# returns the array, flattened
print("x.ravel()...")
print(x.ravel()) 
# returns the array with modified shape
print("x.reshape(6,2)...")
print(x.reshape(6,2)) 
# returns the array , transposed
print("x.T...")
print(x.T) 
print("x.T.shape...")
print(x.T.shape)
print("x.shape...")
print(x.shape)

Output

輸出量

[[3. 1. 0. 6.] [3. 1. 2. 4.]
[7. 0. 0. 1.]]
x.ravel()...
[3. 1. 0. 6. 3. 1. 2. 4. 7. 0. 0. 1.]
x.reshape(6,2)...
[[3. 1.]
[0. 6.]
[3. 1.]
[2. 4.]
[7. 0.]
[0. 1.]]
x.T...
[[3. 3. 7.] [1. 1. 0.]
[0. 2. 0.]
[6. 4. 1.]]
x.T.shape...
(4, 3)
x.shape...
(3, 4)

其他方法 (Additional methods)

# importing package
import numpy as np
x = np.floor(10*np.random.random((3,4)))
print(x)
#Return the maximum value in an array
print("x.max():", x.max())
# Return the minimum value in a array
print("x.min():", x.min())
# Return the index of max value in an array
print("x.argmax():", x.argmax())
# Return the index of min value in an array
print("x.argmin():", x.argmin())

Output

輸出量

[[4. 0. 5. 2.] [8. 5. 9. 7.]
[9. 3. 5. 5.]]
x.max(): 9.0
x.min(): 0.0
x.argmax(): 6
x.argmin(): 1

翻譯自: https://www.includehelp.com/python/numpy.aspx

python中 numpy

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

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

相關文章

[轉載] python之路《第二篇》Python基本數據類型

參考鏈接&#xff1a; Python中的Inplace運算符| 1(iadd()&#xff0c;isub()&#xff0c;iconcat()…) 運算符 1、算數運算&#xff1a; 2、比較運算&#xff1a; 3、賦值運算&#xff1a; 4、邏輯運算&#xff1a; 5、成員運算&#xff1a; 6、三元運算 三元運算&…

數據結構 基礎知識

一。邏輯結構: 是指數據對象中數據 素之間的相互關系。 其實這也是我 今后最需要關注的問題 邏輯結構分為以 四種1. 集合結構 2.線性結構 3.數形結構 4&#xff0c;圖形結構 二。物理結構&#xff1a; 1&#xff0c;順序存儲結,2 2. 鏈式存儲結構 一&#xff0c;時間復雜…

ruby 變量類中范圍_Ruby中的類

ruby 變量類中范圍Ruby類 (Ruby Classes) In the actual world, we have many objects which belong to the same category. For instance, I am working on my laptop and this laptop is one of those laptops which exist around the globe. So, this laptop is an object o…

以云計算的名義 駐云科技牽手阿里云

本文講的是以云計算的名義 駐云科技牽手阿里云一次三個公司的牽手 可能會改變無數企業的命運 2017年4月17日&#xff0c;對于很多人來說可能只是個平常的工作日&#xff0c;但是對于國內無數的企業來說卻可能是個會改變企業命運的日。駐云科技聯合國內云服務提供商阿里云及國外…

[轉載] python學習筆記

參考鏈接&#xff1a; Python | a b并不總是a a b 官網http://www.python.org/ 官網library http://docs.python.org/library/ PyPI https://pypi.python.org/pypi 中文手冊&#xff0c;適合快速入門 http://download.csdn.net/detail/xiarendeniao/4236870 py…

標志寄存器_訪問標志寄存器,并與寄存器B |交換標志寄存器F的內容 8085微處理器...

標志寄存器Problem statement: 問題陳述&#xff1a; Write an assembly language program in 8085 microprocessor to access Flag register and exchange the content of flag register F with register B. 在8085微處理器中編寫匯編語言程序以訪問標志寄存器&#xff0c;并…

瀏覽器端已支持 ES6 規范(包括 export import)

當然&#xff0c;是幾個比較優秀的瀏覽器&#xff0c;既然是優秀的瀏覽器&#xff0c;大家肯定知道是那幾款啦&#xff0c;我就不列舉了&#xff0c;我用的是 chrome。 對 script 聲明 type 為 module 后就可以享受 es6 規范所帶來的模塊快感了。 基礎語法既然是全支持&#xf…

[轉載] Python學習:Python成員運算符和身份運算符

參考鏈接&#xff1a; Python中和is運算符之間的區別 Python成員運算符 除了以上的一些運算符之外&#xff0c;Python還支持成員運算符&#xff0c;測試實例中包含了一系列的成員&#xff0c;包括字符串&#xff0c;列表或元組。 運算符 描述 實例 in 如果在指定的序列中找…

量詞邏輯量詞里面的v表示?_代理知識表示中的量詞簡介(基于人工智能)

量詞邏輯量詞里面的v表示&#xff1f;As we know that in an AI-based agent, the knowledge is represented through two types of logic: The propositional logic and the predicate logic. In the propositional logic, we have declarative sentences, and in the predica…

[轉載] Python 機器學習經典實例

參考鏈接&#xff1a; Python中的邏輯門 內容介紹 在如今這個處處以數據驅動的世界中&#xff0c;機器學習正變得越來越大眾化。它已經被廣泛地應用于不同領域&#xff0c;如搜索引擎、機器人、無人駕駛汽車等。本書首先通過實用的案例介紹機器學習的基礎知識&#xff0c;然后…

哈希表的最差復雜度是n2_給定數組A []和數字X,請檢查A []中是否有對X | 使用哈希O(n)時間復雜度| 套裝1...

哈希表的最差復雜度是n2Prerequisite: 先決條件&#xff1a; Hashing data structure 散列數據結構 Problem statement: 問題陳述&#xff1a; Given an array and a sum X, fins any pair which sums to X. Expected time complexity O(n). 給定一個數組和一個和X &#xff…

一文讀懂深度學習框架下的目標檢測(附數據集)

從簡單的圖像分類到3D位置估算&#xff0c;在機器視覺領域里從來都不乏有趣的問題。其中我們最感興趣的問題之一就是目標檢測。 如同其他的機器視覺問題一樣&#xff0c;目標檢測目前為止還沒有公認最好的解決方法。在了解目標檢測之前&#xff0c;讓我們先快速地了解一下這個領…

[轉載] Python-Strings

參考鏈接&#xff1a; Python成員資格和身份運算符 &#xff5c; in, not in, is, is not Strings 介紹 String是Python中最常用的類型。僅僅用引號括起字符就可以創建string變量。字符串使用單引號或雙引號對Python來說是一樣的。 var1 Hello World! var2 "Pyth…

aes-128算法加密_加密算法問題-人工智能中的一種約束滿意問題

aes-128算法加密The Crypt-Arithmetic problem in Artificial Intelligence is a type of encryption problem in which the written message in an alphabetical form which is easily readable and understandable is converted into a numeric form which is neither easily…

讀書筆記《集體智慧編程》Chapter 2 : Make Recommendations

本章概要本章主要介紹了兩種協同過濾&#xff08;Collaborative Filtering&#xff09;算法&#xff0c;用于個性化推薦&#xff1a;基于用戶的協同過濾&#xff08;User-Based Collaborative Filtering&#xff0c;又稱 K-Nearest Neighbor Collaborative Filtering&#xff0…

[轉載] python中的for循環對象和循環退出

參考鏈接&#xff1a; Python中循環 流程控制-if條件 判斷條件&#xff0c;1位true&#xff0c;0是flesh&#xff0c;成立時true&#xff0c;不成立flesh&#xff0c;not取反 if 1; print hello python print true not取反&#xff0c;匹配取反&#xff0c;表示取非1…

設計一個應用程序,以在C#中的按鈕單擊事件上在MessageBox中顯示TextBox中的文本...

Here, we took two controls on windows form that are TextBox and Button, named txtInput and btnShow respectively. We have to write C# code to display TextBox’s text in the MessageBox on Button Click. 在這里&#xff0c;我們在Windows窗體上使用了兩個控件&…

Oracle優化器:星型轉換(Star Query Transformation )

Oracle優化器&#xff1a;星型轉換&#xff08;Star Query Transformation &#xff09;Star query是一個事實表&#xff08;fact table&#xff09;和一些維度表&#xff08;dimension&#xff09;的join。每個維度表都跟事實表通過主外鍵join&#xff0c;且每個維度表之間不j…

[轉載] python循環中break、continue 、exit() 、pass的區別

參考鏈接&#xff1a; Python中的循環和控制語句(continue, break and pass) 1、break&#xff1a;跳出循環&#xff0c;不再執行 用在while和for循環中 用來終止循環語句&#xff0c;即循環條件沒有False條件或者序列還沒被完全遞歸完&#xff0c;也會停止執行循環語句 如果…

JavaScript | 聲明數組并使用數組索引分配元素的代碼

Declare an array, assign elements by indexes and print all elements in JavaScript. 聲明一個數組&#xff0c;通過索引分配元素&#xff0c;并打印JavaScript中的所有元素。 Code: 碼&#xff1a; <html><head><script>var fruits [];fruits[0]"…