gpt4 book ai didi

python - 如何将 sklearn "LinearRegression"与列表一起使用?

转载 作者:行者123 更新时间:2023-11-30 22:18:53 25 4
gpt4 key购买 nike

我有一个这样的列表:

list1 = [['2012', '1', '3', '1', '832.0', '261.0', '100.00'],
['2012', '1', '5', '1', '507.0', '193.0', '92.50'],
['2012', '2', '3', '1', '412.0', '200.0', '95.00'],
['2012', '2', '5', '1', '560.0', '335.0', '90.00'],
['2012', '3', '3', '1', '584.0', '205.0', '100.00'],
['2012', '3', '5', '1', '595.0', '162.0', '92.50'],
['2012', '4', '3', '1', '504.0', '227.0', '100.00'],
['2012', '4', '5', '1', '591.0', '264.0', '92.50'],
['2012', '5', '3', '1', '489.0', '234.0', '100.00'],
['2012', '5', '5', '1', '561.0', '292.0', '95.00'],
['2012', '6', '3', '1', '622.0', '221.0', '100.00'],
['2012', '6', '5', '1', '478.0', '204.0', '92.50'],
['2012', '7', '3', '1', '974.0', '290.0', '100.00'],
['2012', '7', '5', '1', '553.0', '269.0', '95.00'],
['2012', '8', '3', '1', '473.0', '158.0', '100.00'],
['2012', '8', '5', '1', '526.0', '174.0', '92.50'],
['2012', '9', '3', '1', '701.0', '189.0', '95.00'],
['2012', '9', '5', '1', '502.0', '179.0', '97.50'],
['2012', '10', '3', '1', '470.0', '184.0', '100.00'],
['2012', '10', '5', '1', '579.0', '218.0', '92.50']]

每行第七列为Y数据,每行第五列和第六列为X数据。我想用这个列表计算“截距”和“coef”。如果有一个csv文件,我知道怎么做,例如:

import pandas as pd
from sklearn import linear_model

wine = pd.read_csv( r"D:/1.csv",sep=',' )
clf = linear_model.LinearRegression()
X = wine.drop( 'point', axis=1 )
y = wine['point']
clf.fit( X, y )
print( clf.intercept_,clf.coef_[0],clf.coef_[1] )

我的 csv: enter image description here

但是现在,我需要用列表计算“截距”和“coef”。怎么做?

最佳答案

没有太多变化,只需将 list1 作为 float、slice 和 fit 加载到 DataFrame 中即可:

from sklearn.linear_model import LinearRegression

df = pd.DataFrame(list1, dtype=float)
X = df.iloc[:, 5:7]
y = df.iloc[:, [-1]]

clf = LinearRegression()
clf.fit(X, y);

clf.intercept_
[0.]

clf.coef_
[[0. 1.]]

关于python - 如何将 sklearn "LinearRegression"与列表一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49250151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com