gpt4 book ai didi

python - 使用经过训练的 Keras 模型对新的 csv 数据进行预测

转载 作者:行者123 更新时间:2023-11-30 09:59:01 25 4
gpt4 key购买 nike

所以我正在制作一个项目,基本上我必须预测房价是否高于或低于其中位价格,为此,我使用 Kaggle 的数据集( https://drive.google.com/file/d/1GfvKA0qznNVknghV4botnNxyH-KvODOC/view )。 1 表示“高于中位数”,0 表示“低于中位数”。我编写了这段代码来训练神经网络并将其保存为 .h5 文件:

import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
import h5py

df = pd.read_csv('housepricedata.csv')
dataset = df.values

X = dataset[:,0:10]
Y = dataset[:,10]

min_max_scaler = preprocessing.MinMaxScaler()

X_scale = min_max_scaler.fit_transform(X)

X_train, X_val_and_test, Y_train, Y_val_and_test = train_test_split(X_scale, Y, test_size=0.3)
X_val, X_test, Y_val, Y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.5)

model = Sequential([
Dense(32, activation='relu', input_shape=(10,)),
Dense(32, activation='relu'),
Dense(1, activation='sigmoid'),
])

model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=['accuracy'])

hist = model.fit(X_train, Y_train,
batch_size=32, epochs=100,
validation_data=(X_val, Y_val))

model.save("house_price.h5")

运行后,它成功地将.h5文件保存到我的目录中。我现在想做的是使用经过训练的模型对新的 .csv 文件进行预测,并确定每个文件是否高于或低于中位价格。这是 VSCode 中 csv 文件的图像,我希望它能够对其进行预测: csv file image正如您所看到的,该文件不包含 1(高于中位数)或 0(低于中位数),因为这就是我希望它预测的内容。这是我为此编写的代码:

import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
import h5py

df = pd.read_csv('data.csv')
dataset = df.values

X = dataset[:,0:10]
Y = dataset[:,10]

min_max_scaler = preprocessing.MinMaxScaler()

X_scale = min_max_scaler.fit_transform(X)

X_train, X_val_and_test, Y_train, Y_val_and_test = train_test_split(X_scale, Y, test_size=0.3)
X_val, X_test, Y_val, Y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.5)

model = load_model("house_price.h5")

y_pred = model.predict(X_test)

print(y_pred)

它的输出是[[0.00101464]]我不知道那是什么以及为什么它只返回一个值,即使csv文件有4行。有谁知道我如何解决这个问题并能够预测 csv 文件中每一行的 1 或 0 ?谢谢!

最佳答案

我明白你想要什么! 咱们试试吧 !这段代码对我有用

 import tensorflow
model = tensorflow.keras.models.load_model("house_price.h5")
y_pred=model.predict(X_test)

您仍然无法访问以下网站 1:answer1 2:answer2

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('C:\\Users\\acer\\Downloads\\housepricedata.csv')
dataset.head()

X=dataset.iloc[:,0:10]
y=dataset.iloc[:,10]

X.head()
from sklearn.preprocessing import StandardScaler
obj=StandardScaler()
X=obj.fit_transform(X)

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split
(X,y,random_state=2020,test_size=0.25)

print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)

import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
classifier = Sequential()

# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation =
'relu', input_dim = 10))
# classifier.add(Dropout(p = 0.1))

# Adding the second hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation
= 'relu'))
# classifier.add(Dropout(p = 0.1))

# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation
= 'sigmoid'))

# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics
= ['accuracy'])

classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
print(y_pred)

classifier.save("house_price.h5")

import tensorflow
model = tensorflow.keras.models.load_model("house_price.h5")
y_pred=model.predict(X_test)
y_pred = (y_pred > 0.5)
print(y_pred)

两个 y_pred 为我产生相同的输出

这里你不 y_pred 不包含 0 和 1 因为你使用 sigmoid 函数来确定概率预测所以如果(y_pred>0.5)它的平均值是一

  #True rep one

#false rep zero

#you can use replace function or map function of pandas to get convert true
into 1

关于python - 使用经过训练的 Keras 模型对新的 csv 数据进行预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59797805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com