Получение ValueError: недопустимый литерал для int()Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Получение ValueError: недопустимый литерал для int()

Сообщение Anonymous »

Я получаю ошибку ниже. Я передаю образец входных данных в модель регрессора для прогнозирования, но получаю ошибку. Я пробовал разные способы это исправить, но это не работает. Спасибо за помощь.
#'time' column to Unix timestamp
df['time'] = pd.to_datetime(df['time']).astype(int) // 10**9
label_encoder = LabelEncoder()
df['vehicle_id'] = label_encoder.fit_transform(df['vehicle_id'])
X = df[['time', 'vehicle_id', 'vehicle_lat', 'vehicle_lon', 'cell_lat', 'cell_lon']] # Features
y = df.groupby(['time', 'vehicle_id'])['cell_id'].nunique().reset_index(name='cell_id_count')
df_merged = pd.merge(df, y, on=['time', 'vehicle_id'])

y = df_merged['cell_id_count']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
rf_regressor = RandomForestRegressor(n_estimators=10, random_state=42)
rf_regressor.fit(X_train, y_train)
y_pred = rf_regressor.predict(X_test)

#input
vehicle_id = 'veh2312'
time_step = 2194

#filter DataFrame based on 'vehicle_id' and 'time'
sample_input = df[(df['vehicle_id'] == label_encoder.transform([vehicle_id])[0]) & (df['time'] == time_step)]

if len(sample_input) > 0:
sample_features = sample_input[['time', 'vehicle_id', 'vehicle_lat', 'vehicle_lon', 'cell_lat', 'cell_lon']]

sample_predictions = rf_regressor.predict(sample_features)

print(f"Predicted number of cell_ids for vehicle '{vehicle_id}' at time step {time_step}: {sample_predictions[0]:.0f}")
else:
print(f"No data found for vehicle '{vehicle_id}' at time step {time_step}.")

Ошибка
---> 47 sample_input = df[(df['vehicle_id'] == label_encoder.transform([vehicle_id])[0]) & (df['time'] == time_step)]
/usr/local/lib/python3.10/dist-packages/sklearn/utils/_array_api.py in astype(self, x, dtype, copy, casting)
66 def astype(self, x, dtype, *, copy=True, casting="unsafe"):
67 # astype is not defined in the top level NumPy namespace
---> 68 return x.astype(dtype, copy=copy, casting=casting
ValueError: invalid literal for int() with base 10: 'veh2312'

Пример набора данных
cell_id cell_lat cell_lon time vehicle_lat vehicle_lon vehicle_id distance
2665 43.720118 10.422051 2194 43.70994621 10.42621903 veh2312 1205
2682 43.695238 10.433212 2194 43.70994621 10.42621903 veh2312 1787
20020711 43.716044 10.430057 2194 43.70994621 10.42621903 veh2312 791
27025350 43.710199 10.4247325 2194 43.70994621 10.42621903 veh2312 167
118817885 43.712409 10.424214 2194 43.70994621 10.42621903 veh2312 349
118825175 43.706199 10.431691 2194 43.70994621 10.42621903 veh2312 731
118840263 43.706233 10.432086 2194 43.70994621 10.42621903 veh2312 766


Подробнее здесь: https://stackoverflow.com/questions/785 ... al-for-int
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»