numpy

Applying weighted average function to column in pandas groupby object, carrying over the weights to calculate uncertainties

我的未来我决定 提交于 2021-01-29 12:09:37
问题 I have tried to expand on this question to generalize to the case when one wants to carry over the sum of the weights, in a weighted average, so that one can append to the resulting dataframe the uncertainties on the weighted averages which are 1 / (sqrt(sum_of_weights)) Consider the sample dataframe import pandas as pd import numpy as np df5 = pd.DataFrame.from_dict({'Lab': ['Lab1','Lab1','Lab1','Lab2','Lab2','Lab2','Lab3','Lab3','Lab3'], 'test_type': ['a','a','b','b','c','c','a','a','a'],

How to add 2 columns in numpy ndarray? [duplicate]

随声附和 提交于 2021-01-29 11:43:02
问题 This question already has an answer here : How to combine two columsn/rows of a matrix in numpy by summing values? (1 answer) Closed 10 months ago . I have an numpy ndarray like below: [[1 9 1 1] [9 3 1 1] [1 9 9 1] [8 2 4 7]] I want to add last 2 columns values to get below result [[1 9 2] [9 3 2] [1 9 10] [8 2 11]] 回答1: Sum, then drop the last column myArray[:, -2] = myArray[:,-2] + myArray[:,-1] myArray = myArray[:,:-1] 来源: https://stackoverflow.com/questions/61092371/how-to-add-2-columns

Add multiple arrays in Python

≯℡__Kan透↙ 提交于 2021-01-29 11:40:40
问题 What I have created so far I have created an 18x18 square matrix of zeros called ‘master_matrix’. I have created an array called ingreso_datos , whose column 0 [col 0] indicates the data label . I have created a for loop where: For each data label I will have a little_matrix whose values will be assigned to master_matrix in their corresponding rows and columns. As this happens inside a for loop, for this example I get 6 master_matrix (in my variable list only the last one is read, that is,

Solve the linear equations system AX = B in Python, np.linalg.solve not working

痴心易碎 提交于 2021-01-29 11:29:42
问题 I'm trying to solve the linear equation AX=B where A,X,B are Matrices. I've tried using the np.linalg.solve function of numpy but the result seems to be wrong. Example: Matrix A [9 1 8] [3 2 5] [1 6 5] Matrix B [7 0 5] [7 8 4] [5 6 7] So to solve X, i've used: X = np.linalg.solve(A,B) The result is: X [ 1.17521368 -0.17948718 0.40598291] [ 0.20512821 -0.30769231 0.74358974] [-0.56410256 -0.15384615 1.20512821] But if i try to verify the result by multiplying A by X, the result is anything but

Modified SIR model

陌路散爱 提交于 2021-01-29 11:17:22
问题 I am making a modified SIR model with an added vaccination parameter V. InitIALLY all the nodes in the graph are susceptible and there are a few initially infected people. The initial infected people neighbours are first vaccinated with prob w (which means they cant be infected) and then they are infected with prob b. The total number of vaccinated people is controlled by Vl which is a fraction of the total population. Here is my code- import networkx as nx import random import scipy from

Custom transformer mixin with FeatureUnion in scikit-learn

蹲街弑〆低调 提交于 2021-01-29 10:46:47
问题 I am writing custom transformers in scikit-learn in order to do specific operations on the array. For that I use inheritance of class TransformerMixin. It works fine when I deal only with one transformer. However when I try to chain them using FeatureUnion (or make_union), the array is replicated n-times. What could I do to avoid that? Am I using scikit-learn as it is supposed to be? import numpy as np from sklearn.base import TransformerMixin from sklearn.pipeline import FeatureUnion #

Finding the intersect between a quadratic and line

*爱你&永不变心* 提交于 2021-01-29 10:30:55
问题 I am trying to find the intersect between a straight line and a quadratic curve, however the result I am getting appears to be imaginary although I don't see how this can be the case as I can see them intersect on real axes: Import numpy #quadratic coefficients a,b,c = (-3.09363812e-04, 1.52138019e+03, -1.87044961e+09) # y = ax^2 + bx + c #line coefficients m,d = (1.06446434e-03, -2.61660911e+03) #y = mx + d intersect = (-(b-m)+((b-m)**2 - 4*a*(c-d))**0.5)/(2*a) print(intersect) The output of

Distance matrix between two point layers

倖福魔咒の 提交于 2021-01-29 10:19:25
问题 I have two arrays containing point coordinates as shapely.geometry.Point with different sizes. Eg: [Point(X Y), Point(X Y)...] [Point(X Y), Point(X Y)...] I would like to create a "cross product" of these two arrays with a distance function. Distance function is from shapely.geometry, which is a simple geometry vector distance calculation. I am tryibg to create distance matrix between M:N points: Right now I have this function: source = gpd.read_file(source) near = gpd.read_file(near) source

Numpy multidimensional indexing for np.ufunc.at and np.ix_

拜拜、爱过 提交于 2021-01-29 10:01:00
问题 I would like to know how I can take index from an array and multiply with another array. I have two 4d arrays and one 2d index array: base = np.ones((2, 3, 5, 5)) to_multiply = np.arange(120).reshape(2, 3, 4, 5) index = np.array([[0, 2, 4, 2], [0, 3, 3, 2]]) The row index of the index array corresponds to the 1st dimension of base and to_multiply, and the value of the index array corresponds to the 3rd dimension of base. I want to take the slice from base according to the index and multiply

Printing a Numpy Array [closed]

北城余情 提交于 2021-01-29 09:32:48
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 months ago . Improve this question I am trying to print the results of some previous calculations, and am having some issues using Numpy to print the values out of an array correctly. Each of the variables in the loop is defined by calculations previously, and it needs to run thru the speed permutations to get