Page 231 - Touhpad Ai
P. 231

Program 31: To compare students' marks from different subjects using Z-score normalization

                    import pandas as pd
                    from sklearn.preprocessing import StandardScaler
                    # Sample data
                    data = {'Marks': [65, 75, 80, 92, 100]}
                    df = pd.DataFrame(data)
                    # Z-score normalization
                    scaler = StandardScaler()
                    df['Z_Score'] = scaler.fit_transform(df[['Marks']])
                    print(df)
                    Output:

                         Marks         Z_Score
                    0       65       -1.406523
                    1       75       -0.598176
                    2       80       -0.194003
                    3       92        0.776013
                    4      100        1.422690
                 Scale Adjustment (Unit Conversion)
                 Scale adjustment ensures all data uses the same units of measurement.

                    Program 32: To convert height from feet to centimetres or weight from pounds to kilograms
                    # Sample data
                    df = pd.DataFrame({'Weight_pounds': [120, 152, 185]})
                    # Convert to kilograms (1 pound = 0.453592 kg)

                    df['Weight_kg'] = df['Weight_pounds'] * 0.453592
                    print(df)
                    Output:

                        Weight_pounds           Weight_kg
                    0               120         54.431040
                    1               152         68.945984
                    2               185         83.914520
                 Feature Scaling (Min-Max Normalization)
                 Feature scaling  (Min–Max Normalization) changes values to a fixed range, usually between 0 and 1. It helps in
                 comparing different types of data equally.

                    Program 33: to scale salary data between 0 and 1 for analysis
                    from sklearn.preprocessing import MinMaxScaler

                    # Sample data
                    df = pd.DataFrame({'Salary': [20000, 30000, 40000, 50000, 60000]})
                    # Min-Max Scaling
                    scaler = MinMaxScaler()

                    df['Salary_Scaled'] = scaler.fit_transform(df[['Salary']])
                    print(df)


                                                                      Theoretical and Practical Aspects of Data Processing  229
   226   227   228   229   230   231   232   233   234   235   236