利用python使用线性回归器,根据特征值,预测加州房价
时间: 2024-09-23 22:16:30 浏览: 56
人工智能导论 学生作品五_加利福尼亚房价预测_线性回归.docx
5星 · 资源好评率100%
在Python中,我们可以利用Scikit-Learn库中的`LinearRegression`模型来进行线性回归预测,比如预测加州房价。线性回归是一种简单的预测模型,假设因变量(在这里是房价)与自变量(特征值)之间存在线性关系。以下是基本步骤:
1. **导入必要的库**:
```python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
```
2. **加载数据**,这里假设有一个CSV文件`california_housing.csv`,包含房价和相关特征:
```python
data = pd.read_csv('california_housing.csv')
```
3. **数据预处理**:
- 分割特征和目标变量(房价):
```python
X = data.drop('median_house_value', axis=1)
y = data['median_house_value']
```
- 划分训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
4. **创建并训练线性回归模型**:
```python
model = LinearRegression()
model.fit(X_train, y_train)
```
5. **预测**:
```python
y_pred = model.predict(X_test)
```
6. **评估模型**:
```python
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse}")
print(f"R-squared Score: {r2}")
```
阅读全文