Git Product home page Git Product logo

dsc-oop-sklearn-lab's Introduction

OOP with Scikit-Learn - Lab

Introduction

Now that you have learned some of the basics of object-oriented programming with scikit-learn, let's practice applying it!

Objectives:

In this lesson you will practice:

  • Recall the distinction between mutable and immutable types
  • Define the four main inherited object types in scikit-learn
  • Instantiate scikit-learn transformers and models
  • Invoke scikit-learn methods
  • Access scikit-learn attributes

Mutable and Immutable Types

For each example below, think to yourself whether it is a mutable or immutable type. Then expand the details tag to reveal the answer.

  1. Python dictionary (click to reveal)

    Mutable. For example, the `update` method can be used to modify values within a dictionary.

  2. Python tuple (click to reveal)

    Immutable. If you want to create a modified version of a tuple, you need to use = to reassign it.

  3. pandas DataFrame (click to reveal)

    Mutable. Using the inplace=True argument with various different methods allows you to modify a dataframe in place.

  4. scikit-learn OneHotEncoder (click to reveal)

    Mutable. Calling the fit method causes the transformer to store information about the data that is passed in, modifying its internal attributes.

The Data

For this lab we'll use data from the built-in iris dataset:

from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True, as_frame=True)
X
y

Scikit-Learn Classes

For the following exercises, follow the documentation link to understand the class you are working with, but do not worry about understanding the underlying algorithm. The goal is just to get used to creating and using these types of objects.

Estimators

For all estimators, the steps are:

  1. Import the class from the sklearn library
  2. Instantiate an object from the class
  3. Pass in the appropriate data to the fit method

MinMaxScaler (documentation here)

Import this scaler, instantiate an object called scaler with default parameters, and fit the scaler on X.

# Import

# Instantiate

# Fit

DecisionTreeClassifier (documentation here)

Import the classifier, instantiate an object called clf (short for "classifier") with default parameters, and fit the classifier on X and y.

# Import

# Instantiate

# Fit

Transformers

One of the two objects instantiated above (scaler or clf) is a transformer. Which one is it? Consult the documentation.


Hint (click to reveal)

The class with a transform method is a transformer.


Using the transformer, print out two of the fitted attributes along with descriptions from the documentation.


Hint (click to reveal)

Attributes ending with _ are fitted attributes.

# Your code here

Now, call the transform method on the transformer and pass in X. Assign the result to X_scaled

# Your code here

Predictors and Models

The other of the two scikit-learn objects instantiated above (scaler or clf) is a predictor and a model. Which one is it? Consult the documentation.


Hint (click to reveal)

The class with a predict method and a score method is a predictor and a model.


Using the predictor, print out two of the fitted attributes along with descriptions from the documentation.

# Your code here

Now, call the predict method on the predictor, passing in X. Assign the result to y_pred

# Your code here

Now, call the score method on the predictor, passing in X and y

# Your code here

What does that score represent? Write your answer below

"""
Your answer here
"""

Summary

In this lab, you practiced identifying mutable and immutable types as well as identifying transformers, predictors, and models using scikit-learn. You also instantiated scikit-learn objects, invoked the most common scikit-learn methods, and accessed some scikit-learn attributes.

dsc-oop-sklearn-lab's People

Contributors

hoffm386 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.