A. python連接資料庫後進行操作時出現錯誤
conn = pymssql.connect(host=r"localhost",user=r"sa",password=r"pwd",database=r"proction")
stack overflow 上有個類似的問題 有個答案是這樣的 你試試 沒用過mssql
B. python pandas 怎樣高效地添加一行數據
That's probably as efficient as any, but Pandas/numpy
structures are fundamentally not suited for efficiently growing. They
work best when they are created with a fixed size and stay that way. – BrenBarnDec 6 '12 at 20:43
append
is a wrapper for concat, so concat would be marginally more efficient,
but as @BrenBarn says Pandas is probably not appropriate for updating a
HDF5 file every second. If you absolutely need Pandas for some reason, could you collect a list of Series and update the file periodically instead? – Matti JohnDec 6 '12 at 20:54
Bren is right about numpy/pandas working best when preallocated. If memory is no constraint just preallocate a huge zeros array and append at the end of the program removing any excess zeros. Which I suppose is a bit of what Matti is saying. – arynaqDec 6 '12 at 21:16Intro to Data Structures
DataFrame is a 2-dimensional labeled data structure with columns of potentially different types.
所
以一般說來dataframe就是a set of columns, each column is an array of values. In
pandas, the array is one way or another a (maybe variant of) numpy
ndarray. 而ndarray本身不存在一種in place
append的操作。。。因為它實際上是一段連續內存。。。任何需要改變ndarray長度的操作都涉及分配一段長度合適的新的內存,然後。。。
這是這類操作慢的原因。。。如果pandas dataframe沒有用其他設計減少的話,我相信Bren說的"That's probably
as efficient as any"是很對的。。。
所以in general, 正如Bren說的。。。Pandas/numpy structures are fundamentally not suited for efficiently growing.
Matti 和 arynaq說的是兩種常見的對付這個問題的方法。。。我想Matti實際的意思是把要加的rows收集成起來然後concatenate, 這樣只一次。arynaq的方法就是預先分配內存比較好理解。。。
如果你真的需要incrementally build a dataframe的話,估計你需要實際測試一下兩種方法。。。
我的建議是,如有可能,盡力避免incrementally build a dataframe, 比如用其他data structure 收集齊所有data然後轉變成dataframe做分析。。。
順便。。。這類問題上stackoverflow好得多。。
C. python valueError:too many values unpack,有人知道么
問題不出在getOSDParam函數裡面。return 0,0,0,0,0,0,-2的返回值長度是7,而你代碼中的displayTime,colorOfOSD,lastFrame,extraOSD,displayOSDName,resOSD = getOSDParam()前面明明只有6個變數。
D. 如何使用python來對二維數組進行復合排序
1
2
3
4
5
6
7
8
9
10
11
12
# 例子
import numpy as np
data = np.array([[1,2,3,4,5],
[1,2,3,6,7],
[2,3,4,5,7],
[3,4,5,6,7],
[4,5,6,7,8]])
sorted_cols = []
for col_no in range(data.shape[1]):
sorted_cols.append(data[np.argsort(data[:,col_no])][:,col_no])
sorted_data = np.column_stack(sorted_cols)
E. 怎麼從零開始學習 Python 語言編程
題主你好,前段時間我也自學了一次Python。我比較笨,上手大概用了一天,基本的熟練大概用了一個星期。這里給你推薦一個比較好的簡單教程:
簡明Python教程:http://sebug.net/paper/python/pr01.html#s01
會基本的操作之後,建議你給自己找個事情做,稍微復雜一點的。因為邊用邊學才最快,別怕。
比如寫個爬蟲程序。(因為爬蟲是Python比較常見的應用)
CSDN專欄Python爬蟲入門:
http://blog.csdn.net/column/details/why-bug.html
一個我自己用到的爬蟲心得:
http://yxmhero1989.blog.163.com/blog/static/112157956201311821444664/
如果不想寫爬蟲,可以寫個小軟體,然後自己寫個界面。這一點Python也是挺方便的。寫界面的話,建議使用wxPython。因為這個有一個圖形化的界面設計軟體wxFormBuilder,可以直接給你寫出框架的代碼。
wxFormbuilder教程:
http://www.cppblog.com/xkjy3000/archive/2012/10/31/194120.html
差不多就這些吧。哦對了,給你一個wxPython的文檔連接,很多命令的詳細用法可以查詢。
http://www.wxpython.org/docs/api/wx.StaticBitmap-class.html#GetClassDefaultAttributes
還有著名的StackOverFlow,如果你英文夠好的話。可以在上面查詢到各種各樣的bug/解決不了的問題,我們沿著前人淌出來的路就行了;-)
http://stackoverflow.com/?tab=featured
祝你碼得愉快:)
F. Python基礎 numpy中的常見函數有哪些
有些Python小白對numpy中的常見函數不太了解,今天小編就整理出來分享給大家。
Numpy是Python的一個科學計算的庫,提供了矩陣運算的功能,其一般與Scipy、matplotlib一起使用。其實,list已經提供了類似於矩陣的表示形式,不過numpy為我們提供了更多的函數。
數組常用函數
1.where()按條件返回數組的索引值
2.take(a,index)從數組a中按照索引index取值
3.linspace(a,b,N)返回一個在(a,b)范圍內均勻分布的數組,元素個數為N個
4.a.fill()將數組的所有元素以指定的值填充
5.diff(a)返回數組a相鄰元素的差值構成的數組
6.sign(a)返回數組a的每個元素的正負符號
7.piecewise(a,[condlist],[funclist])數組a根據布爾型條件condlist返回對應元素結果
8.a.argmax(),a.argmin()返回a最大、最小元素的索引
改變數組維度
a.ravel(),a.flatten():將數組a展平成一維數組
a.shape=(m,n),a.reshape(m,n):將數組a轉換成m*n維數組
a.transpose,a.T轉置數組a
數組組合
1.hstack((a,b)),concatenate((a,b),axis=1)將數組a,b沿水平方向組合
2.vstack((a,b)),concatenate((a,b),axis=0)將數組a,b沿豎直方向組合
3.row_stack((a,b))將數組a,b按行方向組合
4.column_stack((a,b))將數組a,b按列方向組合
數組分割
1.split(a,n,axis=0),vsplit(a,n)將數組a沿垂直方向分割成n個數組
2.split(a,n,axis=1),hsplit(a,n)將數組a沿水平方向分割成n個數組
數組修剪和壓縮
1.a.clip(m,n)設置數組a的范圍為(m,n),數組中大於n的元素設定為n,小於m的元素設定為m
2.a.compress()返回根據給定條件篩選後的數組
數組屬性
1.a.dtype數組a的數據類型
2.a.shape數組a的維度
3.a.ndim數組a的維數
4.a.size數組a所含元素的總個數
5.a.itemsize數組a的元素在內存中所佔的位元組數
6.a.nbytes整個數組a所佔的內存空間7.a.astype(int)轉換a數組的類型為int型
數組計算
1.average(a,weights=v)對數組a以權重v進行加權平均
2.mean(a),max(a),min(a),middle(a),var(a),std(a)數組a的均值、最大值、最小值、中位數、方差、標准差
3.a.prod()數組a的所有元素的乘積
4.a.cumprod()數組a的元素的累積乘積
5.cov(a,b),corrcoef(a,b)數組a和b的協方差、相關系數
6.a.diagonal()查看矩陣a對角線上的元素7.a.trace()計算矩陣a的跡,即對角線元素之和
以上就是numpy中的常見函數。更多Python學習推薦:PyThon學習網教學中心。
G. 如何用 Python 寫一個帶 GUI 的科學計算程序
這是個代碼, 使用Tkinter圖形庫,如果你是用的linux系統 記得將第一行改為from tkinter import *
這個代碼實現的挺簡單,並不是很復雜的科學計算器界面,你可以以此為基礎,添加自己想要的東西:給你個截圖:
#!/usr/bin/envpython3.4
fromTkinterimport*
importparser
root=Tk()
root.title('Calculator')
i=0
deffactorial():
"""."""
whole_string=display.get()
number=int(whole_string)
fact=1
counter=number
try:
whilecounter>0:
fact=fact*counter
counter-=1
clear_all()
display.insert(0,fact)
exceptException:
clear_all()
display.insert(0,"Error")
defclear_all():
""""""
display.delete(0,END)
defget_variables(num):
""""""
globali
display.insert(i,num)
i+=1
defget_operation(operator):
""""""
globali
length=len(operator)
display.insert(i,operator)
i+=length
defundo():
"""removesthelastenteredoperator/variablefromentrywidget"""
whole_string=display.get()
iflen(whole_string):##repeatsuntil
##
new_string=whole_string[:-1]
print(new_string)
clear_all()
display.insert(0,new_string)
else:
clear_all()
display.insert(0,"Error,pressAC")
defcalculate():
"""
Evaluatestheexpression
ref:http://stackoverflow.com/questions/594266/equation-parsing-in-python
"""
whole_string=display.get()
try:
formulae=parser.expr(whole_string).compile()
result=eval(formulae)
clear_all()
display.insert(0,result)
exceptException:
clear_all()
display.insert(0,"Error!")
root.columnconfigure(0,pad=3)
root.columnconfigure(1,pad=3)
root.columnconfigure(2,pad=3)
root.columnconfigure(3,pad=3)
root.columnconfigure(4,pad=3)
root.rowconfigure(0,pad=3)
root.rowconfigure(1,pad=3)
root.rowconfigure(2,pad=3)
root.rowconfigure(3,pad=3)
display=Entry(root,font=("Calibri",13))
display.grid(row=1,columnspan=6,sticky=W+E)
one=Button(root,text="1",command=lambda:get_variables(1),font=("Calibri",12))
one.grid(row=2,column=0)
two=Button(root,text="2",command=lambda:get_variables(2),font=("Calibri",12))
two.grid(row=2,column=1)
three=Button(root,text="3",command=lambda:get_variables(3),font=("Calibri",12))
three.grid(row=2,column=2)
four=Button(root,text="4",command=lambda:get_variables(4),font=("Calibri",12))
four.grid(row=3,column=0)
five=Button(root,text="5",command=lambda:get_variables(5),font=("Calibri",12))
five.grid(row=3,column=1)
six=Button(root,text="6",command=lambda:get_variables(6),font=("Calibri",12))
six.grid(row=3,column=2)
seven=Button(root,text="7",command=lambda:get_variables(7),font=("Calibri",12))
seven.grid(row=4,column=0)
eight=Button(root,text="8",command=lambda:get_variables(8),font=("Calibri",12))
eight.grid(row=4,column=1)
nine=Button(root,text="9",command=lambda:get_variables(9),font=("Calibri",12))
nine.grid(row=4,column=2)
cls=Button(root,text="AC",command=clear_all,font=("Calibri",12),foreground="red")
cls.grid(row=5,column=0)
zero=Button(root,text="0",command=lambda:get_variables(0),font=("Calibri",12))
zero.grid(row=5,column=1)
result=Button(root,text="=",command=calculate,font=("Calibri",12),foreground="red")
result.grid(row=5,column=2)
plus=Button(root,text="+",command=lambda:get_operation("+"),font=("Calibri",12))
plus.grid(row=2,column=3)
minus=Button(root,text="-",command=lambda:get_operation("-"),font=("Calibri",12))
minus.grid(row=3,column=3)
multiply=Button(root,text="*",command=lambda:get_operation("*"),font=("Calibri",12))
multiply.grid(row=4,column=3)
divide=Button(root,text="/",command=lambda:get_operation("/"),font=("Calibri",12))
divide.grid(row=5,column=3)
#addingnewoperations
pi=Button(root,text="pi",command=lambda:get_operation("*3.14"),font=("Calibri",12))
pi.grid(row=2,column=4)
molo=Button(root,text="%",command=lambda:get_operation("%"),font=("Calibri",12))
molo.grid(row=3,column=4)
left_bracket=Button(root,text="(",command=lambda:get_operation("("),font=("Calibri",12))
left_bracket.grid(row=4,column=4)
exp=Button(root,text="exp",command=lambda:get_operation("**"),font=("Calibri",10))
exp.grid(row=5,column=4)
##Tobeadded:
#sin,cos,log,ln
undo_button=Button(root,text="<-",command=undo,font=("Calibri",12),foreground="red")
undo_button.grid(row=2,column=5)
fact=Button(root,text="x!",command=factorial,font=("Calibri",12))
fact.grid(row=3,column=5)
right_bracket=Button(root,text=")",command=lambda:get_operation(")"),font=("Calibri",12))
right_bracket.grid(row=4,column=5)
square=Button(root,text="^2",command=lambda:get_operation("**2"),font=("Calibri",10))
square.grid(row=5,column=5)
root.mainloop()
H. Python怎麼生成三維數
importnumpyasnp
a=np.array([1,2,3],dtype=int)#創建1*3維數組array([1,2,3])
type(a)#numpy.ndarray類型
a.shape#維數信息(3L,)
a.dtype.name#'int32'
a.size#元素個數:3
a.itemsize#每個元素所佔用的位元組數目:4
b=np.array([[1,2,3],[4,5,6]],dtype=int)#創建2*3維數組array([[1,2,3],[4,5,6]])
b.shape#維數信息(2L,3L)
b.size#元素個數:6
b.itemsize#每個元素所佔用的位元組數目:4
c=np.array([[1,2,3],[4,5,6]],dtype='int16')#創建2*3維數組array([[1,2,3],[4,5,6]],dtype=int16)
c.shape#維數信息(2L,3L)
c.size#元素個數:6
c.itemsize#每個元素所佔用的位元組數目:2
c.ndim#維數
d=np.array([[1,2,3],[4,5,6]],dtype=complex)#復數二維數組
d.itemsize#每個元素所佔用的位元組數目:16
d.dtype.name#元素類型:'complex128'
importnumpyasnp
a=np.array([1,2,3],dtype=int)#創建1*3維數組array([1,2,3])
type(a)#numpy.ndarray類型
a.shape#維數信息(3L,)
a.dtype.name#'int32'
a.size#元素個數:3
a.itemsize#每個元素所佔用的位元組數目:4
b=np.array([[1,2,3],[4,5,6]],dtype=int)#創建2*3維數組array([[1,2,3],[4,5,6]])
b.shape#維數信息(2L,3L)
b.size#元素個數:6
b.itemsize#每個元素所佔用的位元組數目:4
c=np.array([[1,2,3],[4,5,6]],dtype='int16')#創建2*3維數組array([[1,2,3],[4,5,6]],dtype=int16)
c.shape#維數信息(2L,3L)
c.size#元素個數:6
c.itemsize#每個元素所佔用的位元組數目:2
c.ndim#維數
d=np.array([[1,2,3],[4,5,6]],dtype=complex)#復數二維數組
d.itemsize#每個元素所佔用的位元組數目:16
d.dtype.name#元素類型:'complex128'
a1=np.zeros((3,4))#創建3*4全零二維數組
輸出:
array([[0.,0.,0.,0.],
[0.,0.,0.,0.],
[0.,0.,0.,0.]])
a1.dtype.name#元素類型:'float64'
a1.size#元素個數:12
a1.itemsize#每個元素所佔用的位元組個數:8
a2=np.ones((2,3,4),dtype=np.int16)#創建2*3*4全1三維數組
a2=np.ones((2,3,4),dtype='int16')#創建2*3*4全1三維數組
輸出:
array([[[1,1,1,1],
[1,1,1,1],
[1,1,1,1]],
[[1,1,1,1],
[1,1,1,1],
[1,1,1,1]]],dtype=int16)
a3=np.empty((2,3))#創建2*3的未初始化二維數組
輸出:(mayvary)
array([[1.,2.,3.],
[4.,5.,6.]])
a4=np.arange(10,30,5)#初始值10,結束值:30(不包含),步長:5
輸出:array([10,15,20,25])
a5=np.arange(0,2,0.3)#初始值0,結束值:2(不包含),步長:0.2
輸出:array([0.,0.3,0.6,0.9,1.2,1.5,1.8])
fromnumpyimportpi
np.linspace(0,2,9)#初始值0,結束值:2(包含),元素個數:9
輸出:
array([0.,0.25,0.5,0.75,1.,1.25,1.5,1.75,2.])
x=np.linspace(0,2*pi,9)
輸出:
array([0.,0.78539816,1.57079633,2.35619449,3.14159265,
3.92699082,4.71238898,5.49778714,6.28318531])
a=np.arange(6)
輸出:
array([0,1,2,3,4,5])
b=np.arange(12).reshape(4,3)
輸出:
array([[0,1,2],
[3,4,5],
[6,7,8],
[9,10,11]])
c=np.arange(24).reshape(2,3,4)
輸出:
array([[[0,1,2,3],
[4,5,6,7],
[8,9,10,11]],
[[12,13,14,15],
[16,17,18,19],
[20,21,22,23]]])
使用numpy.set_printoptions可以設置numpy變數的列印格式
在ipython環境下,使用help(numpy.set_printoptions)查詢使用幫助和示例
加法和減法操作要求操作雙方的維數信息一致,均為M*N為數組方可正確執行操作。
a=np.arange(4)
輸出:
array([0,1,2,3])
b=a**2
輸出:
array([0,1,4,9])
c=10*np.sin(a)
輸出:
array([0.,8.41470985,9.09297427,1.41120008])
n<35
輸出:
array([True,True,True,True],dtype=bool)
A=np.array([[1,1],[0,1]])
B=np.array([[2,0],[3,4]])
C=A*B#元素點乘
輸出:
array([[2,0],
[0,4]])
D=A.dot(B)#矩陣乘法
輸出:
array([[5,4],
[3,4]])
E=np.dot(A,B)#矩陣乘法
輸出:
array([[5,4],
[3,4]])
多維數組操作過程中的類型轉換
When operating with arrays of different types, the type of the
resulting array corresponds to the more general or precise one (a
behavior known as upcasting)
即操作不同類型的多維數組時,結果自動轉換為精度更高類型的數組,即upcasting
數組索引、切片和迭代
a=np.ones((2,3),dtype=int)#int32
b=np.random.random((2,3))#float64
b+=a#正確
a+=b#錯誤
a=np.ones(3,dtype=np.int32)
b=np.linspace(0,pi,3)
c=a+b
d=np.exp(c*1j)
輸出:
array([0.54030231+0.84147098j,-0.84147098+0.54030231j,
-0.54030231-0.84147098j])
d.dtype.name
輸出:
'complex128'
多維數組的一元操作,如求和、求最小值、最大值等
a=np.random.random((2,3))
a.sum()
a.min()
a.max()
b=np.arange(12).reshape(3,4)
輸出:
array([[0,1,2,3],
[4,5,6,7],
[8,9,10,11]])
b.sum(axis=0)#按列求和
輸出:
array([12,15,18,21])
b.sum(axis=1)#按行求和
輸出:
array([6,22,38])
b.cumsum(axis=0)#按列進行元素累加
輸出:
array([[0,1,2,3],
[4,6,8,10],
[12,15,18,21]])
b.cumsum(axis=1)#按行進行元素累加
輸出:
array([[0,1,3,6],
[4,9,15,22],
[8,17,27,38]])
universal functions
B=np.arange(3)
np.exp(B)
np.sqrt(B)
C=np.array([2.,-1.,4.])
np.add(B,C)
其他的ufunc函數包括:
all,any,apply_along_axis,argmax,argmin,argsort,average,bincount,ceil,clip,conj,corrcoef,cov,cross,cumprod,cumsum,diff,dot,floor,inner,lexsort,max,maximum,mean,median,min,minimum,nonzero,outer,prod,re,round,sort,std,sum,trace,transpose,var,vdot,vectorize,where
a=np.arange(10)**3
a[2]
a[2:5]
a[::-1]#逆序輸出
foriina:
print(i**(1/3.))
deff(x,y):
return10*x+y
b=np.fromfunction(f,(5,4),dtype=int)
b[2,3]
b[0:5,1]
b[:,1]
b[1:3,:]
b[-1]
c=np.array([[[0,1,2],[10,11,12]],[[100,101,102],[110,111,112]]])
輸出:
array([[[0,1,2],
[10,11,12]],
[[100,101,102],
[110,111,112]]])
c.shape
輸出:
(2L,2L,3L)
c[0,...]
c[0,:,:]
輸出:
array([[0,1,2],
[10,11,12]])
c[:,:,2]
c[...,2]
輸出:
array([[2,12],
[102,112]])
forrowinc:
print(row)
forelementinc.flat:
print(element)
a=np.floor(10*np.random.random((3,4)))
輸出:
array([[3.,9.,8.,4.],
[2.,1.,4.,6.],
[0.,6.,0.,2.]])
a.ravel()
輸出:
array([3.,9.,8.,...,6.,0.,2.])
a.reshape(6,2)
輸出:
array([[3.,9.],
[8.,4.],
[2.,1.],
[4.,6.],
[0.,6.],
[0.,2.]])
a.T
輸出:
array([[3.,2.,0.],
[9.,1.,6.],
[8.,4.,0.],
[4.,6.,2.]])
a.T.shape
輸出:
(4L,3L)
a.resize((2,6))
輸出:
array([[3.,9.,8.,4.,2.,1.],
[4.,6.,0.,6.,0.,2.]])
a.shape
輸出:
(2L,6L)
a.reshape(3,-1)
輸出:
array([[3.,9.,8.,4.],
[2.,1.,4.,6.],
[0.,6.,0.,2.]])
詳查以下函數:
ndarray.shape,reshape,resize,ravel
a=np.floor(10*np.random.random((2,2)))
輸出:
array([[5.,2.],
[6.,2.]])
b=np.floor(10*np.random.random((2,2)))
輸出:
array([[0.,2.],
[4.,1.]])
np.vstack((a,b))
輸出:
array([[5.,2.],
[6.,2.],
[0.,2.],
[4.,1.]])
np.hstack((a,b))
輸出:
array([[5.,2.,0.,2.],
[6.,2.,4.,1.]])
fromnumpyimportnewaxis
np.column_stack((a,b))
輸出:
array([[5.,2.,0.,2.],
[6.,2.,4.,1.]])
a=np.array([4.,2.])
b=np.array([2.,8.])
a[:,newaxis]
輸出:
array([[4.],
[2.]])
b[:,newaxis]
輸出:
array([[2.],
[8.]])
np.column_stack((a[:,newaxis],b[:,newaxis]))
輸出:
array([[4.,2.],
[2.,8.]])
np.vstack((a[:,newaxis],b[:,newaxis]))
輸出:
array([[4.],
[2.],
[2.],
[8.]])
np.r_[1:4,0,4]
輸出:
array([1,2,3,0,4])
np.c_[np.array([[1,2,3]]),0,0,0,np.array([[4,5,6]])]
輸出:
array([[1,2,3,0,0,0,4,5,6]])
詳細使用請查詢以下函數:
hstack,vstack,column_stack,concatenate,c_,r_
a=np.floor(10*np.random.random((2,12)))
輸出:
array([[9.,7.,9.,...,3.,2.,4.],
[5.,3.,3.,...,9.,7.,7.]])
np.hsplit(a,3)
輸出:
[array([[9.,7.,9.,6.],
[5.,3.,3.,1.]]),array([[7.,2.,1.,6.],
[7.,5.,0.,2.]]),array([[9.,3.,2.,4.],
[3.,9.,7.,7.]])]
np.hsplit(a,(3,4))
輸出:
[array([[9.,7.,9.],
[5.,3.,3.]]),array([[6.],
[1.]]),array([[7.,2.,1.,...,3.,2.,4.],
[7.,5.,0.,...,9.,7.,7.]])]
實現類似功能的函數包括:
hsplit,vsplit,array_split
a=np.arange(12)
輸出:
array([0,1,2,...,9,10,11])
notatall
b=a
bisa#True
b.shape=3,4
a.shape#(3L,4L)
deff(x)#,sofunctioncallsmakeno.
print(id(x))#id是python對象的唯一標識符
id(a)#111833936L
id(b)#111833936L
f(a)#111833936L
淺復制
c=a.view()
cisa#False
c.baseisa#True
c.flags.owndata#False
c.shape=2,6
a.shape#(3L,4L)
c[0,4]=1234
print(a)
輸出:
array([[0,1,2,3],
[1234,5,6,7],
[8,9,10,11]])
s=a[:,1:3]
s[:]=10
print(a)
輸出:
array([[0,10,10,3],
[1234,10,10,7],
[8,10,10,11]])
深復制
d=a.()
disa#False
d.baseisa#False
d[0,0]=9999
print(a)
輸出:
array([[0,10,10,3],
[1234,10,10,7],
[8,10,10,11]])
numpy基本函數和方法一覽
Array Creation
arange,array,,empty,empty_like,eye,fromfile,fromfunction,identity,linspace,logspace,mgrid,ogrid,ones,ones_like,r,zeros,zeros_like
Conversions
ndarray.astype,atleast_1d,atleast_2d,atleast_3d,mat
Manipulations
array_split,column_stack,concatenate,diagonal,dsplit,dstack,hsplit,hstack,ndarray.item,newaxis,ravel,repeat,reshape,resize,squeeze,swapaxes,take,transpose,vsplit,vstack
Questionsall,any,nonzero,where
Ordering
argmax,argmin,argsort,max,min,ptp,searchsorted,sort
Operations
choose,compress,cumprod,cumsum,inner,ndarray.fill,imag,prod,put,putmask,real,sum
Basic Statistics
cov,mean,std,var
Basic Linear Algebra
cross,dot,outer,linalg.svd,vdot
完整的函數和方法一覽表鏈接:
https://docs.scipy.org/doc/numpy-dev/reference/routines.html#routines
I. python 如何把一個二維矩陣的元素根據另一個二維矩陣內的向量,做一次投影
#例子
importnumpyasnp
data=np.array([[1,2,3,4,5],
[1,2,3,6,7],
[2,3,4,5,7],
[3,4,5,6,7],
[4,5,6,7,8]])
sorted_cols=[]
forcol_noinrange(data.shape[1]):
sorted_cols.append(data[np.argsort(data[:,col_no])][:,col_no])
sorted_data=np.column_stack(sorted_cols)
J. python 復合列表 怎麼輸出其中某個值的坐標
123456789101112# 例子import numpy as np data = np.array([[1,2,3,4,5], [1,2,3,6,7], [2,3,4,5,7], [3,4,5,6,7], [4,5,6,7,8]])sorted_cols = []for col_no in range(data.shape[1]): sorted_cols.append(data[np.argsort(data[:,col_no])][:,col_no])sorted_data = np.column_stack(sorted_cols)