Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

one-hot encode categorical variables standardize numerical variables

from sklearn.preprocessing import StandardScaler

num_vars = ['pickup_lon', 'pickup_lat', 'dropoff_lon', 'dropoff_lat', 'distance']
cat_vars = ['hour', 'day', 'region']

scaler = StandardScaler()
scaler.fit(train[num_vars])

def design_matrix(t):
    """Create a design matrix from taxi ride dataframe t."""
    scaled = t[num_vars].copy()
    scaled.iloc[:,:] = scaler.transform(scaled) # Convert to standard units
    categoricals = [pd.get_dummies(t[s], prefix=s, drop_first=True) for s in cat_vars]
    return pd.concat([scaled] + categoricals, axis=1)

design_matrix(train).iloc[0,:]  
Comment

PREVIOUS NEXT
Code Example
Python :: python dict items 
Python :: app.py 
Python :: pandas using eval converter excluding nans 
Python :: logging store info to different files 
Python :: numpy array [-1] 
Python :: how to avoid inserting duplicate records in orm django 
Python :: how to get all values from class in python 
Python :: count item in list 
Python :: pd df set index 
Python :: explain the use of return keyword python 
Python :: unicodedata no accent 
Python :: plot dataframe 
Python :: how to sum all the values in a list in python 
Python :: tensorflow 
Python :: python program to calculate the average of numbers in a given list 
Python :: google.protobuf.Struct example python 
Python :: programação funcional python - lambda 
Python :: speech enhancement techniques 
Python :: add item to list python 
Python :: import random python 
Python :: Math Module cos() Function in python 
Python :: python reverse range 
Python :: Converting 12 hour clock time to 24 hour clock time 
Python :: how to limit a command to a role in discord.py 
Python :: pathy python 
Python :: stack python 
Python :: length of queue python 
Python :: nlp spacy medium 
Python :: if or python 
Python :: python in 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =