Python numpy,pythonnumpy

來源:互聯網
上載者:User

Python numpy,pythonnumpy

import numpya=numpy.array([1,2,3,4])b=numpy.array([[1,2,3],[4,5,6],[7,8,9]])print(a.shape)print(b.shape)

建立了一個一維向量和三行散列的矩陣

注意:這裡要求資料是同一結構,shape函數作用:幾行幾列

 

取值:

import numpyb=numpy.array([[1,2,3],[4,5,6],[7,8,9]])print(b[:,1])#這裡列印矩陣的第二列print(b[:,0:2])#這裡取到第一列和第二列

 

修改矩陣中的值:

這裡把5和7的值改成了10

import numpyb=numpy.array([[1,2,3],[4,5,6],[7,8,9]])b[(b==5)|(b==7)] = 10print(b)

 

強轉類型:

把int型轉為str型

import numpyb=numpy.array([[1,2,3],[4,5,6],[7,8,9]])c = b.astype(str)print(c)

 

其他動作:

import numpyb=numpy.array([[1,2,3],[4,5,6],[7,8,9]])print(b.min())#求最小值print(b.max(axis=1))#按行求最大print(b.sum(axis=0))#按列求和

 

import numpy as npa=np.arange(10).reshape(2,5)print(a)'''建立矩陣:[[0 1 2 3 4] [5 6 7 8 9]]'''print(a.ndim)#求維度print(a.shape)#幾行幾列print(a.dtype.name)#矩陣資料類型名字print(a.size)#元素個數

 

矩陣初始化:

import numpy as np#矩陣初始化方法:np.zeros((3,4))#3行4列矩陣初始化為0(預設為float類型)np.ones((3,4),dtype=np.int32)#3行4列初始化值為1的int類型

 

建立矩陣:

import numpy as npnp.arange(10,30,5)#從10到30,每隔5#array([10, 15, 20, 25])np.random.random((2,3))'''隨機建立:2行3列,-1到1之間注意:必須是兩個randomarray([[0.20925672, 0.09790786, 0.00158854],       [0.73711854, 0.83033327, 0.22525092]])'''np.linspace(1,3,100)#從1到3平均地取100個數(float類型)

 

 

運算:

import numpy as npa=np.array([[1,2,3],[4,5,6],[7,8,9]])print(np.hstack((a,a)))print(np.vstack((a,a)))print(a.T)print(a+a)print(a*a)print(a.dot(a))print(np.dot(a,a))print(np.exp(a))print(np.sqrt(a))print(a.shape)print(a.ravel())'''不做解釋,一目瞭然[[1 2 3 1 2 3] [4 5 6 4 5 6] [7 8 9 7 8 9]][[1 2 3] [4 5 6] [7 8 9] [1 2 3] [4 5 6] [7 8 9]][[1 4 7] [2 5 8] [3 6 9]] [[ 2  4  6] [ 8 10 12] [14 16 18]] [[ 1  4  9] [16 25 36] [49 64 81]] [[ 30  36  42] [ 66  81  96] [102 126 150]] [[ 30  36  42] [ 66  81  96] [102 126 150]]  [[2.71828183e+00 7.38905610e+00 2.00855369e+01] [5.45981500e+01 1.48413159e+02 4.03428793e+02] [1.09663316e+03 2.98095799e+03 8.10308393e+03]] [[1.         1.41421356 1.73205081] [2.         2.23606798 2.44948974] [2.64575131 2.82842712 3.        ]]  (3, 3)  [1 2 3 4 5 6 7 8 9]'''

 

import numpy as npa=np.array([[1,2,3],[4,5,6],[7,8,9]])print(a.argmax(axis=0))#[2 2 2]列最大索引值print(a.argmin(axis=1))#[0 0 0]行最小索引值

 

import numpy as npa=np.arange(0,40,10)print(a)b=np.tile(a,(3,2))c=np.tile(a,(2,3))print(b)print(c)'''[ 0 10 20 30][[ 0 10 20 30  0 10 20 30] [ 0 10 20 30  0 10 20 30] [ 0 10 20 30  0 10 20 30]]  [[ 0 10 20 30  0 10 20 30  0 10 20 30] [ 0 10 20 30  0 10 20 30  0 10 20 30]]'''

 

排序:

import numpy as npa=np.array([[1,4,6],[2,9,7],[5,3,8]])print(a)'''[[1 4 6] [2 9 7] [5 3 8]]'''b=np.sort(a,axis=1)#按行排列print(b)'''[[1 4 6] [2 7 9] [3 5 8]]'''c=np.sort(a,axis=0)#按列排列print(c)'''[[1 3 6] [2 4 7] [5 9 8]]'''d=np.argsort(a)#索引值排序print(d)'''[[0 1 2] [0 2 1] [1 0 2]]'''

 

 

特別注意:

import numpy as npa=np.array([[1,2,3],[4,5,6],[7,8,9]])c=a.view()print(c is a)#false(c和a指向記憶體位址不同)#複製了a,賦值給c#如果是c=a,那麼c和a是同一個(指向同一個地址)#print(c is a)地話,就會列印truec[1,2] = 100print(a)'''[[  1   2   3] [  4   5 100] [  7   8   9]]'''#這裡發現修改了c,那麼a也被修改了#c和a雖然地址不同,但是共用一組資料d=a.copy()print(d is a)#falsed[1,3] = 100#這裡沒有改變aprint(a)

 

 

讀取txt檔案:

import numpy#第一個參數為路徑,第二個參數為分隔字元,第三個參數是讀取的類型#最後一個參數意思:是否去掉第一行a=numpy.genfromtxt("d:/a.txt",delimiter=",",dtype="str",skip_header=1)print(a)

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.