ValueError: Expected 2D array, got scalar array instead: array=750
ValueError: Expected 2D array, got scalar array instead: array=750
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.linear_model import LinearRegression
data=pd.read_csv('real_estate_price_size.csv')
y=data['price']
x=data['size']
y.shape
x.shape
两个输出相同 (100,)
x_matrix = x.values.reshape(-1,1)
reg = LinearRegression()
reg.fit(x_matrix,y)
reg.predict(750)
当我 运行 出现此错误时
ValueError: Expected 2D array, got scalar array instead:
array=750.
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.
我无法理解该怎么做我尝试重塑它但它没有成功。
和 (2) 之前我输入时
reg = LinearRegression()
reg.fit(x_matrix,y)
我曾经得到这样的输出
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
现在我只收到类似
的文字
LinearRegression()
我们将不胜感激。
您缺少一个维度。您的预测输入应具有 (n_samples, n_features)
的形状。尝试这样的事情:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.linear_model import LinearRegression
y=np.random.random((100, 1))
x=np.random.random((100, 1))
reg = LinearRegression()
reg.fit(x,y)
predict_input = np.array([[750]])
predict_input.shape
reg.predict(predict_input)
(1, 1)
array([[-26.07506481]])
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.linear_model import LinearRegression
data=pd.read_csv('real_estate_price_size.csv')
y=data['price']
x=data['size']
y.shape
x.shape
两个输出相同 (100,)
x_matrix = x.values.reshape(-1,1)
reg = LinearRegression()
reg.fit(x_matrix,y)
reg.predict(750)
当我 运行 出现此错误时
ValueError: Expected 2D array, got scalar array instead:
array=750.
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.
我无法理解该怎么做我尝试重塑它但它没有成功。
和 (2) 之前我输入时
reg = LinearRegression()
reg.fit(x_matrix,y)
我曾经得到这样的输出
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
现在我只收到类似
的文字 LinearRegression()
我们将不胜感激。
您缺少一个维度。您的预测输入应具有 (n_samples, n_features)
的形状。尝试这样的事情:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.linear_model import LinearRegression
y=np.random.random((100, 1))
x=np.random.random((100, 1))
reg = LinearRegression()
reg.fit(x,y)
predict_input = np.array([[750]])
predict_input.shape
reg.predict(predict_input)
(1, 1)
array([[-26.07506481]])