mean

Pandas fillna() based on specific column attribute

久未见 提交于 2019-12-10 18:53:21
问题 Let's say I have this table Type | Killed | Survived Dog 5 2 Dog 3 4 Cat 1 7 Dog nan 3 cow nan 2 One of the value on Killed is missing for [Type] = Dog . I want to impute the mean in [Killed] for [Type] = Dog . My code is as follow: Search for the mean df[df['Type'] == 'Dog'].mean().round() This will give me the mean (around 2.25) Impute the mean (This is where the problem begins) df.loc[(df['Type'] == 'Dog') & (df['Killed'])].fillna(2.25, inplace = True) The code runs, but the value is not

Mean of vector inside of list of lists

为君一笑 提交于 2019-12-10 15:36:57
问题 I have a list of lists with the following structure: > mylist <- list(list(a=as.numeric(1:3), b=as.numeric(4:6)), list(a=as.numeric(6:8), b=as.numeric(7:9))) > str(mylist) List of 2 $ :List of 2 ..$ a: num [1:3] 1 2 3 ..$ b: num [1:3] 4 5 6 $ :List of 2 ..$ a: num [1:3] 6 7 8 ..$ b: num [1:3] 7 8 9 I would like to get the element-wise mean between the vectors a and b of mylist . For the vector a , the result would be this: > a [1] 3.5 4.5 5.5 I know the functions lapply , rbind and colMeans

Plotting means as a line plot onto a scatter plot with ggplot

倾然丶 夕夏残阳落幕 提交于 2019-12-10 15:26:08
问题 I have this simple data frame holding three replicates (value) for each factor (CT). I would like to plot it as geom_point and than the means of the point as geom_line. gene <- c("Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5") value <- c(0.86443, 0.79032, 0.86517, 0.79782, 0.79439, 0.89221, 0.93071, 0.87170, 0.86488, 0.91133, 0.87202, 0.84028, 0.83242, 0.74016, 0.86656) CT <- c("ET","ET","ET", "HP","HP","HP","HT","HT",

Using mapply with mean function on a matrix

ε祈祈猫儿з 提交于 2019-12-10 15:24:53
问题 I wish to calculate the mean of adjacent values in each column (or row) of a matrix (e.g. mean of [1,1] and [2,1], [2,1] and [3,1], [3,1] and [4,1]) and to apply this across all columns. I have tried to use the mapply function (to avoid using a for loop), to calculate the mean of the first 2 values in each column, and plan to apply this to the whole matrix row-by-row. However mapply which seems to work if I try to sum the values but not for the mean function. See example below: x <- matrix(c

data.table 1.8.x mean() function auto removing NA?

限于喜欢 提交于 2019-12-10 13:24:54
问题 Today I found out a bug in my program due to data.table auto remove NA for mean for example: > a<-data.table(a=c(NA,NA,FALSE,FALSE), b=c(1,1,2,2)) > a > a[,list(mean(a), sum(a)),by=b] b V1 V2 1: 1 0 NA // Why V1 = 0 here? I had expected NA 2: 2 0 0 > mean(c(NA,NA,FALSE,FALSE)) [1] NA > mean(c(NA,NA)) [1] NA > mean(c(FALSE,FALSE)) [1] 0 Is this the intended behaviour? 回答1: This isn't intended. Looks like a problem with optimization ... > a[,list(mean(a), sum(a)),by=b] b V1 V2 1: 1 0 NA 2: 2 0

Calculate means of rows

佐手、 提交于 2019-12-10 12:13:40
问题 I have a dataframe called ants detailing multiple entries per site, looks like this: Site Date Time Temp SpCond Salinity Depth Turbidity Chlorophyll 1 71 6/8/2010 14:50:35 14.32 49.88 32.66 0.397 0.0 1.3 2 71 6/8/2010 14:51:00 14.31 49.94 32.70 1.073 0.0 2.0 3 71 6/8/2010 14:51:16 14.32 49.95 32.71 1.034 -0.1 1.6 4 71 6/8/2010 14:51:29 14.31 49.96 32.71 1.030 -0.2 1.6 5 70 6/8/2010 14:53:55 14.30 50.04 32.77 1.002 -0.2 1.2 6 70 6/8/2010 14:54:09 14.30 50.03 32.77 0.993 -0.5 1.2 Sites have

aggregate a matrix (or data.frame) by column name groups in R

筅森魡賤 提交于 2019-12-10 09:43:08
问题 I have a large matrix with about 3000 columns x 3000 rows. I'd like to aggregate (calculate the mean) grouped by column names for every row. Each column is named similar to this method...(and in random order) Tree Tree House House Tree Car Car House I would need the data result (aggregation of mean of every row) to have the following columns: Tree House Car the tricky part (at least for me) is that I do not know all the column names and they are all in random order! 回答1: You could try res1 <-

Block mean of numpy 2D array

荒凉一梦 提交于 2019-12-10 04:15:07
问题 I want to find block mean of a 2D array in NumPy. For simplicity, let us assume that the array is as follows: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]) I want to divide this array into 3 blocks of size 2x4, and then find the mean of all three blocks (so that the shape of the mean is 2x4. The first block is formed by the first 4 columns, the next one by the next 4 columns and so on. So my blocks are: array([[0, 1, 2, 3], [12, 13, 14, 15]

Efficiently compute mean and standard deviation from a frequency table

ⅰ亾dé卋堺 提交于 2019-12-10 03:40:00
问题 Suppose I have the following frequency table. > print(dat) V1 V2 1 1 11613 2 2 6517 3 3 2442 4 4 687 5 5 159 6 6 29 # V1 = Score # V2 = Frequency How can I efficiently compute the Mean and standard deviation? Yielding: SD=0.87 MEAN=1.66. Replicating the score by frequency takes too long to compute. 回答1: Mean is easy. SD is a little trickier (can't just use fastmean() again because there's an n-1 in the denominator. > dat <- data.frame(freq=seq(6),value=runif(6)*100) > fastmean <- function(dat

TB3_Autorace之交通杆检测

回眸只為那壹抹淺笑 提交于 2019-12-10 01:31:47
利用blob检测算法识别交通杆,控制TB3机器人完成对交通杆的起停动作! 上一篇博文中 《TB3_Autorace之路标检测》 订阅了原始图像信息,经过SIFT检测识别出道路交通标志,这里我们同样订阅树莓派摄像头的原始图像信息对交通杆进行识别,同时我们还订阅了交通杆的状态信息以及任务完成信息,实现杆落即停,杆起即过的功能。 1234567891011121314 self.sub_image_type = "raw" self.pub_image_type = "raw" if self.sub_image_type == "compressed": self.sub_image_original = rospy.Subscriber('/detect/image_input/compressed', CompressedImage, self.cbGetImage, queue_size=1)elif self.sub_image_type == "raw": self.sub_image_original = rospy.Subscriber('/detect/image_input', Image, self.cbGetImage, queue_size=1)self.sub_level_crossing_order = rospy.Subscriber('/detect