Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (120 points)
edited by
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
datasets = pd.read_csv('Salary_Data.csv')
X = datasets.iloc[: , 0].values
Y = datasets.iloc[: , 1].values
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,Y,test_size = 1/3,random_state = 0)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train,y_train)
It is giving error:
  File "C:\Users\mahih\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 521, in check_array
    "if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[ 2.9  5.1  3.2  4.5  8.2  6.8  1.3 10.5  3.   2.2  5.9  6.   3.7  3.2
  9.   2.   1.1  7.1  4.9  4. ].
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.

1 Answer

0 votes
by
edited by
Hi you can use this command to solve the issue
linear_regression.fit(X_train.values.reshape(-1,1),y_train)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...