NumPy: Transforming Data

  1. Data Cleaning

    Question 1 of 4

    • Write a function clean_outlier that takes 1D array, replaces outliers with median & return clean data. (Outliers: data below Q1 - 1.5 * IQR or above Q3 + 1.5 * IQR).
    • Write a function clean_outlier_std that implements similar task but different outlier definition. (data below mean - 2 * std or above mean + 2 * std).
    • You have sensor recording data over time. It records invalid value as -1. Write a function clean_invalid that takes 1D array, replace -1 with surrounding mean & return clean data.
    • Write function fix_invalid_data that takes 2D NumPy array. Array contains invalid value represented as -999. Function has to substitute invalid value with mean of data. Optionally function should have axis argument. If axis=0, replacement has to be done by column mean. If axis=1, replace by row mean else overall mean. [Hint: np.apply_along_axis(fn, axis, data)]
  2. Conditional (where, select)

    Question 2 of 4

    • Write a function incrment_salary that takes salary_array and returns increased salary. Hike Rule: <1000 => 20%, 1000-5000 => 10%, >5000 => 5%. (use: np.select)
    • Write a function categorize_age that takes age_array and categorizes it as Child, Teen, Adult, Senior depending on age range. (use: np.select)
    • Assign temp_data as [15, 22, 30, 5, 18, 25]. Generate new array temp_category that has elements below_avg and above_avg. (use: np.where)
    • Assign player_name as ["Player1", "Player2", "Player3", "Player4"], player_score as [85, 92, 78, 90]. Find the name of players with score above 90 and store as top_players.
  3. Transforming Arrays

    Question 3 of 4

    • Create 4x4 array with values randomly from 0-255. Reshape it to 3x3 with np.resize.
    • Create 6x6 array with values randomly from 0-255. Reshape it to 4x9 with np.shape.
    • Create 4x8 array from normal distribution (u=40, sd=10). Transpose this and print.
    • Create 3x3 array with random value from 0-255. Convert it to 4x4 such that excess element to left and top are filled with zero.
    • Create 4x4 array with random values from 0-20. Convert it into 1D using reshape, resize, flatten and ravel.
    • Practice the questions from Hacker Rank.
  4. Merging Arrays

    Question 4 of 4

    • Create 4x3 array with random values from 0-60. Another 2x3 array with random values from 10-80. Merge them vertically using concat and vstack.
    • Create 3x4 array with random values from 60-80. Another 3x2 array with random values from 20-90. Merge them horizontally using concat and hstack.
    • Ram and Shyam are doing survey on KTM & POKHARA city for pollution. CO2 & Temp reading recorded by them are [[.5, 22], [.3, 20], [.7, 25]], [[.3, 20], [.2, 23], [.24, 21]]. - What is max & min value of CO2 and Temperature? (considering both cities) - Display CO2 & Temp readings for day - 2? (both city)
    • Store brnd_sz_ar as [['Adidas', 'S'], ['Nike', 'L'], ['Adidas', 'M'], ['Nike', 'S']. Store prc_wrnty_ar as [[150, 1], [120, 0.6], [200, 1.2], [180, 0.9]]. Create array tshirt_info by combining feature from these.