使用逻辑回归分析波士顿房价
时间: 2023-06-28 16:14:48 浏览: 86
逻辑回归通常用于二分类问题,而波士顿房价预测是一个回归问题,因此逻辑回归不适合用于波士顿房价预测。但是,可以使用线性回归分析波士顿房价。
下面是使用Python和Scikit-learn库进行波士顿房价预测的示例代码:
```python
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# 加载波士顿房价数据集
boston = load_boston()
# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2, random_state=42)
# 创建线性回归模型
model = LinearRegression()
# 使用训练集拟合模型
model.fit(X_train, y_train)
# 使用测试集评估模型
score = model.score(X_test, y_test)
# 打印模型得分
print("模型得分: {:.2f}".format(score))
```
在上面的代码中,首先加载了波士顿房价数据集,然后将数据集分为训练集和测试集。接着,创建了一个线性回归模型,并使用训练集拟合了模型。最后,使用测试集评估了模型,并打印了模型得分。