numpy

vectorization : too many indices for array

你离开我真会死。 提交于 2021-02-10 17:30:10
问题 a=b=np.arange(9).reshape(3,3) i=np.arange(3) mask=a<i[:,None,None]+3 and b[np.where(mask[0])] >>>array([0, 1, 2]) b[np.where(mask[1])] >>>array([0, 1, 2, 3]) b[np.where(mask[2])] >>>array([0, 1, 2, 3, 4]) Now I wanna vectorize it and print them all together, and I try b[np.where(mask[i])] and b[np.where(mask[i[:,None,None]])] Both of them show IndexError: too many indices for array 回答1: In [165]: a Out[165]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [166]: mask Out[166]: array([[[ True,

vectorized/broadcasted Dot product of numpy arrays with different dimensions

非 Y 不嫁゛ 提交于 2021-02-10 17:27:39
问题 The Problem: I want to calculate the dot product of a very large set of data. I am able to do this in a nested for-loop, but this is way too slow. Here is a small example: import numpy as np points = np.array([[0.5, 2, 3, 5.5, 8, 11], [1, 2, -1.5, 0.5, 4, 5]]) lines = np.array([[0, 2, 4, 6, 10, 10, 0, 0], [0, 0, 0, 0, 0, 4, 4, 0]]) x1 = lines[0][0:-1] y1 = lines[1][0:-1] L1 = np.asarray([x1, y1]) # calculate the relative length of the projection # of each point onto each line a = np.diff

Tensorflow Lite tflite模型的生成与导入

妖精的绣舞 提交于 2021-02-10 16:59:25
假如想要在ARM板上用 tensorflow lite ,那么意味着必须要把PC上的模型生成 tflite 文件,然后在ARM上导入这个 tflite 文件,通过解析这个文件来进行计算。 根据前面所说, tensorflow 的所有计算都会在内部生成一个图,包括变量的初始化,输入定义等,那么即便不是经过训练的神经网络模型,只是简单的三角函数计算,也可以生成一个 tflite 模型用于在 tensorflow lite 上导入。所以,这里我就只做了简单的 sin() 计算来跑一编这个流程。 生成 tflite 模型 这部分主要是调用 TFLiteConverter 函数,直接生成 tflite 文件,不再通过 pb 文件转化。 先上代码: import numpy as np import time import math import tensorflow as tf SIZE = 1000 X = np.random.rand(SIZE, 1 ) X = X*(math.pi/2.0 ) start = time.time() x1 = tf.placeholder(tf.float32, [SIZE, 1], name= ' x1-input ' ) x2 = tf.placeholder(tf.float32, [SIZE, 1], name= ' x2-input ' )

I want to assign labels 0/1 to pandas datafrmae according to columns

徘徊边缘 提交于 2021-02-10 16:57:20
问题 I am fairly new to python.I am trying to assign labels in a pandas dataframe.This is how my dataframe looks : final.head(3) Match Team1 Team2 winner A 2 3 3 B 1 2 1 C 3 1 1 I want to create a new column which demonstrates who won the match.As in if Team1 wins the game label should be 0 and if Team2 wins the game label should be 1. Expected outcome should be : - Match Team1 Team2 winner label A 2 3 3 1 B 1 2 1 0 C 3 1 1 1 Please tell me how should i proceed.Thanks in advance. 回答1: Your label

Shape error when using PolynomialFeatures

匆匆过客 提交于 2021-02-10 16:54:32
问题 The Issue To begin with I'm pretty new to machine learning. I have decided to test up some of the things that I have learned on some financial datam my machine learning model looks like this: import pandas as pd from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures df = pd.read_csv("/Users/Documents/Trading.csv") poly_features = PolynomialFeatures(degree=2, include_bias=False) linear_reg = LinearRegression(fit_intercept = True) X = df_copy[[

How to get 2D array from 3D array of an image by removing black pixels, i.e. [0,0,0], in python

妖精的绣舞 提交于 2021-02-10 16:54:29
问题 I have a picture of the facial skin with black pixels around it. The picture is an 3d array made up of pixels (RGB) picture's array = width * height * RGB The problem is that in the picture there are so many black pixels that do not belong to the skin. The black pixels represent as an array of zero. [0,0,0] I want to get 2d array with non-black pixels as [[218,195,182]. ... [229,0, 133]] -with only the pixels of facial skin color I try to eject the black pixels by finding all the pixels whose

How can I cleanly normalize data and then “unnormalize” it later?

匆匆过客 提交于 2021-02-10 16:21:07
问题 I am using Anaconda with a Tensorflow neural network. Most of my data is stored with pandas . I am attempting to predict cryptocurrency markets. I am aware that this lots of people are probably doing this and it is most likely not going to be very effective, I'm mostly doing it to familiarize myself with Tensorflow and Anaconda tools. I am fairly new to this, so if I am doing something wrong or suboptimally please let me know. Here is how I aquire and handle the data: Download datasets from

How can I cleanly normalize data and then “unnormalize” it later?

风流意气都作罢 提交于 2021-02-10 16:19:52
问题 I am using Anaconda with a Tensorflow neural network. Most of my data is stored with pandas . I am attempting to predict cryptocurrency markets. I am aware that this lots of people are probably doing this and it is most likely not going to be very effective, I'm mostly doing it to familiarize myself with Tensorflow and Anaconda tools. I am fairly new to this, so if I am doing something wrong or suboptimally please let me know. Here is how I aquire and handle the data: Download datasets from

flatten list of lists and scalars

[亡魂溺海] 提交于 2021-02-10 15:58:01
问题 So for a matrix, we have methods like numpy.flatten() np.array([[1,2,3],[4,5,6],[7,8,9]]).flatten() gives [1,2,3,4,5,6,7,8,9] what if I wanted to get from np.array([[1,2,3],[4,5,6],7]) to [1,2,3,4,5,6,7] ? Is there an existing function that performs something like that? 回答1: With uneven lists, the array is a object dtype, (and 1d, so flatten doesn't change it) In [96]: arr=np.array([[1,2,3],[4,5,6],7]) In [97]: arr Out[97]: array([[1, 2, 3], [4, 5, 6], 7], dtype=object) In [98]: arr.sum() ...

Why use absolute instead of relative imports in a Python package?

坚强是说给别人听的谎言 提交于 2021-02-10 15:40:48
问题 I've recently created a Python package, and within it, used only relative imports to access functions stored in other methods. Now, in Numpy, I see a lot of files that make heavy use of absolute imports, e.g. this file. It has a lot of statements like from numpy.core import overrides . I don't see a disadvantage in using relative imports. Why are they doing it like that, instead of from ..core import overrides ? Doesn't the absolute import require numpy to be already installed? 回答1: Absolute