我现在正在尝试仅通过一个预测变量来训练SVM模型做出一些预测。
查看预测时,我得到了所有0
个信息。
这是什么原因?为什么模型没有决定要做出决定?是因为只有一个预测变量?
我想知道我的代码是否有问题。
SVM模型:
library(dplyr)
library(e1071)
train_date <- nrow(data) *0.8
train_date <- data %>%
slice(train_date) %>%
select(date) %>%
pull()
train <- data %>%
filter(date < ymd(train_date))
test <- data %>%
filter(date >= ymd(train_date))
########################################################
svm_model <-
svm(y ~ x, data = train, type = "C-classification",
kernel = "polynomial", degree = 2)
svm_model
#measure training and test accuracy
pred_train <- predict(svm_model, train)
mean(pred_train == train$y)
pred_test <- predict(svm_model, test)
mean(pred_test == test$y)
x <- as.data.frame(pred_test)
数据:
data <- structure(list(date = structure(c(17375, 17378, 17379, 17380,
17381, 17382, 17385, 17386, 17387, 17388, 17389, 17392, 17393,
17394, 17395, 17396, 17399, 17400, 17401, 17402, 17403, 17406,
17407, 17408, 17409, 17410, 17414, 17415, 17416, 17417, 17420,
17421, 17422, 17423, 17424, 17427, 17428, 17429, 17430, 17431,
17434, 17435, 17436, 17437, 17438, 17441, 17442, 17443, 17444,
17445, 17448, 17449, 17450, 17451, 17452, 17455, 17456, 17457,
17458, 17459, 17462, 17463, 17464, 17465, 17466, 17469, 17470,
17471, 17472, 17473, 17476, 17477, 17478, 17479, 17480, 17483,
17484, 17485, 17486, 17487, 17490, 17491, 17492, 17494, 17497,
17498, 17499, 17500, 17501, 17504, 17505, 17506, 17507, 17508,
17511, 17512, 17513, 17514, 17515, 17518, 17519, 17520, 17521,
17522, 17526, 17527, 17528, 17529, 17533, 17534, 17535, 17536,
17539, 17540, 17541, 17542, 17543, 17547, 17548, 17549, 17550,
17553, 17554, 17555, 17556, 17557, 17560, 17561, 17562, 17563,
17564, 17567, 17568, 17569, 17570, 17571, 17574, 17575, 17576,
17577, 17578, 17582, 17583, 17584, 17585, 17588, 17589, 17590,
17591, 17592, 17595, 17596, 17597, 17598, 17599, 17602, 17603,
17604, 17605, 17606, 17609, 17610, 17611, 17612, 17613, 17616,
17617, 17618, 17619, 17623, 17624, 17625, 17626, 17627, 17630,
17631, 17632, 17633, 17634, 17637, 17638, 17639, 17640, 17641,
17644, 17645, 17646, 17647, 17648, 17651, 17652, 17653, 17654,
17655, 17658, 17659, 17660, 17661, 17662, 17665, 17666, 17667,
17668, 17669, 17672, 17673, 17674, 17675, 17676, 17680, 17681,
17682, 17683, 17686, 17687, 17688, 17689, 17690, 17693, 17694,
17695, 17696, 17697, 17700, 17701, 17702, 17703, 17704, 17707,
17708, 17709, 17710, 17711, 17714, 17715, 17717, 17718, 17721,
17722, 17723, 17724, 17725, 17728, 17729, 17730, 17731, 17732,
17735, 17736, 17737, 17738, 17739, 17742, 17743, 17744, 17745,
17746, 17749, 17750, 17751, 17752, 17753, 17756, 17757, 17758,
17759, 17760, 17763, 17764, 17765, 17766, 17767, 17770, 17771,
17772, 17773, 17774, 17778, 17779, 17780, 17781, 17784, 17785,
17786, 17787, 17788, 17791, 17792, 17793, 17794, 17795, 17798,
17799, 17800, 17801, 17802, 17805, 17806, 17807, 17808, 17809,
17812, 17813, 17814, 17815, 17816, 17819, 17820, 17821, 17822,
17823, 17826, 17827, 17828, 17829, 17830, 17833, 17834, 17835,
17836, 17837, 17840, 17841, 17842, 17843, 17844, 17847, 17848,
17849, 17850, 17851, 17854, 17855, 17856, 17858, 17861, 17862,
17863, 17864, 17865), class = "Date"), y = c(0, 1, 1, 0, 1, 0,
0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1,
1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1,
0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1,
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0,
0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1,
1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0,
0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1,
0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1,
1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1,
1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1,
1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0), x = c(55.4941894407854,
50.1632187721403, 44.2057435629332, 42.3277780878597, 34.916071046742,
38.3146934968324, 31.6795222837921, 26.8964608058531, 26.5762418841725,
19.4454931404427, 19.2426086807444, 33.6749929164219, 34.664251589656,
45.2102647300278, 31.5897223352515, 36.2768164306463, 34.0840071047493,
46.9525192134939, 51.8801058990984, 46.3093986201844, 42.8722859382365,
43.0957642799655, 49.175098084696, 62.3766664464771, 63.434587161957,
58.4768395755699, 53.2799994541443, 50.4676873087737, 65.036737044046,
58.5873924866939, 62.3539876687387, 54.8745033541646, 55.2938209354096,
52.3738367508463, 52.7465383120286, 50.7142001647387, 50.3146292014814,
51.194583971026, 45.4279672713402, 44.2918322359654, 45.068547928843,
48.135031282721, 54.7140455891114, 63.3503388014154, 66.1156673174906,
61.0393644854218, 61.638611076124, 64.1858085150276, 73.1864682789322,
78.8442513456682, 76.0748344982738, 70.4160617369582, 74.4321939715647,
76.0682095017227, 81.8013716675612, 81.5329893185474, 77.4049144059356,
76.2072058576538, 65.5689633500972, 72.0267383603007, 55.6390349896244,
60.4198811003328, 52.2463634887349, 45.2655097258959, 68.9263022050927,
70.3371957548591, 64.6863349631492, 68.7059200902, 68.1459609632161,
69.5555458937198, 65.3412385589247, 67.360752439441, 74.1163158004527,
67.9866489434534, 78.9024435671165, 76.7050678391648, 76.1182375524639,
72.9909746088876, 59.4459498647349, 51.2180399554398, 51.0672179692865,
55.0636478144878, 55.756441335519, 54.6240547411303, 64.9109643846478,
57.4655825824172, 41.988898103196, 45.3179785535323, 42.0964437376148,
38.9428915240427, 41.8953683621125, 49.0751009808747, 49.4300638103628,
56.8843996184545, 58.4915483608461, 52.531910623186, 51.9880401490032,
53.5336152483392, 54.0807930604593, 61.574386151279, 72.457440540451,
68.9931756970495, 75.5300930523276, 81.7617764691854, 77.5499100672939,
67.6540858107567, 61.2557041074963, 56.4867362400937, 63.7959042668185,
70.2937801052188, 71.3433532375276, 73.1668927753785, 70.5075349624449,
65.8329958581158, 67.908000976737, 73.4971310903119, 78.8108475293617,
81.2133364284694, 85.6317997241784, 90.0499104679312, 91.8047462184197,
94.3135627996697, 94.1014109220364, 88.1130842668442, 88.3530558178883,
87.1355956107725, 86.2561623445798, 77.1228859956622, 81.0362538530292,
78.8404654349793, 46.4728298378735, 33.7103494024937, 38.1634534707235,
33.5520386736078, 26.2429467891094, 30.5979953728327, 30.5979953728327,
31.2223518673486, 33.7665461425831, 36.6962580582319, 37.7398082531122,
40.5860776927095, 41.0627097257687, 40.7556533339627, 52.526559398101,
67.2093345204357, 57.3558861837519, 61.809628052695, 65.0522479908148,
60.3356537763659, 59.9025026642582, 60.6951031882524, 60.0950548232381,
59.3846485649388, 64.6199416069941, 64.1051430716001, 55.6515339908006,
58.7835089189351, 55.0890845598537, 48.1838706704649, 46.0064642542491,
48.4030879681908, 55.5793562399467, 43.3339041496164, 35.5089178322478,
42.157901440901, 32.5975281088021, 28.6602735068277, 26.9110067493817,
23.5372731683978, 27.6575715257538, 27.7636741048428, 28.4241344813052,
27.7437779358905, 33.8748748481366, 38.0173561927228, 37.3614293051309,
46.7027642395441, 51.6960358269122, 46.2684476430283, 67.9712504992444,
67.4307596718059, 65.3539239654913, 69.3859268680975, 65.8884694613497,
48.7463489665683, 48.3776103610145, 58.1513743683333, 53.5784372311078,
46.4319595892114, 54.1515204375632, 48.0571628692748, 48.6571396623733,
52.2995925118996, 44.9774509790143, 45.2591195805464, 48.7943143049565,
56.0044804919092, 57.6982718090011, 75.947686211121, 66.6475291255686,
63.2031704734223, 66.0494138822722, 66.2641524590373, 64.6800962380417,
66.0941051628946, 68.6330617447997, 62.298871330898, 58.4734193157287,
52.329016147723, 43.5650542408412, 44.6973713488007, 56.9666746925596,
61.477502601121, 70.1850582389349, 68.3785649248245, 64.1672444920065,
68.1060250901431, 67.2130080618559, 73.8468747118516, 69.6113702464934,
73.1570958144156, 74.8830412236628, 85.4049570826199, 81.7882678868151,
79.8159292966814, 65.9053697697576, 57.9091367119927, 44.4025529377091,
43.2388424796772, 42.7803356293289, 47.7057738515549, 44.7755737074884,
45.7557906780512, 40.016244653124, 41.4992896665767, 46.6336286507843,
44.3657650232027, 45.4718259236287, 45.2372613787558, 56.9881807801438,
58.8717301068573, 68.2039283244873, 73.5215112680329, 78.8594307629251,
73.0335410836162, 71.845824268758, 73.323376014074, 89.1748677280385,
88.8275948061702, 88.079358554904, 72.9197089804835, 66.5774741060939,
65.5905607795046, 60.3560855296636, 60.5351059532554, 61.4085229097936,
58.076745639994, 63.2173375817626, 67.2733875032827, 68.7459719049055,
59.9037653356146, 44.6491666372171, 40.4929666577831, 30.2655738215587,
36.0522832244009, 40.7505784647263, 45.517250253278, 41.5835266382263,
41.3526668380199, 41.539756712543, 48.3189167794286, 49.8415866657383,
44.5858982397584, 50.0675010891207, 50.5139938354098, 44.9097955003298,
37.4247186375495, 41.3952548987526, 39.6467050713014, 39.3953595896288,
36.8289128008105, 42.8772642627352, 37.5760511024063, 42.0791664435174,
36.4236440580649, 25.1434697637668, 29.0666072154372, 25.3668839063101,
34.1040319281821, 34.1351918720353, 42.138526061446, 49.3942545777117,
53.2282422165058, 60.0907410718325, 59.6946479180297, 56.5126081396889,
64.5584522103826, 61.6638469740838, 48.5567687748239, 50.4491176695018,
45.8595330253583, 39.1134283844586, 22.2017732449298, 24.6509068125481,
33.7409449463083, 27.0354908046699, 36.9033514343542, 31.849732552439,
28.384694400023, 30.2843907497844, 30.2566110685775, 30.1702095862,
28.1229085893699, 39.7891005017724, 37.8236546439287, 33.4844836408483,
42.9231744072258, 49.6425369989148, 43.9761986844232, 44.7318583977582,
37.1424843378588, 40.8120228103859, 50.807226927847, 47.9214803669887,
44.995279725301, 41.3197867616665, 47.7401787161256, 40.9599257198947,
48.8101085201251, 58.7773921954413, 46.8976151314924, 38.7370234461344,
43.0052200556536, 42.7247275761847, 51.7764243779359, 47.5063348907638,
48.4623219235214, 51.3175593621287, 56.6142900584089)), row.names = c(NA,
-340L), class = c("tbl_df", "tbl", "data.frame"))