LUT = np.genfromtxt('test.out', delimiter=',', dtype=float)
LUT:
12, 25, 136, 6743
13, 26, 139, 6786
14, 27, 142, 6791
15, 28, 145, 6789
Run Code Online (Sandbox Code Playgroud)
要从LUT读取的值如下:
x1, x2, x3 = 12.5, 25.5, 137
Run Code Online (Sandbox Code Playgroud)
对于每个给定值(3列)读取LUT中的相邻两个值,我必须对结果进行线性插值(LUT中的第4列).
给定值(x1,x2,x3)属于LUT的第1行和第2行之间.基于此如何读取第1行和第2行之间的结果?
我有一个2-d numpy数组如下:
a = np.array([[1,5,9,13],
[2,6,10,14],
[3,7,11,15],
[4,8,12,16]]
Run Code Online (Sandbox Code Playgroud)
我想把它提取成2个2个大小的补丁而不重复这些元素.
答案应完全相同.这可以是三维数组或列表,具有相同的元素顺序如下:
[[[1,5],
[2,6]],
[[3,7],
[4,8]],
[[9,13],
[10,14]],
[[11,15],
[12,16]]]
Run Code Online (Sandbox Code Playgroud)
怎么能轻松做到?
在我的实际问题中,a的大小是(36,72).我不能一个接一个地做.我想要以编程方式完成它.
我有1D NumPy数组如下:
import numpy as np
d = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
Run Code Online (Sandbox Code Playgroud)
我想计算(1,2,6,7),(3,4,8,9)等的平均值.这涉及4个元素的平均值:两个连续元素和两个连续元素后面的5个位置.
我尝试了以下方法:
>> import scipy.ndimage.filters as filt
>> res = filt.uniform_filter(d,size=4)
>> print res
[ 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
Run Code Online (Sandbox Code Playgroud)
遗憾的是,这并没有给我预期的结果.我该怎么做?
我正在寻找NumPy计算两个numpy数组(x和y)之间的Mahalanobis距离的方法.以下代码可以使用Scipy的cdist函数正确计算相同的代码.由于此函数在我的情况下计算不必要的matix,我想要更直接的方式使用NumPy计算它.
import numpy as np
from scipy.spatial.distance import cdist
x = np.array([[[1,2,3,4,5],
[5,6,7,8,5],
[5,6,7,8,5]],
[[11,22,23,24,5],
[25,26,27,28,5],
[5,6,7,8,5]]])
i,j,k = x.shape
xx = x.reshape(i,j*k).T
y = np.array([[[31,32,33,34,5],
[35,36,37,38,5],
[5,6,7,8,5]],
[[41,42,43,44,5],
[45,46,47,48,5],
[5,6,7,8,5]]])
yy = y.reshape(i,j*k).T
results = cdist(xx,yy,'mahalanobis')
results = np.diag(results)
print results
[ 2.28765854 2.75165028 2.75165028 2.75165028 0. 2.75165028
2.75165028 2.75165028 2.75165028 0. 0. 0. 0.
0. 0. ]
Run Code Online (Sandbox Code Playgroud)
我的试用版:
VI = np.linalg.inv(np.cov(xx,yy))
print np.sqrt(np.dot(np.dot((xx-yy),VI),(xx-yy).T))
Run Code Online (Sandbox Code Playgroud)
任何人都可以纠正这种方法吗?
这是它的公式:
我尝试根据此处的答案将 GridSearchCV 用于多类案例:
但我得到了价值错误, multiclass format is not supported.
如何将这种方法用于多类案例?
以下代码来自上述链接中的答案。
import numpy as np
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import accuracy_score, recall_score, f1_score, roc_auc_score, make_scorer
X, y = make_classification(n_samples=3000, n_features=5, weights=[0.1, 0.9, 0.3])
pipe = make_pipeline(StandardScaler(), SVC(kernel='rbf', class_weight='auto'))
param_space = dict(svc__C=np.logspace(-5,0,5), svc__gamma=np.logspace(-2, 2, 10))
accuracy_score, recall_score, roc_auc_score
my_scorer = make_scorer(roc_auc_score, greater_is_better=True)
gscv = GridSearchCV(pipe, param_space, scoring=my_scorer)
gscv.fit(X, y)
print gscv.best_params_
Run Code Online (Sandbox Code Playgroud) 可以使用网格搜索交叉验证来使用决策树分类器提取最佳参数吗? http://scikit-learn.org/stable/tutorial/statistical_inference/model_selection.html
我有两个相同形状的2d数组:given_array和reference_array.我必须为reference_array计算平均值的每个唯一值编写一个文件,其中唯一值在给定数组中.
import numpy as np
given_array = np.array([[2,4,5,8,9,11,15],[1,2,3,4,5,6,7]])
reference_array = np.array([[2,2,2,8,8,8,15],[2,2,2,4,8,8,9]])
unique_value = np.unique(reference_array)
file_out = open('file_out', 'w')
for unique in unique_value:
index = reference_array == unique
mean = np.mean(given_array[index])
file_out.write(str(unique) + ',' + str(mean) + '\n')
file_out.close()
Run Code Online (Sandbox Code Playgroud)
上面的代码有效,但在我的实际问题中,从光栅图像中读取的两个数组非常大,并且需要几天才能完成处理.
如果有人能提供产生相同结果的最快方法,将不胜感激.
我对计算两个 numpy 数组(x 和 y)之间的各种空间距离很感兴趣。
http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.cdist.html
import numpy as np
from scipy.spatial.distance import cdist
x = np.array([[[1,2,3,4,5],
[5,6,7,8,5],
[5,6,7,8,5]],
[[11,22,23,24,5],
[25,26,27,28,5],
[5,6,7,8,5]]])
i,j,k = x.shape
xx = x.reshape(i,j*k).T
y = np.array([[[31,32,33,34,5],
[35,36,37,38,5],
[5,6,7,8,5]],
[[41,42,43,44,5],
[45,46,47,48,5],
[5,6,7,8,5]]])
yy = y.reshape(i,j*k).T
results = cdist(xx,yy,'euclidean')
print results
Run Code Online (Sandbox Code Playgroud)
但是,上述结果会产生太多不需要的结果。如何仅针对我需要的结果限制它。
我想计算 [1,11] 和 [31,41] 之间的距离;[2,22] 和 [32,42] 等等。
我有一个 Shapely 多边形列表。从该列表中,我只想提取去除重复项的唯一多边形。
如何以更快的方式做到这一点?(我的列表包含数千个多边形)
from shapely.geometry import Polygon
lists = [[(1,1),(2,2),(3,3),(4,4)], [(6,6),(7,7),(8,8),(9,9)], [(1,1),(2,2),(3,3),(4,4)]]
polys = [Polygon(item) for item in lists] ##This is given condition
for poly in polys:
test = [p.intersects(poly) for p in polys] ##Return true or false
print test
[True, False, True]
[False, True, False]
[True, False, True]
Run Code Online (Sandbox Code Playgroud)
预期的结果是:
[[(1,1),(2,2),(3,3),(4,4)], [(6,6),(7,7),(8,8),(9,9)]]
Run Code Online (Sandbox Code Playgroud) 乘以不同大小的数组.
import numpy as np
a = np.array([1,2,3,4,5])
b = np.array([1,2,3])
print a*b
Run Code Online (Sandbox Code Playgroud)
当然错误:
ValueError: operands could not be broadcast together with shapes (5,) (3,)
Run Code Online (Sandbox Code Playgroud)
预期结果是np.array([1,4,9,0,0])
怎么做?
我有一个通过读取图像获得的二维 numpy 数组。数组的唯一值是 0、1 和 2。我想绘制图像,分别显示值 0、1 和 2 的唯一颜色红色、绿色和蓝色。
plt.imshow(data, cmap=colors.ListedColormap(['red'])
Run Code Online (Sandbox Code Playgroud)
你会怎么做?
以下脚本计算两个 numpy 数组(x 和 y)之间的 R 平方值。
由于数据中存在异常值,R 平方值非常低。如何提取这些异常值的索引?
import numpy as np, matplotlib.pyplot as plt, scipy.stats as stats
x = np.random.random_integers(1,50,50)
y = np.random.random_integers(1,50,50)
r2 = stats.linregress(x, y) [3]**2
print r2
plt.scatter(x, y)
plt.show()
Run Code Online (Sandbox Code Playgroud) numpy ×8
python ×8
scipy ×5
arrays ×3
scikit-learn ×3
matplotlib ×2
performance ×2
duplicates ×1
geometry ×1
lookup ×1
mean ×1
scikit-image ×1
shapely ×1
statistics ×1