How do you scale in python?
Scale FeaturesWhen your data has different values, and even different measurement units, it can be difficult to compare them. What is kilograms compared to meters? Or altitude compared to time? Show The answer to this problem is scaling. We can scale data into new values that are easier to compare. Take a look at the table below, it is the same data set that we used in the multiple regression chapter, but this time the volume column contains values in liters instead of cm3 (1.0 instead of 1000).
It can be difficult to compare the volume 1.0 with the weight 790, but if we scale them both into comparable values, we can easily see how much one value is compared to the other. There are different methods for scaling data, in this tutorial we will use a method called standardization. The standardization method uses this formula:
Where If you take the weight column from the data set above, the first value is 790, and the scaled value will be:
If you take the volume column from the data set above, the first value is 1.0, and the scaled value will be:
Now you can compare -2.1 with -1.59 instead of comparing 790 with 1.0. You do not have to do this manually, the Python sklearn module has a method called ExampleScale all values in the Weight and Volume columns: import pandas df = pandas.read_csv("data.csv") X = df[['Weight', 'Volume']] scaledX = scale.fit_transform(X) print(scaledX) Result:Note that the first two values are -2.1 and -1.59, which corresponds to our calculations: [[-2.10389253 -1.59336644] [-0.55407235 -1.07190106] [-1.52166278 -1.59336644] [-1.78973979 -1.85409913] [-0.63784641 -0.28970299] [-1.52166278 -1.59336644] [-0.76769621 -0.55043568] [ 0.3046118 -0.28970299] [-0.7551301 -0.28970299] [-0.59595938 -0.0289703 ] [-1.30803892 -1.33263375] [-1.26615189 -0.81116837] [-0.7551301 -1.59336644] [-0.16871166 -0.0289703 ] [ 0.14125238 -0.0289703 ] [ 0.15800719 -0.0289703 ] [ 0.3046118 -0.0289703 ] [-0.05142797 1.53542584] [-0.72580918 -0.0289703 ] [ 0.14962979 1.01396046] [ 1.2219378 -0.0289703 ] [ 0.5685001 1.01396046] [ 0.3046118 1.27469315] [ 0.51404696 -0.0289703 ] [ 0.51404696 1.01396046] [ 0.72348212 -0.28970299] [ 0.8281997 1.01396046] [ 1.81254495 1.01396046] [ 0.96642691 -0.0289703 ] [ 1.72877089 1.01396046] [ 1.30990057 1.27469315] [ 1.90050772 1.01396046] [-0.23991961 -0.0289703 ] [ 0.40932938 -0.0289703 ] [ 0.47215993 -0.0289703 ] [ 0.4302729 2.31762392]] Run example » Predict CO2 ValuesThe task in the Multiple Regression chapter was to predict the CO2 emission from a car when you only knew its weight and volume. When the data set is scaled, you will have to use the scale when you predict values: ExamplePredict the CO2 emission from a 1.3 liter car that weighs 2300 kilograms: import pandas df = pandas.read_csv("data.csv") X = df[['Weight', 'Volume']] scaledX = scale.fit_transform(X) regr = linear_model.LinearRegression() scaled = scale.transform([[2300, 1.3]]) predictedCO2 = regr.predict([scaled[0]]) Result:Run example » How do you scale data in Python?There are different methods for scaling data, in this tutorial we will use a method called standardization. Where z is the new value, x is the original value, u is the mean and s is the standard deviation. Now you can compare -2.1 with -1.59 instead of comparing 790 with 1.0.
What does scale mean in Python?Feature Scaling or Standardization: It is a step of Data Pre Processing that is applied to independent variables or features of data. It basically helps to normalize the data within a particular range. Sometimes, it also helps in speeding up the calculations in an algorithm. Package Used: sklearn.preprocessing.
How do you normalize and scale data in Python?Using MinMaxScaler() to Normalize Data in Python
This is a more popular choice for normalizing datasets. You can see that the values in the output are between (0 and 1). MinMaxScaler also gives you the option to select feature range. By default, the range is set to (0,1).
How do you feature scaling?The most common techniques of feature scaling are Normalization and Standardization. Normalization is used when we want to bound our values between two numbers, typically, between [0,1] or [-1,1]. While Standardization transforms the data to have zero mean and a variance of 1, they make our data unitless.
|