Feature Importance — How’s and Why’s

In this article, we will be exploring various feature selection techniques that we need to be familiar with, in order to get the best performance out of your model.

  • SelectKBest
  • Linear Regression
  • Random Forest
  • XGBoost
  • Recursive Feature Elimination
  • Boruta

SelectKBest

SelectKbest is a method provided by sklearn to rank features of a dataset by their “importance ”with respect to the target variable. This “importance” is calculated using a score function which can be one of the following:

  • f_classif: ANOVA F-value between label/feature for classification tasks
  • f_regression: F-value between label/feature for regression tasks.
  • chi2: Chi-squared stats of non-negative features for classification tasks.
  • mutaul_info_classif: Mutual information for a discrete target.
  • SelectPercentile: Select features based on the percentile of the highest scores.
  • SelectFpr: Select features based on a false positive rate test.
  • SelectFdr: Select features based on an estimated false discovery rate.
  • SelectFwe: Select features based on the family-wise error rate.
  • GenericUnivariateSelect: Univariate feature selector with configurable mode.

All of the above-mentioned scoring functions are based on statistics. For instance, the f_regression function arranges the** p_values** of each of the variables in increasing order and picks the best K columns with the least p_value. Features with a p_value of less than** 0.05** are considered “significant” and only these features should be used in the predictive model.

#artificial-intelligence #big-data #data-science #machine-learning

What is GEEK

Buddha Community

Feature Importance — How’s and Why’s

Neural Network Feature Importance and Feature Effect with Simple Scientific Trick

Introduction

So you built your neural network, and, based on its holdout and/or out-of-time performance metrics, it’s looking pretty good. Now you need to “sell it” to your business partners, and for that, you need to be able to explain what is happening under the hood. A lot of modelers will skip that part and say “it’s a black box and it’s difficult to really know how the network does it. I can use eli5 and SHAP to get an idea, but it’s hard to explain how it does it.”

While it is true that there is a lot going on in neural networks (hundreds of weights and biases, multiplied by an activation function), it does not mean that we cannot come up with a business explanation of how our network works.

In this article, I am going to show you a simple trick that scientists use all the time to understand and explain the natural world around us, called “Ceteris Paribus”, which translates as “Other things held constant.” It’s the only way we can truly derive causation vs correlation. We will explore how to leverage Ceteris Paribus in Python to understand how our Neural Networks work.

Image for post

Building Our Neural Network: The Boston Dataset

I have already published an article on building a neural network for predicting house prices. You can find it here. For the purposes of this article, I am going to pick up right where I left off. The referenced article will provide you with all the details you need as background for the remainder of this story.

Feature Effect on Predictions: Ceteris Paribus

To truly understand how one feature affects our predictions, we need to hold all input values constant and only vary the feature that we want to study and understand. By measuring the outcome on our prediction, we can draw a clear relationship between input and prediction.

A good analogy for this is studying plant growth. If you want to really know what causes a plant to grow taller, greener, or produce more fruits, you need to isolate each individual growth factor, vary it, then measure the output and compare to the variation in input. This will give you a good idea of how that factor affects the desired outcome.

Image for post

Now, in our housing example, let’s examine the effect of “# of Bedrooms” against our target outcome, Median Home Value. Based on our correlation matrix and our sns.pairplot, # of bedrooms came out as highly correlated to our outcome, so it would be interesting to see how variations in the # of bedrooms, with everything else held constant (Ceteris Paribus), will affect house prices.

#partial-dependence-plots #feature-importance #feature-effect #machine-learning #neural-networks

WebEngage Flutter Plugin

WebEngage Flutter SDK

For more information checkout our website and documentation.

Installation

Add WebEngage Flutter Plugin

  • Add webengage_flutter in your pubspec.yaml file.
dependencies:
webengage_flutter: 1.0.3
  • Run flutter packages get to install the SDK

Initialization

Android

  1. Initialize WebEngage in main.dart in initState();
WebEngagePlugin _webEngagePlugin = new WebEngagePlugin();
  1. Initialize WebEngage Android SDK in your <your-project>/android/app/src/main/java/<your-package-path>/MainApplication.java class.
...
import com.webengage.sdk.android.WebEngageActivityLifeCycleCallbacks;
import io.flutter.app.FlutterApplication;

public class MainApplication extends FlutterApplication {
    @Override
    public void onCreate() {
        super.onCreate();
         WebEngageConfig webEngageConfig = new WebEngageConfig.Builder()
                .setWebEngageKey("YOUR_LICENCSE_CODE")
                .setAutoGCMRegistrationFlag(false)
                .setLocationTrackingStrategy(LocationTrackingStrategy.ACCURACY_BEST)
                .setDebugMode(true) // only in development mode
                .build();
        WebengageInitializer.initialize(this,webEngageConfig);
        ...
    }
    ...
}

Push Notifications

  1. Add below dependencies in app-level build gradle
    implementation platform('com.google.firebase:firebase-bom:25.12.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-messaging:20.2.1'
    implementation 'com.google.android.gms:play-services-ads:15.0.1'
  1. Add the following to your dependencies section in project/build.gradle
        classpath 'com.google.gms:google-services:4.3.4'
  1. Firebase tokens can be passed to WebEngage using FirebaseMessagingService
import com.google.firebase.messaging.FirebaseMessagingService;
import com.webengage.sdk.android.WebEngage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        WebEngage.get().setRegistrationID(s);
    }
}

It is also recommended that you pass Firebase token to WebEngage from onCreate of your Application class as shown below. This will ensure that changes in user’s Firebase token are communicated to WebEngage.

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.webengage.sdk.android.WebEngage;

public class MainApplication extends FlutterApplication {
    @Override
    public void onCreate() {
        super.onCreate();
    FirebaseMessaging.getInstance().getToken()
    .addOnCompleteListener(new OnCompleteListener<String>() {
        @Override
        public void onComplete(@NonNull Task<String> task) {
          if (!task.isSuccessful()) {
            Log.w(TAG, "Fetching FCM registration token failed", task.getException());
            return;
          }
          // Get new FCM registration token
          String token = task.getResult();
          WebEngage.get().setRegistrationID(token);
        }
    });
     
    }
}
  1. Pass Messages to WebEngage Create a class that extends FirebaseMessagingService and pass messages to WebEngage. All incoming messages from WebEngage will contain key source with the value as webengage.
package your.application.package;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.webengage.sdk.android.WebEngage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();
    if(data != null) {
      if(data.containsKey("source") && "webengage".equals(data.get("source"))) {
        WebEngage.get().receive(data);
      }
    }
  }
}

Next, register the service to the application element of your AndroidManifest.xml as follows.

<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

iOS

  1. Add WebEngage configurations <your-project>/ios/<YourApp>/Info.plist file.
<dict>
	<key>WEGLicenseCode</key>
	<string>YOUR-WEBENGAGE-LICENSE-CODE</string>

	<key>WEGLogLevel</key>
	<string>VERBOSE</string>
    ...
</dict>
  1. Initialize WebEngage iOS SDK in <your-project>/ios/<YourApp>/AppDelegate.m file.
#import <WebEngage/WebEngage.h>
...

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary * launchOptions {
    ...
  
    [[WebEngage sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

Push Notifications

Push Notification Callbacks

  1. Add Below code in AppDelegate.h file
  #import <WebEngagePlugin.h>
  
  @property (nonatomic, strong) WebEngagePlugin *bridge;
  1. Add Below code in AppDelegate.m file
    self.bridge = [WebEngagePlugin new];
    //For setting push click callback set pushNotificationDelegate after webengage SDK is initialised
    
    [[WebEngage sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions notificationDelegate:self.bridge];
    [WebEngage sharedInstance].pushNotificationDelegate = self.bridge;
  1. Add below subscribeToPushCallbacks() method in main.dart and call it from initMethod()
  void subscribeToPushCallbacks() {
      //Push click stream listener
      _webEngagePlugin.pushStream.listen((event) {
        String deepLink = event.deepLink;
        Map<String, dynamic> messagePayload = event.payload;
      });

      //Push action click listener
      _webEngagePlugin.pushActionStream.listen((event) {
        print("pushActionStream:" + event.toString());
        String deepLink = event.deepLink;
        Map<String, dynamic> messagePayload = event.payload;
      });
  }
  1. Add below code in dispose() of the main.dart
  //Close the streams in dispose()
  @override
  void dispose() {
    _webEngagePlugin.pushSink.close();
    _webEngagePlugin.pushActionSink.close();
    super.dispose();
  }

Universal Link

  1. Add Below code in AppDelegate.m file
  - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
  [[[WebEngage sharedInstance] deeplinkManager] getAndTrackDeeplink:userActivity.webpageURL callbackBlock:^(id location){
    [self.bridge trackDeeplinkURLCallback:location];
  }];
  return YES;
}
  1. Add below subscribeToTrackUniversalLink() method in main.dart and call it from initMethod()
 void subscribeToTrackUniversalLink() {
    _webEngagePlugin.trackDeeplinkStream.listen((location) {
      print("trackDeeplinkStream: " + location);
    });
  }
  1. Add below code in dispose() of the main.dart
  //Close the streams in dispose()
  @override
  void dispose() {
    _webEngagePlugin.trackDeeplinkURLStreamSink.close();
    super.dispose();
  }

Track Users

import 'package:webengage_flutter/webengage_flutter.dart';
...
    // User login
    WebEngagePlugin.userLogin('3254');

    // User logout
    WebEngagePlugin.userLogout();

    // Set user first name
    WebEngagePlugin.setUserFirstName('John');

    // Set user last name
    WebEngagePlugin.setUserLastName('Doe');

    // Set user email
    WebEngagePlugin.setUserEmail('john.doe@gmail.com');

    // Set user hashed email
    WebEngagePlugin.setUserHashedEmail('144e0424883546e07dcd727057fd3b62');

    // Set user phone number
    WebEngagePlugin.setUserPhone('+551155256325');

    // Set user hashed phone number
    WebEngagePlugin.setUserHashedPhone('e0ec043b3f9e198ec09041687e4d4e8d');

    // Set user company
    WebEngagePlugin.setUserCompany('WebEngage');

    // Set user birth-date, supported format: 'yyyy-MM-dd'
    WebEngagePlugin.setUserBirthDate('1994-05-24');

    // Set user gender, allowed values are ['male', 'female', 'other']
    WebEngagePlugin.setUserGender('male');

    // Set user channel opt-in status
    WebEngagePlugin.setUserOptIn('in_app', false);

    // Set user location
    WebEngagePlugin.setUserLocation(19.25, 72.45);

    // Set User Attribute with  String value
    WebEngagePlugin.setUserAttribute("twitterusename", "saurav12994");

    // Set User Attribute with  Boolean value
    WebEngagePlugin.setUserAttribute("Subscribed to email", true);

    // Set User Attribute with  Integer value
    WebEngagePlugin.setUserAttribute("Points earned", 2626);

    // Set User Attribute with  Double value
    WebEngagePlugin.setUserAttribute("Dollar Spent", 123.44);

    // Set User Attribute with  Map value
    var details = {'Usrname':'tom','Passiword':'pass@123'};
    WebEngagePlugin.setUserAttributes(details);

Track Events

import 'package:webengage_flutter/webengage_flutter.dart';
...
    // Track simple event
      WebEngagePlugin.trackEvent('Added to Cart');

      // Track event with attributes
      WebEngagePlugin.trackEvent('Order Placed', {'Amount': 808.48});

In-app Notifications

Track Screens

import 'package:webengage_flutter/webengage_flutter.dart';
...
    // Track screen
    WebEngagePlugin.trackScreen('Home Page');

    // Track screen with data
    WebEngagePlugin.trackScreen('Product Page', {'Product Id': 'UHUH799'});

In-app Notification Callbacks

  1. Add Below code in AppDelegate.h file
  #import <WebEngagePlugin.h>
  
  @property (nonatomic, strong) WebEngagePlugin *bridge;
  1. Add Below code in AppDelegate.m file
    self.bridge = [WebEngagePlugin new];
    //For setting in-app click callback set notificationDelegate while initialising WebEngage SDK
    
    [[WebEngage sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions notificationDelegate:self.bridge];
  1. Add Below Method in main.dart
 void _onInAppPrepared(Map<String, dynamic> message) {
    print("This is a inapp Prepated callback from native to flutter. Payload " +
        message.toString());
  }
  void _onInAppClick(Map<String, dynamic> message,String s) {
    print("This is a inapp click callback from native to flutter. Payload " +
        message.toString());

  }

  void _onInAppShown(Map<String, dynamic> message) {
    print("This is a callback on inapp shown from native to flutter. Payload " +
        message.toString());
  }

  void _onInAppDismiss(Map<String, dynamic> message) {
    print("This is a callback on inapp dismiss from native to flutter. Payload " +
        message.toString());
  }
  1. Add Below code inside initmethod() in main.dart
_webEngagePlugin.setUpInAppCallbacks(
        _onInAppClick, _onInAppShown, _onInAppDismiss, _onInAppPrepared);

More Info

Questions?

Reach out to our Support Team for further assistance.

Plugin info

WebEngage Flutter SDK

Use this package as a library

Depend on it

Run this command:

With Flutter:

 $ flutter pub add webengage_flutter

This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):


dependencies:
  webengage_flutter: ^1.0.3

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:webengage_flutter/webengage_flutter.dart';

#fluter  #dart #mobile-apps

Jack Salvator

Jack Salvator

1608113009

New Angular 7 Features With Example - Info Stans

What is new in New Angular 7? New Angular 7 features have turned out as a powerful release that really brought advancement in the application development structure.

Here, we have listed new Angular 7 features with examples and write the difference between Angular 6 and Angular 7.

  • Bundle Budget
  • Virtual Scrolling
  • Error Handling
  • Documentation Updates
  • Application Performance
  • Native Script
  • CLI Prompts
  • Component in angular 7
  • Drag and Drop
  • Angular Do-Bootstrap

Read more: Angular 7 Features With Example

#angular 7 features #what’s new angular 7 #new angular 7 features #angular 7 features with examples

August  Larson

August Larson

1625009220

How Imports Work in Python

And a bit about packages

The Python import system is pretty straightforward… to a point. Importing code present in the same directory you’re working in is very different from importing between multiple files present in multiple directories. Through this post, I try to analyse the different import scenarios that one might encounter, hopefully making it easier to create your own packages.

Table of Contents

An Example
What Happens When You Import a Python File?
Terminology
Import Scenarios
Analysis
Building a Package
The Syntax of Your Import Statement
Notes and Resources

#machine-learning #python #software-development #how imports work in python #imports work #imports

Art  Lind

Art Lind

1602939600

Feature Selection in Python

We will provide a walk-through example of how you can choose the most important features. For this example, we will work with a classification problem but can be extended to regression cases too by adjusting the parameters of the function.

We will work with the breast-cancer dataset. Let’s start:

import pandas as pd
import numpy as np
from scipy import stats
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectFpr, chi2, SelectKBest, SelectFwe, f_classif, SelectFdr
import matplotlib.pyplot as plt
%matplotlib inline
## https://www.kaggle.com/uciml/breast-cancer-wisconsin-data?select=data.csv
df = pd.read_csv("data.csv")
## replace M with 1 and B with 0
my_map = {
          'M':1,
          'B' :0
         }
df['diagnosis'] = df['diagnosis'].map(my_map)
## remove the id column
df.drop(['id'], axis=1, inplace=True)
df

Image for post

#scikit-learn #feature-importance #feature-selection #machine-learning #python