Home > front end >  TypeError: fit_transform() missing 1 required positional argument: 'y'
TypeError: fit_transform() missing 1 required positional argument: 'y'

Time:04-27

import pandas as pd
import calendar
from datetime import datetime
data1 = pd.read_csv(train.csv")
data1['date']= data1.datetime.apply(lambda x : x.split()[0])
data1['hour']= data1.datetime.apply(lambda x : x.split()[1].split(":")[0])
data1["weekday"]= data1.date.apply(lambda dateString : calendar.day_name[datetime.strptime(dateString,"%m/%d/%Y").weekday()])
data1["month"]= data1.date.apply(lambda dateString : calendar.month_name[datetime.strptime(dateString,"%m/%d/%Y").month])
data1.to_csv("output1.csv")
bookings = data1.groupby('month')['Total_booking'].mean().sort_values(ascending=False)
bookings.to_csv("output2.csv")
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
data1['holiday'] =  le.fit_transform(data1['holiday'])

newdata = get_dummies(data,columns=['weekday','month','season','weather'])
data2 = pd.concat(newdata,axis=1)

newdata1 = data1.drop(['weekday','month','season','weather'],axis=1,inplace=true)
newdata1.to_csv('output/output3.csv')

NameError          Traceback (most recent call last)
 ~\AppData\Local\Temp/ipykernel_11228/1515056018.py in <module>
    3 data1['holiday'] = le.fit_transform(data1['holiday'])
    4 
     ----> 5 newdata = get_dummies(data1,columns= 
  ['weekday','month','season','weather'])
  6 data2 = pd.concat(newdata,axis=1)
  7 

  NameError: name 'get_dummies' is not defined

I am trying to save the new CSV file by dropping some values and changing the DateTime format....a little help...................... I DID RECTIFY THE ERRORS BUT NOW IT SHOWING THESE .......................................................

CodePudding user response:

You might have missed out on calling your dataset.

Try updating le.fit_transform('holiday') to le.fit_transform(data['holiday']) instead.

LabelEncoder.fit_transform() takes in an array-like parameter containing the target values and return the encoded labels.

CodePudding user response:

It seems you have two main errors.

1. LabelEncoder

This code of line is erroneous. fit_transform() is is not a class method, it is an instance method. This means that, first, you have to create an instance of the class.

You should add () like this to create and instance of the class:

le = LabelEncoder()

2. le.fit_transform

You should refererence to you dataframe (data1 in your case) like this:

le.fit_transform(data1['holiday'])
  • Related