我有一个数据集,每个样本的结构都与此类似
X=[ [[],[],[],[]], [[],[]] , [[],[],[]] ,[[][]]]
Run Code Online (Sandbox Code Playgroud)
例如:
X=np.array([ [ [1,2,3], [2,4,5] ,[2,3,4] ] , [ [5,6], [6,6] ] , [[2,3,1],[2,3,10],[23,1,2],[1,4,5]] ] ,"object")
Y=np.array([ [ [12,14,15] ,[12,13,14] ] , [ [15,16], [16,16] ] , [[22,23,21],[32,33,11],[12,44,55]] ] ,"object")
Run Code Online (Sandbox Code Playgroud)
因此,对于每个样本,我需要计算x的每个元素与相同索引的y的相应元素之间的点积并对结果求和.即:
result=0
for i in range(3):
for n,m in itertools.product(X[i],Y[i]):
print "%s, %s" % (n,m)
result+=np.dot(n,m)
.....:
[1, 2, 3], [12, 14, 15]
[1, 2, 3], [12, 13, 14]
[2, 4, 5], [12, 14, 15]
[2, 4, 5], [12, 13, 14] …Run Code Online (Sandbox Code Playgroud) 我最近开始使用 pandas,并且有一些关于 date_range 的问题。
In [168]: pd.date_range("2013-07-01", "2013-10-03", freq='W').to_series()
Out[168]:
2013-07-07 2013-07-07
2013-07-14 2013-07-14
2013-07-21 2013-07-21
2013-07-28 2013-07-28
2013-08-04 2013-08-04
2013-08-11 2013-08-11
2013-08-18 2013-08-18
2013-08-25 2013-08-25
2013-09-01 2013-09-01
2013-09-08 2013-09-08
2013-09-15 2013-09-15
2013-09-22 2013-09-22
2013-09-29 2013-09-29
Freq: W-SUN, dtype: datetime64[ns]
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我预计第一个索引2013-07-01不是2013-07-07. 我检查了一下,发现默认情况下 date_range 考虑左右闭合。还尝试了一些其他频率,例如W-MON,但没有帮助。
作为我项目的一部分,我需要找到一个向量中是否有4个或更多个连续元素以及它们的索引.目前我使用以下代码:
#sample arrays:
#a1 = np.array([0, 1, 2, 3, 5])
#a2 = np.array([0, 1, 3, 4, 5, 6])
#a3 = np.array([0, 1, 3, 4, 5])
a4 = array([0, 1, 2, 4, 5, 6])
dd = np.diff(a4) #array([1, 1, 2, 1, 1])
c = 0
idx = []
for i in range(len(dd)):
if dd[i]==1 and c<3:
idx.append(i)
c+=1
elif dd[i]!=1 and c>=3:
break
else:
c=0
idx=[]
Run Code Online (Sandbox Code Playgroud)
我有兴趣看看是否有可能避免for循环并只使用numpy函数来完成这项任务.
我刚刚开始编程并且正在尝试学习Python.第四章的最后一部分,练习5,how to think like a computer scientist是我的难过.我正在尝试修改shell脚本,以便用户可以输入"a","b"或"c",并根据用户选择打印出正确的响应.这就是我到目前为止实现它的方式,并希望有人可以告诉我我在这里缺少的东西.
def dispatch(choice):
if choice == 'a':
function_a()
elif choice == 'b':
function_b()
elif choice == 'c':
function_c()
else:
print "Invalid choice."
def function_a():
print "function_a was called ..."
def function_b():
print "function_b was called ..."
def function_c():
print "function_c was called ..."
dispatch1 = raw_input ("Please Enter a Function.")
print dispatch(choice)
Run Code Online (Sandbox Code Playgroud)
当我运行这个我得到名称选择没有定义错误.我正试图让它吐出来后函数被调用...当它被输入raw_input时.
感谢您的任何帮助,
约翰
python ×4
algorithm ×1
datetime ×1
nested-lists ×1
nested-loops ×1
numpy ×1
pandas ×1
time-series ×1