为了账号安全,请及时绑定邮箱和手机立即绑定

在二维列表中获取正确的值

在二维列表中获取正确的值

桃花长相依 2023-03-16 09:52:17
我有一个多项式回归图,我正在尝试使用下一个 X 值找到预测值 y。import numpy as npimport matplotlib.pyplot as pltfrom sklearn.linear_model import LinearRegressionimport jsonimport matplotlib.pyplot as pltfrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import PolynomialFeatureswith 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 获取正确的值作为预测值。我不确定为什么它为零,因为当我打印预测值时,我得到了正确的值
查看完整描述

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)



查看完整回答
反对 回复 2023-03-16
  • 1 回答
  • 0 关注
  • 77 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信