goofyreader
goofyreader

Reputation: 21

How to create a Neural Network using MLPClassifier from Scikit Learn using 5 X inputs and 1 Y output?

So I'm trying to map a set of features to a value using a neural network in Python (Scikit-Learn). Basically I'm reading some values from a csv and then plugging them into the classifier and an error keeps occurring. I do not have a lot of experience with ML and this library is new to me so any help would be appreciated.

Code:

from sklearn.neural_network import MLPClassifier
import csv
with open('training.csv') as csvfile:
    reader = csv.reader(csvfile,delimiter=' ')
    for row in reader:
        print(row)
        x.append([row[0],row[1],row[2],row[3],row[4]])
        y.append(row[5])
clf = MLPClassifier(solver='lbfgs',alpha=1e-5,hidden_layer_sizes=(15,),random_state=1)
print (clf.fit(x,y))

Error:

Traceback (most recent call last):
  File "analyzeTrainingData.py", line 13, in <module>
print (clf.fit(x,y))
  File "/usr/local/lib/python3.5/dist-packages/sklearn/neural_network/multilayer_perceptron.py", line 973, in fit
hasattr(self, "classes_")))
  File "/usr/local/lib/python3.5/dist-packages/sklearn/neural_network/multilayer_perceptron.py", line 331, in _fit
X, y = self._validate_input(X, y, incremental)
  File "/usr/local/lib/python3.5/dist-packages/sklearn/neural_network/multilayer_perceptron.py", line 910, in _validate_input
multi_output=True)
  File "/usr/local/lib/python3.5/dist-packages/sklearn/utils/validation.py", line 542, in check_X_y
ensure_min_features, warn_on_dtype, estimator)
  File "/usr/local/lib/python3.5/dist-packages/sklearn/utils/validation.py", line 410, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:

It then prints out a list of all the X data in this format:

list(['237', '128', '352', '721.6', '11.275'])]

and then states:

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Git Repo

Upvotes: 0

Views: 802

Answers (1)

Nick W.
Nick W.

Reputation: 46

Assuming x is a numpy array, you are creating a (N,) shape vector, which cannot be fed in as training data. Try print(x.shape()) and see what it prints out.

If you want to create a matrix with 5 columns, you should use np.append(x, [row[0],row[1],row[2],row[3],row[4]], axis=0) as is specified in the numpy documentation

Upvotes: 1

Related Questions