首页 > 解决方案 > Sklearn 错误无法将大小为 6912 的数组重塑为形状 (614,154)

问题描述

我观看了有关如何构建您的第一个神经网络的视频,但在 27:00 ValueError: cannot reshape array of size 6912 into shape (614,154) 时遇到了这个错误

https://www.youtube.com/watch?v=S2sZNlr-4_4

代码如下:

# This algorithm detects if a person has diabetes or not

# Load libraries
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
import pandas as pd
# load data
from google.colab import files
uploaded = files.upload()
# Store the dataset
df = pd.read_csv('datasets_4511_6897_diabetes.csv')
# convert the data into an array
dataset = df.values
# get all of the rows from the first eight columns of the dataset
X = dataset[:,0:8]
y = dataset[:,8]
# process the data
from sklearn import preprocessing 
min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)

# split the data into 80% training and 20% testing
X_train, X_test, y_train, y_test = train_test_split(X_scale, y, test_size = 0.2, random_state = 4)
# build the model

model = Sequential([
    Dense(12, activation ='relu', input_shape = (8,)),
    Dense(15, activation = 'relu'),
    Dense(1, activation = 'sigmoid')
])
#Compile the model (sgd = stochastic gradient descent)

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

hist = model.fit(X_train, y_train, batch_size = 57, epochs = 1000, validation_split=0.2)
# evaluate the model on the training data set
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

pred = model.predict(X_train)
pred = [1 if y >= 0.5 else 0 for y in prediction]
#df = df.values.reshape(614,154)
print('confusion_matrix : /n', confusion_matrix(y_train, pred))

你知道如何处理这个问题吗?

先感谢您

标签: pythonkerasneural-networksklearn-pandas

解决方案


推荐阅读