juypter多元线性回归分析南京房价
时间: 2023-09-27 09:08:44 浏览: 234
好的,首先需要准备一些数据,包括南京的房价、房屋面积、房龄、楼层等相关信息。可以从网上搜集到这些数据,或者自己手动整理。
然后,我们需要安装并导入一些必要的库,包括numpy、pandas、matplotlib等:
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
```
接着,我们可以读取数据文件并进行数据清洗和预处理:
```python
data = pd.read_csv('data.csv')
# 剔除缺失值
data = data.dropna()
# 将非数值型变量转换为数值型变量
data = pd.get_dummies(data, columns=['floor'])
# 将数据分为特征和标签
X = data.drop(['price'], axis=1)
y = data['price']
```
接下来,我们可以使用sklearn库中的LinearRegression模型进行多元线性回归分析:
```python
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# 将数据分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建并训练模型
model = LinearRegression()
model.fit(X_train, y_train)
# 输出模型的R2分数
print('R2 score:', model.score(X_test, y_test))
```
最后,我们可以使用matplotlib库进行可视化展示:
```python
# 可视化预测结果与真实结果的对比
plt.scatter(y_test, model.predict(X_test))
plt.xlabel('True Values')
plt.ylabel('Predictions')
plt.plot([0, max(y_test)], [0, max(y_test)], color='red')
plt.show()
```
这样就完成了南京房价的多元线性回归分析。
阅读全文
相关推荐















