我正在使用Keras波士顿数据集,使用单个特征并尝试执行线性回归模型。我已经标准化了输入功能。输出图似乎是一条直线,未与数据分布对齐:
-我在这里想念什么?跳过这些行以加载和规范化下面的数据。
import requests
from bs4 import BeautifulSoup
url = 'https://www.carlist.my/used-cars-for-sale/proton/malaysia?profile_type=Dealer'
response = requests.get(url, params={'page_number': 1})
soup = BeautifulSoup(response.text, 'lxml')
articles = soup.find_all('article')[:25]
seller_type = articles[4].find('div', class_ = 'item push-quarter--ends listing__spec--dealer')
print(seller_type.contents[2])
答案 0 :(得分:1)
我用keras实现了简单的线性回归模型。
from keras.models import Sequential
from keras.layers import Dense, Dropout
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
class Train:
def __init__(self):
self.__lr = 0.1
self.x_train, self.y_train, self.x_test, self.y_test, self.y_scaler = self.load_data('train.csv')
self.train()
这是预处理的一部分,如下所示:
def load_data(self, fname):
data = np.loadtxt(fname, skiprows=1, delimiter=',')
x_data = data[:, 1:-1]
x_scaler = MinMaxScaler(feature_range=(0, 1))
x_data = x_scaler.fit_transform(x_data)
y_data = data[:, [-1]]
y_scaler = MinMaxScaler(feature_range=(0, 1))
y_data = y_scaler.fit_transform(y_data)
train_size = int(len(x_data)*0.7)
test_size = len(x_data) - train_size
x_train, x_test = x_data[0:train_size], x_data[train_size:len(x_data)]
y_train, y_test = y_data[0:train_size], y_data[train_size:len(y_data)]
return x_train, y_train, x_test, y_test, y_scaler
而且,这是训练的一部分,包括图层。您必须使用relu
和mse
。
请参考以下代码:
def train(self):
model = Sequential()
model.add(Dense(128, input_dim=len(self.x_train[0]), activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='relu'))
model.compile(loss='mse', optimizer='sgd', metrics=['mse'])
model.fit(self.x_train, self.y_train, epochs=5000)
result = model.evaluate(self.x_test, self.y_test)
predictions = model.predict(self.x_test)
predictions = self.y_scaler.inverse_transform(predictions)
print (predictions)
y_test = self.y_scaler.inverse_transform(self.y_test)
plt.plot(y_test)
plt.plot(predictions)
plt.show()
您知道它是主要的:
if __name__ == "__main__":
train = Train()