sklearn.preprocessing.Binarizer()是一種屬於預處理模塊的方法。它在離散連續特征值中起關鍵作用。
範例1:
一個8位灰度圖像的像素值的連續數據的值範圍在0(黑色)和255(白色)之間,並且需要它是黑白的。因此,使用Binarizer()
可以設置一個閾值,將像素值從0-127轉換為0和128-255轉換為1。
範例2:
一個機器記錄具有“Success Percentage”作為特征。這些值是連續的,範圍從10%到99%,但是研究人員隻是想使用此數據基於其他給定參數來預測機器的通過或失敗狀態。
用法:
sklearn.preprocessing.Binarizer(threshold, copy)
參數:
threshold:[float, optional] Values less than or equal to threshold is mapped to 0, else to 1. By default threshold value is 0.0.
copy :[boolean, optional] If set to False, it avoids a copy. By default it is True.
返回:
Binarized Feature values
下載數據集:
轉到鏈接並下載Data.csv
下麵是解釋sklearn的Python代碼.Binarizer()
# Python code explaining how
# to Binarize feature values
""" PART 1
Importing Libraries """
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Sklearn library
from sklearn import preprocessing
""" PART 2
Importing Data """
data_set = pd.read_csv(
'C:\\Users\\dell\\Desktop\\Data_for_Feature_Scaling.csv')
data_set.head()
# here Features - Age and Salary columns
# are taken using slicing
# to binarize values
age = data_set.iloc[:, 1].values
salary = data_set.iloc[:, 2].values
print ("\nOriginal age data values:\n", age)
print ("\nOriginal salary data values:\n", salary)
""" PART 4
Binarizing values """
from sklearn.preprocessing import Binarizer
x = age
x = x.reshape(1, -1)
y = salary
y = y.reshape(1, -1)
# For age, let threshold be 35
# For salary, let threshold be 61000
binarizer_1 = Binarizer(35)
binarizer_2 = Binarizer(61000)
# Transformed feature
print ("\nBinarized age:\n", binarizer_1.fit_transform(x))
print ("\nBinarized salary:\n", binarizer_2.fit_transform(y))
輸出:
Country Age Salary Purchased 0 France 44 72000 0 1 Spain 27 48000 1 2 Germany 30 54000 0 3 Spain 38 61000 0 4 Germany 40 1000 1 Original age data values: [44 27 30 38 40 35 78 48 50 37] Original salary data values: [72000 48000 54000 61000 1000 58000 52000 79000 83000 67000] Binarized age: [[1 0 0 1 1 0 1 1 1 1]] Binarized salary: [[1 0 0 0 0 0 0 1 1 1]]
相關用法
- Python pwd用法及代碼示例
- Python grp用法及代碼示例
- Python os.walk()用法及代碼示例
- Python pandas.map()用法及代碼示例
- Python spwd用法及代碼示例
- Python os.chdir()用法及代碼示例
- Python os.access()用法及代碼示例
- Python os.chflags()用法及代碼示例
注:本文由純淨天空篩選整理自Mohit Gupta_OMG 大神的英文原創作品 sklearn.Binarizer() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。