numpy

How to fix “IndexError: list index out of range” in Tensorflow

半城伤御伤魂 提交于 2021-02-19 04:29:48
问题 I'm creating an Image Classifier using Tensorflow and Keras, but when I tried to train my model I got an error: IndexError: list index out of range. I think the problem is with my model, because when I remove the conv2D layers, then the code throws no error. model = Sequential() model.add(Conv2D(64,(3,3),activation='relu',padding='same')) model.add(Conv2D(64,(3,3),activation='relu',padding='same')) model.add(MaxPool2D((2,2),strides=(2,2))) model.add(Conv2D(128,(3,3),activation='relu',padding=

plotting polynomial regression in same plot as the real data

梦想的初衷 提交于 2021-02-19 04:24:15
问题 I have some snippets of code that read two csvs and plot them using matplotlib.pyplot and perform polynomial regression on the same two csvs. What I want to be able to do is plot both the data and my polynomial regression on the same graph. import matplotlib.pyplot as plt import csv import numpy as np datax=np.genfromtxt('Delta R.csv') datay=np.genfromtxt('Example R.csv') plt.title ('Test graph ') plt.xlabel('x axis') plt.ylabel('y axis ') plt.plot(datax, datay,'o-') plt.show() and my second

Cleanest way to combine reduce and map in Python

痞子三分冷 提交于 2021-02-19 03:44:01
问题 I'm doing a little deep learning, and I want to grab the values of all hidden layers. So I end up writing functions like this: def forward_pass(x, ws, bs): activations = [] u = x for w, b in zip(ws, bs): u = np.maximum(0, u.dot(w)+b) activations.append(u) return activations If I didn't have to get the intermediate values, I'd use the much less verbose form: out = reduce(lambda u, (w, b): np.maximum(0, u.dot(w)+b), zip(ws, bs), x) Bam. All one line, nice and compact. But I can't keep any of

Cleanest way to combine reduce and map in Python

穿精又带淫゛_ 提交于 2021-02-19 03:43:10
问题 I'm doing a little deep learning, and I want to grab the values of all hidden layers. So I end up writing functions like this: def forward_pass(x, ws, bs): activations = [] u = x for w, b in zip(ws, bs): u = np.maximum(0, u.dot(w)+b) activations.append(u) return activations If I didn't have to get the intermediate values, I'd use the much less verbose form: out = reduce(lambda u, (w, b): np.maximum(0, u.dot(w)+b), zip(ws, bs), x) Bam. All one line, nice and compact. But I can't keep any of

complementary slicing in a numpy array

[亡魂溺海] 提交于 2021-02-19 03:36:47
问题 If I have a numpy array for example : A = np.array([[3, 2], [2, -1], [2, 3], [5, 6], [7,-1] , [8, 9]]) I would like to separate the part of the array with the subarrays having -1 from the ones who don't. Keep in mind that I'm working on very big data set, so every operation can be very long so I try to have the most effective way memory and CPU-time wise. What I am doing for the moment is : slicing1 = np.where(A[:, 1] == -1) with_ones = A[slicing1] slicing2 = np.setdiff1d(np.arange(A.shape[0]

Numpy filter using condition on each element

折月煮酒 提交于 2021-02-19 03:22:04
问题 I have a filter expression as follows: feasible_agents = filter(lambda agent: agent >= cost[task, agent], agents) where agents is a python list. Now, to get speedup, I am trying to implement this using numpy. What would be the equivalent using numpy? I know that this works: threshold = 5.0 feasible_agents = np_agents[np_agents > threshold] where np_agents is the numpy equivalent of agents . However, I want threshold to be a function of each element in the numpy array. 回答1: Since you don't

Numpy filter using condition on each element

风格不统一 提交于 2021-02-19 03:21:25
问题 I have a filter expression as follows: feasible_agents = filter(lambda agent: agent >= cost[task, agent], agents) where agents is a python list. Now, to get speedup, I am trying to implement this using numpy. What would be the equivalent using numpy? I know that this works: threshold = 5.0 feasible_agents = np_agents[np_agents > threshold] where np_agents is the numpy equivalent of agents . However, I want threshold to be a function of each element in the numpy array. 回答1: Since you don't

numpy how to slice index an array using arrays?

僤鯓⒐⒋嵵緔 提交于 2021-02-19 03:21:05
问题 Perhaps this has been raised and addressed somewhere else but I haven't found it. Suppose we have a numpy array: a = np.arange(100).reshape(10,10) b = np.zeros(a.shape) start = np.array([1,4,7]) # can be arbitrary but valid values end = np.array([3,6,9]) # can be arbitrary but valid values start and end both have valid values so that each slicing is also valid for a . I wanted to copy value of subarrays in a to corresponding spots in in b : b[:, start:end] = a[:, start:end] #error this syntax

How to understand the pivot matrix of scipy.linalg.lu_factor?

时光怂恿深爱的人放手 提交于 2021-02-19 02:58:12
问题 How can I manually reconstruct a matrix A that was factorized by lu_factor? ( A = PLU ) My current attempts all failed due to the setup of matrix P . Here is what I have so far: A = np.random.rand(3,3) lu, piv = lu_factor(A) U = np.triu(lu) L = np.tril(lu, -1) L[np.diag_indices_from(L)] = 1.0 I am looking for the matrix P that makes this line print True : print np.allclose(A, np.dot(P, np.dot(L, U))) Any hint/link/suggestion is appreciated! 回答1: The permutation vector needs to be interpreted

How to mask image with binary mask

与世无争的帅哥 提交于 2021-02-19 02:40:21
问题 Suppose I have a greyscale image here: And a binary masked image here: With the same dimensions and shape. How do I generate something like this: Where the values indicated by the 1 in the binary mask are the real values, and values that are 0 in the mask are null in the final image. 回答1: Use cv2.bitwise_and to mask an image with a binary mask. Any white pixels on the mask (values with 1) will be kept while black pixels (value with 0) will be ignored. Here's a example: Input image (left),