我正在尝试使用naivebayes算法预测一门课。
R代码:
library(naivebayes)
df = read.csv("mapingsample.csv")
str(df)
sf=sample(2,nrow(df),replace = TRUE,prob = c(0.9,0.1))
trd=df[sf==1,]
tsd=df[sf==2,]
nb=naive_bayes(RetailerName ~ .,data = trd)
predicteddata=predict(nb,newdata = tsd,type = "class")
predicteddata
bindprediction = cbind(tsd,predicteddata)
bindprediction
dput(df)
structure(list(RegionName = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "MUMBAI", class = "factor"), RetailerId = c(297L,
297L, 297L, 297L, 297L, 297L, 297L, 297L, 297L, 297L, 297L, 297L,
297L, 297L, 297L, 297L, 297L, 297L, 297L, 297L, 297L, 389L, 389L,
389L, 389L, 389L, 389L, 389L, 389L, 389L, 389L, 389L, 389L, 389L,
389L, 389L, 389L, 389L, 389L, 389L, 389L, 389L), RetailerName = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("BHAGWATI MEDICAL AND GENERAL STORES",
"GOPALKRISHNA MEDICAL AND GENERAL STORE"), class = "factor"),
StoreId = c(37L, 86L, 87L, 88L, 99L, 117L, 133L, 147L, 332L,
546L, 579L, 604L, 628L, 675L, 713L, 744L, 806L, 1032L, 1138L,
1240L, 2035L, 37L, 54L, 61L, 69L, 82L, 86L, 87L, 88L, 138L,
170L, 341L, 472L, 487L, 604L, 605L, 624L, 634L, 1247L, 1376L,
1385L, 1571L), PartyCode = c(10086L, 6131L, 6131L, 6131L,
15977L, 8968L, 3078L, 6626L, 12910L, 279L, 6558L, 462L, 1583L,
1432L, 2772L, 21166L, 22015L, 17186L, 36121L, 972L, 59542L,
1207L, 1433L, 1887L, 31850L, 1404L, 4758L, 4758L, 4758L,
697L, 2947L, 179345L, 713L, 10697L, 576L, 1128L, 3339L, 3179L,
51822L, 2704L, 13059L, 60217L), PartyName = structure(c(12L,
15L, 15L, 15L, 14L, 1L, 11L, 5L, 13L, 8L, 10L, 6L, 13L, 2L,
7L, 4L, 8L, 3L, 16L, 9L, 12L, 23L, 20L, 28L, 31L, 26L, 24L,
24L, 24L, 21L, 22L, 27L, 29L, 21L, 32L, 20L, 30L, 27L, 25L,
17L, 18L, 19L), .Label = c("$BHAGWATI MEDICAL.", "BHAGWATI MEDICAL",
"BHAGWATI MED & GEN STORES", "BHAGWATI MED ST 29207788",
"BHAGWATI MED. & GEN. STORES.", "BHAGWATI MED.& GEN. ST.",
"BHAGWATI MEDI & GEN.STORES", "BHAGWATI MEDICAL", "BHAGWATI MEDICAL MAROL",
"BHAGWATI MEDICAL & GENERAL STORES.", "BHAGWATI MEDICAL & GENERAL STORE",
"BHAGWATI MEDICAL & GENERAL STORES", "BHAGWATI MEDICAL & GENERAL STORES.",
"BHAGWATI MEDICAL & GENRAL STORE", "BHAGWATI MEDICAL [MAROL]",
"BHAGWATI MEDICAL STORE", "GOPAL KRISHNA MED.(CLOSED-",
"GOPAL KRISHNA MED & GEN STO", "GOPAL KRISHNA MED & GEN STORE",
"GOPAL KRISHNA MED & GEN STORES", "GOPAL KRISHNA MED &. GENERAL STORES",
"GOPAL KRISHNA MED. & GEN. STORES", "GOPAL KRISHNA MED.& GEN.ST. #",
"GOPAL KRISHNA MED.& GEN.ST;[BORIVALI-W]", "GOPAL KRISHNA MED.&GEN.STORE",
"GOPAL KRISHNA MED.&GEN.STORES", "GOPAL KRISHNA MEDICAL",
"GOPAL KRISHNA MEDICAL & GENERAL STORES", "GOPALKRISHNA MEDICAL STORE",
"GOPALKRISHNA MEDICAL.", "NEW GOPAL KRISHNA MEDICAL & GEN.STORES",
"ZZGOPAL KRISHNA MED.ST."), class = "factor"), Address1 = structure(c(4L,
32L, 32L, 32L, 34L, 27L, 25L, 29L, 26L, 31L, 28L, 3L, 24L,
30L, 33L, 2L, 36L, 1L, 22L, 23L, 35L, 11L, 15L, 10L, 9L,
16L, 12L, 12L, 12L, 7L, 13L, 19L, 1L, 8L, 5L, 14L, 20L, 21L,
18L, 13L, 6L, 17L), .Label = c("", "2 GR FL MEZZANIN ABDUL REHAMAN",
"2 GR.FL.ABDUL REHMAN CHAWL", "2,GROUND FLOOR,ABDUL REHAMAN CHAWL,MAROL GAON",
"22 LAXMI CHAYA", "22 LAXMI CHHAYA L T RD", "22, LAXMI CHHAYA BLDG,",
"22, LAXMI CHHAYA BLDG,L.T.RD", "22, LAXMI CHHAYA, BABHAI NAKA",
"22, LAXMI CHHAYA, L.T.ROAD", "22,LAXMI CHAYYA BLDG", "22,LAXMI CHHAYA ,L.T.RD;BHABAI NAKA,",
"22,LAXMI CHHAYA,", "22,LAXMI CHHAYA, L.T.RD,BABHAI NAKA",
"22,LAXMI CHHAYA,L.T.ROAD", "22,LAXMI CHHAYA,L.T.ROAD,BABHAI",
"22/LAXMI CHHAYA", "22/LAXMI CHHAYA; L.T.ROAD", "22LAXMI CHHAYYA",
"L.T.ROAD, BABHAI NAKA", "SH-22,L.T.RD,BABAI NAKA", "SH NO.2BRFLR.MAZALIN FLR.",
"SHOP NO 2 ABDULREHMAN CHAWL SH", "SHOP NO 2.ABDUL REHMAN CHAWL.",
"SHOP NO.2 ABDUL REHMAN CHAWL SHRI HANUMAN MANDIR MARG",
"SHOP NO.2, GR FLOOR, MEZZAINI FLR,ABDULREHMAN CHAWL,", "SHOP NO.2,ABDUL REHMAN CHAWL SHRI HANUMAN MANDIR ROAD",
"SHOP NO.2,ABDUL REHMAN CHWAL,HANUMAN MANDIR,MAROL VILLADGE REZY COELHO CHAWL,",
"SHREE HANUMAN MANDIR ROAD, MAROL VILLEG", "SHRI HANUMAN MANDIR ROAD,",
"SHRI HANUMAN MANDIR RD,", "SHRI HANUMAN MANDIR RD;MAROL GAON,",
"SHRI HANUMAN MANDIR ROAD MAROL VILLAGE", "SHRI HANUMAN MANDIR ROAD,",
"SHRI HANUMAN MANDIR ROAD,, MAROL VILLAGE,, ANDHERI (E),",
"SHRI HANUMAN MANDIR ROAD,MAROL GAON,MAROL,ANDHERI-E"), class = "factor"),
Address2 = structure(c(29L, 7L, 7L, 7L, 26L, 1L, 27L, 1L,
30L, 25L, 6L, 18L, 31L, 24L, 23L, 16L, 1L, 4L, 3L, 5L, 2L,
21L, 8L, 9L, 17L, 28L, 15L, 15L, 15L, 11L, 19L, 10L, 1L,
11L, 9L, 1L, 14L, 1L, 13L, 22L, 12L, 20L), .Label = c("",
", MUMBAI.", "ABDUL REHMAN CHL.HANUMAN MAND", "ANDHERI (E)",
"ANDHERI E", "ANDHERI(E)", "ANDHERI[E],MUMBAI-59.", "BABAI NAKA ,EKSAR ROAD",
"BABHAI NAKA", "BABHAI NAKA EKSAR ROAD", "BABHAI NAKA, EKSAR RD,",
"BABHAI NAKABORIVLI W MUM-92", "BORIVALI (WEST) BABHAI NAKA",
"BORIVALI (W)", "BORIVALI[W],MUMBAI-92.", "CHAWLHUMAN MANDIR RDMAROL",
"EKSAR ROAD", "HANUMAN MANDIR RD.", "L.T. ROAD,BABHAI NAKA,",
"L.T.ROAD", "L.T.ROAD,BABHAI NAKA", "L.T.ROAD,BABHAI NAKA, BORAVALI WEST,MUMBAI-400092",
"MAROL", "MAROL GAON,", "MAROL GAON,MAROL, ANDHERI(E)",
"MAROL VILLAGE", "MAROL VILLAGE,", "NAKA,WEST MUMBAI", "SHREE HANUMAN MANDIR ROAD",
"SHREE HANUMAN MANDIR ROAD,MAROL GAON", "SHRI HANUMAN MANDIR ROAD, MAROL VILLAGE"
), class = "factor"), Area = structure(c(16L, 1L, 1L, 1L,
16L, 7L, 1L, 16L, 7L, 19L, 4L, 18L, 17L, 4L, 1L, 3L, 1L,
6L, 5L, 1L, 4L, 13L, 10L, 11L, 15L, 1L, 1L, 1L, 1L, 9L, 1L,
2L, 10L, 9L, 14L, 1L, 1L, 12L, 8L, 1L, 8L, 10L), .Label = c("",
"(S) BORIVALI (WEST).", "ANDHERI", "ANDHERI (E)", "ANDHERI (WEST)",
"ANDHERI [W]", "ANDHERI(E)", "BORIVALI", "BORIVALI (W)",
"BORIVALI (WEST)", "BORIVALI W", "BORIVALI(W)", "BORIVLI",
"L.T.RD", "L.T.ROAD", "MAROL", "MAROL - ANDHERI - EAST",
"MAROL GAON", "VP(E)-A(E)-MA"), class = "factor"), Category = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"RETAILER"), class = "factor"), City = structure(c(5L, 4L,
4L, 4L, 16L, 15L, 3L, 16L, 16L, 16L, 16L, 2L, 16L, 16L, 14L,
1L, 1L, 1L, 1L, 13L, 16L, 11L, 16L, 16L, 9L, 6L, 10L, 10L,
10L, 16L, 12L, 1L, 16L, 16L, 8L, 8L, 9L, 16L, 1L, 7L, 1L,
1L), .Label = c("", "ANDHERI-E", "ANDHERI (E)", "ANDHERI [E]",
"ANDHERI EAST", "BORIVALI", "BORIVALI- WEST", "BORIVALI-W",
"BORIVALI (W)", "BORIVALI [W]", "BORIVALI WEST", "BORIVALI{WEST}",
"GENERAL", "MAROL", "MUMABAI", "MUMBAI"), class = "factor"),
ContactPerson = structure(c(12L, 16L, 16L, 16L, 8L, 1L, 1L,
10L, 1L, 1L, 9L, 11L, 13L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 14L, 1L, 4L, 5L, 15L, 15L, 15L, 1L, 1L, 1L, 1L, 1L, 6L,
1L, 1L, 1L, 7L, 1L, 3L, 1L), .Label = c("", "8959202", "9821287221 9892695575",
"CHANDHUBHAI", "CHANDRAKANTBHAI", "CHANDU BHAI", "CHANDU BHAI - 9833819296",
"DINESH", "DINESH BHAI", "DINESH BHIMRAJ", "DINESH KOTHARI",
"HARSHIT JAIN", "MAROL", "MR CHANDRAKANT", "MR.CHANDUBHAI",
"MR.DINESH KOTHARI"), class = "factor"), CSTNumber = structure(c(8L,
18L, 18L, 18L, 14L, 2L, 14L, 19L, 11L, 10L, 21L, 10L, 7L,
1L, 15L, 23L, 1L, 1L, 22L, 13L, 26L, 17L, 17L, 16L, 3L, 20L,
6L, 6L, 6L, 16L, 9L, 1L, 1L, 3L, 12L, 5L, 1L, 1L, 25L, 4L,
24L, 1L), .Label = c("", "20-21-Z-1", "20-MH-MZ7-192791",
"20-Z-7/92/1536", "20-Z-7/92/1536,21-Z-7/92/1536", "20-Z-7/92/2221",
"20-Z6/59/908", "20 Z6 59 90B", "20&21-Z-7/92/2221", "20,21/Z-6/59/908",
"20,21/Z-6/59/90B", "20,21/Z-7/92/2221", "20/21-Z6-59-908",
"20/Z-6/59/908", "20/Z/6/59/749", "20/Z/7/92/2221", "20/Z7/92/2221",
"20Z-6-59-908", "20Z-6/59/908", "20Z-7/92/2221", "21Z-6/59/908",
"27390646287 V", "27390646287V", "27480593421.V", "27480593421V",
"C_00121689190"), class = "factor"), Email = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L,
1L, 1L, 2L, 1L, 4L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L,
1L, 1L, 3L, 3L, 1L, 1L, 1L, 3L, 1L, 3L, 1L), .Label = c("",
"BHAGWATIMEDICAL7@YAHOO.COM", "GOPALKRISHNAMED22@GMAIL.COM",
"MUMBAI"), class = "factor"), LicenseNumber = structure(c(24L,
28L, 28L, 28L, 14L, 30L, 25L, 11L, 15L, 15L, 17L, 15L, 23L,
1L, 9L, 4L, 1L, 2L, 3L, 13L, 10L, 27L, 27L, 26L, 20L, 29L,
22L, 22L, 22L, 26L, 12L, 7L, 1L, 19L, 16L, 18L, 1L, 1L, 8L,
21L, 5L, 6L), .Label = c("", "/", "20-21-Z-6-59-90B", "20-21Z-59-908-20CZ6-59-940",
"20-21Z7922221 20C2124", "20-7-7/92/1536 /21-Z-7/92/1536",
"20-Z-7/92/187121-Z-7/92/1871 20C-Z-7/92/1817. DT.6.10.08",
"20-Z-7/92/2221*21-Z-7/92/2221 20C-Z-7/92/2124", "20 C/Z-6/59/788",
"20/21-Z-6/59/908", "20C-Z-6/59/940", "20C-Z-7/92/2124",
"20C-Z6-59-940", "20C/Z-6/59/908", "20C/Z-6/59/940", "20C/Z-7/92/2124",
"20Z-6/59/908", "21-C-Z/92/1481", "21-MH-MZ7-192792", "21-MH-MZ7/192792",
"21-Z-7/92/1536", "21-Z-7/92/2221", "21-Z6/59/908", "21 Z6 59 90B",
"21/Z-6/59/908", "21/Z/7/92/2221", "21/Z7/92/2221", "21Z-6-59-908",
"21Z-7/92/2221", "59-908-20C"), class = "factor"), Telephone = structure(c(9L,
7L, 7L, 7L, 12L, 12L, 8L, 7L, 13L, 7L, 10L, 18L, 11L, 12L,
7L, 1L, 1L, 1L, 17L, 7L, 1L, 15L, 6L, 2L, 2L, 5L, 2L, 2L,
2L, 3L, 4L, 16L, 2L, 3L, 2L, 1L, 14L, 15L, 2L, 2L, 2L, 1L
), .Label = c("", "28959202", "28959202 / 9821287221", "28959202,9821287221",
"28959202/69931501", "28959202/983381929", "29207788", "29207788 / 9867097667",
"29207788 / 07666464888", "29207788/7666464888", "29207788/7738788474/9869260832",
"29207788/8286453461", "7977458967", "9821287221", "9821287221/28959202",
"9821287221/9892695575", "9867097667 / 8286453461", "9869260832"
), class = "factor"), MobileNumber = structure(c(6L, 1L,
1L, 1L, 11L, 1L, 3L, 12L, 10L, 4L, 1L, 2L, 10L, 1L, 1L, 3L,
5L, 1L, 1L, 1L, 10L, 1L, 7L, 1L, 9L, 8L, 1L, 1L, 1L, 1L,
8L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"29207788", "7666464888", "7738788474", "8286453461", "8286453461,9867097667",
"9821287221", "9833819296", "9833819296/9821287221", "9867097667",
"98670976670", "9869260832"), class = "factor")), class = "data.frame", row.names = c(NA,
-42L))
答案 0 :(得分:1)
问题是您指定的newdata
包含因变量。代替使用
predicteddata <- predict(nb,newdata = tsd[!names(tsd) %in% "RetailerName"])
有效。