Keras NN回归模型具有低损失和0精度 [英] Keras NN regression model gives low loss, and 0 acuracy

查看:125
本文介绍了Keras NN回归模型具有低损失和0精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在keras中的这个NN回归模型有问题.我正在研究汽车数据集,以根据13个维度预测价格.简而言之,我将其读取为pandas数据框,将数值转换为浮点数,缩放了这些值,然后对分类值使用了一键编码,这创建了许多新列,但是对此我并不太关心观点.我担心的是精度几乎为0%,我无法弄清原因.数据集可在此处找到: https://www.kaggle.com/CooperUnion/cardataset/data .下面是代码:

I am having a problem with this NN regression model in keras. I am working on a cars dataset to predict the price based on 13 dimensions. In short, I have read it as pandas dataframe, converted numeric values to float, scaled the values, and then used one-hot encoding for categorical values, which has created a lot of new columns, but that does not concern me much at this point. What concerns me is that the accuracy is practically 0%, and I cannot figure out why. Dataset can be found here: https://www.kaggle.com/CooperUnion/cardataset/data. Below is the code:

import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from keras.utils import to_categorical

# load dataset
# Columns : Make, Model, Year, Engine Fuel Type, Engine HP, Engine Cylinders, Transmission Type, Driven_Wheels, Number of Doors, Vehicle Size, Vehicle Style, highway MPG, city mpg, Popularity, MSRP

import pandas as pd
dataframe = pd.read_csv("cars.csv", header = 'infer', names=['Make', 'Model', 'Year', 'Engine Fuel Type', 'Engine HP', 'Engine Cylinders', 'Transmission Type', 'Driven_Wheels', 'Number of Doors', 'Vehicle Size', 'Vehicle Style', 'highway MPG', 'city mpg', 'Popularity', 'MSRP'])

#convert data columns to float
dataframe[['Engine HP', 'highway MPG', 'city mpg', 'Popularity', 'MSRP']] = dataframe[['Engine HP', 'highway MPG', 'city mpg', 'Popularity', 'MSRP']].apply(pd.to_numeric)

#normalize the values - divide my their max value
dataframe["Engine HP"] = dataframe["Engine HP"] / dataframe["Engine HP"].max()

dataframe["highway MPG"] = dataframe["highway MPG"] / dataframe["highway MPG"].max()

dataframe["city mpg"] = dataframe["city mpg"] / dataframe["city mpg"].max()

dataframe["Popularity"] = dataframe["Popularity"] / dataframe["Popularity"].max()

dataframe["MSRP"] = dataframe["MSRP"] / dataframe["MSRP"].max()

#split input and label
x = dataframe.iloc[:,0:14] 
y = dataframe.iloc[:,14] 

#one-hot encoding for categorical values

def one_hot(df, cols):
    for each in cols:
        dummies = pd.get_dummies(df[each], prefix=each, drop_first=False)
        df = pd.concat([df, dummies], axis=1)
    return df

#columns to transform
cols_to_tran = ['Make', 'Model', 'Year', 'Engine Fuel Type', 'Engine Cylinders', 'Transmission Type', 'Driven_Wheels', 'Number of Doors', 'Vehicle Size', 'Vehicle Style']
d = one_hot(x, cols_to_tran)

list(d.columns.values)

#drop first original 11 columns
e = d.drop(d.columns[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], axis=1)
list(e.columns.values)

#create train and test datasets - 80% for train and 20% for validation
t = len(e)*0.8
t = int(t)

train_data = e[0:t]
train_targets = y[0:t]

test_data = e[t:]
test_targets = y[t:]


#convert to numpy array
train_data = train_data.values
train_targets = train_targets.values

test_data = test_data.values
test_targets = test_targets.values


# Sample Multilayer Perceptron Neural Network in Keras
from keras.models import Sequential
from keras.layers import Dense
import numpy

model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(train_data.shape[1],)))
model.add(Dense(32, activation='relu'))
#model.add(Dense(1, activation='sigmoid'))
model.add(Dense(1))

# 2. compile the network
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# 3. fit the network
history = model.fit(train_data, train_targets, epochs=100, batch_size=50)

# 4. evaluate the network
loss, accuracy = model.evaluate(test_data, test_targets)
print("\nLoss: %.2f, Accuracy: %.2f%%" % (loss, accuracy*100))

# 5. make predictions
probabilities = model.predict(test_data)
predictions = [float(x) for x in probabilities]
accuracy = numpy.mean(predictions == test_targets)
print("Prediction Accuracy: %.2f%%" % (accuracy*100))

结果如下:

任何帮助将不胜感激.

推荐答案

精度是一种分类指标,将其用于回归没有任何意义.没有实际的问题.

Accuracy is a classification metric, it makes no sense to use it for regression. There is no actual problem.

这篇关于Keras NN回归模型具有低损失和0精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆