我想在共享内存中使用numpy数组与多处理模块一起使用.困难是使用它像一个numpy数组,而不仅仅是一个ctypes数组.
from multiprocessing import Process, Array
import scipy
def f(a):
a[0] = -a[0]
if __name__ == '__main__':
# Create the array
N = int(10)
unshared_arr = scipy.rand(N)
arr = Array('d', unshared_arr)
print "Originally, the first two elements of arr = %s"%(arr[:2])
# Create, start, and finish the child processes
p = Process(target=f, args=(arr,))
p.start()
p.join()
# Printing out the changed values
print "Now, the first two elements of arr = %s"%arr[:2]
Run Code Online (Sandbox Code Playgroud)
这会产生如下输出:
Originally, the first two elements of arr = …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main() {
int t;
scanf("%d", &t);
printf("%d", t);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用ideone.com编译了上面的C代码,并弹出以下警告:
prog.c:在函数'main'中:
prog.c:5:警告:忽略'scanf'的返回值,使用属性warn_unused_result声明
有人能帮我理解这个警告吗?
我有一个用Python和Haskell编写的简单脚本.它读取一个包含1,000,000个换行符分隔整数的文件,将该文件解析为整数列表,对其进行快速排序,然后将其写入已排序的其他文件.此文件的格式与未排序的文件相同.简单.
这是Haskell:
quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
main = do
file <- readFile "data"
let un = lines file
let f = map (\x -> read x::Int ) un
let done = quicksort f
writeFile "sorted" (unlines (map show done))
Run Code Online (Sandbox Code Playgroud)
这是Python:
def qs(ar):
if len(ar) == 0:
return ar
p …Run Code Online (Sandbox Code Playgroud) 在这里研究一些矩阵代数.有时我需要反转可能是单数或病态的矩阵.我知道简单地执行此操作是pythonic:
try:
i = linalg.inv(x)
except LinAlgErr as err:
#handle it
Run Code Online (Sandbox Code Playgroud)
但我不确定它的效率如何.这会不会更好?
if linalg.cond(x) < 1/sys.float_info.epsilon:
i = linalg.inv(x)
else:
#handle it
Run Code Online (Sandbox Code Playgroud)
numpy.linalg只是简单地执行我禁止的测试吗?
我有一组看起来像这样的数据:
Table-1
X1 | Y1
------+--------
0.1 | 0.52147
0.02 | 0.8879
0.08 | 0.901
0.11 | 1.55
0.15 | 1.82
0.152 | 1.95
Table-2
X2 | Y2
-----+------
0.2 | 0.11
0.21 | 0.112
0.34 | 0.120
0.33 | 1.121
Run Code Online (Sandbox Code Playgroud)
我必须为Y2Table-2中的X1值插入表1中的值,即,我需要找到Y2以下值的值X:
X1 | Y2
-------+-------
0.1 |
0.02 |
0.08 |
0.11 |
0.15 |
0.152 |
Run Code Online (Sandbox Code Playgroud)
注意:表-1和表2的间隔不等.(X,Y)条目的数量将不同,例如,这里我们在表-1中具有6个(X1,Y1)条目并且在表-2中仅具有4个(X2,Y2).
我应该在Numpy中使用哪种插值算法,如何继续?
我正在尝试实现此算法以查找单个变量的截距和斜率:
这是我更新拦截和斜率的Python代码.但它并没有趋同.RSS随着迭代而不是减少而增加,并且在一些迭代之后它变得无限.我没有发现任何实现算法的错误.我怎么能解决这个问题?我也附上了csv文件.这是代码.
import pandas as pd
import numpy as np
#Defining gradient_decend
#This Function takes X value, Y value and vector of w0(intercept),w1(slope)
#INPUT FEATURES=X(sq.feet of house size)
#TARGET VALUE=Y (Price of House)
#W=np.array([w0,w1]).reshape(2,1)
#W=[w0,
# w1]
def gradient_decend(X,Y,W):
intercept=W[0][0]
slope=W[1][0]
#Here i will get a list
#list is like this
#gd=[sum(predicted_value-(intercept+slope*x)),
# sum(predicted_value-(intercept+slope*x)*x)]
gd=[sum(y-(intercept+slope*x) for x,y in zip(X,Y)),
sum(((y-(intercept+slope*x))*x) for x,y in zip(X,Y))]
return np.array(gd).reshape(2,1)
#Defining Resudual sum of squares
def RSS(X,Y,W):
return sum((y-(W[0][0]+W[1][0]*x))**2 for x,y in zip(X,Y))
#Reading …Run Code Online (Sandbox Code Playgroud) python numpy machine-learning linear-regression gradient-descent
立即更新到新发布的ipython5.启动交互式提示并收到:
/usr/local/lib/python3.5/site-packages/IPython/core/interactiveshell.py:440: UserWarning: As of IPython 5.0 `PromptManager` config will have no effect and has been replaced by TerminalInteractiveShell.prompts_class
warn('As of IPython 5.0 `PromptManager` config will have no effect'
Run Code Online (Sandbox Code Playgroud)
拉出我的旧配置设置来自定义和着色提示,然后寻找新的方式来自定义提示并找到它,非常酷.使用示例代码中的新类样式:
class MyPrompt(Prompts):
def in_prompt_tokens(self, cli=None):
return [(Token, os.getcwd()),
(Token.Prompt, ' >>>')]
Run Code Online (Sandbox Code Playgroud)
把它放到一个启动脚本中它运行得很好,除了它默认不着色令牌行,Token.Prompt变成浅绿色.
尝试使用旧的配置方法颜色(r'{color.Green}')但这在这里不起作用.任何正确方向的指针都会很棒.
谢谢!
我试图绘制具有边界条件的一维空间扩展系统的分叉图
x[i,n+1] = (1-eps)*(r*x[i,n]*(1-x[i,n])) + 0.5*eps*( r*x[i-1,n]*(1-x[i-1,n]) + r*x[i+1,n]*(1-x[i+1,n])) + p
Run Code Online (Sandbox Code Playgroud)
我正在面临获得所需输出数字的问题可能是因为我正在使用的瞬态数量.有人可以通过交叉检查我的代码来帮助我,我应该选择nTransients的值或者我应该忽略多少个瞬态?
我的Python代码如下:
import numpy as np
from numpy import *
from pylab import *
L = 60 # no. of lattice sites
eps = 0.6 # diffusive coupling strength
r = 4.0 # control parameter r
np.random.seed(1010)
ic = np.random.uniform(0.1, 0.9, L) # random initial condition betn. (0,1)
nTransients = 900 # The iterates we'll throw away
nIterates = 1000 # This sets how much the attractor is filled …Run Code Online (Sandbox Code Playgroud) 你如何排序,操作,然后取消结果?
假设我有一个浮点数组p1 = 0.15,0.3, 0.25, 0.12, ....它分类为:p2 = sort(p1).函数(p2作为输入的操作)导致p3:p3 = f(p2, x, y, ...)对于某些函数f.
我怎样才能p3以最聪明的方式解开?(反过来如何p1排序)
ie: p4 = unsort(p3)< - p4未分类到相同的顺序p1,用于比较(x-plot)p1?
我有两个点向量x和y,形状分别为(n, p)和(m, p)。举个例子:
x = np.array([[ 0. , -0.16341, 0.98656],
[-0.05937, -0.25205, 0.96589],
[ 0.05937, -0.25205, 0.96589],
[-0.11608, -0.33488, 0.93508],
[ 0. , -0.33416, 0.94252]])
y = np.array([[ 0. , -0.36836, 0.92968],
[-0.12103, -0.54558, 0.82928],
[ 0.12103, -0.54558, 0.82928]])
Run Code Online (Sandbox Code Playgroud)
我想计算一个(n, m)大小的矩阵,其中包含两点之间的角度,就像这个问题一样。即,矢量化版本:
theta = np.array(
[ np.arccos(np.dot(i, j) / (la.norm(i) * la.norm(j)))
for i in x for j in y ]
).reshape((n, m))
Run Code Online (Sandbox Code Playgroud)
注意:每个n和m的数量级可以约为 10000。
python ×9
numpy ×7
arrays ×1
c ×1
distance ×1
haskell ×1
ipython ×1
matplotlib ×1
python-2.7 ×1
python-3.x ×1
quicksort ×1
scipy ×1
shared ×1
sorting ×1