numpy

Implicit transposing in numpy array indexing

天涯浪子 提交于 2021-02-16 18:33:31
问题 I came across a weird problem: from numpy import zeros, arange aa = zeros([1, 3, 10]) aa[0, :, arange(5)].shape Running this gives me (5,3) , but I'm expecting (3,5) . However, running the following gives me (3,5) as expected. aa = zeros([3, 10]) aa[:, arange(5)] This is easy to fix as part of my program, but it completely ruined my belief. I tried to search for similar questions that have already been answered but have no idea what to search for. Thank you and Happy Chinese New Year! 回答1:

Small discrepancy in eigenvectors between NumPy and MATLAB [duplicate]

那年仲夏 提交于 2021-02-16 18:07:16
问题 This question already has answers here : Sign difference in eigenvectors taken form Matlab and Python (1 answer) Conflicting eigen vector outputs between Matlab and Numpy (1 answer) Matlab VS Python - eig(A,B) VS sc.linalg.eig(A,B) (1 answer) eigenvalue and eigenvectors in python vs matlab (1 answer) Closed 2 years ago . I have stiffness matrix and mass matrix. I want to calculate my structure vibration shapes and period (eigenvalue/vector) so I am using NumPy for this. Eigenvalues are the

Python学习笔记(matplotlib篇)--坐标轴刻度

落爺英雄遲暮 提交于 2021-02-16 17:02:25
Python学习笔记--坐标轴刻度   参靠视频:《 Python数据可视化分析 matplotlib教程》链接: https://www.bilibili.com/video/av6989413/?p=6 所用的库及环境 :   IDE:Pycharm   Python环境:python3.7   Matplotlib: Matplotlib 1.11   Numpy: Numpy1.15.   Datetime :Datetime 坐标轴刻度 概念 当需要把x,y坐标轴刻度调整的更密集些或者更宽松点 学习如何调整x,y坐标轴刻度 坐标轴刻度调整  面向对象形式 pyplort形式 locater_params 介绍 文档: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.locator_params.html#matplotlib.pyplot.locator_params 介绍:Control behavior of tick locators. 属性: axis: 介绍:此属性参数表示要操作的轴,默认是both, 如果想只改变x轴就把参数置位‘x’,y轴相同   可选参数:both,x,y   nbins: 介绍:表示要操作的坐标轴一共有多少格   可选参数:可以是数字,表示坐标轴一共有多少格,数字越大格越多,越密集

Pass regex to delimiter field in python's csv module or numpy's genfromtxt / loadtxt?

≡放荡痞女 提交于 2021-02-16 16:59:08
问题 I have tabulated data with some strange delimination (i.e. groups of values separated by commas, seperated from other values by tabs): A,345,567 56 67 test Is there a clean and clever way of handling multiple delimiters in any of the following: csv module, numpy.genfromtxt, or numpy.loadtxt? I have found methods such as this, but I'm hoping there is a better solution out there. Ideally I'd like to use a genfromtxt and a regex for the delimiter. 回答1: I’m afraid the answer is no in the three

机器学习进阶-案例实战-图像全景拼接-书籍SIFT特征点连接 1.cv2.drawMatches(对两个图像的关键点进行连线操作)

浪尽此生 提交于 2021-02-16 16:30:49
1.cv2.drawMatches(imageA, kpsA, imageB, kpsB, matches[:10], None, flags=2) # 对两个图像关键点进行连线操作 参数说明:imageA和imageB表示图片,kpsA和kpsB表示关键点, matches表示进过cv2.BFMatcher获得的匹配的索引值,也有距离, flags表示有几个图像 书籍的SIFT特征点连接: 第一步:使用sift.detectAndComputer找出关键点和sift特征向量 第二步:构建BFMatcher()蛮力匹配器,bf.match匹配sift特征向量,使用的是欧式距离 第三步:根据匹配结果matches.distance对matches按照距离进行排序 第四步:进行画图操作,使用cv2.drawMatches进行画图操作 import cv2 import numpy as np # 读入图片 imgA = cv2.imread( ' box.png ' , 0) imgB = cv2.imread( ' box_in_scene.png ' , 0) def cv_show(img, name): cv2.imshow(name, img) cv2.waitKey(0) cv2.destroyAllWindows() # 第一步:构造sift

Transpose of a matrix in numpy

三世轮回 提交于 2021-02-16 16:18:09
问题 I have this numpy array: a = np.array([[[1,2,3],[-1,-2,-3]],[[4,5,6],[-4,-5,-6]]]) b is a transpose of a . I want b be like this: b = np.array([[[1,-1],[2,-2],[3,-3]],[[4,-4],[5,-5],[6,-6]]]) Is it possible to do it in one line? EDIT: And if I have this instead: a = np.empty(3,dtype = object) a[0] = np.array([[1,2,3],[-1,-2,-3]]) a[1] = np.array([[4,5,6],[-4,-5,-6]]) How can I get b? 回答1: You can do it using np.transpose(a,(0,2,1)) : In [26]: a = np.array([[[1,2,3],[-1,-2,-3]],[[4,5,6],[-4,-5

How to save numpy ndarray as .csv file?

狂风中的少年 提交于 2021-02-16 15:51:05
问题 I created a numpy array as follows: import numpy as np names = np.array(['NAME_1', 'NAME_2', 'NAME_3']) floats = np.array([ 0.1234 , 0.5678 , 0.9123 ]) ab = np.zeros(names.size, dtype=[('var1', 'U6'), ('var2', float)]) ab['var1'] = names ab['var2'] = floats The values in ab are shown below: array([(u'NAME_1', 0.1234), (u'NAME_2', 0.5678), (u'NAME_3', 0.9123)], dtype=[('var1', '<U6'), ('var2', '<f8')]) When I try to save ab as a .csv file using savetxt() command, np.savetxt('D:\test.csv',ab

Object of type 'ndarray' is not JSON serializable

不羁岁月 提交于 2021-02-16 15:50:28
问题 I am new to python and machine learning. I have a Linear Regression model which is able to predict output based on the input which I have dumped to be used with a web service. See the code below: X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25) regression_model = LinearRegression() regression_model.fit(X_train, y_train) print(regression_model.predict(np.array([[21, 0, 0, 0, 1, 0, 0, 1, 1, 1]]))) # this is returning my expected output joblib.dump(regression_model, '..

Multidimensional cumulative sum in numpy

天大地大妈咪最大 提交于 2021-02-16 13:33:41
问题 I want to be able to calculate the cumulative sum of a large n-dimensional numpy array. The value of each element in the final array should be the sum of all elements which have indices greater than or equal to the current element. 2D: xᶦʲ = ∑xᵐⁿ ∀ m ≥ i and n ≥ j 3D: xᶦʲᵏ = ∑xᵐⁿᵒ ∀ m ≥ i and n ≥ j and o ≥ k Examples in 2D: 1 1 0 2 1 0 1 1 1 -> 5 3 1 1 1 1 8 5 2 1 2 3 6 5 3 4 5 6 -> 21 16 9 7 8 9 45 33 18 Example in 3D: 1 1 1 3 2 1 1 1 1 6 4 2 1 1 1 9 6 3 1 1 1 6 4 2 1 1 1 -> 12 8 4 1 1 1 18

numpy memmap memory usage - want to iterate once

允我心安 提交于 2021-02-16 13:17:10
问题 let say I have some big matrix saved on disk. storing it all in memory is not really feasible so I use memmap to access it A = np.memmap(filename, dtype='float32', mode='r', shape=(3000000,162)) now let say I want to iterate over this matrix (not essentially in an ordered fashion) such that each row will be accessed exactly once. p = some_permutation_of_0_to_2999999() I would like to do something like that: start = 0 end = 3000000 num_rows_to_load_at_once = some_size_that_will_fit_in_memory()