Skip to content

phamduy15/bigdata

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ĐỀ TÀI: XÂY DỰNG MÔ HÌNH DỰ ĐOÁN DOANH SỐ BÁN HÀNG ĐIỆN TỬ

Kết quả đạt được: Biểu đồ doanh số theo tháng

Biểu đồ doanh số theo thành phố

Biểu đồ doanh số theo giờ

Biểu đồ phân phối số lượng sản phẩm

Rplot01

🚩 Cài đặt thư viện cần thiết

Để bắt đầu, bạn cần cài đặt các thư viện cần thiết

library(tidyr)
library(dplyr)
library(ggplot2)
library(caret)
library(rpart)
library(rpart.plot)
library(sparklyr)
library(Metrics)
🚩 Kết ni đến Spark
Kết ni đến Spark vi lnh sau:
sc <- spark_connect(master = "local")

🚩 Thiết lp đường dn và đọc dliu
Thiết lp đường dn đến file CSV và đọc dliu:
file_path <- "sales2019clean.csv"
df_combined <- read.csv(file_path, stringsAsFactors = FALSE)

🚩 Chuyn đổi dliu sang Spark DataFrame
Chuyn đổi dliu sang Spark DataFrame:

df_spark <- copy_to(sc, df_combined, "df_combined", overwrite = TRUE)

🚩 Xlý dliu thiếu
Lc ra các hàng có dliu thiếu:
df_spark <- df_spark %>% filter(!is.na('Quantity.Ordered') & !is.na('Price.Each'))

🚩 Lưu trDataFrame trong bnhLưu trDataFrame trong bnhSpark:

df_spark <- df_spark %>% sparklyr::sdf_persist()

🚩 Kim tra dliu trong Spark
df_spark %>%
  summarize(count = n()) %>%
  collect()

🚩 Chuyn đổi kiu dliu và tính toán doanh sdf_combined <- df_combined %>%
  filter(!is.na(Quantity.Ordered) & !is.na(Price.Each)) %>%
  mutate(
    Quantity.Ordered = as.integer(Quantity.Ordered),
    Price.Each = as.numeric(Price.Each),
    Sales = Quantity.Ordered * Price.Each
  )

🚩 Trích xut tháng và giờ đặt hàng
Trích xut tháng và gitct Order.Date:

df_combined$Order.Date <- as.POSIXct(df_combined$Order.Date, format="%m/%d/%Y %H:%M")
df_combined$Month <- format(df_combined$Order.Date, "%m")
df_combined$Hours <- format(df_combined$Order.Date, "%H")

🚩 Trích xut thành phtừ địa chTrích xut tên thành phtPurchase.Address:

df_combined$City <- sapply(strsplit(df_combined$Purchase.Address, ","), 
                           function(x) ifelse(length(x) >= 2, trimws(x[2]), NA))
🚩 Tng hp doanh stheo tháng, thành phvà giTng hp doanh s:

sales_value_month <- aggregate(Sales ~ Month, data = df_combined, sum)
sales_value_city <- aggregate(Sales ~ City, data = df_combined, sum)
sales_value_hours <- aggregate(Sales ~ Hours, data = df_combined, sum)

🚩 Vbiu đồ trc quan hóa
Vbiu đồ doanh stheo tháng, thành phố, và gi:


par(mfrow=c(2, 2))
# Biểu đồ doanh số theo tháng
barplot(sales_value_month$Sales, names.arg = sales_value_month$Month, 
        xlab = "Months", ylab = "Sales in USD", col = "blue", main = "Sales by Month")


# Biểu đồ doanh số theo thành phố
barplot(sales_value_city$Sales, names.arg = sales_value_city$City, las = 2, 
        xlab = "Cities", ylab = "Sales in USD", col = "red", main = "Sales by City")
# Biểu đồ doanh số theo giờ
plot(as.numeric(sales_value_hours$Hours), sales_value_hours$Sales, type = "o", 
     xlab = "Hours", ylab = "Sales in USD", xaxt='n', main = "Sales by Hour")
axis(1, at = as.numeric(sales_value_hours$Hours), labels = sales_value_hours$Hours)
# Biểu đồ phân phối số lượng sản phẩm
all_products <- aggregate(Quantity.Ordered ~ Product, data = df_combined, sum)
barplot(all_products$Quantity.Ordered, names.arg = all_products$Product, las = 2,col = "green", xlab = "Products", ylab = "Quantity Ordered", main = "Product Demand")

🚩 Xây dng mô hình dự đoán doanh sChia dliu thành tp train/test:

set.seed(42)
trainIndex <- createDataPartition(df_combined$Quantity.Ordered, p = 0.7, list = FALSE)
train_data <- df_combined[trainIndex, ]
test_data <- df_combined[-trainIndex, ]

🚩 Hun luyn mô hình cây quyết định
model <- rpart(Quantity.Ordered ~ Month + Hours + City + Price.Each, 
               data = train_data, method = "anova")
rpart.plot(model)

🚩 Dự đoán và đánh giá mô hình
y_pred <- predict(model, test_data)
accuracy <- cor(y_pred, test_data$Quantity.Ordered)
print(paste("Độ chính xác của mô hình (tương quan Pearson):", round(accuracy, 1)))


🚩 Dự đoán và đánh giá mô hình
y_pred <- predict(model, test_data)
accuracy <- cor(y_pred, test_data$Quantity.Ordered)
print(paste("Độ chính xác của mô hình (tương quan Pearson):", round(accuracy, 1)))

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages