1 回答
TA贡献1934条经验 获得超2个赞
弄清楚了。图中没有 X 值,因此自动设置为 0。
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 = pol_reg.predict(poly_reg.fit_transform([[len(X)+1]]))
def viz_polymonial():
plt.scatter(X, y, color='red')
plt.plot(X, pol_reg.predict(poly_reg.fit_transform(X)), color='blue')
plt.plot(len(X)+1, pol_reg.predict(poly_reg.fit_transform([[len(X)+1]])), marker='x', color='green')
plt.title('Projected Scrambling Percentage')
plt.xlabel('Tournaments')
plt.ylabel('Scrambling Percentage')
plt.show()
return
viz_polymonial()
print(obj['Name'], prediction)
添加回答
举报