reduction of multiclass classification to binary classification

Solutions on MaxInterview for reduction of multiclass classification to binary classification by the best coders in the world

showing results for - "reduction of multiclass classification to binary classification"
Leanna
31 Apr 2020
1# Reduction of Multiclass Classification to Binary Classification
2
3from pyspark.sql import Row
4from pyspark.ml.linalg import Vectors
5df = sc.parallelize([
6  Row(label=0.0, features=Vectors.dense(1.0, 0.8)),
7  Row(label=1.0, features=Vectors.sparse(2, [], [])),
8  Row(label=2.0, features=Vectors.dense(0.5, 0.5))]).toDF()
9lr = LogisticRegression(maxIter=5, regParam=0.01)
10ovr = OneVsRest(classifier=lr)
11model = ovr.fit(df)
12[x.coefficients for x in model.models]
13# [DenseVector([3.3925, 1.8785]), DenseVector([-4.3016, -6.3163]), DenseVector([-4.5855, 6.1785])
14[x.intercept for x in model.models]
15# [-3.64747..., 2.55078..., -1.10165...]
16test0 = sc.parallelize([Row(features=Vectors.dense(-1.0, 0.0))]).toDF()
17model.transform(test0).head().prediction
18# 1.0
19test1 = sc.parallelize([Row(features=Vectors.sparse(2, [0], [1.0]))]).toDF()
20model.transform(test1).head().prediction
21# 0.0
22test2 = sc.parallelize([Row(features=Vectors.dense(0.5, 0.4))]).toDF()
23model.transform(test2).head().prediction
24# 2.0