Question

Can you write the function in python? def remove_outliers(data, num_outliers): # When analyzing data collected as...

Can you write the function in python?

def remove_outliers(data, num_outliers):
# When analyzing data collected as a part of a science experiment it
# may be desriable to remove the most extreme values before performing
# other calculations. Complete this function which takes a list of
# values and an non-negative integer, num_outliers, as its parameters.
# The function should create a new copy of the list with the num_outliers
# largest elements and the num_outliers smallest elements removed.
# Then it should return teh new copy of the list as the function's only
# result. The order of the elements in the returned list does not have to
# match the order of the lemetns in the original list.
# input1: data (list)
# input2: num_outliers (int)

# output: list

It has to pass this test.

import random
random.seed(1234)
data = [random.randint(50, 150) for ele in range(100)]
data[45] = 1
data[46] = 2
data[90] = 250
data[34] = 300

result = [50, 50, 51, 52, 52, 52, 53, 53, 54,
55, 55, 55, 55, 56, 58, 58, 59, 59, 59, 60, 61, 61,
61, 62, 64, 64, 64, 68, 68, 68, 69, 69, 69, 70, 71,
72, 73, 75, 77, 80, 81, 81, 84, 84, 84, 85, 88, 88, 89,
92, 94, 94, 95, 95, 98, 103, 106, 108, 108, 109, 109, 109,
110, 111, 111, 112, 113, 114, 115, 115, 117, 117, 119, 121,
124, 124, 125, 126, 128, 129, 132, 132, 133, 134, 135, 135,
135, 136, 138, 140, 141, 148, 148, 149, 149, 150]

assert remove_outliers(data, 2) == result

Homework Answers

Answer #1

def remove_outliers(data, num_outliers):
copy = data[:]
copy=sorted(copy)
copy=copy[num_outliers:len(data)-num_outliers]
return copy

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the below vector x, which you can copy and paste directly into Matlab. The vector...
Consider the below vector x, which you can copy and paste directly into Matlab. The vector contains the final grades for each student in a large linear algebra course. x = [61 52 63 58 66 92 64 55 76 60 70 78 76 73 45 63 97 70 100 76 50 64 42 100 67 81 81 59 68 62 72 99 66 76 81 59 47 84 67 75 63 86 73 44 51 69 48 74 61...
What can we say about Company's current financial condition with below data between the tears 2009...
What can we say about Company's current financial condition with below data between the tears 2009 to 2015? Company A 2009 2010 2011 2012 2013 2014 2015 Collection period 51 76 59 64 70 52 67 Inventory period 47 52 55 56 62 59 56 Payment period 36 49 46 50 59 56 92 Cashtocashperiod 62 79 68 70 73 55 31 Company B 2009 2010 2011 2012 2013 2014 2015 Collection period 66 82 90 110 111 115 154...
You want to see if there is a statistical difference between the two groups. Run descriptives...
You want to see if there is a statistical difference between the two groups. Run descriptives and a two-tailed, two sample assuming equal variance t-test. Here's your data: Weight of Apples in Grams Apple ID Farm A Farm B 1 131 151 2 147 159 3 134 162 4 134 158 5 136 159 6 137 160 7 140 150 8 134 160 9 136 160 10 133 160 11 134 160 12 132 158 13 139 162 14 136...
A business analyst wants to know if a new website design compared to an old website...
A business analyst wants to know if a new website design compared to an old website design is showing increased page views per visit. Two different samples of customers are randomly sent to one of two different websites, offering the same products, but with different designs. Is there a difference in average page views between the new and old website design? Use data from "website" sheet in excel. Which of the following was the p-value you used for this statistical...
Refer to the accompanying data set and construct a 95​% confidence interval estimate of the mean...
Refer to the accompanying data set and construct a 95​% confidence interval estimate of the mean pulse rate of adult​ females; then do the same for adult males. Compare the results. Males    Females 81           82 72           94 52           57 59           66 51           54 62           80 52           77 74           85 51           89 62           57 73           35 61           64 62           87 80           74 80           79 63           62 63           67 97           76 43           60 85           65 72           86 66           85 73           69 72          ...
The following data represent trunk circumferences (in mm) for a random sample of 60 four-year-old apple...
The following data represent trunk circumferences (in mm) for a random sample of 60 four-year-old apple trees at East Malling Agriculture Research Station in England.† 108 99 106 102 115 120 120 117 122 142 106 111 119 109 125 108 116 105 117 123 103 114 101 99 112 120 108 91 115 109 114 105 99 122 106 113 114 75 96 124 91 102 108 110 83 90 69 117 84 142 122 113 105 112 117...
Many standard statistical methods that you will study in Part II of this book are intended...
Many standard statistical methods that you will study in Part II of this book are intended for use with distributions that are symmetric and have no outliers. These methods start with the mean and standard deviation, x and s. For example, standard methods would typically be used for the IQ and GPA data here data215.dat. (a) Find x and s for the IQ data. (Round your answers to two decimal places.) s= Here are the numbers obs gpa iq gender...
have a java application need to create an application which is able to do some analysis...
have a java application need to create an application which is able to do some analysis on temperature data stored in a data file. You will be given the “temperatures.dat” data file which contains the data you must analyze. The analysis you’ll need to do is: Total number of data points Find coldest temperature Find warmest temperature Find average temperature Find the frequency of each temperature Find the most frequent temperature Find the least frequent temperature All classes must be...
#10.     You are to determine if a new online teaching model is better than the existing...
#10.     You are to determine if a new online teaching model is better than the existing model, labelled old. You administer random tests to a pre-selected group of students first for material taught the old model and then under the new model. The data is given in the Excel file and is labelled problem # 10. Carry out an appropriate procedure in hypothesis testing to determine if the new model is more effective for learning. new old 78 4 86...
A business analyst wants to know if a new website design compared to an old website...
A business analyst wants to know if a new website design compared to an old website design is showing increased page views per visit. Two different samples of customers are randomly sent to one of two different websites, offering the same products, but with different designs. Is there a difference in average page views between the new and old website design? Use data from "website" sheet in excel. Run the appropriate statistical test using α=0.01 . Which of the following...