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)