简体   繁体   中英

my simple prediction with linear regression wont execute

Below is my trial code:

from sklearn import linear_model

# plt.title("Time-independent variant student performance analysis")

x_train = [5, 9, 33, 25, 4]
y_train = [35, 2, 14 ,9, 7]
x_test = [14, 2, 8, 1, 11]

# create linear regression object
linear = linear_model.LinearRegression()

#train the model using the training sets and check score
linear.fit(x_train, y_train)
linear.score(x_train, y_train)

# predict output
predicted = linear.predict(x_test)

when run, this is the output:

ValueError: Found arrays with inconsistent numbers of samples: [1 5]

Redefine

x_train = [[5],[9],[33],[25],[4]]
y_train = [35,2,14,9,7]
x_test = [[14],[2],[8],[1],[11]]

From doc of fit(X, y) : X : numpy array or sparse matrix of shape [n_samples,n_features]

In your case, every example has only one feature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM