numpy

Issue vectorizing a recursive function that is used in iterative scheme to calculate Numpy array

醉酒当歌 提交于 2021-02-11 15:06:29
问题 I have the following recursive function, def subspaceiterate(A,V,v,j): if j == 0: return v else: v_jm1 = V[:,j-1] v_jm1 = np.reshape(v_jm1,(np.size(V,axis=0),1)) v = v - np.matmul(v_jm1.T,np.matmul(A,v_jm1)) j = j - 1 subspaceiterate(A,V,v,j) A is an mxm matrix whose eigenvalues and eigenvectors I want to compute using an iterative method, V is an mxm matrix that stores my initial guess for the eigenvectors of A , v_j is a particular column of V , and j is an index that I descend and use to

Size-1 array error when preparing decision model

半腔热情 提交于 2021-02-11 15:02:26
问题 I have DataFrame called data with 477154 rows. PDB_ID Chain Sequence Secstr 0 101M A GEWQLVLHVWAKVEA | HHHH HHHHGG| 1 102L A MVLSEGEWKVEA |HHHH HHHHHH| 2 102M A MVLSEGEWQLVLHVWAKVEA |HHHHHHHHHGGHH HHH | 3 103L A MVLSEGEWQLVLHVWAKV | HHHHH HHHHHH HH| 4 103L B MVLSEGEWQLVLHVWAKVEAVAL | HHHHH HHHHHH HHHHH | My goal is to get each character one by one from columns: 'Sequence' and 'Secstr' to arrays and make it usable for classification. Every row has different number of elements. I tried to do it

Numpy matrix rotation for any degrees

做~自己de王妃 提交于 2021-02-11 14:51:18
问题 I try to find a way to apply a matrix rotation of any degrees on my matrix that contains three bands like RGB but values are bigger than (0-255). It is an example of my data its shape is (100, 100, 3): [[ 847.5 877. 886. ... 821.5 856.5 898. ] [ 850. 883. 969.5 ... 885. 878.5 947.5] [ 982. 968.5 927.5 ... 909.5 958. 1037. ] ... [ 912. 827. 893. ... 1335. 1180. 1131. ] [ 954. 855.5 882. ... 1252. 1266. 1335. ] [ 984. 916. 930. ... 1080.5 1278. 1385.5]] I found a function scipy.misc.imrotate

Decode image bytes data stream to JPEG

守給你的承諾、 提交于 2021-02-11 14:35:31
问题 I am struggling to successfully decode a JPEG image from bytes, back to JPEG again. I started from encoded frame from a MJPG bytes stream, which I want to decode in order to manipulate with OpenCV. I am a bit of a newbie at Python, numpy, opencv etc! I now have the frame JPG data in a text file as: b'\xf\xd8\xff\xdb\x00....etc etc for purposes of testing: code seems to fail when I try to Resize the numpy array to the original video stream resolution (640, 480) on line 14 (npFlat.reshape((640

KeyError: ((1, 1, 1280), '|u1') while using PIL's Image.fromarray - PIL

為{幸葍}努か 提交于 2021-02-11 14:34:57
问题 I have this code: from PIL import Image import numpy as np img = Image.open('img.jpg') Image.fromarray(np.array([[np.mean(i, axis=1).astype(int).tolist()]*len(i) for i in np.array(img).tolist()]).astype('uint8')).show() And I am trying to modify the pixels of the image in PIL, however when I run it it gives an error as follows: KeyError: ((1, 1, 1280), '|u1') Not just that, it also outputs a second error as follows: TypeError: Cannot handle this data type Is there a way to overcome this? P.S.

numpy: Print matrix with random elements, columns and rows

旧街凉风 提交于 2021-02-11 14:30:19
问题 I want a matrix to be printed with random columns(0, 9) and random rows(0, 9) with random elements(0, 9) Where (0, 9) is any random number between 0 and 9. 回答1: If what you're looking for is a 10x10 matrix filled with random numbers between 0 and 9, here's what you want: # this randomizes the size of the matrix. rows, cols = np.random.randint(9, size=(2)) # this prints a matrix filled with random numbers, with the given size. print(np.random.randint(9, size=(rows, cols))) Output: [[1 7 1 4 4

How to extract the boundary values from k-nearest neighbors predict

点点圈 提交于 2021-02-11 14:24:30
问题 How can only the boundary values be extracted , or returned, from .predict , for sklearn.neighbors.KNeighborsClassifier()? MRE import pandas as pd import numpy as np from sklearn.datasets import load_iris from sklearn.neighbors import KNeighborsClassifier import seaborn as sns import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap # prepare data iris = load_iris() X = iris.data y = iris.target df = pd.DataFrame(X, columns=iris.feature_names) df['label'] = y species_map =

create a 20x20 matrix using numpy broadcast

廉价感情. 提交于 2021-02-11 14:24:19
问题 I'm looking how to create a matrix of 20x20 using Numpy broadcasting, result should look like: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 0, 5, 10, 15, 20, 25, 30

Combine Numpy “where” statements

这一生的挚爱 提交于 2021-02-11 14:21:36
问题 I am trying to speed up a code that is using Numpy's where() function. There are two calls to where() , which return an array of indices for where the statement is evaluated as True , which are then compared for overlap with numpy's intersect1d() function, of which the length of the intersection is returned. import numpy as np def find_match(x,y,z): A = np.where(x == z) B = np.where(y == z) #A = True #B = True return len(np.intersect1d(A,B)) N = np.power(10, 8) M = 10 X = np.random.randint(M,

Calculate nearest distance to certain points in python

微笑、不失礼 提交于 2021-02-11 14:16:19
问题 I have a dataset as shown below, each sample has x and y values and the corresponding result Sr. X Y Resut 1 2 12 Positive 2 4 3 positive .... Visualization Grid size is 12 * 8 How I can calculate the nearest distance for each sample from red points (positive ones)? Red = Positive, Blue = Negative Sr. X Y Result Nearest-distance-red 1 2 23 Positive ? 2 4 3 Negative ? .... dataset 回答1: Its a lot easier when there is sample data, make sure to include that next time. I generate random data