根据这篇文章,我应该能够访问ndarray中列的名称作为a.dtype.names
但是,如果我将pandas DataFrame转换为带有df.as_matrix()或df.values的ndarray,则dtype.names字段为None.此外,如果我尝试将列名称分配给ndarray
X = pd.DataFrame(dict(age=[40., 50., 60.], sys_blood_pressure=[140.,150.,160.]))
print X
print type(X.as_matrix())# <type 'numpy.ndarray'>
print type(X.as_matrix()[0]) # <type 'numpy.ndarray'>
m = X.as_matrix()
m.dtype.names = list(X.columns)
Run Code Online (Sandbox Code Playgroud)
我明白了
ValueError: there are no fields defined
Run Code Online (Sandbox Code Playgroud)
更新:
我特别感兴趣的是矩阵只需要保存一个类型(它是一个特定数字类型的ndarray),因为我也想使用cython进行优化.(我怀疑numpy记录和结构化数组更难以处理,因为它们更自由地输入.)
实际上,我只想维护通过sci-kit预测器深层树的数组的column_name元数据.它的接口的.fit(X,y)和.predict(X)API不允许传递关于X和y对象之外的列标签的附加元数据.
s1 = image.shape # Image is an opencv2 image (ndarray)
w,h,d = s1
image_ = cv2.resize(image, (w*2, h*2), interpolation = cv2.INTER_CUBIC)
s2 = image_.shape
Run Code Online (Sandbox Code Playgroud)
在上面,我要求将输出大小调整为 (960,1280),但我得到 (1280, 960)。
那是怎么回事?
image_ = cv2.resize(image, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
Run Code Online (Sandbox Code Playgroud)
...按预期工作。(没有必要指出这一点)。
可运行示例:
import cv2
def run():
cap = cv2.VideoCapture(0)
while cap.isOpened():
success, image = cap.read() # (480, 640, 3)
if not success:
print("Ignoring empty camera frame.")
continue
s1 = image.shape
w, h, d = s1
s2 = (w * 2, h * …Run Code Online (Sandbox Code Playgroud) 可以在内存中完全创建的不同类型的Linux文件是什么?
例如,可能会创建一个管道文件,但是创建文件的位置(或文件路径的文件系统类型)是否会影响是否涉及磁盘访问?如果我在ext3文件系统中创建管道文件,是否可以获得物理磁盘访问结果?
假设我在Spring中有两组控制器:
/jsonapi1/*/jsonapi2/*两者都返回要解释为JSON文本的对象.
我想要某种过滤器来包装来自这些控制器的响应,以便:
原始响应包含在另一个对象中.
例如,如果/ jsonapi1/count返回:
{"num_humans":123, "num_androids":456}
Run Code Online (Sandbox Code Playgroud)
那么响应应该被包装并返回如下:
{ "status":0,
"content":{"num_humans":123, "num_androids":456}
}
Run Code Online (Sandbox Code Playgroud)如果控制器中发生异常,则过滤器应捕获异常并按如下方式报告
{ "status":5,
"content":"Something terrible happened"
}
Run Code Online (Sandbox Code Playgroud)其他控制器的响应将保持不变.
我们目前正在自定义MappingJackson2HttpMessageConverter传递给以WebMvcConfigurerAdapter.configureMessageConverters执行上述任务.工作得很好,除了这种方法似乎不可能选择它适用的URL(或控制器类).
是否可以将这些类型的包装器应用于单个控制器类或URL?
更新:Servlet过滤器看起来像一个解决方案.是否可以选择将哪个过滤器应用于哪些控制器方法或哪些URL?
我想在Spring WebApplicationContext中添加一个特定的控制器类.我遇到了以下示例:(它在Scala中,但是从这里改编:使用ComponentScan或context:仅使用一个类的component-scan)
@Configuration
@ComponentScan(
basePackages = Array("com.example.controllers"),
useDefaultFilters = false,
includeFilters = Array(
new ComponentScan.Filter(`type` = FilterType.ASSIGNABLE_TYPE,
value = Array(classOf[com.example.controllers.MyController]))))
class MyConfig {
}
Run Code Online (Sandbox Code Playgroud)
这很好用(但非常冗长).但Spring的@ComponentScan也有basePackageClasses
@Configuration
@ComponentScan( basePackageClasses=Array(classOf[com.example.controllers.MyController]))
class MyConfig {
}
Run Code Online (Sandbox Code Playgroud)
在basePackageClasses中,Spring的文档说:
Type-safe alternative to basePackages() for specifying the packages to
scan for annotated components.
Run Code Online (Sandbox Code Playgroud)
但是,虽然第一个ComponentScan正确添加了com.example.controllers.MyController,但第二个导致我的所有@Controller都被扫描并添加!为什么?basePackageClasses有什么用?
示例如:https://github.com/mikaelhg/springmvc-example/blob/master/src/main/java/mikaelhg/example/ExampleConfiguration.java 建议basePackageClasses可用于加载单个组件.
更新:
另外,替换:
@Configuration
@ComponentScan(
basePackages = Array("com.example.controllers"),
useDefaultFilters = false,
includeFilters = Array(
new ComponentScan.Filter(`type` = FilterType.ASSIGNABLE_TYPE,
value = Array(classOf[com.example.controllers.MyController]))))
class MyConfig {
}
Run Code Online (Sandbox Code Playgroud)
同
@Configuration …Run Code Online (Sandbox Code Playgroud) 我可以运行(对于命令行)
myscript.py '--pm execute.shell(cmd="ls -l", nonInteractive=True)'
Run Code Online (Sandbox Code Playgroud)
但是,假设我要调试 myscript.py 并将“编辑配置 Python”对话框中的“脚本参数:”字段设置为:
--pm execute.shell(cmd="ls -l", nonInteractive=True)
Run Code Online (Sandbox Code Playgroud)
它在“ls”和“-l”之间的空格处呕吐。有没有办法解决这个问题?使用 ' 或 " 引号似乎不是这样做的:
--pm 'execute.shell(cmd="ls -l", nonInteractive=True)'
--pm "execute.shell(cmd='ls -l', nonInteractive=True)"
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,引号都以参数结尾。
更新:运行:
import sys
for x in sys.argv:
print "<%s>"%(x,)
Run Code Online (Sandbox Code Playgroud)
和
Script parameters: a \ b " c" "\ d" ' e' '\ f' "a 'b' \"c\""
Run Code Online (Sandbox Code Playgroud)
给出:
<cmd.py>
<a>
<\>
<b>
< c>
<\ d>
<'>
<e'>
<'\>
<f'>
<a 'b' "c">
Run Code Online (Sandbox Code Playgroud)
所以它看起来像:
所以,神奇的答案是:
Script parameters: …Run Code Online (Sandbox Code Playgroud) 我正在尝试解决两个python安装之间的行为差异,这些安装通常是确定性的并且安装了相同的python pip包.
我怀疑不同的.so文件.有没有什么可以看到哪些二进制二进制轮子pip已安装,以及哪些架构?
更新 -
% pip show
Name: scipy
Version: 1.0.0rc1
Summary: SciPy: Scientific Library for Python
Home-page: https://www.scipy.org
Author: SciPy Developers
Author-email: scipy-dev@python.org
License: BSD
Location: /usr/local/lib/python2.7/dist-packages
Requires: numpy
上面的包依赖于像libopenblas这样必须编译的东西.我不知道如果PIP所使用的系统安装,或编译的BLAS画中画在安装过程中,或用作BLAS的预编译版本的I386,或i686的 - 谁知道.
以上情况我有:
/usr/local/lib/python2.7/dist-packages/scipy/.libs/libopenblasp-r0-39a31c03.2.18.so
Run Code Online (Sandbox Code Playgroud)
我想看看哪个软件包在差异系统中的已安装.sos有差异.
作为疾病风险模型的一部分,我正在尝试实现论文(Python/numpy)中的计算,其中一部分是以下矩阵计算:
\n\n在哪里:
\n\n另外,我只需要获取 Q 的对角元素作为输出。
\n\n是否有一些 numpy 矩阵魔法可以让我有效地计算这个?
\n\n笔记:
\n\n\n\nQdiag <- lm.influence(lm(y ~ X-1, weights=W))$hat/W\nRun Code Online (Sandbox Code Playgroud)\n\nR\'s lm.influence$hat 文档表示这给出了“一个包含 \xe2\x80\x98hat\xe2\x80\x99 矩阵对角线的向量”。尽管维基百科的定义听起来有点像我想要的对帽子矩阵(==影响或投影矩阵)的定义看起来略有不同。
\n\n--
\n\n我认为以下是一个有效的(天真的)实现。对于大 n 来说内存不足
\n\nm = 3\nn = 20 # 500000 -- out of memory for large n\n\nnp.random.seed(123)\nX = np.random.random((n,m))\nW = np.random.random(n)\nW = np.diag(W)\nxtwx = …Run Code Online (Sandbox Code Playgroud) 有时候我只是讨厌使用中间件。以这个为例:我想拥有一个查找表,该表将一组输入(域)值中的值映射到输出(范围)值中。映射是唯一的。Python映射可以做到这一点,但是由于我认为该映射很大,所以为什么不使用ps.Series及其索引,它可以带来更多好处:
像这样:
domain2range = pd.Series(allrangevals, index=alldomainvals)
# Apply the map
query_vals = pd.Series(domainvals, index=someindex)
result = query_vals.map(domain2range)
assert result.index is someindex # Nice
assert (result.values in allrangevals).all() # Nice
Run Code Online (Sandbox Code Playgroud)
可以正常工作。但不是。上面的.map的时间成本len(domain2range)没有(更明智地)增加O(len(query_vals)),如下所示:
numiter = 100
for n in [10, 1000, 1000000, 10000000,]:
domain = np.arange(0, n)
range = domain+10
maptable = pd.Series(range, index=domain).sort_index()
query_vals = pd.Series([1,2,3])
def f():
query_vals.map(maptable)
print n, timeit.timeit(stmt=f, number=numiter)/numiter
10 0.000630810260773
1000 0.000978469848633
1000000 0.00130645036697
10000000 0.0162791204453
Run Code Online (Sandbox Code Playgroud)
脸庞。在n = 10000000时,每个映射值占用(0.01 / …