python小白之數組索引

來源:互聯網
上載者:User

標籤:維數   least   產生   不同的   fun   function   學習   nat   位元組   

索引

numpy中的數組索引形式和Python是一致的。如:

np.arange(10)

print x[2]  #單個元素,從前往後正向索引。注意下標是從0開始的。

print x[-2]  #從後往前索引。最後一個元素的下標是-1

print x[2:5]  #多個元素,左閉右開,預設步長值是1

print x[:-7]  #多個元素,從後向前,制定了結束的位置,使用預設步長值

print x[1:7:2]  #指定步長值

x.shape=(2,5)  #x的shape屬性被重新賦值,要求就是元素個數不變。2*5=10

print x[1,3]  #二維數組索引單個元素,第2行第4列的那個元素

print x[0]  #第一行所有的元素

y=np.arange(35).reshape(5,7)  #reshape()函數用於改變數組的維度

print y[1:5:2,::2]  #選擇二維數組中的某些合格元素

 

 


#python學習之數組 2018.4.17# -*- coding: UTF-8 -*-from numpy import *import mathA =arange(15).reshape(3,5)#reshape參數開頭可以再加一個數字,表示reshape後產生的個數
print(A.sum(axis=0))#A列合計
print(A.min(axis=1))#A行最小值 
print(A.cumsum(axis=1))#A中每行的累計和print(A.shape)#A的形狀 幾行幾列print(A.ndim)#A的秩 即數組軸的個數print(A.dtype.name)#查看array裡面資料類型print(A.itemsize)#數組中每個元素的位元組大小int32/8=4print(A.size)#資料元素個數print(type(A))#A的類型為numpy.ndarray
# C=array([(1.5,2,3),(4,5,6)])#可以使用但是1.5,2,3等數字無法改變了C,所以不適用# C=array([1.5,2,3],[4,5,6])#錯誤C=array([[1.5,2,3],[4,5,6]],dtype=complex)#正確,並指定數群組類型print (C)
# print (zeros((3,4)))#一個三行四列都是0的數組# print (ones((2,3,4),dtype=int16))#兩個三行四列都是1的數組print (empty((3,4)))#內容隨機依賴記憶體狀態的數組,預設都是float64
# set_printoptions(threshold=‘nan‘)#表示強制列印整個數組,中間部分不會省略D=array([1,2,3,4],dtype=int)print(D**2)print(D<2)
E=array([[1,1],[0,1]])F=array([[2,0],[3,4]])print(E*F)#矩陣對應位置相乘print(dot(E,F))#矩陣乘法print(linspace(0,pi,3,endpoint=False,retstep=True))#從0到pi之間均分,產生3個數,不包括末尾,返回數及間隔大小
#一維數組可以被索引、切片迭代G=arange(10)**3print(G[2:5])#輸出2的3次方、3的3次方、4的三次方G[:6:2]=-5#相當於G[0:6:2],是指從第0到6個數,變為-5,步長為2,即第一個=-5,第三個等於-5print(G[::-1])#倒序輸出
def f(x,y):    return 10*x+yH=fromfunction(f,(5,4))#多維陣列每個軸可以有索引
print(H[0:4,1])#第一行到第四行的第二列的數值輸出print(H[:,1])#第二列全部的數值輸出print(H[1:3,])#第二行到第三行的數值輸出print(H[-1])#相當於H[-1,:]#點(…)代表許多產生一個完整的索引元組必要的分號。如果x是秩為5的數組(即它有5個軸),那麼:# x[1,2,…] 等同於 x[1,2,:,:,:],#x[…,3] 等同於 x[:,:,:,:,3]#x[4,…,5,:] 等同 x[4,:,:,5,:].# for element in H.flat:#flag是數組的一個屬性,可以用來對數組中元素運算 # print (element)
#向下取整floor()print(H.ravel())#將數組全展平

 

#複製與視圖#1.完全不拷貝 簡單的賦值不拷貝數組對象或它們的資料。a=arange(12)b=aprint(b is a)
b.shape = 3,4#2.視圖和淺複製 不同的數組對象分享同一個資料 c的形狀不會變化但數值會c=a.view()print(c is a)
print(c.base is a) 
c.shape = 2,6print(a.shape)c[0,4]=12345print(a)
#3.深複製 完全複製數組和它的資料 即兩個數組完全獨立開d=a.copy()print(d is a)print(d.base is a)d[0,0]=-1print(a)
#-----------------------------------------------------------------#numpy函數方法歸類#1.建立數組 arange array copy empty empty_like eye fromfile fromfunction identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r , zeros, zeros_like#2.轉化astype, atleast 1d, atleast 2d, atleast 3d, mat#3.操作array split, column stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack#4.詢問all, any, nonzero, where#5.排序argmax, argmin, argsort, max, min, ptp, searchsorted, sort#6.運算choose, compress, cumprod, cumsum, inner, fill, imag, prod, put, putmask, real, sum#7.基本統計cov, mean, std, var#8.基本線性代數cross, dot, outer, svd, vdot#---------------------------------------------------------------#numpyj進階#花哨的索引NumPy比普通Python序列提供更多的索引功能。數組可以被整數數組和布爾數組索引。k=arange(12)**2i=array([1,1,3,8,5])print(k[i])
#線性代數#a=array([[2,3],[3,4]])#inv(a)求逆 trace(a)求跡 a.transpose()轉置# solve(A,b) 求解AX=b# eigvals 求解特徵值# eig(C) 返回的c1是特徵值 c2是特徵向量# U,sigma,V=np.linalg.svd(D,full_matric=false) svd奇異值分解# pinv 廣義逆# det行列式#矩陣類#Amatrix(‘1.0 2.0;3.0 4.0‘)#A.TL = arange(12)L.shape=3,4print(L[:,[1,3]])
print(L[:,L[0,:]>1])#保留第一行大於1的列
print(L[L[:,0]>2,L[0,:]>1])#在矩陣兩個方向有條件的切片
#技巧L.shape=2,-1,3 #-1表示省略一個尺寸 將會自動推導 12個數 不知道要輸出幾行,但要輸出3列 同時要輸出兩個print(L)

 

 

 

 

建立數組

 tip: 可以採用的形式from numpy import *+array~

 import numpy as np+np.array

#常見錯誤 錯誤要點:array應該提供一個由數值組成的列表作為參數而不是用多個數值參數調用

# B=[[6,7],[8]] #這是list,而我們需要使用array
# B=array(6,7,8)  #錯# B=array[6,7,8] # 錯# B=array[[6,7,8]] #錯
B=array([6,7,8]) #對

python小白之數組索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.