我使用Python和NumPy并且在"转置"方面存在一些问题:
import numpy as np
a = np.array([5,4])
print(a)
print(a.T)
Run Code Online (Sandbox Code Playgroud)
调用a.T不是转置数组.如果a是例如[[],[]]然后它正确转置,但我需要转置[...,...,...].
hh=[[82.5], [168.5]]
N=1./5
ll=N*hh
Run Code Online (Sandbox Code Playgroud)
我做错了什么?我收到错误:
"不能将序列乘以'浮动'类型的非int"
我尝试添加float(),但这不能解决我的问题;
我需要将数组中的每个元素相乘...感谢所有
hh=[[82.5], [168.5]]
N=zip(*hh)
ll = [[x*N for x in y] for y in hh]
Run Code Online (Sandbox Code Playgroud)
???
eigenvalues, eigenvectors = linalg.eig(K)
Run Code Online (Sandbox Code Playgroud)
我怎样才能打印出特征向量len(K).所以,如果有K2x2矩阵,我得到4个特征向量,如果有len(K)=2.... 我怎么能打印其中2个...
非常感谢
我需要一些Python的帮助,打印:
我有:
input = [(3,'x1'),(5,'x3'),(2,'x2')]
需要以这种形式打印:
x1=3 x2=2 x3=3
Run Code Online (Sandbox Code Playgroud)
非常感谢
例如,如果我有矩阵:
x=[['1', '7', 'U1'], ['1.5', '8', 'U1'], ['2', '5.5', 'U2']]
Run Code Online (Sandbox Code Playgroud)
除了最后一个,我如何从x获取所有数据.然后我需要总结这些元素.
这就是我需要的: sum=1+7+1.5+8+2+5.5= ??
谢谢
我尝试:
> x=[['1', '7', 'U1'], ['1.5', '8',
> 'U1'], ['2', '5.5', 'U2']]
>
> sum(sum(el[:-1]) for el in x)
Run Code Online (Sandbox Code Playgroud)
但收到错误:
回溯(最近一次调用最后一次):
文件"xxx.py",第3行,总和(sum(el [: - 1]),el为x)文件"xxx.py",第3行,总和(sum(sum) el [: - 1])for el in x)TypeError:+:'int'和'str'的不支持的操作数类型
我有下一个问题:
x=[['1', '7', 'U1'], ['1.5', '8', 'U1']]
y=sum(sum(float(el) for el in els[:-1]) for els in x)
print(x)
print(y)
Run Code Online (Sandbox Code Playgroud)
在这个代码总和中,对所有数字求和,但我想首先从第一个['1','7','U1'],第一个数字和第二个['1.5','8','U1']求和数字,第二个相同......
所以最终结果填充看起来像"矩阵":
y=
[ [2.5], #1+1.5=2.5
[15]] #7+8=15
Run Code Online (Sandbox Code Playgroud) A=[['2' '7' 'fas']
['4' '8' 'sda']
['1' '5' 'daf']
['2' '24' 'gag']]
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到矩阵A,而不是每一行中的"atribute":
A=[['2' '7']
['4' '8']
['1' '5' ]
['2' '24']]
Run Code Online (Sandbox Code Playgroud)
我知道行中的最后一个元素是[:-1]
我尝试了numpy:
A[:, ?? ]
Run Code Online (Sandbox Code Playgroud)
矩阵A是随机元素,所以我以这种方式思考:A[:,end-1]但是numpy不知道结局是什么
好问题是我的原始输入是:
x=[['1', '7', 'gg'], ['1.5', '8', 'as']...]
Run Code Online (Sandbox Code Playgroud)
我需要在每一行中删除最后一个元素.
我尝试用矩阵切割矩阵中的最后一个元素:
hl=x[:,:-1]
Run Code Online (Sandbox Code Playgroud)
但这不起作用,所以我尝试这样:
kl=array(x)
hl=x[:,:-1]
Run Code Online (Sandbox Code Playgroud)
现在我得到:
[['1' '7']
['1.5' '8']]
Run Code Online (Sandbox Code Playgroud)
而不是:[['1','7'] ['1.5','8']]
有解决方案吗
我有矩阵乘法的一些问题:
我想要乘以例如a和b:
a=array([1,3]) # a is random and is array!!! (I have no impact on that)
# there is a just for example what I want to do...
b=[[[1], [2]], #b is also random but always size(b)= even
[[3], [2]],
[[4], [6]],
[[2], [3]]]
Run Code Online (Sandbox Code Playgroud)
所以我想要的是以这种方式成倍增长
[1,3]*[1;2]=7
[1,3]*[3;2]=9
[1,3]*[4;6]=22
[1,3]*[2;3]=11
Run Code Online (Sandbox Code Playgroud)
所以我需要的结果是:
x1=[7,9]
x2=[22,8]
Run Code Online (Sandbox Code Playgroud)
我知道非常复杂,但我尝试了2个小时来实现这个但没有成功:(
任何人都可以解释哪些'导入'是通用的,所以我不需要写例如:
from numpy import *
import numpy
import numpy as np
from numpy.linalg import *
Run Code Online (Sandbox Code Playgroud)
为什么不import numpy或from numpy import *从"numpy"中扼杀所有人?
python ×10
numpy ×4
matrix ×3
arrays ×1
eigenvector ×1
multiplying ×1
printing ×1
sum ×1
transpose ×1