Python KeyError:0 hatası

Mali_7

Femtopat
Katılım
10 Haziran 2024
Mesajlar
1
Daha fazla  
Cinsiyet
Erkek
Multiple Linear Regression modeli oluşturup cost fonksiyonuyla başarısını ölçmek istiyordum. Kod şöyle:

Python:
import pandas as pd
import numpy as np

df = pd.read_csv('/Users/mali/Downloads/multiple_linear_regression_dataset.csv')

x_train = df.iloc[:,:2]
y_train = df.iloc[:,2]

print(f"x_tran shape{x_train.shape}, x_train value {x_train}")
print(f"y_tran shape{y_train.shape}, y_train value {y_train}")





w_init=np.array([0.3,30])
b_init=50

print(f"w_init shape {w_init.shape}, type of b_init {type(b_init)}")

# our multiple linear regression model : f_wb = w1 * x1 + w2 * x2 + b
x_vec = x_train.iloc[0,:]
print(f"x_vec shape {x_vec.shape}, x_vec value {x_vec}")

def predict_single_vector(w,b,x):
    m = x.shape[0]
    p=0
    for i in range(m):
        v_d = x[i] * w[i]
        p += v_d
   
    p = p + b
    return p
# also we can use np.dot() method

def predict_dot(w,b,x):
    p = np.dot(x,w) + b
    return p



def compute_cost(w,b,x,y):
    m=x.shape[0]
    cost = 0
    for i in range(m):
        f_wb = np.dot(x[i],w) + b
        cost = cost + (f_wb - y[i])**2
   
    total_cost = 1/(2*m)*cost
   
    return total_cost

Tüm fonksiyonlar çalışırken compute_cost fonksiyonunu çalıştırınca şu hatayı aldım:

compute_cost(x_train,y_train,w_init,b_init)
Traceback (most recent call last):

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/indexes/base.py:3653 in get_loc
return self._engine.get_loc(casted_key)

File pandas/_libs/index.pyx:147 in pandas._libs.index.IndexEngine.get_loc

File pandas/_libs/index.pyx:176 in pandas._libs.index.IndexEngine.get_loc

File pandas/_libs/hashtable_class_helper.pxi:7080 in pandas._libs.hashtable.PyObjectHashTable.get_item

File pandas/_libs/hashtable_class_helper.pxi:7088 in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 0


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

Cell In[39], line 1
compute_cost(x_train,y_train,w_init,b_init)

Cell In[38], line 16 in compute_cost
f_wb_i = np.dot(X, w) + b #(n,)(n,) = scalar (see np.dot)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/frame.py:3761 in getitem
indexer = self.columns.get_loc(key)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/indexes/base.py:3655 in get_loc
raise KeyError(key) from err

KeyError: 0

Yardımcı olduğunuz için şimdiden teşekkür ederim.
 
Son düzenleyen: Moderatör:

Bu konuyu görüntüleyen kullanıcılar

Technopat Haberler

Geri
Yukarı