-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIQR.py
106 lines (106 loc) · 3.25 KB
/
IQR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNF26FwKGA7xvemx/8UN6Hz",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/ashithapallath/Feature-Engineering/blob/main/IQR.py\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"IQR"
],
"metadata": {
"id": "Q7gNG6a4GzYp"
}
},
{
"cell_type": "code",
"source": [
"def calculate_iqr(data):\n",
" sorted_data = sorted(data)\n",
" n = len(sorted_data)\n",
"\n",
" # Calculate quartiles and IQR\n",
" q1_index = n // 4\n",
" q3_index = 3 * n // 4\n",
" q1 = sorted_data[q1_index]\n",
" q3 = sorted_data[q3_index]\n",
" iqr = q3 - q1\n",
"\n",
" # Define lower and upper fences for outliers\n",
" lower_fence = q1 - 1.5 * iqr\n",
" upper_fence = q3 + 1.5 * iqr\n",
"\n",
" # Identify outliers\n",
" outliers = [x for x in sorted_data if x < lower_fence or x > upper_fence]\n",
"\n",
" # Calculate median\n",
" median = (sorted_data[(n - 1) // 2] + sorted_data[n // 2]) / 2\n",
"\n",
" return q1, q3, iqr, lower_fence, upper_fence, outliers, median\n",
"\n",
"# Get user input for data\n",
"user_input = input(\"Enter your dataset (comma-separated values): \")\n",
"data = [float(x.strip()) for x in user_input.split(\",\")]\n",
"\n",
"# Calculate quartiles, IQR, fences, median, and identify outliers\n",
"q1, q3, iqr, lower_fence, upper_fence, outliers, median = calculate_iqr(data)\n",
"\n",
"# Print results\n",
"print(f\"First Quartile (Q1): {q1}\")\n",
"print(f\"Third Quartile (Q3): {q3}\")\n",
"print(f\"IQR: {iqr}\")\n",
"print(f\"Lower Fence: {lower_fence}\")\n",
"print(f\"Upper Fence: {upper_fence}\")\n",
"print(f\"Median: {median}\")\n",
"print(f\"outliers: {outliers}\")\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "MiE96wI4JJdJ",
"outputId": "bc5e8052-c4a0-4f5f-dd1a-c8aeb4cef810"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Enter your dataset (comma-separated values): 26,37,24,28,35,22,31,53,41,64,29\n",
"First Quartile (Q1): 26.0\n",
"Third Quartile (Q3): 41.0\n",
"IQR: 15.0\n",
"Lower Fence: 3.5\n",
"Upper Fence: 63.5\n",
"Median: 31.0\n",
"outliers: [64.0]\n"
]
}
]
}
]
}