X = sports[:][predictors]
时间: 2023-10-30 10:08:52 浏览: 40
这段代码是从 Pandas 数据框 sports 中提取预测变量作为自变量 X。其中,sports[:] 表示选取 sports 数据框的所有行,而 [predictors] 则表示选取 sports 数据框中的预测变量列作为自变量 X。具体来说,这里的 predictors 是一个保存了预测变量列名的列表或数组。这样做是为了将数据集中的自变量和因变量分开,方便后续的建模和分析。
相关问题
for i in df[0][:15]:#i对15行之前的数据进行遍历(不含第15行) for keys, values in vocabList_d.items():#使用了for循环和字典的items()方法,用于遍历一个字典vocabList_d中的所有键-值对,其中键是词汇表中的单词,值是对应单词的编号 if str(int(i)) == values:# predictors.append(keys) idx.append(int(values))
这段代码主要是将DataFrame中的某一列数据(第0列)转化为词汇表中的单词编号,然后将这些编号存储到一个列表中。具体来说,代码的执行过程如下:
1. 遍历DataFrame中第0列的前15行数据(不含第15行)。
2. 对于每一个遍历到的数据,将其转化为整数,并将其转化为字符串类型(`str(int(i))`)。
3. 在词汇表字典中遍历每一个键-值对,对于每一个键-值对,将其值转化为整数,并将其转化为字符串类型(`str(int(values))`)。
4. 如果字符串类型的数据和字符串类型的键相等(即该单词在词汇表中有对应编号),则将该单词存储到`predictors`列表中,并将该单词的编号存储到`idx`列表中。
最终,`predictors`列表中存储的是DataFrame中第0列数据对应的词汇表中的单词,而`idx`列表中存储的是单词的编号。
运行library(caret) library(neuralnet) data <- read.csv("housing_renamed.csv") data$year_built[is.na(data$year_built)] <- median(data$year_built, na.rm = TRUE) predictors <- subset(data, select = -c(value_per_sq_ft)) response <- data$value_per_sq_ft ctrl <- trainControl(method = "cv", number = 3) model1 <- train(response ~ ., data = predictors, method = "neuralnet", trControl = ctrl) model2 <- train(response ~ ., data = predictors, method = "neuralnet", trControl = ctrl) print(model1) print(model2)出现Something is wrong; all the RMSE metric values are missing:报错
这个错误可能是由于数据中存在缺失值导致的。你需要检查数据中是否存在其他缺失值,并对其进行处理。另外,你可以尝试在train函数中添加一个参数preProcess,将其设置为"impute",以对缺失值进行处理。例如:
```
model1 <- train(response ~ ., data = predictors, method = "neuralnet",
trControl = ctrl, preProcess = "impute")
```
你可以尝试这个方法来解决这个错误。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)