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

我有一个多项式回归图,我正在尝试使用下一个 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 获取正确的值作为预测值。我不确定为什么它为零,因为当我打印预测值时,我得到了正确的值


桃花长相依
浏览 78回答 1
1回答

撒科打诨

弄清楚了。图中没有 X 值,因此自动设置为 0。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:&nbsp; &nbsp; players_data = json.load(json_file)for obj in players_data:&nbsp; &nbsp; obj['Scrambling_List'] = [i for i in obj['Scrambling_List'] if i]for obj in players_data:&nbsp; &nbsp; def create_2d_lst(lst):&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(lst) < 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [0, 0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [[i, j] for i, j in enumerate(lst)]&nbsp; &nbsp; &nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; scrambling = create_2d_lst(obj['Scrambling_List'])&nbsp; &nbsp; &nbsp; &nbsp; total_putts_GIR = create_2d_lst(obj['Total_Putts_GIR_List'])&nbsp; &nbsp; &nbsp; &nbsp; SG_Putting = create_2d_lst(obj['SG_Putting_List'])&nbsp; &nbsp; except Exception:&nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; data = scrambling&nbsp; &nbsp; X = np.array(data)[:,0].reshape(-1,1)&nbsp; &nbsp; y = np.array(data)[:,1].reshape(-1,1)&nbsp; &nbsp; poly_reg = PolynomialFeatures(degree=4)&nbsp; &nbsp; X_poly = poly_reg.fit_transform(X)&nbsp; &nbsp; pol_reg = LinearRegression()&nbsp; &nbsp; pol_reg.fit(X_poly, y)&nbsp; &nbsp; predicted_y = poly_reg.fit_transform(X)&nbsp; &nbsp; m = pol_reg.coef_&nbsp; &nbsp; c = pol_reg.intercept_&nbsp; &nbsp; prediction = pol_reg.predict(poly_reg.fit_transform([[len(X)+1]]))&nbsp; &nbsp; def viz_polymonial():&nbsp; &nbsp; &nbsp; &nbsp; plt.scatter(X, y, color='red')&nbsp; &nbsp; &nbsp; &nbsp; plt.plot(X, pol_reg.predict(poly_reg.fit_transform(X)), color='blue')&nbsp; &nbsp; &nbsp; &nbsp; plt.plot(len(X)+1, pol_reg.predict(poly_reg.fit_transform([[len(X)+1]])), marker='x', color='green')&nbsp; &nbsp; &nbsp; &nbsp; plt.title('Projected Scrambling Percentage')&nbsp; &nbsp; &nbsp; &nbsp; plt.xlabel('Tournaments')&nbsp; &nbsp; &nbsp; &nbsp; plt.ylabel('Scrambling Percentage')&nbsp; &nbsp; &nbsp; &nbsp; plt.show()&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; viz_polymonial()&nbsp; &nbsp; print(obj['Name'], prediction)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python