在这种情况下,索引是如何工作的



我不明白这个特定的行是如何在这个程序中工作的

price += cabin[c.index(coefficient)]*coefficient

这个程序是在考虑一些因素的情况下对3间客舱的价格进行预测。(平方米,距离城市、浴室等(

X中的每一行代表1个客舱,因此在X中总共有3个客舱的信息

在C中,有一些值对应于每个因素的系数

`

X = [[66, 5, 15, 2, 500], 
[21, 3, 50, 1, 100], 
[120, 15, 5, 2, 1200]]
c = [3000, 200, -50, 5000, 100]    # coefficient values
def predict(X, c):
price = 0 
for cabin in X:
for coefficient in c:
price += cabin[c.index(coefficient)]*coefficient
print(price)
price = 0

predict(X, c)

`正确的价格是

客舱1=258250

客舱2=76100

客舱3=492750

我的新手方法非常简单,只需键入每个舱室,所以如果我需要30个舱室,我会键入30行不同的行。我在上找到了上面的答案

添加这样的打印行可以帮助您实际可视化正在发生的事情

有一个线性回归的标准库,它不仅使用简单,而且具有大量扩展功能。

https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html

X = [[66, 5, 15, 2, 500], 
[21, 3, 50, 1, 100], 
[120, 15, 5, 2, 1200]]
c = [3000, 200, -50, 5000, 100]    # coefficient values
def predict(X, c):
price = 0 
for cabin in X:
for coefficient in c:
print(f" 'price' variable = {price} + {cabin[c.index(coefficient)]} * {coefficient}", end = ' = ') # helps you visualize
price += cabin[c.index(coefficient)]*coefficient
print(f'{price}')
print(price) #
price = 0

predict(X, c)

输出:

'price' variable = 0 + 66 * 3000 = 198000
'price' variable = 198000 + 5 * 200 = 199000
'price' variable = 199000 + 15 * -50 = 198250
'price' variable = 198250 + 2 * 5000 = 208250
'price' variable = 208250 + 500 * 100 = 258250
258250
'price' variable = 0 + 21 * 3000 = 63000
'price' variable = 63000 + 3 * 200 = 63600
'price' variable = 63600 + 50 * -50 = 61100
'price' variable = 61100 + 1 * 5000 = 66100
'price' variable = 66100 + 100 * 100 = 76100
76100
'price' variable = 0 + 120 * 3000 = 360000
'price' variable = 360000 + 15 * 200 = 363000
'price' variable = 363000 + 5 * -50 = 362750
'price' variable = 362750 + 2 * 5000 = 372750
'price' variable = 372750 + 1200 * 100 = 492750
492750

最新更新