我想打印一个没有截断的numpy数组.我见过其他解决方案,但那些似乎不起作用.
这是代码片段:
total_list = np.array(total_list)
np.set_printoptions(threshold=np.inf)
print(total_list)
Run Code Online (Sandbox Code Playgroud)
这就是输出的样子:
22 A
23 G
24 C
25 T
26 A
27 A
28 A
29 G
..
232272 G
232273 T
232274 G
232275 C
232276 T
232277 C
232278 G
232279 T
Run Code Online (Sandbox Code Playgroud)
这是整个代码.我可能在打字时犯了一个错误.
import csv
import pandas as pd
import numpy as np
seqs = pd.read_csv('BAP_GBS_BTXv2_imp801.hmp.csv')
plts = pd.read_csv('BAP16_PlotPlan.csv')
required_rows = np.array([7,11,14,19,22,31,35,47,50,55,58,63,66,72,74,79,82,87,90,93,99])
total_list = []
for i in range(len(required_rows)):
curr_row = required_rows[i];
print(curr_row)
for j in range(len(plts.RW)):
if(curr_row == plts.RW[j]):
curr_plt …Run Code Online (Sandbox Code Playgroud) 我在保存到磁盘时尝试了各种方法进行数据压缩numpy arrays.
这些一维阵列包含一定采样率的采样数据(可以使用麦克风录制声音,或使用任何传感器进行任何其他测量):数据基本上是连续的(在数学意义上;当然,采样后它现在是离散数据) .
我试过HDF5(h5py):
f.create_dataset("myarray1", myarray, compression="gzip", compression_opts=9)
Run Code Online (Sandbox Code Playgroud)
但这很慢,压缩比不是我们所能期望的最好.
我也尝试过
numpy.savez_compressed()
Run Code Online (Sandbox Code Playgroud)
但是再一次,它可能不是这种数据的最佳压缩算法(如前所述).
对于numpy array这样的数据,你会选择什么来获得更好的压缩比?
(我考虑过无损FLAC(最初是为音频而设计),但是有一种简单的方法可以在numpy数据上应用这样的算法吗?)
我想用Python进行卡方检验.我已经创建了代码来执行此操作,但我不知道我所做的是否正确,因为scipy docs非常稀疏.
背景优先:我有两组用户.我的零假设是,两组中的人是否更有可能使用桌面设备,移动设备或平板电脑没有显着差异.
这些是两组中观察到的频率:
[[u'desktop', 14452], [u'mobile', 4073], [u'tablet', 4287]]
[[u'desktop', 30864], [u'mobile', 11439], [u'tablet', 9887]]
Run Code Online (Sandbox Code Playgroud)
这是我的代码使用scipy.stats.chi2_contingency:
obs = np.array([[14452, 4073, 4287], [30864, 11439, 9887]])
chi2, p, dof, expected = stats.chi2_contingency(obs)
print p
Run Code Online (Sandbox Code Playgroud)
这给了我一个p值2.02258737401e-38,这显然是重要的.
我的问题是:这段代码看起来有效吗?特别是,我不确定我是否应该使用,scipy.stats.chi2_contingency或者scipy.stats.chisquare根据我的数据.
对于我的神经网络,我想通过向我的图像添加小的随机旋转和缩放来增加我的训练数据.我遇到的问题是scipy在应用旋转和缩放时会改变图像的大小.如果图像的一部分超出边界,我需要剪切边缘.我的所有图像都必须大小相同.
def loadImageData(img, distort = False):
c, fn = img
img = scipy.ndimage.imread(fn, True)
if distort:
img = scipy.ndimage.zoom(img, 1 + 0.05 * rnd(), mode = 'constant')
img = scipy.ndimage.rotate(img, 10 * rnd(), mode = 'constant')
print(img.shape)
img = img - np.min(img)
img = img / np.max(img)
img = np.reshape(img, (1, *img.shape))
y = np.zeros(ncats)
y[c] = 1
return (img, y)
Run Code Online (Sandbox Code Playgroud) 这是我用来检查 convolve2d 正确性的代码
import numpy as np
from scipy.signal import convolve2d
X = np.random.randint(5, size=(10,10))
K = np.random.randint(5, size=(3,3))
print "Input's top-left corner:"
print X[:3,:3]
print 'Kernel:'
print K
print 'Hardcording the calculation of a valid convolution (top-left)'
print (X[:3,:3]*K)
print 'Sums to'
print (X[:3,:3]*K).sum()
print 'However the top-left value of the convolve2d result'
Y = convolve2d(X, K, 'valid')
print Y[0,0]
Run Code Online (Sandbox Code Playgroud)
在我的电脑上,结果如下:
Input's top-left (3x3) corner:
[[0 0 0]
[1 1 2]
[1 3 0]]
Kernel:
[[4 1 1]
[0 …Run Code Online (Sandbox Code Playgroud) 我有两个csr_matrix,uniFeature而且biFeature.
我想要一个新的矩阵Feature = [uniFeature, biFeature].但是,如果我以这种方式直接连接它们,则会出现一个错误,表明矩阵Feature是一个列表.如何实现矩阵连接并仍然获得相同类型的矩阵,即csr_matrix?
如果我在连接后执行此操作它不起作用:Feature = csr_matrix(Feature)
它给出错误:
Traceback (most recent call last):
File "yelpfilter.py", line 91, in <module>
Feature = csr_matrix(Feature)
File "c:\python27\lib\site-packages\scipy\sparse\compressed.py", line 66, in __init__
self._set_self( self.__class__(coo_matrix(arg1, dtype=dtype)) )
File "c:\python27\lib\site-packages\scipy\sparse\coo.py", line 185, in __init__
self.row, self.col = M.nonzero()
TypeError: __nonzero__ should return bool or int, returned numpy.bool_
Run Code Online (Sandbox Code Playgroud) 据我所知,这些方法都在各自的DLL中实现为C函数,并且看起来ndimage版本更快(两种实现都不使用并行化代码,如调用blas或MKL).
此外,当我尝试通过运行以下代码检查它们返回相同的结果时,相等的断言失败.我无法从文档中弄清楚这两种方法之间究竟存在什么功能差异(文档也不是很清楚0相对于内核原点位置的含义;从示例中,我推断它在中心,但我可能错了).
from numpy import random, allclose
from scipy.ndimage.filters import convolve as convolveim
from scipy.signal import convolve as convolvesig
a = random.random((100, 100, 100))
b = random.random((10,10,10))
conv1 = convolveim(a,b, mode = 'constant')
conv2 = convolvesig(a,b, mode = 'same')
assert(allclose(conv1,conv2))
Run Code Online (Sandbox Code Playgroud)
谢谢!
美好的一天,我正在为一些数字工作编写一个Python模块.由于有很多事情要发生,我过去几天一直在优化代码以改善计算时间.但是,我有一个关于Numba的问题.基本上,我有一个类,其中一些字段是numpy数组,我按以下方式初始化:
def init(self):
a = numpy.arange(0, self.max_i, 1)
self.vibr_energy = self.calculate_vibr_energy(a)
def calculate_vibr_energy(i):
return numpy.exp(-self.harmonic * i - self.anharmonic * (i ** 2))
Run Code Online (Sandbox Code Playgroud)
因此,代码是矢量化的,使用Numba的JIT会带来一些改进.但是,有时我需要从类外部访问calculate_vibr_energy函数,并传递单个整数而不是数组来代替i.据我所知,如果我在calculate_vibr_energy上使用Numba的JIT,它必须始终将数组作为参数.
那么,以下哪个选项更好:1)创建一个新函数calculate_vibr_energy_single(i),它只取一个整数,并在其上使用Numba 2)替换与此类似的函数的所有用法:
myclass.calculate_vibr_energy(1)
Run Code Online (Sandbox Code Playgroud)
有了这个:
tmp = np.array([1])
myclass.calculate_vibr_energy(tmp)[0]
Run Code Online (Sandbox Code Playgroud)
或者还有其他更有效(或至少更多的Python-ic)方法吗?
我有一个 Dicom 文件,我想在体素中应用多个具有不同操作的掩模。但是,我想保留初始数组中的位置,因为我想重建图像。这就是我所做的:
#The dicom data
a=dicomm_data.pixel_array
#start function
my_modified_array = np.zeros_like(a)
#mask 1
m1=a[(a>limit_1) & (a<limit_2)]
m1=m1*chosen_number1 + chose_number2
my_modified_array [(a>limit_1) & (a<limit_2)] = m1
#mask 2
m2=a[(a>limit_2+1) & (a<limit_3)]
m2=m1*chosen_number3+chosen_number_4
my_modified_array [(a>limit_2+1) & (a<limit_3)] = m2
Run Code Online (Sandbox Code Playgroud)
我继续戴更多面具。但是,我收到标题中提到的错误。我读过另一篇文章,它使用 numpy.where 函数,但仍然无法修复它。
我正在尝试确定一个时间序列(如一个浮点数列表)是否与自身相关.我已经acf在statsmodels中使用了这个函数(http://statsmodels.sourceforge.net/devel/generated/statsmodels.tsa.stattools.acf.html),现在我正在研究Durbin-Watson统计数据有任何价值.
看起来这种事情应该有效:
from statsmodels.regression.linear_model import OLS
import numpy as np
data = np.arange(100) # this should be highly correlated
ols_res = OLS(data)
dw_res = np.sum(np.diff(ols_res.resid.values))
Run Code Online (Sandbox Code Playgroud)
如果你要运行它,你会得到:
Traceback (most recent call last):
...
File "/usr/lib/pymodules/python2.7/statsmodels/regression/linear_model.py", line 165, in initialize
self.nobs = float(self.wexog.shape[0])
AttributeError: 'NoneType' object has no attribute 'shape'
Run Code Online (Sandbox Code Playgroud)
似乎D/W通常用于比较两个时间序列(例如http://connor-johnson.com/2014/02/18/linear-regression-with-python/)的相关性,所以我认为问题是因为我没有通过另一个时间序列来比较.也许这应该在exog参数中传递给OLS?
exog : array-like
A nobs x k array where nobs is the number of observations and k is
the …Run Code Online (Sandbox Code Playgroud) python ×10
numpy ×8
scipy ×5
arrays ×3
convolution ×2
chi-squared ×1
compression ×1
correlation ×1
debugging ×1
image ×1
matrix ×1
numba ×1
pandas ×1
statistics ×1
statsmodels ×1