import numpy as np
# ma trận A
A = np.array([[1, 1, 3],
[5, 2, 6],
[-2, -1, -3]])
# Tạo ma trận zero 3x3
matrix_zero = np.zeros((3, 3))
# Tính A^3
A3 = np.linalg.matrix_power(A, 3)
# So sánh A^3 với ma trận zero
if np.array_equal(A3, matrix_zero):
print("equal")
else:
print("not equal")
## equal
# tính tổng cột 1 và cột 2
sum_col_1_2 = A[:, 0] + A[:, 1]
#trích xuất dữ liệu phầ tử
print(sum_col_1_2)
## [ 2 7 -3]
# thay thế cột 3 bằng tổng 2 cột 1 và 2
A[:, 2] = sum_col_1_2
print(A)
## [[ 1 1 2]
## [ 5 2 7]
## [-2 -1 -3]]
vertec_1 <- c(0,1,2,3,4)
vertec_2 <- c(0,1,2,3,4)
result <- outer(vertec_1, vertec_2,"+")
result
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0 1 2 3 4
## [2,] 1 2 3 4 5
## [3,] 2 3 4 5 6
## [4,] 3 4 5 6 7
## [5,] 4 5 6 7 8
`` ## Bài tập 3
# Định nghĩa ma trận A và vectơ y
A <- matrix(c(1, 2, 3, 4, 5, 2,1,2,3,4,3,2,1,2,3,4,3,2,1,2,5,4,3,2,1), nrow = 5, byrow = TRUE)
y <- c(7,-1,-3,5,17)
# In ra ma trận A và vectơ y
cat("Ma trận A:\n")
## Ma trận A:
print(A)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 2 1 2 3 4
## [3,] 3 2 1 2 3
## [4,] 4 3 2 1 2
## [5,] 5 4 3 2 1
cat("Vectơ y:\n")
## Vectơ y:
print(y)
## [1] 7 -1 -3 5 17
# Giải hệ phương trình Ax = y
x <- solve(A, y)
cat("nghiệ bài toán là: \n")
## nghiệ bài toán là:
x
## [1] -2 3 5 2 -4
total <- 0
for (i in range(20)) {
for (j in range(5) ) {
total <- (i^4)/(3+j)
}
}
print(total)
## [1] 20000
library(gapminder)
VN_data <- subset(gapminder, country == 'Vietnam')
VN_data
## # A tibble: 12 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Vietnam Asia 1952 40.4 26246839 605.
## 2 Vietnam Asia 1957 42.9 28998543 676.
## 3 Vietnam Asia 1962 45.4 33796140 772.
## 4 Vietnam Asia 1967 47.8 39463910 637.
## 5 Vietnam Asia 1972 50.3 44655014 700.
## 6 Vietnam Asia 1977 55.8 50533506 714.
## 7 Vietnam Asia 1982 58.8 56142181 707.
## 8 Vietnam Asia 1987 62.8 62826491 821.
## 9 Vietnam Asia 1992 67.7 69940728 989.
## 10 Vietnam Asia 1997 70.7 76048996 1386.
## 11 Vietnam Asia 2002 73.0 80908147 1764.
## 12 Vietnam Asia 2007 74.2 85262356 2442.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
aver_life_exp <- VN_data %>% group_by(year) %>% summarise(mean_life_expectancy = mean(lifeExp))
print(aver_life_exp)
## # A tibble: 12 × 2
## year mean_life_expectancy
## <int> <dbl>
## 1 1952 40.4
## 2 1957 42.9
## 3 1962 45.4
## 4 1967 47.8
## 5 1972 50.3
## 6 1977 55.8
## 7 1982 58.8
## 8 1987 62.8
## 9 1992 67.7
## 10 1997 70.7
## 11 2002 73.0
## 12 2007 74.2
library(ggplot2)
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
# Lọc dữ liệu cho năm 2007 và các châu lục châu Á và châu Âu
data_Asia_E_2007 <- subset(gapminder, year == 2007 & (continent == "Asia" | continent == "Europe"))
plot_asia <- ggplot(data = data_Asia_E_2007, aes(x = reorder(country, -gdpPercap), y = gdpPercap, fill = country)) +
geom_bar(stat = "identity", width = 0.9) +
coord_flip() +
theme(legend.position = "none") +
labs(title = "GDP per Capita in Asia (2007)", x = "Country", y = "GDP per Capita")
# Vẽ biểu đồ cột cho châu Á và châu Âu
plot_asia <- ggplot(data = data_Asia_E_2007, aes(x = reorder(country, -gdpPercap), y = gdpPercap, fill = country)) +
geom_bar(stat = "identity", width = 0.9) +
coord_flip() +
theme(legend.position = "none") +
labs(title = "GDP per Capita in Asia (2007)", x = "Country", y = "GDP per Capita")
plot_europe <- ggplot(data= data_Asia_E_2007, aes(x = reorder(country, -gdpPercap), y = gdpPercap, fill = country)) +
geom_bar(stat = "identity", width = 0.9) +
coord_flip() + theme(legend.position = "none")+
labs(title = "GDP per Capita in Europe (2007)", x = "Country", y = "GDP per Capita")
# Xếp biểu đồ cột cùng nhau
grid.arrange(plot_asia, plot_europe, ncol = 2)
Hưỡng dãn vẽ biểu đồ 1D và 2D dựa vào atapj dữ liệu gapminder
library(ggplot2)
library(dplyr) # data munging
library(scales) # nicer axis scale labels
if(!require(gapminder)) {install.packages("gapminder"); library(gapminder)}
-xem thông tin tổng quan về các biến trong tập dữ liệu.
summary(gapminder)
## country continent year lifeExp
## Afghanistan: 12 Africa :624 Min. :1952 Min. :23.60
## Albania : 12 Americas:300 1st Qu.:1966 1st Qu.:48.20
## Algeria : 12 Asia :396 Median :1980 Median :60.71
## Angola : 12 Europe :360 Mean :1980 Mean :59.47
## Argentina : 12 Oceania : 24 3rd Qu.:1993 3rd Qu.:70.85
## Australia : 12 Max. :2007 Max. :82.60
## (Other) :1632
## pop gdpPercap
## Min. :6.001e+04 Min. : 241.2
## 1st Qu.:2.794e+06 1st Qu.: 1202.1
## Median :7.024e+06 Median : 3531.8
## Mean :2.960e+07 Mean : 7215.3
## 3rd Qu.:1.959e+07 3rd Qu.: 9325.5
## Max. :1.319e+09 Max. :113523.1
##
table(gapminder$continent, gapminder$year)
##
## 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 2002 2007
## Africa 52 52 52 52 52 52 52 52 52 52 52 52
## Americas 25 25 25 25 25 25 25 25 25 25 25 25
## Asia 33 33 33 33 33 33 33 33 33 33 33 33
## Europe 30 30 30 30 30 30 30 30 30 30 30 30
## Oceania 2 2 2 2 2 2 2 2 2 2 2 2
ggplot(gapminder, aes(x=continent)) + geom_bar()
- Để làm cho ô này (và các ô khác) trở nên nhiều màu sắc hơn, bạn cũng
có thể ánh xạ fillthuộc tính tới continent, bạn cũng có thể thay đổi 1
số cái khác.
ggplot(gapminder, aes(x=continent, fill=continent)) + geom_bar()
trong geom_barchúng tôi chuyển đổi tính toán ..count..để ..count../12nó đại diện cho số lượng quốc gia thêm nhãn tốt hơn cho ytrục loại bỏ chú giải mặc định cho continent, điều này là dư thừa ở đây, vì các lục địa được gắn nhãn trên trục x.
ggplot(gapminder, aes(x=continent, fill=continent)) +
geom_bar(aes(y=..count../12)) +
labs(y="Number of countries") +
guides(fill=FALSE)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Một đặc điểm khác ggplot2là mỗi cốt truyện đều là một ggplotđối tượng. Nếu bạn thích một cốt truyện nhất định, bạn có thể lưu nó vào một tên nào đó bằng cách sử dụng mybar <- ggplot() + …, hoặc sau khi thực hiện xong, sử dụngmybar <-last_plot()
# record a plot for future use
mybar <- last_plot()
mybar + coord_flip()
mybar + coord_polar()
Có một số biến liên tục trong bộ dữ liệu này: tuổi thọ ( lifeExp), dân số ( pop) và tổng sản phẩm quốc nội bình quân đầu người ( gdpPercap) cho mỗi yearvà country. Đối với các biến như vậy, biểu đồ mật độ cung cấp một bản tóm tắt đồ họa hữu ích.
Chúng ta sẽ bắt đầu với lifeExp. Biểu đồ đơn giản nhất sử dụng trục này làm trục hoành, aes(x=lifeExp)sau đó cộng thêm geom_density()để tính toán và vẽ biểu đồ phân bố tần số được làm mịn.
ggplot(data=gapminder, aes(x=lifeExp)) +
geom_density()
Để làm cho những ô như vậy đẹp hơn, bạn có thể thay đổi độ dày của đường kẻ ( size=), thêm màu tô ( fill=““) và làm cho màu tô trong suốt một phần ( alpha=). Bạn có thể thấy các đường lưới bên dưới các ô sau
ggplot(data=gapminder, aes(x=lifeExp)) +
geom_density(size=1.5, fill="pink", alpha=0.3)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
bạn cũng có thể thêm biểu đồ sau. Điều này phức tạp hơn một chút để làm
đúng vì lịch sử được tính toán khác nhau và cần một số đối số bổ
sung.
ggplot(data=gapminder, aes(x=lifeExp)) +
geom_density(size=1.5, fill="pink", alpha=0.5) +
geom_histogram(aes(y=..density..), binwidth=4, color="black", fill="lightblue", alpha=0.5)