我有一个多项式回归图,我正在尝试使用下一个 X 值找到预测值 y。
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
import json
import matplotlib.pyplot as plt
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures
with open('/Users/aus10/Desktop/PGA/Data_Cleanup/Combined_Player_Stats.json') as json_file:
players_data = json.load(json_file)
for obj in players_data:
obj['Scrambling_List'] = [i for i in obj['Scrambling_List'] if i]
for obj in players_data:
def create_2d_lst(lst):
try:
if len(lst) < 1:
return [0, 0]
else:
return [[i, j] for i, j in enumerate(lst)]
except:
pass
try:
scrambling = create_2d_lst(obj['Scrambling_List'])
total_putts_GIR = create_2d_lst(obj['Total_Putts_GIR_List'])
SG_Putting = create_2d_lst(obj['SG_Putting_List'])
except Exception:
pass
data = scrambling
X = np.array(data)[:,0].reshape(-1,1)
y = np.array(data)[:,1].reshape(-1,1)
poly_reg = PolynomialFeatures(degree=4)
X_poly = poly_reg.fit_transform(X)
pol_reg = LinearRegression()
pol_reg.fit(X_poly, y)
predicted_y = poly_reg.fit_transform(X)
m = pol_reg.coef_
c = pol_reg.intercept_
prediction_value = (len(X) + 1)
prediction = pol_reg.predict(poly_reg.fit_transform([[prediction_value]]))
def viz_polymonial():
plt.scatter(X, y, color='red')
plt.plot(X, pol_reg.predict(poly_reg.fit_transform(X)), color='blue')
plt.plot(prediction, marker='x', color='green')
plt.title('Projected Scrambling Percentage')
plt.xlabel('Tournaments')
plt.ylabel('Scrambling Percentage')
plt.show()
return
viz_polymonial()
当我使用 时prediction = prediction_value = (len(X) + 1), prediction = pol_reg.predict(poly_reg.fit_transform([[prediction_value]]))我应该获得 的下一个值X,但它返回 0,而它应该是 len(X) + 1。我需要为Xset 获取正确的值作为预测值。我不确定为什么它为零,因为当我打印预测值时,我得到了正确的值
撒科打诨
相关分类