myawesomepkg 0.1.3__py3-none-any.whl → 0.1.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- myawesomepkg/TSAPY1/10-A_Load_stringr.py +77 -0
- myawesomepkg/TSAPY1/10-B_Forcats.py +70 -0
- myawesomepkg/TSAPY1/9A_Dplyr.py +85 -0
- myawesomepkg/TSAPY1/9B_Tidyr.py +71 -0
- myawesomepkg/TSAPY1/Print_R.py +123 -0
- myawesomepkg/TSAPY1/R_Graph.py +32 -0
- myawesomepkg/TSAPY1/Working_Ggplot.py +53 -0
- myawesomepkg/TSAPY1/practical_no_3.py +167 -0
- myawesomepkg/TSAPY1/practical_no_4.py +215 -0
- myawesomepkg/TSAPY1/practical_no_4b.py +78 -0
- myawesomepkg/TSAPY1/practical_no_5_ac_and_pca.py +39 -0
- myawesomepkg/TSAPY1/practical_no_6.py +37 -0
- myawesomepkg/TSAPY1/practical_no_7.py +69 -0
- myawesomepkg/TSAPY1/practical_no_8.py +79 -0
- myawesomepkg/TSAPY1/tsa_practical_no_1.py +287 -0
- myawesomepkg/TSAPY1/tsa_practical_no_2.py +121 -0
- myawesomepkg-0.1.5.dist-info/METADATA +12 -0
- myawesomepkg-0.1.5.dist-info/RECORD +32 -0
- myawesomepkg/d.py +0 -36
- myawesomepkg-0.1.3.dist-info/METADATA +0 -7
- myawesomepkg-0.1.3.dist-info/RECORD +0 -17
- /myawesomepkg/{TSAPY → TSAPY1}/Practical No 1.py +0 -0
- /myawesomepkg/{TSAPY → TSAPY1}/Practical No 2.py +0 -0
- /myawesomepkg/{TSAPY → TSAPY1}/Practical No 3.py +0 -0
- /myawesomepkg/{TSAPY → TSAPY1}/Practical No 4 A.py +0 -0
- /myawesomepkg/{TSAPY → TSAPY1}/Practical No 4 B.py +0 -0
- /myawesomepkg/{TSAPY → TSAPY1}/Practical No 5.py +0 -0
- /myawesomepkg/{TSAPY → TSAPY1}/Practical No 6.py +0 -0
- /myawesomepkg/{TSAPY → TSAPY1}/Practical No 7.py +0 -0
- /myawesomepkg/{TSAPY → TSAPY1}/Practical No 8.py +0 -0
- /myawesomepkg/{TSAPY → TSAPY1}/__init__.py +0 -0
- {myawesomepkg-0.1.3.dist-info → myawesomepkg-0.1.5.dist-info}/WHEEL +0 -0
- {myawesomepkg-0.1.3.dist-info → myawesomepkg-0.1.5.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,77 @@
|
|
1
|
+
✅ Step 1: Load stringr
|
2
|
+
r
|
3
|
+
Copy
|
4
|
+
Edit
|
5
|
+
library(stringr)
|
6
|
+
🔹 1. String Basics
|
7
|
+
r
|
8
|
+
Copy
|
9
|
+
Edit
|
10
|
+
str <- "Hello Boss"
|
11
|
+
str_length(str) # Length of string
|
12
|
+
str_to_upper(str) # Uppercase
|
13
|
+
str_to_lower(str) # Lowercase
|
14
|
+
str_trim(" Hello ") # Trim spaces
|
15
|
+
🔹 2. Combining Strings
|
16
|
+
r
|
17
|
+
Copy
|
18
|
+
Edit
|
19
|
+
str1 <- "Hello"
|
20
|
+
str2 <- "Boss"
|
21
|
+
str_c(str1, str2, sep = " ") # Combine with space
|
22
|
+
🔹 3. Subsetting Strings
|
23
|
+
r
|
24
|
+
Copy
|
25
|
+
Edit
|
26
|
+
str_sub("DataScience", 1, 4) # "Data"
|
27
|
+
str_sub("DataScience", -7, -1) # "Science"
|
28
|
+
🔹 4. Locales
|
29
|
+
r
|
30
|
+
Copy
|
31
|
+
Edit
|
32
|
+
str_to_upper("straße", locale = "de") # German-specific case
|
33
|
+
🔹 5. Basic Matches
|
34
|
+
r
|
35
|
+
Copy
|
36
|
+
Edit
|
37
|
+
str_detect("apple", "pp") # TRUE
|
38
|
+
str_detect("apple", "z") # FALSE
|
39
|
+
🔹 6. Anchors (^, $)
|
40
|
+
r
|
41
|
+
Copy
|
42
|
+
Edit
|
43
|
+
str_detect("Boss is here", "^Boss") # TRUE
|
44
|
+
str_detect("Boss is here", "here$") # TRUE
|
45
|
+
🔹 7. Repetition
|
46
|
+
r
|
47
|
+
Copy
|
48
|
+
Edit
|
49
|
+
str_view("banana", "na{2}") # Match "n" followed by two "a"s
|
50
|
+
str_view("aaa", "a{2,3}") # Match 2 to 3 a's
|
51
|
+
🔹 8. Detect Matches
|
52
|
+
r
|
53
|
+
Copy
|
54
|
+
Edit
|
55
|
+
texts <- c("cat", "dog", "cow")
|
56
|
+
str_detect(texts, "c") # TRUE FALSE TRUE
|
57
|
+
🔹 9. Extract Matches
|
58
|
+
r
|
59
|
+
Copy
|
60
|
+
Edit
|
61
|
+
str_extract("Price: Rs 999", "\\d+") # "999"
|
62
|
+
🔹 10. Grouped Matches
|
63
|
+
r
|
64
|
+
Copy
|
65
|
+
Edit
|
66
|
+
str_match("ID: 12345", "ID: (\\d+)") # Group match returns matrix
|
67
|
+
🔹 11. Replacing Matches
|
68
|
+
r
|
69
|
+
Copy
|
70
|
+
Edit
|
71
|
+
str_replace("I love cats", "cats", "dogs") # "I love dogs"
|
72
|
+
🔹 12. Splitting
|
73
|
+
r
|
74
|
+
Copy
|
75
|
+
Edit
|
76
|
+
str_split("one,two,three", ",")[[1]] # "one" "two" "three"
|
77
|
+
Let me know if you want all these examples as a downloadable .R script or a reference
|
@@ -0,0 +1,70 @@
|
|
1
|
+
✅ Step 1: Load forcats package
|
2
|
+
r
|
3
|
+
Copy
|
4
|
+
Edit
|
5
|
+
library(forcats)
|
6
|
+
🔹 1. Creating Factors
|
7
|
+
r
|
8
|
+
Copy
|
9
|
+
Edit
|
10
|
+
grades <- c("B", "A", "C", "A", "B")
|
11
|
+
f_grades <- factor(grades)
|
12
|
+
f_grades
|
13
|
+
With specified order:
|
14
|
+
|
15
|
+
r
|
16
|
+
Copy
|
17
|
+
Edit
|
18
|
+
f_grades <- factor(grades, levels = c("A", "B", "C"), ordered = TRUE)
|
19
|
+
🔹 2. Modifying Factor Orders
|
20
|
+
r
|
21
|
+
Copy
|
22
|
+
Edit
|
23
|
+
# Reorder by frequency
|
24
|
+
fct_infreq(f_grades)
|
25
|
+
|
26
|
+
# Reorder manually
|
27
|
+
fct_relevel(f_grades, "C", "B", "A")
|
28
|
+
🔹 3. Modifying Factor Levels (Renaming)
|
29
|
+
r
|
30
|
+
Copy
|
31
|
+
Edit
|
32
|
+
fct_recode(f_grades,
|
33
|
+
"Excellent" = "A",
|
34
|
+
"Good" = "B",
|
35
|
+
"Average" = "C")
|
36
|
+
🔹 4. Lump Less Frequent Levels
|
37
|
+
r
|
38
|
+
Copy
|
39
|
+
Edit
|
40
|
+
items <- c("apple", "banana", "apple", "cherry", "banana", "fig", "fig", "fig")
|
41
|
+
f_items <- factor(items)
|
42
|
+
|
43
|
+
# Combine less frequent into "Other"
|
44
|
+
fct_lump(f_items, n = 2)
|
45
|
+
🔹 5. Drop Unused Levels
|
46
|
+
r
|
47
|
+
Copy
|
48
|
+
Edit
|
49
|
+
f <- factor(c("high", "medium", "low"), levels = c("low", "medium", "high", "extreme"))
|
50
|
+
f_dropped <- fct_drop(f)
|
51
|
+
🔹 6. Reverse Factor Order
|
52
|
+
r
|
53
|
+
Copy
|
54
|
+
Edit
|
55
|
+
fct_rev(f_grades)
|
56
|
+
🔹 7. Count Factors
|
57
|
+
r
|
58
|
+
Copy
|
59
|
+
Edit
|
60
|
+
fct_count(f_grades)
|
61
|
+
📌 Summary of Key forcats Functions
|
62
|
+
|
63
|
+
Function Use Case
|
64
|
+
fct_relevel() Change order of levels manually
|
65
|
+
fct_infreq() Order by frequency
|
66
|
+
fct_recode() Rename factor levels
|
67
|
+
fct_lump() Combine low-freq levels
|
68
|
+
fct_drop() Drop unused levels
|
69
|
+
fct_rev() Reverse order
|
70
|
+
fct_count() Count frequencies
|
@@ -0,0 +1,85 @@
|
|
1
|
+
✅ Step 1: Load dplyr
|
2
|
+
r
|
3
|
+
Copy
|
4
|
+
Edit
|
5
|
+
library(dplyr)
|
6
|
+
🔹 Sample Data
|
7
|
+
r
|
8
|
+
Copy
|
9
|
+
Edit
|
10
|
+
employees <- data.frame(
|
11
|
+
emp_id = c(1, 2, 3, 4, 5),
|
12
|
+
name = c("John", "Emma", "Raj", "Sara", "Mike"),
|
13
|
+
dept_id = c(10, 20, 10, 30, 20)
|
14
|
+
)
|
15
|
+
|
16
|
+
departments <- data.frame(
|
17
|
+
dept_id = c(10, 20, 30),
|
18
|
+
dept_name = c("HR", "Finance", "IT")
|
19
|
+
)
|
20
|
+
🔹 1. Filtering Rows
|
21
|
+
r
|
22
|
+
Copy
|
23
|
+
Edit
|
24
|
+
# Filter employees from dept 10
|
25
|
+
employees %>%
|
26
|
+
filter(dept_id == 10)
|
27
|
+
🔹 2. Mutating Joins (left_join)
|
28
|
+
r
|
29
|
+
Copy
|
30
|
+
Edit
|
31
|
+
# Add department name to employees
|
32
|
+
employees %>%
|
33
|
+
left_join(departments, by = "dept_id")
|
34
|
+
🔹 3. Inner Join
|
35
|
+
r
|
36
|
+
Copy
|
37
|
+
Edit
|
38
|
+
# Only matching employees with department info
|
39
|
+
employees %>%
|
40
|
+
inner_join(departments, by = "dept_id")
|
41
|
+
🔹 4. Handling Duplicate Keys
|
42
|
+
r
|
43
|
+
Copy
|
44
|
+
Edit
|
45
|
+
# Add a duplicate dept row
|
46
|
+
departments2 <- rbind(departments, data.frame(dept_id = 10, dept_name = "HR-Duplicate"))
|
47
|
+
|
48
|
+
# Join - will create multiple rows for duplicate keys
|
49
|
+
employees %>%
|
50
|
+
left_join(departments2, by = "dept_id")
|
51
|
+
🔹 5. Defining Key Column (custom join keys)
|
52
|
+
r
|
53
|
+
Copy
|
54
|
+
Edit
|
55
|
+
emp <- data.frame(id = c(1, 2), val = c("A", "B"))
|
56
|
+
ref <- data.frame(key = c(1, 2), desc = c("X", "Y"))
|
57
|
+
|
58
|
+
emp %>%
|
59
|
+
left_join(ref, by = c("id" = "key"))
|
60
|
+
🔹 6. Filtering Joins
|
61
|
+
r
|
62
|
+
Copy
|
63
|
+
Edit
|
64
|
+
# Semi Join: Keep rows in employees that match departments
|
65
|
+
employees %>%
|
66
|
+
semi_join(departments, by = "dept_id")
|
67
|
+
|
68
|
+
# Anti Join: Keep rows in employees that don't match departments
|
69
|
+
employees %>%
|
70
|
+
anti_join(departments, by = "dept_id")
|
71
|
+
🔹 7. Set Operations
|
72
|
+
r
|
73
|
+
Copy
|
74
|
+
Edit
|
75
|
+
a <- data.frame(x = c(1, 2, 3))
|
76
|
+
b <- data.frame(x = c(2, 3, 4))
|
77
|
+
|
78
|
+
# Union (unique values)
|
79
|
+
union(a, b)
|
80
|
+
|
81
|
+
# Intersect (common values)
|
82
|
+
intersect(a, b)
|
83
|
+
|
84
|
+
# Set difference (in a but not in b)
|
85
|
+
setdiff(a, b)
|
@@ -0,0 +1,71 @@
|
|
1
|
+
✅ Step 1: Load tidyr and dplyr
|
2
|
+
r
|
3
|
+
Copy
|
4
|
+
Edit
|
5
|
+
library(tidyr)
|
6
|
+
library(dplyr)
|
7
|
+
🔹 Sample Data
|
8
|
+
r
|
9
|
+
Copy
|
10
|
+
Edit
|
11
|
+
data <- data.frame(
|
12
|
+
name = c("Alice", "Bob"),
|
13
|
+
math = c(90, 85),
|
14
|
+
science = c(95, 80)
|
15
|
+
)
|
16
|
+
🔹 1. Gathering → pivot_longer()
|
17
|
+
r
|
18
|
+
Copy
|
19
|
+
Edit
|
20
|
+
data_long <- data %>%
|
21
|
+
pivot_longer(cols = c(math, science), names_to = "subject", values_to = "score")
|
22
|
+
|
23
|
+
print(data_long)
|
24
|
+
🔹 2. Spreading → pivot_wider()
|
25
|
+
r
|
26
|
+
Copy
|
27
|
+
Edit
|
28
|
+
data_wide <- data_long %>%
|
29
|
+
pivot_wider(names_from = subject, values_from = score)
|
30
|
+
|
31
|
+
print(data_wide)
|
32
|
+
🔹 3. Separate Columns
|
33
|
+
r
|
34
|
+
Copy
|
35
|
+
Edit
|
36
|
+
full_name <- data.frame(name = c("Alice_Smith", "Bob_Jones"))
|
37
|
+
|
38
|
+
# Separate name into first and last
|
39
|
+
full_name_sep <- full_name %>%
|
40
|
+
separate(name, into = c("first_name", "last_name"), sep = "_")
|
41
|
+
|
42
|
+
print(full_name_sep)
|
43
|
+
🔹 4. Unite Columns
|
44
|
+
r
|
45
|
+
Copy
|
46
|
+
Edit
|
47
|
+
# Combine first_name and last_name
|
48
|
+
full_name_united <- full_name_sep %>%
|
49
|
+
unite("full_name", first_name, last_name, sep = " ")
|
50
|
+
|
51
|
+
print(full_name_united)
|
52
|
+
🔹 5. Handling Missing Values
|
53
|
+
r
|
54
|
+
Copy
|
55
|
+
Edit
|
56
|
+
missing_data <- data.frame(
|
57
|
+
name = c("A", "B", "C"),
|
58
|
+
score = c(85, NA, 90)
|
59
|
+
)
|
60
|
+
|
61
|
+
# Remove rows with NA
|
62
|
+
missing_data_clean <- missing_data %>%
|
63
|
+
drop_na()
|
64
|
+
|
65
|
+
# Replace NA with value
|
66
|
+
missing_data_filled <- missing_data %>%
|
67
|
+
replace_na(list(score = 0))
|
68
|
+
|
69
|
+
print(missing_data_clean)
|
70
|
+
print(missing_data_filled)
|
71
|
+
Let m
|
@@ -0,0 +1,123 @@
|
|
1
|
+
1. Print in R
|
2
|
+
r
|
3
|
+
Copy
|
4
|
+
Edit
|
5
|
+
print("Hello Boss!")
|
6
|
+
🔹 2. Comments in R
|
7
|
+
r
|
8
|
+
Copy
|
9
|
+
Edit
|
10
|
+
# This is a single-line comment
|
11
|
+
🔹 3. Variables in R
|
12
|
+
r
|
13
|
+
Copy
|
14
|
+
Edit
|
15
|
+
x <- 10
|
16
|
+
y <- "Data"
|
17
|
+
🔹 4. Concatenate Elements
|
18
|
+
r
|
19
|
+
Copy
|
20
|
+
Edit
|
21
|
+
v <- c(1, 2, 3, 4)
|
22
|
+
print(v)
|
23
|
+
🔹 5. Multiple Variables
|
24
|
+
r
|
25
|
+
Copy
|
26
|
+
Edit
|
27
|
+
a <- 5
|
28
|
+
b <- 10
|
29
|
+
c <- a + b
|
30
|
+
print(c)
|
31
|
+
🔹 6. Variable Names
|
32
|
+
r
|
33
|
+
Copy
|
34
|
+
Edit
|
35
|
+
user_name <- "Boss"
|
36
|
+
user_age <- 25
|
37
|
+
🔹 7. Data Types
|
38
|
+
r
|
39
|
+
Copy
|
40
|
+
Edit
|
41
|
+
num <- 10.5 # Numeric
|
42
|
+
str <- "Hello" # Character
|
43
|
+
bool <- TRUE # Logical
|
44
|
+
vec <- c(1, 2, 3) # Vector
|
45
|
+
🔹 8. Strings
|
46
|
+
r
|
47
|
+
Copy
|
48
|
+
Edit
|
49
|
+
name <- "R Programming"
|
50
|
+
paste("Welcome to", name)
|
51
|
+
🔹 9. Boolean
|
52
|
+
r
|
53
|
+
Copy
|
54
|
+
Edit
|
55
|
+
is_true <- TRUE
|
56
|
+
is_false <- FALSE
|
57
|
+
🔹 10. Operators
|
58
|
+
r
|
59
|
+
Copy
|
60
|
+
Edit
|
61
|
+
a <- 10
|
62
|
+
b <- 3
|
63
|
+
a + b # Addition
|
64
|
+
a > b # Comparison
|
65
|
+
a == b # Equal
|
66
|
+
a %% b # Modulus
|
67
|
+
🔹 11. If Else
|
68
|
+
r
|
69
|
+
Copy
|
70
|
+
Edit
|
71
|
+
x <- 10
|
72
|
+
if (x > 5) {
|
73
|
+
print("Greater than 5")
|
74
|
+
} else {
|
75
|
+
print("5 or less")
|
76
|
+
}
|
77
|
+
🔹 12. List
|
78
|
+
r
|
79
|
+
Copy
|
80
|
+
Edit
|
81
|
+
my_list <- list(name="Boss", age=25, scores=c(90, 85))
|
82
|
+
print(my_list)
|
83
|
+
🔹 13. Matrices
|
84
|
+
r
|
85
|
+
Copy
|
86
|
+
Edit
|
87
|
+
matrix_data <- matrix(1:6, nrow=2, ncol=3)
|
88
|
+
print(matrix_data)
|
89
|
+
🔹 14. Data Frames
|
90
|
+
r
|
91
|
+
Copy
|
92
|
+
Edit
|
93
|
+
df <- data.frame(Name=c("A", "B"), Age=c(20, 25))
|
94
|
+
print(df)
|
95
|
+
🔹 15. Functions
|
96
|
+
r
|
97
|
+
Copy
|
98
|
+
Edit
|
99
|
+
add_numbers <- function(x, y) {
|
100
|
+
return(x + y)
|
101
|
+
}
|
102
|
+
🔹 16. Call a Function
|
103
|
+
r
|
104
|
+
Copy
|
105
|
+
Edit
|
106
|
+
result <- add_numbers(5, 3)
|
107
|
+
print(result)
|
108
|
+
🔹 17. Global Variable
|
109
|
+
r
|
110
|
+
Copy
|
111
|
+
Edit
|
112
|
+
x <- 5
|
113
|
+
my_func <- function() {
|
114
|
+
x <<- 10 # Modify global x
|
115
|
+
}
|
116
|
+
my_func()
|
117
|
+
print(x)
|
118
|
+
🔹 18. Vectors
|
119
|
+
r
|
120
|
+
Copy
|
121
|
+
Edit
|
122
|
+
my_vector <- c(1, 2, 3, 4, 5)
|
123
|
+
print(my_vector)
|
@@ -0,0 +1,32 @@
|
|
1
|
+
1. Line Plot
|
2
|
+
r
|
3
|
+
Copy
|
4
|
+
Edit
|
5
|
+
x <- c(1, 2, 3, 4, 5)
|
6
|
+
y <- c(2, 4, 6, 8, 10)
|
7
|
+
|
8
|
+
plot(x, y, type="l", col="blue", main="Line Plot", xlab="X-axis", ylab="Y-axis")
|
9
|
+
🔹 2. Scatter Plot
|
10
|
+
r
|
11
|
+
Copy
|
12
|
+
Edit
|
13
|
+
x <- c(1, 2, 3, 4, 5)
|
14
|
+
y <- c(5, 3, 6, 2, 7)
|
15
|
+
|
16
|
+
plot(x, y, main="Scatter Plot", xlab="X", ylab="Y", col="red", pch=19)
|
17
|
+
🔹 3. Pie Chart
|
18
|
+
r
|
19
|
+
Copy
|
20
|
+
Edit
|
21
|
+
slices <- c(10, 20, 30, 40)
|
22
|
+
labels <- c("A", "B", "C", "D")
|
23
|
+
|
24
|
+
pie(slices, labels=labels, main="Pie Chart")
|
25
|
+
🔹 4. Bar Chart
|
26
|
+
r
|
27
|
+
Copy
|
28
|
+
Edit
|
29
|
+
values <- c(5, 10, 15, 20)
|
30
|
+
names <- c("A", "B", "C", "D")
|
31
|
+
|
32
|
+
barplot(values, names.arg=names, col="green", main="Bar Chart", ylab="Values")
|
@@ -0,0 +1,53 @@
|
|
1
|
+
✅ Step 1: Install & Load ggplot2
|
2
|
+
r
|
3
|
+
Copy
|
4
|
+
Edit
|
5
|
+
install.packages("ggplot2") # Run once
|
6
|
+
library(ggplot2)
|
7
|
+
✅ Step 2: Sample Data
|
8
|
+
r
|
9
|
+
Copy
|
10
|
+
Edit
|
11
|
+
data <- data.frame(
|
12
|
+
category = rep(c("A", "B", "C"), each=4),
|
13
|
+
subcat = rep(c("X", "Y"), times=6),
|
14
|
+
value = c(4, 7, 6, 9, 5, 3, 8, 4, 7, 5, 6, 2)
|
15
|
+
)
|
16
|
+
✅ Step 3: Basic ggplot
|
17
|
+
r
|
18
|
+
Copy
|
19
|
+
Edit
|
20
|
+
ggplot(data, aes(x=subcat, y=value)) +
|
21
|
+
geom_bar(stat="identity", fill="steelblue") +
|
22
|
+
ggtitle("Basic Bar Chart")
|
23
|
+
✅ Step 4: Facets
|
24
|
+
r
|
25
|
+
Copy
|
26
|
+
Edit
|
27
|
+
ggplot(data, aes(x=subcat, y=value)) +
|
28
|
+
geom_bar(stat="identity", fill="tomato") +
|
29
|
+
facet_wrap(~ category) +
|
30
|
+
ggtitle("Faceted by Category")
|
31
|
+
✅ Step 5: Geometric Objects
|
32
|
+
r
|
33
|
+
Copy
|
34
|
+
Edit
|
35
|
+
ggplot(data, aes(x=subcat, y=value, fill=category)) +
|
36
|
+
geom_bar(stat="identity", position="dodge") + # Bar chart
|
37
|
+
geom_point(aes(color=category), size=3, shape=21) + # Add points
|
38
|
+
ggtitle("Geometric Objects: Bars + Points")
|
39
|
+
✅ Step 6: Position Adjustment
|
40
|
+
r
|
41
|
+
Copy
|
42
|
+
Edit
|
43
|
+
ggplot(data, aes(x=subcat, y=value, fill=category)) +
|
44
|
+
geom_bar(stat="identity", position=position_dodge(width=0.7)) +
|
45
|
+
ggtitle("Position: Dodge for Side-by-Side Bars")
|
46
|
+
✅ Step 7: Coordinate System (Flip Axis)
|
47
|
+
r
|
48
|
+
Copy
|
49
|
+
Edit
|
50
|
+
ggplot(data, aes(x=subcat, y=value, fill=category)) +
|
51
|
+
geom_bar(stat="identity") +
|
52
|
+
coord_flip() +
|
53
|
+
ggtitle("Flipped Coordinates")
|
@@ -0,0 +1,167 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
"""Practical No 3.ipynb
|
3
|
+
|
4
|
+
Automatically generated by Colab.
|
5
|
+
|
6
|
+
Original file is located at
|
7
|
+
https://colab.research.google.com/drive/1aCeoKiFV3QbdraoAWb562bOjZDdBw0be
|
8
|
+
|
9
|
+
## **Practical No 3:**
|
10
|
+
# Aim: Detrending, deseasonalizing timeseries, detecting Cyclic variations and decomposing Time Series.
|
11
|
+
|
12
|
+
## Trend
|
13
|
+
"""
|
14
|
+
|
15
|
+
# Commented out IPython magic to ensure Python compatibility.
|
16
|
+
import pandas as pd
|
17
|
+
# %matplotlib inline
|
18
|
+
from statsmodels.tsa.filters.hp_filter import hpfilter
|
19
|
+
df = pd.read_excel(r'/content/drive/MyDrive/MScDS TSA/India_Exchange_Rate_Dataset.xls',index_col=0,parse_dates=True)
|
20
|
+
EXINUS_cycle,EXINUS_trend = hpfilter(df['EXINUS'], lamb=1600)
|
21
|
+
EXINUS_trend.plot(figsize=(15,6)).autoscale(axis='x',tight=True)
|
22
|
+
|
23
|
+
"""**Detrending using Differencing**"""
|
24
|
+
|
25
|
+
# Commented out IPython magic to ensure Python compatibility.
|
26
|
+
import pandas as pd
|
27
|
+
import matplotlib.pyplot as plt
|
28
|
+
import warnings
|
29
|
+
warnings.filterwarnings("ignore")
|
30
|
+
# %matplotlib inline
|
31
|
+
df = pd.read_excel(r'/content/drive/MyDrive/MScDS TSA/India_Exchange_Rate_Dataset.xls',index_col=0,parse_dates=True)
|
32
|
+
diff = df.EXINUS.diff()
|
33
|
+
plt.figure(figsize=(15,6))
|
34
|
+
plt.plot(diff)
|
35
|
+
plt.title('Detrending using Differencing', fontsize=16)
|
36
|
+
plt.xlabel('Year')
|
37
|
+
plt.ylabel('EXINUS exchange rate')
|
38
|
+
plt.show()
|
39
|
+
|
40
|
+
"""** **bold text**Detrending using Scipy Signal**"""
|
41
|
+
|
42
|
+
# Commented out IPython magic to ensure Python compatibility.
|
43
|
+
import pandas as pd
|
44
|
+
import matplotlib.pyplot as plt
|
45
|
+
from scipy import signal
|
46
|
+
import warnings
|
47
|
+
warnings.filterwarnings("ignore")
|
48
|
+
# %matplotlib inline
|
49
|
+
df = pd.read_excel(r'/content/drive/MyDrive/MScDS TSA/India_Exchange_Rate_Dataset.xls',index_col=0,parse_dates=True)
|
50
|
+
detrended = signal.detrend(df.EXINUS.values)
|
51
|
+
plt.figure(figsize=(15,6))
|
52
|
+
plt.plot(detrended)
|
53
|
+
plt.xlabel('EXINUS')
|
54
|
+
plt.ylabel('Frequency')
|
55
|
+
plt.title('Detrending using Scipy Signal', fontsize=16)
|
56
|
+
plt.show()
|
57
|
+
|
58
|
+
"""**Detrending using HP Filter**"""
|
59
|
+
|
60
|
+
# Commented out IPython magic to ensure Python compatibility.
|
61
|
+
import pandas as pd
|
62
|
+
import matplotlib.pyplot as plt
|
63
|
+
from statsmodels.tsa.filters.hp_filter import hpfilter
|
64
|
+
import warnings
|
65
|
+
warnings.filterwarnings("ignore")
|
66
|
+
# %matplotlib inline
|
67
|
+
df = pd.read_excel(r'/content/drive/MyDrive/MScDS TSA/India_Exchange_Rate_Dataset.xls',index_col=0,parse_dates=True)
|
68
|
+
EXINUS_cycle,EXINUS_trend = hpfilter(df['EXINUS'], lamb=1600)
|
69
|
+
df['trend'] = EXINUS_trend
|
70
|
+
detrended = df.EXINUS - df['trend']
|
71
|
+
plt.figure(figsize=(15,6))
|
72
|
+
plt.plot(detrended)
|
73
|
+
plt.title('Detrending using HP Filter', fontsize=16)
|
74
|
+
plt.xlabel('Year')
|
75
|
+
plt.ylabel('EXINUS exchange rate')
|
76
|
+
plt.show()
|
77
|
+
|
78
|
+
"""## Seasonality
|
79
|
+
|
80
|
+
Methods can be used to detect seasonality:
|
81
|
+
a. Multiple box plots
|
82
|
+
b. Autocorrelation plots
|
83
|
+
|
84
|
+
A. **Multi Month-wise Box Plot**
|
85
|
+
"""
|
86
|
+
|
87
|
+
# Commented out IPython magic to ensure Python compatibility.
|
88
|
+
import pandas as pd
|
89
|
+
import seaborn as sns
|
90
|
+
import matplotlib.pyplot as plt
|
91
|
+
from statsmodels.tsa.filters.hp_filter import hpfilter
|
92
|
+
import warnings
|
93
|
+
warnings.filterwarnings("ignore")
|
94
|
+
# %matplotlib inline
|
95
|
+
df = pd.read_excel(r'/content/drive/MyDrive/MScDS TSA/India_Exchange_Rate_Dataset.xls',parse_dates=True)
|
96
|
+
df['month'] = df['observation_date'].dt.strftime('%b')
|
97
|
+
df['year'] = [d.year for d in df.observation_date]
|
98
|
+
df['month'] = [d.strftime('%b') for d in df.observation_date]
|
99
|
+
years = df['year'].unique()
|
100
|
+
plt.figure(figsize=(15,6))
|
101
|
+
sns.boxplot(x='month', y='EXINUS', data=df).set_title("Multi Month-wise Box Plot")
|
102
|
+
plt.show()
|
103
|
+
|
104
|
+
"""B. **Autocorrelation plot for seasonality**"""
|
105
|
+
|
106
|
+
# Commented out IPython magic to ensure Python compatibility.
|
107
|
+
from pandas.plotting import autocorrelation_plot
|
108
|
+
import pandas as pd
|
109
|
+
import matplotlib.pyplot as plt
|
110
|
+
# %matplotlib inline
|
111
|
+
df = pd.read_excel(r'/content/drive/MyDrive/MScDS TSA/India_Exchange_Rate_Dataset.xls',index_col=0,parse_dates=True)
|
112
|
+
#plt.rcParams.update({'figure.figsize':(15,6), 'figure.dpi':220})
|
113
|
+
autocorrelation_plot(df.EXINUS.tolist())
|
114
|
+
|
115
|
+
"""**Deseasoning Time series**"""
|
116
|
+
|
117
|
+
# Commented out IPython magic to ensure Python compatibility.
|
118
|
+
import pandas as pd
|
119
|
+
import matplotlib.pyplot as plt
|
120
|
+
from statsmodels.tsa.seasonal import seasonal_decompose
|
121
|
+
import warnings
|
122
|
+
warnings.filterwarnings("ignore")
|
123
|
+
# %matplotlib inline
|
124
|
+
df = pd.read_excel(r'/content/drive/MyDrive/MScDS TSA/India_Exchange_Rate_Dataset.xls',index_col=0,parse_dates=True)
|
125
|
+
result_mul = seasonal_decompose(df['EXINUS'], model='multiplicative', extrapolate_trend='freq')
|
126
|
+
deseason = df['EXINUS'] - result_mul.seasonal
|
127
|
+
plt.figure(figsize=(15,6))
|
128
|
+
plt.plot(deseason)
|
129
|
+
plt.title('Deseasoning using seasonal_decompose', fontsize=16)
|
130
|
+
plt.xlabel('Year')
|
131
|
+
plt.ylabel('EXINUS exchange rate')
|
132
|
+
plt.show()
|
133
|
+
|
134
|
+
"""**Detecting cyclical variation**"""
|
135
|
+
|
136
|
+
# Commented out IPython magic to ensure Python compatibility.
|
137
|
+
from statsmodels.tsa.filters.hp_filter import hpfilter
|
138
|
+
import pandas as pd
|
139
|
+
import matplotlib.pyplot as plt
|
140
|
+
import warnings
|
141
|
+
warnings.filterwarnings("ignore")
|
142
|
+
# %matplotlib inline
|
143
|
+
df = pd.read_excel(r'/content/drive/MyDrive/MScDS TSA/India_Exchange_Rate_Dataset.xls',index_col=0,parse_dates=True)
|
144
|
+
EXINUS_cycle,EXINUS_trend = hpfilter(df['EXINUS'], lamb=1600)
|
145
|
+
df['cycle'] =EXINUS_cycle
|
146
|
+
df['trend'] =EXINUS_trend
|
147
|
+
df[['cycle']].plot(figsize=(15,6)).autoscale(axis='x',tight=True)
|
148
|
+
plt.title('Extracting Cyclic Variations', fontsize=16)
|
149
|
+
plt.xlabel('Year')
|
150
|
+
plt.ylabel('EXINUS exchange rate')
|
151
|
+
plt.show()
|
152
|
+
|
153
|
+
"""**Decompose Time series**"""
|
154
|
+
|
155
|
+
# Commented out IPython magic to ensure Python compatibility.
|
156
|
+
from statsmodels.tsa.seasonal import seasonal_decompose
|
157
|
+
import pandas as pd
|
158
|
+
import matplotlib.pyplot as plt
|
159
|
+
import warnings
|
160
|
+
warnings.filterwarnings("ignore")
|
161
|
+
# %matplotlib inline
|
162
|
+
df = pd.read_excel(r'/content/drive/MyDrive/MScDS TSA/India_Exchange_Rate_Dataset.xls',
|
163
|
+
index_col=0,parse_dates=True)
|
164
|
+
result = seasonal_decompose(df['EXINUS'], model='add')
|
165
|
+
result.plot();
|
166
|
+
result = seasonal_decompose(df['EXINUS'], model='mul')
|
167
|
+
result.plot();
|