我正在尝试加载存储在CSV头文件位于第一列的文件中的一些数据.我正在使用numpy.loadtxt(v1.6).
我想知道是否有办法加载除第一列以外的所有列?
我知道可以选择我们要加载的列.问题是,我不知道有多少列(我只知道有超过255列).
如果有人有诀窍,那就太好了!
假设我有以下2D NumPy由四行三列组成的数组:
>>> a = numpy.array([[True, False],[False, False], [True, False]])
>>> array([[ True, False],
[False, False],
[ True, False]], dtype=bool)
Run Code Online (Sandbox Code Playgroud)
什么是生成1D包含逻辑或所有列(如[True, False])的数组的有效方法?
我在网上搜索,发现有人指的sum(axis=)是计算sum.
我想知道逻辑运算是否有类似的方法?
假设我有以下两个numpy数组:
In [251]: m=np.array([[1,4],[2,5],[3,6]])
In [252]: m
Out[252]:
array([[1, 4],
[2, 5],
[3, 6]])
In [253]: c= np.array([200,400])
In [254]: c
Out[254]: array([200, 400])
Run Code Online (Sandbox Code Playgroud)
我想一步得到以下数组,但对于我的生活,我无法弄清楚:
In [252]: k
Out[252]:
array([[200, 800, 400, 1600],
[400, 1000, 800, 2000],
[600, 1200, 1200,2400]])
Run Code Online (Sandbox Code Playgroud) 我目前正在学习Flutter开发(以及Dart)。这是我的第一种移动编程语言。
我很好奇-是否可以将屏幕拆分为.dart文件,或者它们都必须位于同一main.dart文件中?
我正在构建的应用程序将有很多页面,所以我不想制作一个包含20多个不同页面的巨型main.dart文件。
如果我试图制作一个大型应用程序,Flutter是正确的选择吗?
我有许多名称以前缀开头的变量indoor.接下来的indoor不是数字(这会使一切变得更简单).
我想为每个变量制作一个表格.
我的代码如下:
local indoor indoor*
foreach i of local indoor {
tab `i' group, col freq exact chi2
}
Run Code Online (Sandbox Code Playgroud)
问题是indoor在foreach命令中解析indoor*而不是indoor问题列表,正如我所希望的那样.出于这个原因,该tab命令后面跟着太多变量(它只能处理两个),这会导致错误.
简单的解决方法是用第一个命令替换:
local indoor <full list of indoor questions>
Run Code Online (Sandbox Code Playgroud)
但这是我想要避免的,那就是必须找到这些变量的所有名称,然后将它们粘贴到代码中.似乎有一个更快的解决方案,但我想不出任何.
这个问题是关于NumPy ndarray根据一些列值过滤a .
我有一个相当大NumPy ndarray(300000,50),我根据某些特定列中的值过滤它.我有,ndtypes所以我可以按名称访问每一列.
第一列被命名为category_code和我需要过滤矩阵只返回中行category_code是("A", "B", "C").
结果将需要是另一个名称NumPy ndarray仍可访问其列的列dtype.
这是我现在所做的:
index = numpy.asarray([row['category_code'] in ('A', 'B', 'C') for row in data])
filtered_data = data[index]
Run Code Online (Sandbox Code Playgroud)
列表理解如:
list = [row for row in data if row['category_code'] in ('A', 'B', 'C')]
filtered_data = numpy.asarray(list)
Run Code Online (Sandbox Code Playgroud)
不行,因为dtypes我原来不再可以访问.
是否有更好/更多的Pythonic方法来实现相同的结果?
可能看起来像的东西:
filtered_data = data.where({'category_code': ('A', 'B','C'})
Run Code Online (Sandbox Code Playgroud)
谢谢!
我是一个Stata用户,试图利用Stata的Java API。我想使用Java将文件的最后修改时间返回给Stata。但是,我认为我遇到了时区问题。
非Stata用户的Stata中的时间快速背景:时间由以来的毫秒数表示January 1, 1960 00:00:00.000。Stata没有时区,因此我希望Java函数的返回值是January 1, 1960我所在时区(美国/纽约)午夜以来的毫秒数。
这是我对Java函数的尝试(如果有帮助,我可以将其完整发布):
import com.stata.sfi.*;
// Other imports
public class SFileAttribs
{
// Syntax: lastModified(String filename, String scalar_name)
// Stores in the Stata scalar scalar_name the time of last
// modification of filename.
public static int lastModified(String[] args)
{
// Code here...
// Get the time.
FileTime time;
try {
time = (FileTime) Files.getAttribute(path, "lastModifiedTime");
}
catch (IOException e) {
SFIToolkit.errorln("file I/O error on read");
return(692);
/*NOTREACHED*/ …Run Code Online (Sandbox Code Playgroud) 我有数百个像这样的文本文件,每列用三个空格分隔.数据为一年:每月12个月和31天.
下面,我只是在下面显示与问题相关的内容:
001 DIST - ADILABAD ANDHRA MEAN TEMP
DATE JAN FEB MAR . . . . NOV DEC
01 21.5 24.3 27.1 25.8 22.4
02 21.4 24.2 27.1 25.8 22.4
. . . . . .
. . . . . .
. . . . . .
27 23.6 26.8 30.3 23.1 21.3
28 23.8 27.0 30.6 22.9 21.3
29 23.4 31.0 22.9 21.2
30 23.5 31.1 22.6 21.4
31 23.8 31.2 . . . . 21.6
Run Code Online (Sandbox Code Playgroud)
我想将每列读入一个数组,然后对其进行平均. …
当我column_stack用来连接NumPy数组时,dtype转换为:
a = numpy.array([1., 2., 3.], dtype=numpy.float64)
b = numpy.array([1, 2, 3], dtype=numpy.int64)
print numpy.column_stack((a, b)).dtype
>>> float64
Run Code Online (Sandbox Code Playgroud)
有没有办法保留dtype各列?
假设我有以下两个带有均值和标准偏差的数组:
mu = np.array([2000, 3000, 5000, 1000])
sigma = np.array([250, 152, 397, 180])
Run Code Online (Sandbox Code Playgroud)
然后:
a = np.random.normal(mu, sigma)
In [1]: a
Out[1]: array([1715.6903716 , 3028.54168667, 4731.34048645, 933.18903575])
Run Code Online (Sandbox Code Playgroud)
但是,如果我要求亩的每个元素100抽奖,西格玛:
a = np.random.normal(mu, sigma, 100)
a = np.random.normal(mu, sigma, 100)
Traceback (most recent call last):
File "<ipython-input-417-4aadd7d15875>", line 1, in <module>
a = np.random.normal(mu, sigma, 100)
File "mtrand.pyx", line 1652, in mtrand.RandomState.normal
File "mtrand.pyx", line 265, in mtrand.cont2_array
ValueError: shape mismatch: objects cannot be broadcast to a single shape
Run Code Online (Sandbox Code Playgroud)
我也尝试使用大小的元组:
s = …Run Code Online (Sandbox Code Playgroud) numpy ×7
python ×7
python-3.x ×2
stata ×2
arrays ×1
flutter ×1
genfromtxt ×1
java ×1
loops ×1
matrix ×1
stata-macros ×1