{"id":20742,"date":"2021-11-12T19:24:50","date_gmt":"2021-11-12T13:54:50","guid":{"rendered":"http:\/\/ismiletechnologies.com\/?p=20742"},"modified":"2022-12-09T00:28:32","modified_gmt":"2022-12-08T18:58:32","slug":"predictors-and-featureunion-in-ml","status":"publish","type":"post","link":"https:\/\/ismiletechnologies.com\/en_us\/machine-learning\/predictors-and-featureunion-in-ml\/","title":{"rendered":"Predictors and FeatureUnion in ML"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"20742\" class=\"elementor elementor-20742\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-5275174 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"5275174\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-a342213\" data-id=\"a342213\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-07f9f2c elementor-widget elementor-widget-text-editor\" data-id=\"07f9f2c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p><span class=\"TextRun Highlight SCXW132119565 BCX0\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW132119565 BCX0\">Scikit-learn refers to <a href=\"http:\/\/ismiletechnologies.com\/ai-and-machine-learning\/\"><span style=\"color: #333399;\">machine learning<\/span><\/a> algorithms as estimators. There are three different types of estimators: classifiers, regressors, and transformers. The inheritance of the second class determines what kind of estimator the model represents. We&#8217;ll divide the estimators into two groups based on their interface. These two groups are predictors and transformers, and in this blog, we&#8217;ll discuss Predictors.\u00a0<\/span><\/span><span class=\"EOP SCXW132119565 BCX0\" data-ccp-props=\"{\">\u00a0<\/span><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2da82fa elementor-widget elementor-widget-heading\" data-id=\"2da82fa\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Predictors: classifiers and regressors  <\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-88b7485 elementor-widget elementor-widget-text-editor\" data-id=\"88b7485\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p><span class=\"TextRun Highlight SCXW3502114 BCX0\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW3502114 BCX0\">As the name suggests, predictors are models that make predictions. There are two main methods: Fit (X, y): trains\/fit the object to the feature matrix X and label vector y and Predict (X): makes predictions on the passed data set X.<\/span><\/span><\/p>\n<p><span class=\"EOP SCXW3502114 BCX0\" style=\"color: #000000;\" data-ccp-props=\"{\">Code :<\/span><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f74dcbe elementor-widget elementor-widget-code-highlight\" data-id=\"f74dcbe\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-javascript \">\n\t\t\t\t<code readonly=\"true\" class=\"language-javascript\">\n\t\t\t\t\t<xmp>from sklearn.linear_model import LinearRegression \r\n\r\n# create model and train\/fit\r\nmodel = LinearRegression() \r\nmodel.fit(X, y) \r\n\r\n# predict label values on X \r\ny_pred = model.predict(X) \r\nprint(y_pred) \r\nprint(\"shape of the  prediction array: {}\".format(y_pred.shape)) \r\nprint(\"shape of the training set: {}\".format(X.shape)) <\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-da573e6 elementor-widget elementor-widget-text-editor\" data-id=\"da573e6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p><span class=\"TextRun Highlight SCXW172087286 BCX0\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW172087286 BCX0\">There is one thing to note that the output of predict(X) is a NumPy array of one dimension.<\/span><\/span><span class=\"EOP SCXW172087286 BCX0\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559737&quot;:440,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-42a26ae elementor-widget elementor-widget-heading\" data-id=\"42a26ae\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Pipelines  <\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5d5ce85 elementor-widget elementor-widget-text-editor\" data-id=\"5d5ce85\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p><span data-contrast=\"none\">Before discussing feature unions, let\u2019s first have a brief knowledge about Pipelines. As our <a href=\"http:\/\/ismiletechnologies.com\/data-and-analytics\/\"><span style=\"color: #333399;\">analysis<\/span><\/a> and workflow become more complicated, we need a tool that helps with scaling up. For example, suppose you need to apply multiple transformations to your data before using a supervised machine learning model. In that case, you can apply the transformations explicitly, creating intermediate variables of the transformed data. But this work is a little bit tiresome.<\/span><span data-ccp-props=\"{\">\u00a0<\/span><\/p><p><span data-contrast=\"none\">Here, the Pipelines approach helps prevent keeping track of intermediate transformations and scales up our code for more complicated analysis. Essentially, a pipeline is an estimator object that holds a series of transformers with a final estimator.<\/span><span data-ccp-props=\"{\">\u00a0<\/span><\/p><p><span style=\"color: #000000;\">Code Example:\u00a0<\/span><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b17f155 elementor-widget elementor-widget-code-highlight\" data-id=\"b17f155\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-javascript \">\n\t\t\t\t<code readonly=\"true\" class=\"language-javascript\">\n\t\t\t\t\t<xmp>from sklearn.pipeline import Pipeline \r\nfrom sklearn.preprocessing import PolynomialFeatures \r\n\r\n# construct pipeline \r\nscaler = StandardScaler() \r\npoly_features = PolynomialFeatures(degree=2) \r\nlin_reg = LinearRegression() \r\n\r\npipe = Pipeline([ \r\n  ('scaler', scaler), \r\n  ('poly', poly_features), \r\n  ('regressor', lin_reg) \r\n]) <\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-104cfa9 elementor-widget elementor-widget-heading\" data-id=\"104cfa9\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Feature Union <\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1b6f19e elementor-widget elementor-widget-text-editor\" data-id=\"1b6f19e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p><span class=\"TextRun Highlight SCXW244844383 BCX0\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW244844383 BCX0\">A FeatureUnion is another tool for dealing with situations where your data requires different transformation processes for various features. Because in ColumnTransformer, processes feature separately and combine the results into a single feature matrix, but in feature union, it can handle more complex workflows where you need to use distinct transformers\u00a0<\/span><span class=\"NormalTextRun SCXW244844383 BCX0\">and\u00a0<\/span><span class=\"NormalTextRun SCXW244844383 BCX0\">estimators together before passing the complete feature matrix to a final estimator.<\/span><\/span><span class=\"EOP SCXW244844383 BCX0\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:220,&quot;335559740&quot;:342}\">\u00a0<\/span><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c01f362 elementor-widget elementor-widget-image\" data-id=\"c01f362\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"602\" height=\"222\" src=\"https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/11\/image-18.png\" class=\"attachment-full size-full wp-image-20744\" alt=\"\" srcset=\"https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/11\/image-18.png 602w, https:\/\/ismiletechnologies.com\/wp-content\/uploads\/2021\/11\/image-18-300x111.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-74fa711 elementor-widget elementor-widget-text-editor\" data-id=\"74fa711\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p><span data-contrast=\"none\">We already know that Pipeline objects arrange estimators in a series, but FeatureUnion objects arrange transformers in parallel. A FeatureUnion object combines the output of each of the transformers in parallel to generate one output matrix. Using a combination of Pipeline and FeatureUnion objects, we can construct complicated machine learning workflows within a single scikit-learn estimator object. So, the ultimate aim of feature union is to combine several feature extraction mechanisms into a single transformer.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:220,&quot;335559740&quot;:342}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.pipeline.FeatureUnion.html\"><span data-contrast=\"none\"><span style=\"color: #333399;\">Code:<\/span><\/span><\/a><span style=\"color: #333399;\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559738&quot;:220,&quot;335559740&quot;:342}\">\u00a0<\/span><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5ec1a26 elementor-widget elementor-widget-code-highlight\" data-id=\"5ec1a26\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-javascript \">\n\t\t\t\t<code readonly=\"true\" class=\"language-javascript\">\n\t\t\t\t\t<xmp>from sklearn.pipeline import FeatureUnion \r\nfrom sklearn.decomposition import PCA, TruncatedSVD \r\n\r\nunion = FeatureUnion([(\"pca\", PCA(n_components=1)), \r\n           (\"svd\", TruncatedSVD(n_components=2))]) \r\n\r\nX = [[0., 1., 3], [2., 2., 5]] \r\nunion.fit_transform(X) \r\narray([[ 1.5   ,  3.0...,  0.8...], \r\n      [-1.5    ,  5.7..., -0.4...]]) <\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Scikit-learn refers to machine learning algorithms as estimators. There are three different types of estimators: classifiers, regressors, and transformers. The inheritance of the second class determines what kind of estimator the model represents. We&#8217;ll divide the estimators into two groups based on their interface. These two groups are predictors and transformers, and in this blog, [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":20953,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-20742","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning"],"_links":{"self":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/20742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/comments?post=20742"}],"version-history":[{"count":15,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/20742\/revisions"}],"predecessor-version":[{"id":36034,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/posts\/20742\/revisions\/36034"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/media\/20953"}],"wp:attachment":[{"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/media?parent=20742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/categories?post=20742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ismiletechnologies.com\/en_us\/wp-json\/wp\/v2\/tags?post=20742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}