日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 模块学习--Numpy

發(fā)布時間:2023/12/10 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 模块学习--Numpy 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Numpy是Python的一個科學(xué)計算庫,提供了矩陣運算的功能。安裝方法可以直接使用pip install numpy命令,也可以在http://sourceforge.net/projects/numpy/files/NumPy/上下載與python相應(yīng)版本的exe文件。

這里就記錄下在學(xué)習(xí)和使用Numpy中所用過的一些函數(shù)方法,隨時進行補充。
numpy的官方文檔:http://docs.scipy.org/doc/numpy/reference/

1.導(dǎo)入Numpy

導(dǎo)入方法如下:

2.多維數(shù)組

使用numpy.arange


這是生成一維數(shù)組,如果是要生成多維數(shù)組,則再使用reshape函數(shù),如下圖:

這里就生成了一個5*2的數(shù)組

使用numpy.array

可以使用list列表或者tuple元組作為參數(shù)生成一維或多維數(shù)組

另外,還可以指定數(shù)據(jù)類型,如numpy.int32,numpy.int16,numpy.float64等等。

使用numpy.zeros,numpy.ones,numpy.eye函數(shù)可以生成特定的矩陣

使用mat函數(shù)可以將數(shù)組轉(zhuǎn)化為矩陣

數(shù)組的屬性

如上圖所示,
ndim:表示數(shù)組的維度
shape:表示數(shù)組的行數(shù)和列數(shù)
size:表示數(shù)組的元素個數(shù)
dtype:表示數(shù)組的數(shù)據(jù)類型
itemsize:表示每個元素所占的字節(jié)數(shù)

tile函數(shù)可以用于擴充數(shù)組元素
tile(A,repl)可以用于擴充數(shù)組的元素,A表示需要擴充的矩陣或數(shù)組,而repl參數(shù)如果是一個數(shù),表示對A中元素的重復(fù)次數(shù),如果repl = (x,y),則y表示對A中元素重復(fù)的次數(shù),而x是對進行y次重復(fù)得到的數(shù)組所要擴充的次數(shù),下圖是官方的例子

3.numpy的argsort函數(shù)用法

通過help函數(shù)獲取argsort函數(shù)的介紹,從介紹可以得知其返回的是數(shù)組值從小到大排序的索引值

這里也參考[numpy中argsort函數(shù)用法]這篇文章的介紹。
(http://www.aichengxu.com/view/15541)
以下分別是一維數(shù)組和二維數(shù)組的使用例子:

另外,對于二維數(shù)組還可以設(shè)置axis參數(shù),當(dāng)axis= 0 表示按列排序,而axis=1表示按行排序。

另外說到排序,還有兩個常用的函數(shù)sort和sorted,詳細(xì)內(nèi)容請看:http://maoersong.blog.163.com/blog/static/171557351201424105925681/?newFollowBlog

4.對多維數(shù)組的索引

對多維數(shù)組的索引有以下幾種方法:

(1)切割索引

import numpy as np# Slicing indexing # Create the following rank 2 array with shape (3,4) # [[1 2 3 4] # [5 6 7 8] # [9 10 11 12]] a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])# Use slicing to pull out the subarray consisting of the first 2 rows # and columns 1 and 2; b is the following array of shape (2,2): # [[2 3] # [6 7]] b = a[:2, 1:3]# A slice of an array is a view into the same data, so modifying it # will modify the original array print a[0,1] # Prints "2" b[0, 0] = 55 # b[0, 0] is the same piece of data as a[0, 1] print a[0, 1] # Prints "55"

(2)整數(shù)索引

# Integer array indexing # The returned array will have shape(3,) print a[[0, 1 , 2], [2, 3, 2]] # Prints "[ 3 8 11]"# The above example of integer array indexing is equivalent to this: print np.array([a[0, 2], a[1, 3], a[2, 2]]) # Prints "[ 3 8 11]"# When using integer array indexing, you can reuse the same # element from the source array: print a[[0, 0], [1, 1]] # Prints "[2 2]"# Equivalent to the previous integer array indexing example print np.array([a[0, 1], a[0,1]]) # Prints "[2 2]"

(3)布爾值索引

# Boolean array indexing bool_idx = (a > 3) # Find the elements of a that are bigger than 3;# this returns a numpy array of Boolean of the same# shape as a, where each slot of bool_idx tells# whether that element of a is > 2 print bool_idx # Prints "[[False False False True]"# [ True True True True]# [ True True True True]]# We use boolean array indexing to construct a rank 1 array # consisiting of the elements of a corresponding to the True values # of bool_idx print a[bool_idx] # Prints "[ 4 5 6 7 8 9 10 11 12]"# We can do all of the above in a single concise statement: print a[a > 3] # Prints "[ 4 5 6 7 8 9 10 11 12]"

總結(jié)

以上是生活随笔為你收集整理的python 模块学习--Numpy的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。