想了解在python中添加两个矩阵的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于python矩阵添加一行的相关问题,此外,我们还将为您介绍关于python–使用NaN添加两个系列、pyth
想了解在python中添加两个矩阵的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于python矩阵添加一行的相关问题,此外,我们还将为您介绍关于python – 使用NaN添加两个系列、python-2.7 – 读取矩阵并在python中获取行名和列名、Python-如何在Python中串联两个列表?、Python如何在numpy中合并两个矩阵的新知识。
本文目录一览:- 在python中添加两个矩阵(python矩阵添加一行)
- python – 使用NaN添加两个系列
- python-2.7 – 读取矩阵并在python中获取行名和列名
- Python-如何在Python中串联两个列表?
- Python如何在numpy中合并两个矩阵
在python中添加两个矩阵(python矩阵添加一行)
我正在尝试编写一个添加两个矩阵以通过以下doctest的函数:
>>> a = [[1,2],[3,4]]
>>> b = [[2,[2,2]]
>>> add_matrices(a,b)
[[3,4],[5,6]]
>>> c = [[8,7]]
>>> d = [[3,[9,[10,12]]
>>> add_matrices(c,d)
[[11,[12,6],[15,19]]
所以我写了一个函数:
def add(x,y):
return x + y
然后我编写了以下函数:
def add_matrices(c,d):
for i in range(len(c)):
print map(add,c[i],d[i])
我 排序 的得到正确的答案。
python – 使用NaN添加两个系列
a = Series([35000,71000,16000,5000],index=['Ohio','Texas','Oregon','Utah']) b = Series([NaN,35000],index=['California','Ohio'])
结果:
In [63]: a Out[63]: Ohio 35000 Texas 71000 Oregon 16000 Utah 5000 In [64]: b Out[64]: California NaN Texas 71000 Oregon 16000 Ohio 35000
当我把它们加在一起时,我得到了……
In [65]: a+b Out[65]: California NaN Ohio 70000 Oregon 32000 Texas 142000 Utah NaN
那么为什么犹他州的价值是NaN而不是500?似乎500 NaN = 500.是什么赋予了?我错过了什么,请解释一下.
更新:
In [92]: # fill NaN with zero b = b.fillna(0) b Out[92]: California 0 Texas 71000 Oregon 16000 Ohio 35000 In [93]: a Out[93]: Ohio 35000 Texas 71000 Oregon 16000 Utah 5000 In [94]: # a is still good a+b Out[94]: California NaN Ohio 70000 Oregon 32000 Texas 142000 Utah NaN
解决方法
python-2.7 – 读取矩阵并在python中获取行名和列名
sample sample1 sample2 sample3 sample1 1 0.7 0.8 sample2 0.7 1 0.8 sample3 0.8 0.8 1
我想获取所有值为>的对0.8.例如:sample1,sample3 0.8 sample2,sample3 0.8等在一个大文件中.
当我使用csv.reader时,每一行都会转入一个列表并跟踪行名和列名会使程序变得狡猾.我想知道一种优雅的方式,比如使用numpy或pandas.
期望的输出:
sample1,sample3 0.8 sample2,sample3 0.8
1可以忽略,因为在相同的样本之间,它总是1.
解决方法
np.triu
屏蔽掉上三角形值:
In [11]: df Out[11]: sample1 sample2 sample3 sample sample1 1.0 0.7 0.8 sample2 0.7 1.0 0.8 sample3 0.8 0.8 1.0 In [12]: np.triu(df,1) Out[12]: array([[ 0.,0.7,0.8],[ 0.,0.,0. ]]) In [13]: np.triu(df,1) >= 0.8 Out[13]: array([[False,False,True],[False,False]],dtype=bool)
然后提取它是真的索引/列我认为你必须使用np.where *:
In [14]: np.where(np.triu(df,1) >= 0.8) Out[14]: (array([0,1]),array([2,2]))
这为您提供了第一个索引索引和列索引的数组(这是这个numpy版本中效率最低的部分):
In [16]: index,cols = np.where(np.triu(df,1) >= 0.8) In [17]: [(df.index[i],df.columns[j],df.iloc[i,j]) for i,j in zip(index,cols)] Out[17]: [('sample1','sample3',0.80000000000000004),('sample2',0.80000000000000004)]
如预期的.
*我可能忘记了获取最后一个块的更简单方法(编辑:下面的pandas代码可以做到,但我认为可能还有其他方法.)
您可以在pandas中使用相同的技巧,但使用stack来本机获取索引/列:
In [21]: (np.triu(df,1) >= 0.8) * df Out[21]: sample1 sample2 sample3 sample sample1 0 0 0.8 sample2 0 0 0.8 sample3 0 0 0.0 In [22]: res = ((np.triu(df,1) >= 0.8) * df).stack() In [23]: res Out[23]: sample sample1 sample1 0.0 sample2 0.0 sample3 0.8 sample2 sample1 0.0 sample2 0.0 sample3 0.8 sample3 sample1 0.0 sample2 0.0 sample3 0.0 dtype: float64 In [24]: res[res!=0] Out[24]: sample sample1 sample3 0.8 sample2 sample3 0.8 dtype: float64
Python-如何在Python中串联两个列表?
如何解决Python-如何在Python中串联两个列表??
你可以使用+运算符来组合它们:
listone = [1,2,3]
listtwo = [4,5,6]
joinedlist = listone + listtwo
输出:
>>> joinedlist
[1,2,3,4,5,6]
也可以创建一个生成器,使用来简单地遍历两个列表中的项目itertools.chain()
。这使你可以将列表(或任何可迭代的)链接在一起进行处理,而无需将项目复制到新列表中:
import itertools
for item in itertools.chain(listone, listtwo):
# Do something with each list item
解决方法
如何在Python中串联两个列表?
例:
listone = [1,2,3]
listtwo = [4,5,6]
预期结果:
>>> joinedlist
[1,3,4,6]
Python如何在numpy中合并两个矩阵
Python的新手,在numpy中挣扎,希望有人能帮助我,谢谢!
from numpy import *
A = matrix('1.0 2.0; 3.0 4.0')
B = matrix('5.0 6.0')
C = matrix('1.0 2.0; 3.0 4.0; 5.0 6.0')
print "A=",A
print "B=",B
print "C=",C
结果:
A= [[ 1. 2.]
[ 3. 4.]]
B= [[ 5. 6.]]
C= [[ 1. 2.]
[ 3. 4.]
[ 5. 6.]]
问题:像在matlab中一样,如何使用A和B生成C C=[A;B]
?
我们今天的关于在python中添加两个矩阵和python矩阵添加一行的分享就到这里,谢谢您的阅读,如果想了解更多关于python – 使用NaN添加两个系列、python-2.7 – 读取矩阵并在python中获取行名和列名、Python-如何在Python中串联两个列表?、Python如何在numpy中合并两个矩阵的相关信息,可以在本站进行搜索。
本文标签: