亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在二維列表中獲取正確的值

在二維列表中獲取正確的值

桃花長相依 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 關注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號