statsmodels predict
时间: 2023-09-21 12:10:47 浏览: 116
统计预测模型
`statsmodels` is a Python library used for statistical modeling and analysis. To make predictions using a fitted model in `statsmodels`, you can use the `predict()` method.
Here's an example of how to use `predict()` with a linear regression model:
```python
import statsmodels.api as sm
# create data
X = sm.add_constant([[1, 2], [1, 3], [1, 4], [1, 5]])
y = [5, 7, 9, 11]
# fit linear regression model
model = sm.OLS(y, X).fit()
# make predictions on new data
new_data = sm.add_constant([[1, 6], [1, 7]])
predictions = model.predict(new_data)
print(predictions)
```
In this example, we first create some data that we want to fit a linear regression model to. We then fit the model using `sm.OLS()` and the `fit()` method. Finally, we use the `predict()` method to make predictions on new data (`new_data`). The predicted values are stored in the `predictions` variable and printed to the console.
阅读全文