ValueError при работе с фиктивными переменными
Я делаю задание на построение модели линейной регрессии и тут возникает ошибка при присвоении переменной нового значения Для начала мой датасет:
import pandas as pd
ind = [5375, 11681, 5325, 679, 12625, 8090, 11518, 16341, 2607,1742]
dats = { 'index' : [5376, 11682, 5326, 680, 12626, 8091, 11519, 16342, 2608,1743],
'date': [2011-8-16, 2012-5-6,2011-8-14,2011-1-31,2012-6-15,2011-12-8, 2011-4-30,2012-11-18,
2011-4-23,2011-3-18],
'season': [4,3,4,2,3,1,3,1,3,2],
'yr': [0,1,0,0,1,0,1,1,0,0],
'mnth': [8,5,8,1,6,12,4,11,4,3],
'hr': [21,22,19,15,6,18,3,15,13,7],
'holiday': [0,0,0,0,0,0,0,0,0,0],
'weekday': [2,0,0,1,5,4,1,0,6,5],
'workingday': [1,0,0,1,1,1,1,0,0,1],
'weathersit': [1,1,1,2,1,1,1,1,1,1],
'temp':[0.72,0.56,0.72,0.16,0.54,0.28,0.40,0.42,0.52,0.4],
'atemp': [0.6667, 0.5303, 0.6970, 0.1818, 0.5152, 0.2727, 0.4091, 0.4242, 0.5000, 0.4091],
'hum':[0.54, 0.73, 0.74, 0.59, 0.83, 0.52, 0.76, 0.54, 0.83, 0.66],
'windspeed': [0.0000, 0.1642, 0.2239, 0.1343, 0.1343, 0.2239, 0.1642, 0.2836, 0.3881, 0.1940],
'cnt': [215, 157, 259, 44, 151, 318, 4, 407, 312, 123]}
data = pd.DataFrame(data=dats, index=ind)
Задания:
Для дальнейшей работы с моделями для каждой категориальной переменной мы создадим фиктивные переменные во избежание неправильного порядка категорий
def get_dummies(df: pd.DataFrame): features = pd.concat([df, pd.get_dummies(df[['season', 'yr','mnth', 'hr','holiday','weekday', 'workingday', 'weathersit']]), ], axis = 1 ) features = features.drop(['season', 'mnth', 'weekday', 'weathersit' ], axis = 1) return features features = get_dummies(data) 2. На основе переменной времени создайте новую переменную, которая будет отвечать за день и ночь. features['night_hours'] = features['hr'] features = features.drop('hr', axis = 1)
Выскакивает ошибка
KeyError: 'night_hours'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
KeyError: 'night_hours'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/internals/blocks.py in check_ndim(values,
placement, ndim)
1978 if len(placement) != len(values):
1979 raise ValueError(
-> 1980 f"Wrong number of items passed {len(values)}, "
1981 f"placement implies {len(placement)}"
1982 )
ValueError: Wrong number of items passed 2, placement implies 1