无法使用 python 中的给定值进行预测
Unable to predict using a given value in python
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('poly.csv')
x = dataset.iloc[:,1:2].values
y = dataset.iloc[:,3:4].values
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y,train_size = 0.8, shuffle= False, random_state = 0)
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=3)
x_poly = poly_reg.fit_transform(x)
poly_reg.fit(x_poly, y)
from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(x_poly, y)
reg.predict([[13000]])
数据集
https://drive.google.com/open?id=10kbNBEEvShSZ_svxrYxPNXinB9Jw1f61
错误:
> ...: reg.predict([[13000]])
> C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py:2026:
> FutureWarning: From version 0.21, test_size will always complement
> train_size unless both are specified. FutureWarning) Traceback (most
> recent call last):
>
> File "<ipython-input-1-f94f25f36c83>", line 24, in <module>
> reg.predict([[13000]])
>
> File
> "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\base.py",
> line 256, in predict
> return self._decision_function(X)
>
> File
> "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\base.py",
> line 241, in _decision_function
> dense_output=True) + self.intercept_
>
> File
> "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\extmath.py",
> line 140, in safe_sparse_dot
> return np.dot(a, b)
>
> ValueError: shapes (1,1) and (4,1) not aligned: 1 (dim 1) != 4 (dim 0)
您首先需要转换要进行预测的 x 值。
所以你只需要将最后一行更改为:
reg.predict(poly_reg.fit_transform([[13000]]))
那么它应该可以正常工作。
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('poly.csv')
x = dataset.iloc[:,1:2].values
y = dataset.iloc[:,3:4].values
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y,train_size = 0.8, shuffle= False, random_state = 0)
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=3)
x_poly = poly_reg.fit_transform(x)
poly_reg.fit(x_poly, y)
from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(x_poly, y)
reg.predict([[13000]])
数据集
https://drive.google.com/open?id=10kbNBEEvShSZ_svxrYxPNXinB9Jw1f61
错误:
> ...: reg.predict([[13000]])
> C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py:2026:
> FutureWarning: From version 0.21, test_size will always complement
> train_size unless both are specified. FutureWarning) Traceback (most
> recent call last):
>
> File "<ipython-input-1-f94f25f36c83>", line 24, in <module>
> reg.predict([[13000]])
>
> File
> "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\base.py",
> line 256, in predict
> return self._decision_function(X)
>
> File
> "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\base.py",
> line 241, in _decision_function
> dense_output=True) + self.intercept_
>
> File
> "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\extmath.py",
> line 140, in safe_sparse_dot
> return np.dot(a, b)
>
> ValueError: shapes (1,1) and (4,1) not aligned: 1 (dim 1) != 4 (dim 0)
您首先需要转换要进行预测的 x 值。 所以你只需要将最后一行更改为:
reg.predict(poly_reg.fit_transform([[13000]]))
那么它应该可以正常工作。