在Python的标准max函数中(我也可以传入一个关键参数):
s = numpy.array(['one','two','three'])
max(s) # 'two' (lexicographically last)
max(s, key=len) # 'three' (longest string)
Run Code Online (Sandbox Code Playgroud)
有了一个更大的(多维)数组,我不能再使用max,所以我尝试使用numpy.amax,但我似乎无法使用amax字符串...
t = np.array([['one','two','three'],['four','five','six']])
t.dtype # dtype('|S5')
numpy.amax(t, axis=0) #Error! Hoping for: [`two`, `six`]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 1833, in amax
return amax(axis, out)
TypeError: cannot perform reduce with flexible type
Run Code Online (Sandbox Code Playgroud)
是否可以使用amax(我正确使用它!),还是有其他numpy工具可以做到这一点?
37101000ssd48800^A1420asd938987^A2011-09-10^A18:47:50.000^A99.00^A1^A0^A
37101000sd48801^A44557asd03082^A2011-09-06^A13:24:58.000^A42.01^A1^A0^A
Run Code Online (Sandbox Code Playgroud)
所以首先我从字面上理解并尝试:
line = line.split("^A")
Run Code Online (Sandbox Code Playgroud)
并且
line = line.split("\\u001")
Run Code Online (Sandbox Code Playgroud)
如果我这样做,第一种方法适用于我的本地机器:
cat input.txt | python mapper.py
Run Code Online (Sandbox Code Playgroud)
它在本地运行正常(input.txt是上面的数据),但在hadoop流集群上失败.
有人告诉我,我应该使用它"\\u001"作为分隔符,但这在我的本地机器或集群上也不起作用.
如果我在本地调试它使用:
cat input.txt | python mapper.py | sort | python reducer.py
Run Code Online (Sandbox Code Playgroud)
这运行得很好,如果我"^A"在本地使用分隔符,但我在群集上运行时遇到错误,并且错误代码也没有太大帮助......
关于如何调试这个的任何建议?
谢谢
为什么是inSetStates,inInputAlph并isCorrectDirection评估变量False在下面的代码:
class POC(object):
def __init__(self):
self.__set_states = (1,2,3,4,5)
self.__input_alph = ('a','b')
self.__directions = ('i','d')
def enterTransition(self):
while True:
print "enter transition tuple format:"
trans = raw_input(" Example (1,'a',2,'b','d') : ")
inSetStates = (trans[0] in self.__set_states) and (trans[2] in self.__set_states)
inInputAlph = (trans[1]in self.__input_alph) and (trans[3] in self.__input_alph)
isCorrectDirection = (trans[4].lower() in self.__directions) or (trans[4].lower() in self.__directions)
if (inSetStates and inInputAlph and isCorrectDirection):
return trans
break
else:
print "ERROR: Something is wrong"
poc …Run Code Online (Sandbox Code Playgroud) 如何将MATLAB 的quantiz函数
(其中 xd 是估计信号)转换为 python/scipy?
我正在尝试使用 python 和 scipy、numpy、pygtk 和 matplotlib 等库将我在 MATLAB 中开发的用于语音处理的算法实现到软件包中,以便将该算法转换为完整的包。
我正在使用 scipy 进行算法开发,但我找不到合适的函数来在 python 中“量化信号”:
[I,xq] = quantiz(xd,1:step:1-step, -1:step:1);
Run Code Online (Sandbox Code Playgroud)
我该如何用 python 写这个?
我正在使用unittest和selenium来自动化我的浏览器测试.
我将如何进行多次运行的测试,用户创建票证.票证必须有一个标题名称,每次我运行测试我希望标题名称是随机的.
我想格式:"测试票,1 |测试票,2 ......"
在Python中,
n**0.5 # or
math.sqrt(n)
Run Code Online (Sandbox Code Playgroud)
认识到一个数字是一个完美的平方?具体来说,我应该担心使用时
int(n**0.5) # instead of
int(n**0.5 + 0.000000001)
Run Code Online (Sandbox Code Playgroud)
由于精度误差,我可能会不小心以小于实际平方根的数字结束运算?
我使用pandas包生成了这样的交通流量系列:
data = np.array(data)
index = date_range(time_start[0],time_end[0],freq='30S')
s = Series(data, index=index)
Run Code Online (Sandbox Code Playgroud)
样本输出如下:
2013-07-02 10:04:30 13242.0
2013-07-02 10:05:00 12354.3
................... .......
Run Code Online (Sandbox Code Playgroud)
这里第一列是索引,第二列是值.我的任务是收集他们的价值观(第二栏)缺失的所有时刻.
我的想法是这样的:
for i in s:
if isnull(i):
s.iloc['i']
Run Code Online (Sandbox Code Playgroud)
但'无'不能用来引用索引......
如果缺失值和s都很大,这会导致效率吗?有更好的主意吗?
我有以下DataFrame:
Date Year Month Day Security Trade Value
0 2011-01-10 00:00:00 2011 1 10 AAPL Buy 1500
1 2011-01-13 00:00:00 2011 1 13 AAPL Sell 1500
Run Code Online (Sandbox Code Playgroud)
当我将其写入csv时,会在文件的开头创建一个额外的列:
,Date,Year,Month,Day,Security,Trade,Value
0,2011-01-10 00:00:00,2011,1,10,AAPL,Buy,1500
1,2011-01-13 00:00:00,2011,1,13,AAPL,Sell,1500
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
您好我有以下数据帧
df =
A B
John Tom
Homer Bart
Tom Maggie
Lisa John
Run Code Online (Sandbox Code Playgroud)
我想为每个名称分配一个唯一的ID并返回
df =
A B C D
John Tom 0 1
Homer Bart 2 3
Tom Maggie 1 4
Lisa John 5 0
Run Code Online (Sandbox Code Playgroud)
我所做的是以下内容:
LL1 = pd.concat([df.a,df.b],ignore_index=True)
LL1 = pd.DataFrame(LL1)
LL1.columns=['a']
nameun = pd.unique(LL1.a.ravel())
LLout['c'] = 0
LLout['d'] = 0
NN = list(nameun)
for i in range(1,len(LLout)):
LLout.c[i] = NN.index(LLout.a[i])
LLout.d[i] = NN.index(LLout.b[i])
Run Code Online (Sandbox Code Playgroud)
但由于我有一个非常大的数据集,这个过程非常缓慢.
是否有一个Python库可以检测(并可能解码)字符串的编码?
我找到chardet但它给了我一个错误,使用:
chardet.detect(self.ui.TextFrom.toPlainText())
got: = chardet.detect(self.ui.TextFrom.toPlainText())
File .... u.feed(aBuf) File ....
if self._highBitDetector.search(aBuf):
TypeError: buffer size mismatch
Run Code Online (Sandbox Code Playgroud)
也:
print type(self.ui.TextFrom.toPlainText())
# <class 'PyQt4.QtCore.QString'>
Run Code Online (Sandbox Code Playgroud)