numpy

“The truth value of an array with more than one element is ambiguous” in python

非 Y 不嫁゛ 提交于 2021-02-11 12:33:39
问题 this is how I got the two arrays (array 1 and array2) for my function: x = np.arange(-5, 5,0.01) prob=stats.norm.pdf(x,0,1) prob_array=numpy.array(prob).reshape(1000,1) #array1 x_tran=m.transpose() x_tran_array=array(x_tran) mu_array=array(mu) # mu is stock return mu_array1=numpy.array(mu_array).reshape(54966,1) sigma_array=array(sigma) #sigma is the historical volatility sigma_array1=numpy.array(sigma_array).reshape(54966,1) mu1_mat=mat(ones((1,1000))) #for matrix calculation original_x=mu

Python/PyTables: Is it possible to have different data types for different columns of an array?

耗尽温柔 提交于 2021-02-11 12:33:29
问题 I create an expandable earray of Nx4 columns. Some columns require float64 datatype, the others can be managed with int32. Is it possible to vary the data types among the columns? Right now I just use one (float64, below) for all, but it takes huge disk space for (>10 GB) files. For example, how can I ensure column 1-2 elements are int32 and 3-4 elements are float64 ? import tables f1 = tables.open_file("table.h5", "w") a = f1.create_earray(f1.root, "dataset_1", atom=tables.Float32Atom(),

how to select some lines created by points based on their distances in python

纵饮孤独 提交于 2021-02-11 12:30:14
问题 I have some lines created by connecting points of a regular grid and want to pair the correct lines to create surfces. This is coordinates of my point array: coord=np.array([[0.,0.,2.], [0.,1.,3.], [0.,2.,2.], [1.,0.,1.], [1.,1.,3.],\ [1.,2.,1.], [2.,0.,1.], [2.,1.,1.], [3.,0.,1.], [4.,0.,1.]]) Then, I created lines by connecting points. My points are from a regular grid. So, I have two perpendicular sets of lines. I called them blue (vertical) and red (horizontal) lines. To do so: blue_line=

Python 3 numpy.load() until End of File

孤人 提交于 2021-02-11 12:27:10
问题 Suppose I'm generating a random number of arrays that I need to serialize def generator(): num = 0 while num < random.randint(0, 10): yield np.array(range(2)) num += 1 with open('out.npy', 'wb') as f: for item in generator(): np.save(f, item) Now how do I know exactly how many times I have to np.load() to get all the arrays back? np.load() will eventually throw an exception so I came up with with open('out.npy', 'rb') as f: try: while 1: item = np.load(f) print(item) except: print("EoF") but

numpy concatenate over dimension

☆樱花仙子☆ 提交于 2021-02-11 12:21:30
问题 I find myself doing the following quite frequently and am wondering if there's a "canonical" way of doing it. I have an ndarray say shape = (100, 4, 6) and I want to reduce to (100, 24) by concatenating the 4 vectors of length 6 into one vector I can use reshape to do this but I've been manually computing the new shape i.e. np.reshape(x,shape=(a.shape[0],a.shape[1]*a.shape[2])) ideally I'd simply supply the dimension I want to reduce on np.concatenate(x,dim=-1) but np.concatenate operates on

“ImportError: DLL load failed: The specified module could not be found” when trying to import gensim

风流意气都作罢 提交于 2021-02-11 12:20:19
问题 While trying to import gensim, I run into the following error Traceback (most recent call last): File "c:\Users\usr\Documents\hello\test.py", line 3, in <module> import gensim File "C:\Users\usr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\gensim\__init__.py", line 5, in <module> from gensim import parsing, corpora, matutils, interfaces, models, similarities, summarization, utils # noqa:F401 File "C:\Users\usr

How do I build a TFmodel from NumPy array files?

家住魔仙堡 提交于 2021-02-11 12:19:54
问题 I have a dir with NumPy array files: bias1.npy, kernel1.npy, bias2.npy, kernel2.npy . How can I build a TF model that uses those arrays as kernels and biases of layers? 回答1: To avoid confusion bias matrix for the consistency of the numpy file is the 2D matrix with one column. This post shows how did I reproduce tf's model based on the numpy weights and biases. class NumpyInitializer(tf.keras.initializers.Initializer): # custom class converting numpy arrays to tf's initializers # used to

Python C API crashes on 'import numpy' when initilizing multiple times

好久不见. 提交于 2021-02-11 12:13:30
问题 While working with the Python C API, I found that the python interpreter crashes when initializing it a second time and executing import numpy after each initilization. Any other command (e.g. import time ) will do just fine. #include <Python.h> int main(int argc, char ** argv) { while(1){ printf("Initializing python interpreter...\n"); Py_Initialize(); if(PyRun_SimpleString("import numpy")) { exit(1); } printf("Finalizing python interpreter...\n"); Py_Finalize(); } return 0; } The above

TypeError: data must be list or dict-like in CUDF

好久不见. 提交于 2021-02-11 12:03:47
问题 I am implementing CUDF to speed up my python process. Firstly, I import CUDF and removed multiprocessing code, and initialize variables with CUDF. After changing into CUDF it gives a dictionary error. How I can remove these loops to make effective implementation? Code import more_itertools import pandas as pd import numpy as np import itertools from os import cpu_count from sklearn.metrics import confusion_matrix, accuracy_score, roc_curve, auc import matplotlib.pyplot as plt import json

用 Pyqt5 制作一个炫酷水波进度条

时光怂恿深爱的人放手 提交于 2021-02-11 11:39:08
提出需求 最近做了一个小项目,里面有一个需求需要添加一个动态进度条,进度条的样式就类似于水波来回起伏的那种形状,下面就是最初的展示效果(有一点区别,这里我加了一个进度自动增加的功能): 下面先说一下这个效果的制作原理 原理介绍 在介绍动态效果之前需要先看一下静态的: 如果仔细观察的话,静态图效果的呈现在于先后的两个线条的绘制,产生水波的主要是由于两线条的 左右的水平错位 以及 设置的透明度不同 所造成的; 想要形成最后的水波荡漾的视觉效果,只需要把数张线条连续走向的静态图拼接在一起就能达到 线条中的波浪效果,这里用到的是 正弦函数 的走向,教程代码中的两根线条主要借助于 y = Asin(wx+l)+k 函数 绘制 y = Asin(wx+l)+k 函数中,各参数的作用如下图所示 从图中我们可以了解到,两个线条就能实现交叉起伏的效果, 是因为两正弦函数中设置的 l 值不一样 , 基本原理讲解完了,下面就是代码实现部分,图像呈现效果是由 Qt 中的 QPellete(画板) 实现,图像中的线条区域主要用到了 QPainterPath() 函数,函数可以借助绘制的线条围城一个区域 动态图效果的实现主要是在代码中加入了一个 startTimer() 函数,起到计时器效果:每隔一定的时间就会更新一次界面(所得到的静态图也不一样,这个静态图的视觉差实现动态效果);