numpy

Numpy […,None]

房东的猫 提交于 2021-02-19 02:31:05
问题 I have found myself needing to add features to existing numpy arrays which has led to a question around what the last portion of the following code is actually doing: np.ones(shape=feature_set.shape)[...,None] Set-up As an example, let's say I wish to solve for linear regression parameter estimates by using numpy and solving: Assume I have a feature set shape (50,1), a target variable of shape (50,), and I wish to use the shape of my target variable to add a column for intercept values. It

Is there a significant overhead in calling `np.asarray' on a NumPy array?

假如想象 提交于 2021-02-19 02:26:29
问题 I am quite new to the Python world, so please excuse my dumb question. In a number of circumstances, I implement a functions that works on array-like numerical inputs, and it is usually advantageous to make use of NumPy utilities for basic operations on the sequences. To this end, I would write something like this: import numpy as np def f(x): if not isinstance(x, np.ndarray): x = np.asarray(x) # and from now on we know that x is a NumPy array, with all standard methods (Note that I don't

Removing NaNs in numpy arrays

瘦欲@ 提交于 2021-02-19 01:42:10
问题 I have two numpy arrays that contains NaNs: A = np.array([np.nan, 2, np.nan, 3, 4]) B = np.array([ 1 , 2, 3 , 4, np.nan]) are there any smart way using numpy to remove the NaNs in both arrays, and also remove whats on the corresponding index in the other list? Making it look like this: A = array([ 2, 3, ]) B = array([ 2, 4, ]) 回答1: What you could do is add the 2 arrays together this will overwrite with NaN values where they are none, then use this to generate a boolean mask index and then use

How can I make seaborn distribution subplots in a loop?

混江龙づ霸主 提交于 2021-02-19 01:16:49
问题 I have a 5D array called data for i in range(10): sns.distplot(data[i,0,0,0], hist=False) But I want to make them inside subplots instead. How can I do it? Tried this: plt.rc('figure', figsize=(4, 4)) fig=plt.figure() fig, ax = plt.subplots(ncols=4, nrows=3) for i in range(10): ax[i].sns.distplot(data[i,0,0,0], hist=False) plt.show() This obviously doesn't work. 回答1: You would want to use the ax argument of the seaborn distplot function to supply an existing axes to it. Looping can be

How can I make seaborn distribution subplots in a loop?

和自甴很熟 提交于 2021-02-19 01:14:26
问题 I have a 5D array called data for i in range(10): sns.distplot(data[i,0,0,0], hist=False) But I want to make them inside subplots instead. How can I do it? Tried this: plt.rc('figure', figsize=(4, 4)) fig=plt.figure() fig, ax = plt.subplots(ncols=4, nrows=3) for i in range(10): ax[i].sns.distplot(data[i,0,0,0], hist=False) plt.show() This obviously doesn't work. 回答1: You would want to use the ax argument of the seaborn distplot function to supply an existing axes to it. Looping can be

How do I “randomly” select numbers with a specified bias toward a particular number

蓝咒 提交于 2021-02-19 00:42:10
问题 How do I generate random numbers with a specified bias toward one number. For example, how would I pick between two numbers, 1 and 2, with a 90% bias toward 1. The best I can come up with is... import random print random.choice([1, 1, 1, 1, 1, 1, 1, 1, 1, 2]) Is there a better way to do this? The method I showed works in simple examples but eventually I'll have to do more complicated selections with biases that are very specific (such as 37.65% bias) which would require a very long list. EDIT

How do I “randomly” select numbers with a specified bias toward a particular number

旧城冷巷雨未停 提交于 2021-02-19 00:39:47
问题 How do I generate random numbers with a specified bias toward one number. For example, how would I pick between two numbers, 1 and 2, with a 90% bias toward 1. The best I can come up with is... import random print random.choice([1, 1, 1, 1, 1, 1, 1, 1, 1, 2]) Is there a better way to do this? The method I showed works in simple examples but eventually I'll have to do more complicated selections with biases that are very specific (such as 37.65% bias) which would require a very long list. EDIT

How to replace values in a numpy array based on another column?

本小妞迷上赌 提交于 2021-02-18 22:09:20
问题 Let say i have the following: import numpy as np data = np.array([ [1,2,3], [1,2,3], [1,2,3], [4,5,6], ]) How would I go about changing values in column 3 based on values in column 2? For instance, If column 3 == 3, column 2 = 9. [[1,9,3], [1,9,3], [1,9,3], [4,5,6]] I've looked at np.any() , but I can't figure out how to alter the array in place. 回答1: You can use Numpy's slicing and indexing to achieve this. Take all the rows where the third column is 3 , and change the second column of each

How to replace values in a numpy array based on another column?

非 Y 不嫁゛ 提交于 2021-02-18 22:08:57
问题 Let say i have the following: import numpy as np data = np.array([ [1,2,3], [1,2,3], [1,2,3], [4,5,6], ]) How would I go about changing values in column 3 based on values in column 2? For instance, If column 3 == 3, column 2 = 9. [[1,9,3], [1,9,3], [1,9,3], [4,5,6]] I've looked at np.any() , but I can't figure out how to alter the array in place. 回答1: You can use Numpy's slicing and indexing to achieve this. Take all the rows where the third column is 3 , and change the second column of each

How to replace values in a numpy array based on another column?

余生长醉 提交于 2021-02-18 22:06:04
问题 Let say i have the following: import numpy as np data = np.array([ [1,2,3], [1,2,3], [1,2,3], [4,5,6], ]) How would I go about changing values in column 3 based on values in column 2? For instance, If column 3 == 3, column 2 = 9. [[1,9,3], [1,9,3], [1,9,3], [4,5,6]] I've looked at np.any() , but I can't figure out how to alter the array in place. 回答1: You can use Numpy's slicing and indexing to achieve this. Take all the rows where the third column is 3 , and change the second column of each