我有一个关于python的简单问题。ndarray我想选择整列,而不是遍历 3D 的第一行。所以让我们说:而不是:
print test[0][20][33]
print test[1][20][33]
...
Run Code Online (Sandbox Code Playgroud)
我想放一些类似的东西:
print test[:][20][33]
Run Code Online (Sandbox Code Playgroud)
但这不起作用。我该怎么做?
所以我想更好地学习python,我一直在使用这个网站http://www.learnpython.org/
我现在就开始工作了,继承了代码
#Add your functions here (before the existing functions)
def list_benefits():
myList = ['More organized code','More readable code','Easier code reuse','Allowing programmers to share and connect code together']
return myList
def build_sentence(info):
addMe = " is a benefit of functions!"
for i in info:
meInfo = i + addMe
return meInfo
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefit in list_of_benefits:
print build_sentence(benefit)
name_the_benefits_of_functions()
Run Code Online (Sandbox Code Playgroud)
输出是
e是功能的好处!
e是功能的好处!
e是功能的好处!
r是功能的好处!
我错过了什么才能归还整个场景
在一些成员的帮助下,我确实建立了一个在 Python 中运行的代码,并评估一个需要两个巨大的np.arrays输入的函数。
并行运行的矢量化版本仍然非常耗时,并且比用串行 Fortran 编写的参考程序慢约 50 倍......
我想使用 cython 循环,我可以使用 OpenMP 或 MPI 来并行化。C++ 中的想法是这样的:
#pragma omp parallel for
for (i=0;i<np1;i++){
for (i=0;i<np2;i++){
double dist = sph(coord1_particle1,coord1_particle2,coord2_particle1,coord2_particle2)
int bin=binning_function(dist)
hist_array[bin]++
}
}
Run Code Online (Sandbox Code Playgroud)
任何想法都是完全受欢迎的。这是 Python 版本:
#a is an array containing two coordinates of two objects
def dist_vec(a): # a like [[array1,array2,array2,array2],[],[]...]
return sph(a[0],a[1],a[2],a[3]) # sph operates on coordinates
def vec_chunk(array_ab, bins) :
dist = dist_vec(array_ab)
hist, _ = np.histogram(dist, bins=bins)
return hist
def mp_dist(array_a,array_b, d, bins): #d …Run Code Online (Sandbox Code Playgroud) 我不明白为什么将 float32-Array 转换为 float64-Array 会显着改变数组的平均值。
import numpy as n
a = n.float32(100. * n.random.random_sample((10000000))+1000.)
b = a.astype(n.float64)
print n.mean(a), a.dtype, a.shape
print n.mean(b), b.dtype, b.shape
Run Code Online (Sandbox Code Playgroud)
结果(应该是大约 1050,所以 float64 是正确的):
1028.346368 float32 (10000000,)
1049.98284473 float64 (10000000,)
Run Code Online (Sandbox Code Playgroud) 我有一些x,y数据,我使用scipy.stats.gaussian_kde函数获得高斯核密度估算器(KDE).我可以绘制这个图,以显示MWE下面显示的轮廓密度曲线.
这是MWE和结果图.
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
# Data.
x = [1.81,1.715,1.78,1.613,1.629,1.714,1.62,1.738,1.495,1.669,1.57,1.877,1.385,2.129,2.016,1.606,1.444,2.103,1.397,1.854,1.327,1.377,1.798,1.684,2.186,2.079,1.32,1.452,2.272,1.313,1.762,2.308,2.285,2.328,2.288,2.345,2.237,2.078,2.057,1.505,2.595,2.176,2.501,0.942,2.424,2.49,2.65,1.303,2.43,2.241,0.897,1.731,2.464,1.638,0.867,2.392,3.248,2.608,2.733,0.745,2.715,3.078,2.571,0.771,1.071,2.574,3.343,2.835,2.629,3.421,0.642,2.571,2.698,0.595,2.912,0.563,2.832,2.636,3.149,2.522,0.836,0.894,0.447,1.304,1.132,2.488,3.363,2.961,1.317,2.387,0.036,2.199,0.356,3.036,2.103,2.894,-0.097,0.069,2.688,-0.083,0.653,3.247,3.045,3.197,2.963,2.473,2.571,3.333,3.009,1.281,3.257,3.116,2.673,2.901,2.903,2.634,-0.291,-0.29,0.212]
y = [0.924,0.915,0.914,0.91,0.909,0.905,0.905,0.893,0.886,0.881,0.873,0.873,0.844,0.838,0.83,0.817,0.811,0.809,0.807,0.803,0.802,0.792,0.777,0.774,0.774,0.77,0.748,0.746,0.742,0.734,0.729,0.726,0.722,0.677,0.676,0.672,0.635,0.62,0.62,0.608,0.605,0.587,0.586,0.578,0.571,0.569,0.549,0.544,0.535,0.53,0.529,0.513,0.499,0.497,0.496,0.496,0.49,0.486,0.482,0.476,0.474,0.473,0.471,0.47,0.459,0.444,0.438,0.435,0.428,0.419,0.411,0.4,0.396,0.384,0.378,0.368,0.362,0.362,0.361,0.357,0.347,0.346,0.344,0.33,0.322,0.319,0.318,0.305,0.296,0.296,0.289,0.288,0.288,0.288,0.287,0.286,0.283,0.283,0.278,0.274,0.264,0.259,0.248,0.244,0.241,0.239,0.238,0.237,0.23,0.222,0.221,0.218,0.214,0.212,0.207,0.205,0.196,0.19,0.182]
xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)
# Generate KDE.
x1, y1 = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([x1.ravel(), y1.ravel()])
values = np.vstack([x, y])
kernel = stats.gaussian_kde(values)
kde = np.reshape(kernel(positions).T, x1.shape)
# Make plot.
plt.figure()
CS = plt.contour(x1,y1,kde)
plt.clabel(CS, inline=1, fontsize=10, zorder=6)
plt.show()
Run Code Online (Sandbox Code Playgroud)

我对这个输出的形状感兴趣.我特别需要的是获得每个密度曲线x,y的两个最远点的坐标的方法.例如,对于右下角的红色曲线,坐标将围绕:和(标记为黑色圆圈的点).0.7 …
我正在尝试制作一个等高线图,例如:

使用 txt 文件中的 3 列数据表,行数很长。
使用此代码:
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
data = np.loadtxt(r'dataa.txt')
a = [data[:,0]]
b = [data[:,1]]
n = [data[:,2]]
x = np.asarray(a)
y = np.asarray(b)
z = np.asarray(n)
print "x = ", x
print "y = ", y
print "z = ", z
fig=plt.figure()
CF = contour(x,y,z,colors = 'k')
plt.xlabel("X")
plt.ylabel("Y")
plt.colorbar()
plt.show()
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,它不起作用。Python 为我提供了我期望看到的值的正确轴,但图中只是一个空白,我知道它正在以正确的方式导入数据,因为它在绘图之前向我显示了我的值。
表格示例:(差异是因为我的表格有 90000 行)

使用此代码:
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
N …Run Code Online (Sandbox Code Playgroud) 我有一本字典,我想将该字典中的所有键更改为另一个字典中的值.
例如:
X = {"apple" : 42}
Y = {"apple" : "Apples"}
Run Code Online (Sandbox Code Playgroud)
转换后:
快译通 X = {"Apples" : 42}
def convert(items, ID):
for key, value in items.items():
for keys, values in ID.items():
if keys == key:
key = values
return items
Run Code Online (Sandbox Code Playgroud)
所以我已经编写了上面的代码来执行此操作,但是在执行此函数后,我打印字典并且键没有更改.
假设我有以下numpy结构化数组:
>>> a = numpy.array((1, 2.0, 'buckle_my_shoe'),dtype=('i4,f8,a14'))
array((1, 2.0, 'buckle_my_shoe'),
dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', 'S14')])
Run Code Online (Sandbox Code Playgroud)
我想将它保存到文本文件中的单个空格或制表符分隔的行.如果阵列都是我可以使用的相同类型numpy.savetxt('myfile.dat,myarray,newline=" ").但是,这似乎不像混合数据类型/结构化数组,例如:
file('myfile.dat', 'a')
numpy.savetxt('myfile.dat',a,newline=" ")
Run Code Online (Sandbox Code Playgroud)
给出了这个错误:
IndexError: tuple index out of range
Run Code Online (Sandbox Code Playgroud)
任何人都可以推荐一种方法吗?
我有以下数据:
array([[33, 250, 196, 136, 32],
[55, 293, 190, 71, 13]])
Run Code Online (Sandbox Code Playgroud)
我可以从中得到p值stats.chi2_contingency(data).
有没有类似于这个R对象 - data.chisq$residuals获得Pearson的残差和标准化残差?
我有一个关于如何Python函数问题的作品.我有一个我写的程序,涉及生成几个数字的素数因子分解.
for a in range(2,25):
print primefactorize(a)
Run Code Online (Sandbox Code Playgroud)
当我在上面的范围内测试它时,终端中弹出的最后一个列表是24的主要因子分解输出,而不是25.
for a in range(1,x):
Run Code Online (Sandbox Code Playgroud)
只运行每个值到x-1?
谢谢