波士顿房价python
时间: 2023-08-29 14:13:50 浏览: 98
波士顿房价数据集是一个经典的回归问题,可以使用Python来解决。以下是使用Scikit-learn库解决此问题的示例代码:
```python
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# 加载数据集
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)
# 在测试集上进行预测
y_pred = model.predict(X_test)
# 计算模型的均方误差
mse = mean_squared_error(y_test, y_pred)
print("均方误差:", mse)
```
输出结果为均方误差。可以尝试使用其他回归模型,如决策树回归、随机森林回归等,以提高预测效果。
相关问题
lstm回归预测波士顿房价python代码
以下是使用LSTM回归预测波士顿房价的Python代码:
```python
import numpy
import pandas
import math
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# load the dataset
dataframe = pandas.read_csv('boston_housing.csv', usecols=[13], engine='python')
dataset = dataframe.values
dataset = dataset.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return numpy.array(dataX), numpy.array(dataY)
# reshape into X=t and Y=t+1
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
print('Test Score: %.2f RMSE' % (testScore))
# shift predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()
```
注意:这个代码不是一个完整的程序,只是对以LSTM回归预测波士顿房价的过程进行了演示,可能需要自己加入一些细节处理才能运行成功。
神经网络预测波士顿房价python绘图代码
以下是使用Python绘制波士顿房价神经网络预测图的示例代码:
```python
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.preprocessing import scale
# 加载波士顿房价数据集
boston = load_boston()
# 数据预处理
x_data = scale(boston.data)
y_data = boston.target.reshape(-1, 1)
# 定义模型
x = tf.placeholder(tf.float32, shape=[None, 13])
y = tf.placeholder(tf.float32, shape=[None, 1])
w = tf.Variable(tf.random_normal([13, 1]))
b = tf.Variable(tf.zeros([1]))
y_pred = tf.matmul(x, w) + b
# 定义损失函数和优化器
loss = tf.reduce_mean(tf.square(y - y_pred))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss)
# 训练模型
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(500):
_, train_loss = sess.run([optimizer, loss], feed_dict={x: x_data, y: y_data})
if i % 50 == 0:
print("step:", i, "loss:", train_loss)
# 预测结果
y_pred_value = sess.run(y_pred, feed_dict={x: x_data})
# 绘制图像
plt.scatter(y_data, y_pred_value)
plt.plot([y_data.min(), y_data.max()], [y_data.min(), y_data.max()], 'k--', lw=4)
plt.xlabel('True Prices')
plt.ylabel('Predicted Prices')
plt.show()
```
这段代码使用TensorFlow实现了一个简单的线性回归模型,用于预测波士顿房价。在训练模型后,使用matplotlib绘制了一个散点图,展示了预测结果与真实结果之间的关系。
阅读全文