Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how does the index works in this case

i cant understand how this specific line works in this programm

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

this programm is making a prediction about the price of 3 cabins considering some factors.(square m,distances form city ,bathrooms ,etc)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Each row in X represents 1 cabin ,so in X there are infos about 3 cabins in total

In C there are some values that correspond to the coefficient for every factor

`

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)

`
the correct prices are

cabin 1 = 258250

cabin 2 = 76100

cabin 3 = 492750

my newbie approach was very simple ,typing each cabin alone ,so if i need 30 cabins i would type 30 different lines . I found the above answer online

>Solution :

Adding print lines like this helps you actually visualize what is going on.

There is a standard library for linear regression which is not only simple to use, has tons of extended functionality.

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)

Output:

 '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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading