Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。

10
熊猫获得列平均值/平均值
我无法获得熊猫列的平均值或均值。有一个数据框。我在下面尝试的任何事情都没有给我该列的平均值weight >>> allDF ID birthyear weight 0 619040 1962 0.1231231 1 600161 1963 0.981742 2 25602033 1963 1.3123124 3 624870 1987 0.94212 以下返回几个值,而不是一个: allDF[['weight']].mean(axis=1) 这样: allDF.groupby('weight').mean()
155 python  pandas 

3
将Pandas Multi-Index转换为专栏
我有一个具有2个索引级别的数据框: value Trial measurement 1 0 13 1 3 2 4 2 0 NaN 1 12 3 0 34 我想变成这样: Trial measurement value 1 0 13 1 1 3 1 2 4 2 0 NaN 2 1 12 3 0 34 我怎样才能最好地做到这一点? 我需要这样做是因为我想按照此处的指示汇总数据,但是如果将它们用作索引,则无法选择这样的列。

10
Keras,如何获得每一层的输出?
我已经使用CNN训练了二进制分类模型,这是我的代码 model = Sequential() model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1], border_mode='valid', input_shape=input_shape)) model.add(Activation('relu')) model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1])) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=pool_size)) # (16, 16, 32) model.add(Convolution2D(nb_filters*2, kernel_size[0], kernel_size[1])) model.add(Activation('relu')) model.add(Convolution2D(nb_filters*2, kernel_size[0], kernel_size[1])) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=pool_size)) # (8, 8, 64) = (2048) model.add(Flatten()) model.add(Dense(1024)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(2)) # define a binary classification problem model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) model.fit(x_train, …

6
Pelican 3.3 pelican-quickstart错误“ ValueError:未知语言环境:UTF-8”
当我尝试使用pelican3.3时,我键入了推荐的“ pelican-quickstart”,但出现了一些错误。 这些是错误: (PelicanEnv)59-127-113-90:myblog Richo$ pelican-quickstart Traceback (most recent call last): File "/Users/Richo/Dropbox/Github/PelicanEnv/bin/pelican-quickstart", line 9, in <module> load_entry_point('pelican==3.3', 'console_scripts', 'pelican-quickstart')() File "/Users/Richo/Dropbox/Github/PelicanEnv/lib/python2.7/site-packages/pkg_resources.py", line 378, in load_entry_point return get_distribution(dist).load_entry_point(group, name) File "/Users/Richo/Dropbox/Github/PelicanEnv/lib/python2.7/site-packages/pkg_resources.py", line 2566, in load_entry_point return ep.load() File "/Users/Richo/Dropbox/Github/PelicanEnv/lib/python2.7/site-packages/pkg_resources.py", line 2260, in load entry = __import__(self.module_name, globals(),globals(), ['__name__']) File "/Users/Richo/Dropbox/Github/PelicanEnv/lib/python2.7/site-packages/pelican/__init__.py", …


12
如何枚举从1开始的数字范围
我正在使用Python 2.5,我想要这样的枚举(从1而不是0开始): [(1, 2000), (2, 2001), (3, 2002), (4, 2003), (5, 2004)] 我知道在python 2.6中可以执行以下操作:h = enumerate(range(2000,2005),1)给出上述结果,但是在python2.5中您不能... 使用python2.5: >>> h = enumerate(range(2000, 2005)) >>> [x for x in h] [(0, 2000), (1, 2001), (2, 2002), (3, 2003), (4, 2004)] 有谁知道在python 2.5中获得理想结果的方法? 谢谢, 杰夫
154 python 

9
“克隆”行或列向量
有时将行或列向量“克隆”到矩阵很有用。克隆是指将行向量转换为 [1,2,3] 入矩阵 [[1,2,3] [1,2,3] [1,2,3] ] 或列向量,例如 [1 2 3 ] 进入 [[1,1,1] [2,2,2] [3,3,3] ] 在matlab或octave中,这很容易做到: x = [1,2,3] a = ones(3,1) * x a = 1 2 3 1 2 3 1 2 3 b = (x') * ones(1,3) b = 1 1 1 2 2 2 …

6
如何将列表中的每个元素除以int?
我只想将一个列表中的每个元素都用一个int分隔。 myList = [10,20,30,40,50,60,70,80,90] myInt = 10 newList = myList/myInt 这是错误: TypeError: unsupported operand type(s) for /: 'list' and 'int' 我了解为什么收到此错误。但是我为找不到解决方案感到沮丧。 还尝试了: newList = [ a/b for a, b in (myList,myInt)] 错误: ValueError: too many values to unpack 预期结果: newList = [1,2,3,4,5,6,7,8,9] 编辑: 以下代码给了我预期的结果: newList = [] for x in …
154 python 




7
为什么“ return list.sort()”返回None,而不返回列表?
我已经能够验证findUniqueWords结果是否为sorted list。但是,它不返回列表。为什么? def findUniqueWords(theList): newList = [] words = [] # Read a line at a time for item in theList: # Remove any punctuation from the line cleaned = cleanUp(item) # Split the line into separate words words = cleaned.split() # Evaluate each word for word in words: # …
154 python  list  sorting  return 

10
在熊猫系列中查找元素的索引
我知道这是一个非常基本的问题,但是由于某种原因我找不到答案。如何获取python pandas中Series某些元素的索引?(第一次出现就足够了) 即,我想要类似的东西: import pandas as pd myseries = pd.Series([1,4,0,7,5], index=[0,1,2,3,4]) print myseries.find(7) # should output 3 当然,可以使用循环定义这样的方法: def find(s, el): for i in s.index: if s[i] == el: return i return None print find(myseries, 7) 但我认为应该有更好的方法。在那儿?
154 python  pandas 

13
在Django模板中格式化数字
我正在尝试格式化数字。例子: 1 => 1 12 => 12 123 => 123 1234 => 1,234 12345 => 12,345 它很常见,但是我无法弄清楚应该使用哪个过滤器。 编辑:如果您有通用的Python方法可以执行此操作,则很高兴在模型中添加格式化的字段。
154 python  django 

3
在argparse中带有破折号的选项
我想在argparse模块中有一些选项,例如,--pm-export当我尝试使用它时,就像args.pm-export出现没有属性的错误一样pm。我该如何解决这个问题?-命令行选项中可以有吗?
154 python  argparse 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.