// API callback
showlatestposts({"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$blogger":"http://schemas.google.com/blogger/2008","xmlns$georss":"http://www.georss.org/georss","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$thr":"http://purl.org/syndication/thread/1.0","id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684"},"updated":{"$t":"2021-09-16T02:37:02.390-07:00"},"category":[{"term":"R"},{"term":"python"},{"term":"Classification"},{"term":"regression"},{"term":"keras"},{"term":"analytics"},{"term":"DeepLearning"},{"term":"anomaly detection"},{"term":"boosting"},{"term":"clustering"},{"term":"autoencoder"},{"term":"svm"},{"term":"CNN"},{"term":"LSTM"},{"term":"RNN"},{"term":"accuracy"},{"term":"c#"},{"term":"decision-tree"},{"term":"enseble learning"},{"term":"regularization"},{"term":"sentiment analysis"},{"term":"curve-fitting"},{"term":"time-series"},{"term":"ML.NET"},{"term":"PCA"},{"term":"csharp"},{"term":"feature-selection"},{"term":"knn"},{"term":"naivebayes"},{"term":"projection"},{"term":"pyspark"},{"term":"svr"},{"term":"xgboost"},{"term":"GBM"},{"term":"Shiny"},{"term":"SimpleRNN"},{"term":"cross-validation"},{"term":"elasticsearch"},{"term":"k-means"},{"term":"lasso"},{"term":"machine learning"},{"term":"mllib"},{"term":"models"},{"term":"multioutput"},{"term":"plotting"},{"term":"randomforest"},{"term":"sgd"},{"term":"statistics"},{"term":"yearinreview"},{"term":"BatchNormalization"},{"term":"Data Science"},{"term":"Dropout"},{"term":"GAM"},{"term":"MixedReality"},{"term":"ROC-Curve"},{"term":"adaboost"},{"term":"augmentation"},{"term":"bagging"},{"term":"blockchain"},{"term":"book"},{"term":"dbscan"},{"term":"elasticnet"},{"term":"gradient boosting"},{"term":"gradient descent"},{"term":"hololens"},{"term":"k-neighbors"},{"term":"lars"},{"term":"linux"},{"term":"mxnet"},{"term":"nlp"},{"term":"nltk"},{"term":"ocr"},{"term":"one-hot encoding"},{"term":"optics"},{"term":"polynomial"},{"term":"stacking"},{"term":"t-test"},{"term":"transform"},{"term":"tsne"},{"term":"vae"}],"title":{"type":"text","$t":"DataTechNotes"},"subtitle":{"type":"html","$t":"A blog about data science and machine learning"},"link":[{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/posts\/default"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default?alt=json-in-script\u0026orderby=published"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/"},{"rel":"hub","href":"http://pubsubhubbub.appspot.com/"},{"rel":"next","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default?alt=json-in-script\u0026start-index=26\u0026max-results=25\u0026orderby=published"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"generator":{"version":"7.00","uri":"http://www.blogger.com","$t":"Blogger"},"openSearch$totalResults":{"$t":"205"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"25"},"entry":[{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-4882983085976161759"},"published":{"$t":"2021-08-02T22:39:00.007-07:00"},"updated":{"$t":"2021-08-05T13:12:08.139-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"python"},{"scheme":"http://www.blogger.com/atom/ns#","term":"transform"}],"title":{"type":"text","$t":"Fourier Transform Example with SciPy Functions"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EA Fourier transform is a method to decompose signal data in a frequency components. By using this function, we can transform a time domain signal into the frequency domain one and a vice versa. It is widely used in signal processing and many other applications.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EDiscrete Fourier Transform (DFT) is an algorithm to transform a discrete (finite-duration) signal data\u003C\/span\u003E. Fast Fourier Transform (FFT) is an efficient algorithm that implements DFT.\u0026nbsp;\u003C\/span\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; SciPy API provides several functions to implement Fourier transform.\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn  this tutorial, we'll briefly learn how to transform and inverse transform a signal data by SciPy API functions. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe  tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ETransform with fft()\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ETransform with rfft()\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EInverse transform \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries for this tutorial.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enp\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.preprocessing\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E normalize\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Escipy.fft\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E fft, fftfreq\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Escipy.fft\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E rfft, rfftfreq\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Escipy.fft\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E irfft\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; First, we'll generate sample data for this tutorial. Here, we define frequency, duration, and sampling rate to generate sequence data.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Efrequency \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cbr \/\u003Eduration \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cbr \/\u003Esampling_rate\u003Cspan style=\"color: #666666;\"\u003E = \u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2000\u003C\/span\u003E\u003Cbr \/\u003EN \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sampling_rate \u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E duration\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003ENext, we'll generate x and y data by using above definitions and visualize it in a plot. We can add some noise or the data with different frequency into the y.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elinspace(\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, duration, N, endpoint \u003Cspan style=\"color: #666666;\"\u003E= \u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u003Cbr \/\u003Efrequencies \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E x \u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E frequency\u003Cbr \/\u003E\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esin((\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epi) \u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E x \u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E frequency)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Enoise \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Erandom\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Enormal(\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, size\u003Cspan style=\"color: #666666;\"\u003E = \u003C\/span\u003E\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(x))\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y \u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003E noise \u003Cspan style=\"color: #666666;\"\u003E* \u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.1\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, y)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-5p078s9RHIc\/YQidQqXu9dI\/AAAAAAAABIA\/G2lzlA6AdGoc61yoO3mwbhD0QwpRmH7tACLcBGAsYHQ\/s380\/noise_signal.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"248\" data-original-width=\"380\" height=\"261\" src=\"https:\/\/1.bp.blogspot.com\/-5p078s9RHIc\/YQidQqXu9dI\/AAAAAAAABIA\/G2lzlA6AdGoc61yoO3mwbhD0QwpRmH7tACLcBGAsYHQ\/w400-h261\/noise_signal.png\" width=\"400\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn a next step we'll normalize y data. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Enormalized_y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E normalize(y[:,np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Enewaxis], axis\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eravel()\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, normalized_y)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;;\"\u003E\u003Cb\u003ETransform with fft()\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; To transform prepared data, we use fft() and freqfft() function of SciPy API. The fft() function returns \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Ediscrete Fourier transform of real or complex sequence and the fftfreq() returns t\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Ehe discrete Fourier transform sample frequencies. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Effty \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E fft(normalized_y)\u003Cbr \/\u003Efftx \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E fftfreq(N, \u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E sampling_rate)\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(fftx, np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eabs(ffty))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003C\/p\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-XaSfhKK6tS0\/YQifFN4uBVI\/AAAAAAAABII\/CPGXkM1xavgxuJL9ThRRMOskI44DNrRmwCLcBGAsYHQ\/s368\/fft_output.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"248\" data-original-width=\"368\" height=\"270\" src=\"https:\/\/1.bp.blogspot.com\/-XaSfhKK6tS0\/YQifFN4uBVI\/AAAAAAAABII\/CPGXkM1xavgxuJL9ThRRMOskI44DNrRmwCLcBGAsYHQ\/w400-h270\/fft_output.png\" width=\"400\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cp\u003E\u003C\/p\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003ETransform with rfft()\u003C\/b\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EThe rfft() function transforms real sequence sample data and it runs faster than fft(). \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Erffty \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfft(normalized_y)\u003Cbr \/\u003Erfftx \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfftfreq(N, \u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E sampling_rate)\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(rfftx, np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eabs(rffty))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cp\u003E\u003C\/p\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-aHHEk6tR60k\/YQjR3uZ342I\/AAAAAAAABIQ\/dRMLGhMu5UMHt9ib6P2tVNxFvsiSqx3iwCLcBGAsYHQ\/s368\/rfft_output.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"248\" data-original-width=\"368\" height=\"270\" src=\"https:\/\/1.bp.blogspot.com\/-aHHEk6tR60k\/YQjR3uZ342I\/AAAAAAAABIQ\/dRMLGhMu5UMHt9ib6P2tVNxFvsiSqx3iwCLcBGAsYHQ\/w400-h270\/rfft_output.png\" width=\"400\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cp\u003E\u0026nbsp;\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EAs you've noticed, the negative part of the output data will be eliminated in this transform.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cp\u003E\u0026nbsp;\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003EInverse FFT\u003C\/b\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EWe  can transform back from the fft() output data by using irfft() function. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Einvers_y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E irfft(rffty)\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(invers_y)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-KiNY6OOL-aA\/YQjTPIWmRgI\/AAAAAAAABIY\/-dftcP8BT1k63SoyD7IRHfQNTkWQdOvrACLcBGAsYHQ\/s386\/inverse_output.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"248\" data-original-width=\"386\" height=\"258\" src=\"https:\/\/1.bp.blogspot.com\/-KiNY6OOL-aA\/YQjTPIWmRgI\/AAAAAAAABIY\/-dftcP8BT1k63SoyD7IRHfQNTkWQdOvrACLcBGAsYHQ\/w400-h258\/inverse_output.png\" width=\"400\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  In this tutorial, we've briefly learned how to implement Fourier transform for a given signal data and by using SciPy function. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe full  source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enp\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.preprocessing\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E normalize\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Escipy.fft\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E fft, fftfreq\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Escipy.fft\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E rfft, rfftfreq\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Escipy.fft\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E irfft\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Efrequency \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cbr \/\u003Eduration \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cbr \/\u003Esampling_rate\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2000\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EN \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sampling_rate \u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E duration\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elinspace(\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, duration, N, endpoint\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esin((\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epi) \u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E x \u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E frequency)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, y)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003Enoise \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Erandom\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Enormal(\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, size\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(x))\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y \u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003E noise\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.1\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, y)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003Enormalized_y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E normalize(y[:,np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Enewaxis], axis\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eravel()\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, normalized_y)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003Effty \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E fft(normalized_y)\u003Cbr \/\u003Efftx \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E fftfreq(N, \u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E sampling_rate)\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(fftx, np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eabs(ffty))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003Erffty \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfft(normalized_y)\u003Cbr \/\u003Erfftx \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfftfreq(N, \u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E sampling_rate)\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(rfftx, np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eabs(rffty))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003Einvers_y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E irfft(rffty)\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(invers_y)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Ca href=\"https:\/\/docs.scipy.org\/doc\/scipy\/reference\/tutorial\/fft.html\" target=\"_blank\"\u003ESciPy Fourier Transforms\u003C\/a\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/4882983085976161759\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/08\/fourier-transform-example-with-scipy.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/4882983085976161759"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/4882983085976161759"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/08\/fourier-transform-example-with-scipy.html","title":"Fourier Transform Example with SciPy Functions"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-5p078s9RHIc\/YQidQqXu9dI\/AAAAAAAABIA\/G2lzlA6AdGoc61yoO3mwbhD0QwpRmH7tACLcBGAsYHQ\/s72-w400-h261-c\/noise_signal.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-2092020704539637336"},"published":{"$t":"2021-06-29T02:06:00.003-07:00"},"updated":{"$t":"2021-06-29T02:16:16.437-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Classification"},{"scheme":"http://www.blogger.com/atom/ns#","term":"pyspark"}],"title":{"type":"text","$t":"PySpark Decision Tree Classification Example"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003C\/span\u003E \u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003Cspan style=\"font-family: inherit;\"\u003E PySpark MLlib library provides a DecisionTreeClassifier model to implement classification with decision tree method. \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003EA  decision tree method is one of the well known and powerful supervised machine  learning algorithms that can be used for classification and regression  tasks. It is a tree-like, top-down flow learning method to extract rules  from the training data. The branches of the tree are based on certain  decision outcomes. \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn  this tutorial, we'll briefly learn how to fit and classify data by using PySpark DecisionTreeClassifier. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe  tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPrediction and accuracy check\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries for this tutorial.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SparkContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.sql\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SQLContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.classification\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E DecisionTreeClassifier\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.evaluation\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E MulticlassClassificationEvaluator\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.feature\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E VectorAssembler\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E confusion_matrix\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We use Iris dataset to perform classification and it can be easily loaded from the Scikit-learn dataset module. Below code explains how to load  dataset and transform it into the pandas data frame type.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Edf_iris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame(iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, columns\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eiris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003Edf_iris[\u003Cspan style=\"color: #4070a0;\"\u003E'label'\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ESeries(iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(df_iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ehead())\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E   sepal length (cm)  sepal width (cm)  ...  petal width (cm)  label\u003Cbr \/\u003E0                5.1               3.5  ...               0.2      0\u003Cbr \/\u003E1                4.9               3.0  ...               0.2      0\u003Cbr \/\u003E2                4.7               3.2  ...               0.2      0\u003Cbr \/\u003E3                4.6               3.1  ...               0.2      0\u003Cbr \/\u003E4                5.0               3.6  ...               0.2      0\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003ENext, we'll define SqlConext and create data frame by using df_iris data.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Esc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparkContext()\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EgetOrCreate()\u003Cbr \/\u003EsqlContext \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SQLContext(sc)\u003Cbr \/\u003E\u003Cbr \/\u003Edata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sqlContext\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EcreateDataFrame(df_iris)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EprintSchema())\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003Eroot\u003Cbr \/\u003E |-- sepal length (cm): double (nullable = true)\u003Cbr \/\u003E |-- sepal width (cm): double (nullable = true)\u003Cbr \/\u003E |-- petal length (cm): double (nullable = true)\u003Cbr \/\u003E |-- petal width (cm): double (nullable = true)\u003Cbr \/\u003E |-- label: long (nullable = true)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ETo combine all feature data and separate 'label' data in a dataset, we use VectorAssembler. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names\u003Cbr \/\u003E\u003Cbr \/\u003Eva \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E VectorAssembler(inputCols \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E features, outputCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(data)\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect([\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E, \u003Cspan style=\"color: #4070a0;\"\u003E'label'\u003C\/span\u003E])\u003Cbr \/\u003Eva_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E+-----------------+-----+\u003Cbr \/\u003E|         features|label|\u003Cbr \/\u003E+-----------------+-----+\u003Cbr \/\u003E|[5.1,3.5,1.4,0.2]|    0|\u003Cbr \/\u003E|[4.9,3.0,1.4,0.2]|    0|\u003Cbr \/\u003E|[4.7,3.2,1.3,0.2]|    0|\u003Cbr \/\u003E+-----------------+-----+\u003Cbr \/\u003Eonly showing top 3 rows\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll split data into the train and test parts.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E(train, test) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ErandomSplit([\u003Cspan style=\"color: #40a070;\"\u003E0.8\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E0.2\u003C\/span\u003E])\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003EPrediction and Accuracy Check\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; Next, we'll define the decision tree classifier model by using the DecisionTreeClassifier\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Eclass and fit model on train data. We can predict test data by using trasnform() method.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Edtc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E DecisionTreeClassifier(featuresCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"features\"\u003C\/span\u003E, labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"label\"\u003C\/span\u003E)\u003Cbr \/\u003Edtc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(train)\u003Cbr \/\u003E\u003Cbr \/\u003Epred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(test)\u003Cbr \/\u003Epred\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E+-----------------+-----+--------------+-------------+----------+\u003Cbr \/\u003E|         features|label| rawPrediction|  probability|prediction|\u003Cbr \/\u003E+-----------------+-----+--------------+-------------+----------+\u003Cbr \/\u003E|[4.4,2.9,1.4,0.2]|    0|[35.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|\u003Cbr \/\u003E|[4.6,3.1,1.5,0.2]|    0|[35.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|\u003Cbr \/\u003E|[5.0,3.6,1.4,0.2]|    0|[35.0,0.0,0.0]|[1.0,0.0,0.0]|       0.0|\u003Cbr \/\u003E+-----------------+-----+--------------+-------------+----------+\u003Cbr \/\u003Eonly showing top 3 rows\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EAfter training the model, we'll predict test data and check the accuracy metrics. Here, we can use MulticlassClassificationEvaluator to check the accuracy. Confusion matrix can be created by using confusion_matrix function of sklearn.metrics module.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eevaluator\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003EMulticlassClassificationEvaluator(predictionCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E)\u003Cbr \/\u003Eacc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E evaluator\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eevaluate(pred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Prediction Accuracy: \"\u003C\/span\u003E, acc)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ey_pred\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Epred\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003Ey_orig\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Epred\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"label\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003E\u003Cbr \/\u003Ecm \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E confusion_matrix(y_orig, y_pred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Confusion Matrix:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(cm)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EPrediction Accuracy:  1.0\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EConfusion Matrix:\u003Cbr \/\u003E[[13  0  0]\u003Cbr \/\u003E [ 0  5  0]\u003Cbr \/\u003E [ 0  0 10]]\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u0026nbsp;\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EFinally, we'll stop spark context session.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# Stop session \u003C\/span\u003E\u003Cbr \/\u003Esc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Estop()\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E  \u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  In this tutorial, we've briefly learned how to fit and classify data by using PySpark DecisionTreeClassifier class. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe full  source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SparkContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.sql\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SQLContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.classification\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E DecisionTreeClassifier\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.evaluation\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E MulticlassClassificationEvaluator\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.feature\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E VectorAssembler\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E confusion_matrix\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Edf_iris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame(iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, columns\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eiris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003Edf_iris[\u003Cspan style=\"color: #4070a0;\"\u003E'label'\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ESeries(iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(df_iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ehead())\u003Cbr \/\u003E\u003Cbr \/\u003Esc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparkContext()\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EgetOrCreate()\u003Cbr \/\u003EsqlContext \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SQLContext(sc)\u003Cbr \/\u003E\u003Cbr \/\u003Edata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sqlContext\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EcreateDataFrame(df_iris)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EprintSchema())\u003Cbr \/\u003E\u003Cbr \/\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names\u003Cbr \/\u003E\u003Cbr \/\u003Eva \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E VectorAssembler(inputCols \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E features, outputCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(data)\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect([\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E, \u003Cspan style=\"color: #4070a0;\"\u003E'label'\u003C\/span\u003E])\u003Cbr \/\u003Eva_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003E(train, test) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ErandomSplit([\u003Cspan style=\"color: #40a070;\"\u003E0.8\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E0.2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cbr \/\u003Edtc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E DecisionTreeClassifier(featuresCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"features\"\u003C\/span\u003E, labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"label\"\u003C\/span\u003E)\u003Cbr \/\u003Edtc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(train)\u003Cbr \/\u003E\u003Cbr \/\u003Epred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(test)\u003Cbr \/\u003Epred\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eevaluator\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003EMulticlassClassificationEvaluator(predictionCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E)\u003Cbr \/\u003Eacc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E evaluator\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eevaluate(pred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Prediction Accuracy: \"\u003C\/span\u003E, acc)\u003Cbr \/\u003E\u003Cbr \/\u003Ey_pred\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Epred\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003Ey_orig\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Epred\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"label\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003E\u003Cbr \/\u003Ecm \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E confusion_matrix(y_orig, y_pred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Confusion Matrix:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(cm)\u003Cbr \/\u003E\u003Cbr \/\u003Esc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Estop()\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/spark.apache.org\/docs\/latest\/api\/python\/reference\/api\/pyspark.ml.classification.DecisionTreeClassifier.html\" target=\"_blank\"\u003EPySpark Decision tree classifier\u003C\/a\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/2092020704539637336\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/06\/pyspark-decision-tree-classification.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/2092020704539637336"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/2092020704539637336"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/06\/pyspark-decision-tree-classification.html","title":"PySpark Decision Tree Classification Example"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-2982029382986474414"},"published":{"$t":"2021-05-27T01:08:00.005-07:00"},"updated":{"$t":"2021-06-29T02:16:00.145-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"mllib"},{"scheme":"http://www.blogger.com/atom/ns#","term":"pyspark"}],"title":{"type":"text","$t":"MLlib Gradient-boosted Tree Regression Example with PySpark"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003C\/span\u003E \u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; PySpark MLlib library provides a GBTRegressor model to implement \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Egradient-boosted tree regression method. \u003C\/span\u003E\u003C\/span\u003EGradient tree boosting is an ensemble of decision trees model to solve regression and classification tasks in machine learning. Improving the weak learners by different set of train data is the main concept of this model.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn  this tutorial, we'll briefly learn how to fit and predict regression  data by using PySpark GBTRegressor in Python. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe  tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPrediction and accuracy check\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EVisualizing the results \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries for this tutorial.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SparkContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.sql\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SQLContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.feature\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E VectorAssembler\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.regression\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E GBTRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.evaluation\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E RegressionEvaluator\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We use Boston Housing Price dataset as a target regression data and we can easily load it from sclearn.datasets module. Below code shows how to load dataset and transform it into the pandas data frame type.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Edf_boston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata,columns\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eboston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003Edf_boston[\u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ESeries(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget)\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003C\/span\u003Edf_boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ehead())\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003ENext, we'll define SqlConext and create data frame by using df_boston data.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Esc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparkContext()\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EgetOrCreate()\u003Cbr \/\u003EsqlContext \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SQLContext(sc)\u003Cbr \/\u003E\u003Cbr \/\u003Edata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sqlContext\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EcreateDataFrame(df_boston)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EprintSchema())\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003Eroot\u003Cbr \/\u003E |-- CRIM: double (nullable = true)\u003Cbr \/\u003E |-- ZN: double (nullable = true)\u003Cbr \/\u003E |-- INDUS: double (nullable = true)\u003Cbr \/\u003E |-- CHAS: double (nullable = true)\u003Cbr \/\u003E |-- NOX: double (nullable = true)\u003Cbr \/\u003E |-- RM: double (nullable = true)\u003Cbr \/\u003E |-- AGE: double (nullable = true)\u003Cbr \/\u003E |-- DIS: double (nullable = true)\u003Cbr \/\u003E |-- RAD: double (nullable = true)\u003Cbr \/\u003E |-- TAX: double (nullable = true)\u003Cbr \/\u003E |-- PTRATIO: double (nullable = true)\u003Cbr \/\u003E |-- B: double (nullable = true)\u003Cbr \/\u003E |-- LSTAT: double (nullable = true)\u003Cbr \/\u003E |-- target: double (nullable = true)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ETo combine all feature data and separate 'label' data in a dataset, we use VectorAssembler. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist()\u003Cbr \/\u003E\u003Cbr \/\u003Eva \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E VectorAssembler(inputCols\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Efeatures, outputCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(data)\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect([\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E, \u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E])\u003Cbr \/\u003Eva_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E+--------------------+------+\u003Cbr \/\u003E|            features|target|\u003Cbr \/\u003E+--------------------+------+\u003Cbr \/\u003E|[0.00632,18.0,2.3...|  24.0|\u003Cbr \/\u003E|[0.02731,0.0,7.07...|  21.6|\u003Cbr \/\u003E|[0.02729,0.0,7.07...|  34.7|\u003Cbr \/\u003E+--------------------+------+\u003Cbr \/\u003Eonly showing top 3 rows\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll split data into the train and test parts.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E(train, test) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ErandomSplit([\u003Cspan style=\"color: #40a070;\"\u003E0.8\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E0.2\u003C\/span\u003E])\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003EPrediction and Accuracy Check\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; Next, we'll define the regressor model by using the GBTRegressor\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Eclass. Here, we can change the parameters according to data content\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E.\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Egbtr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E GBTRegressor(featuresCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E, labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E, maxIter\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003Egbtr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E gbtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(train)\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EAfter training the model, we'll predict test data and check the accuracy metrics.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Emdata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E gbtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(test)\u003Cbr \/\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ermse\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003ERegressionEvaluator(labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"target\"\u003C\/span\u003E, predictionCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E, metricName\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"rmse\"\u003C\/span\u003E)\u003Cbr \/\u003Ermse\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Ermse\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eevaluate(mdata)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Emae\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003ERegressionEvaluator(labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"target\"\u003C\/span\u003E, predictionCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E, metricName\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"mae\"\u003C\/span\u003E)\u003Cbr \/\u003Emae\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Emae\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eevaluate(mdata)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Er2\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003ERegressionEvaluator(labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"target\"\u003C\/span\u003E, predictionCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E, metricName\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"r2\"\u003C\/span\u003E)\u003Cbr \/\u003Er2\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Er2\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eevaluate(mdata)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, rmse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MAE: \"\u003C\/span\u003E, mae)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared: \"\u003C\/span\u003E, r2)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #999999;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #999999;\"\u003E--------------------+------+------------------+\u003Cbr \/\u003E|            features|target|        prediction|\u003Cbr \/\u003E+--------------------+------+------------------+\u003Cbr \/\u003E|[0.02729,0.0,7.07...|  34.7| 33.78460260004924|\u003Cbr \/\u003E|[0.09744,0.0,5.96...|  20.0|21.706837297006956|\u003Cbr \/\u003E|[0.80271,0.0,8.14...|  20.2| 18.51482812579027|\u003Cbr \/\u003E+--------------------+------+------------------+\u003Cbr \/\u003Eonly showing top 3 rows\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #999999;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #999999;\"\u003ERMSE:  4.416076755529045\u003Cbr \/\u003EMAE:  2.826016527055411\u003Cbr \/\u003ER-squared:  0.7808863473079463\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u0026nbsp;\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003EVisualizing the results\u003C\/b\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003ETo visualize the origianl and predicted data, we can use 'matplotlib' library. We'll extract those data from the 'mdata' object.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, mdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecount())\u003Cbr \/\u003Ey_pred\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003Ey_orig\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"target\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, y_orig, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, y_pred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\u003C\/div\u003E  \u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-X15HWiSwwBs\/YK9SW2a_-SI\/AAAAAAAABGk\/ivec4zYDViMB7G75e5-5IcXafKOYJiyxwCLcBGAsYHQ\/s382\/gbtr_pyspark.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"278\" data-original-width=\"382\" height=\"466\" src=\"https:\/\/1.bp.blogspot.com\/-X15HWiSwwBs\/YK9SW2a_-SI\/AAAAAAAABGk\/ivec4zYDViMB7G75e5-5IcXafKOYJiyxwCLcBGAsYHQ\/w640-h466\/gbtr_pyspark.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EIf you do new executions of your code, do not forget to close the spark context session.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# Stop session \u003C\/span\u003E\u003Cbr \/\u003Esc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Estop()\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E  \u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  In this tutorial, we've briefly learned how to fit and predict  regression data by using PySpark GBTRegressor model\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E in Python. The full  source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SparkContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.sql\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SQLContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.feature\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E VectorAssembler\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.regression\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E GBTRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.evaluation\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E RegressionEvaluator\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Edf_boston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata,columns\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eboston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003Edf_boston[\u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ESeries(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(df_boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ehead())\u003Cbr \/\u003E\u003Cbr \/\u003Esc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparkContext()\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EgetOrCreate()\u003Cbr \/\u003EsqlContext \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SQLContext(sc)\u003Cbr \/\u003E\u003Cbr \/\u003Edata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sqlContext\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EcreateDataFrame(df_boston)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EprintSchema())\u003Cbr \/\u003E\u003Cbr \/\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist()\u003Cbr \/\u003E\u003Cbr \/\u003Eva \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E VectorAssembler(inputCols \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E features, outputCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(data)\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect([\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E, \u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E])\u003Cbr \/\u003Eva_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003E(train, test) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ErandomSplit([\u003Cspan style=\"color: #40a070;\"\u003E0.8\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E0.2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cbr \/\u003Egbtr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E GBTRegressor(featuresCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E, labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E, maxIter\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003Egbtr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E gbtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(train)\u003Cbr \/\u003E\u003Cbr \/\u003Emdata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E gbtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(test)\u003Cbr \/\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Ermse\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003ERegressionEvaluator(labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"target\"\u003C\/span\u003E, predictionCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E, metricName\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"rmse\"\u003C\/span\u003E)\u003Cbr \/\u003Ermse\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Ermse\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eevaluate(mdata)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Emae\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003ERegressionEvaluator(labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"target\"\u003C\/span\u003E, predictionCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E, metricName\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"mae\"\u003C\/span\u003E)\u003Cbr \/\u003Emae\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Emae\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eevaluate(mdata)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Er2\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003ERegressionEvaluator(labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"target\"\u003C\/span\u003E, predictionCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E, metricName\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"r2\"\u003C\/span\u003E)\u003Cbr \/\u003Er2\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Er2\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eevaluate(mdata)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, rmse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MAE: \"\u003C\/span\u003E, mae)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared: \"\u003C\/span\u003E, r2)\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, mdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecount())\u003Cbr \/\u003Ey_pred\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003Ey_orig\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"target\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, y_orig, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, y_pred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()  \u003Cbr \/\u003E\u003Cbr \/\u003Esc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Estop()\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/spark.apache.org\/docs\/latest\/ml-classification-regression.html#gradient-boosted-tree-regression\" target=\"_blank\"\u003EPySpark Gradient-boosted tree regression\u003C\/a\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/2982029382986474414\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/05\/mllib-gradient-boosted-tree-regression.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/2982029382986474414"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/2982029382986474414"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/05\/mllib-gradient-boosted-tree-regression.html","title":"MLlib Gradient-boosted Tree Regression Example with PySpark"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-X15HWiSwwBs\/YK9SW2a_-SI\/AAAAAAAABGk\/ivec4zYDViMB7G75e5-5IcXafKOYJiyxwCLcBGAsYHQ\/s72-w640-h466-c\/gbtr_pyspark.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-2385747311802849247"},"published":{"$t":"2021-05-20T19:18:00.011-07:00"},"updated":{"$t":"2021-05-27T01:09:26.381-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"mllib"},{"scheme":"http://www.blogger.com/atom/ns#","term":"pyspark"}],"title":{"type":"text","$t":"MLLib Linear Regression Example with PySpark"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003C\/span\u003E \u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; Apache Spark is an analytic engine to process large scale dataset by using tools such as Spark SQL, MLLib and others.\u0026nbsp;\u003C\/span\u003EPySpark is a Python API to execute Spark applications in Python.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn this tutorial, we'll briefly learn how to fit and predict regression data by using PySpark and MLLib Linear Regression model. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe  tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EFitting and accuracy check\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EVisualizing the results \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries for this tutorial.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SparkContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.sql\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SQLContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.feature\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E VectorAssembler\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.regression\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E LinearRegression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We use Boston Housing Price dataset of Scikit-learn. We'll load dataset,\u0026nbsp; transform it into the data frame type, and combine into single features type by using VectorAssembler in order to make the appropriate input data format for LinearRegression class of PySpark ML library.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Edf_boston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata,columns\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eboston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003Edf_boston[\u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ESeries(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget)\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003C\/span\u003Edf_boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ehead())\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003ENext, we'll define SqlConext and create data frame by using df_boston data.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Esc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparkContext()\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EgetOrCreate()\u003Cbr \/\u003EsqlContext \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SQLContext(sc)\u003Cbr \/\u003E\u003Cbr \/\u003Edata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sqlContext\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EcreateDataFrame(df_boston)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EprintSchema())\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003Eroot\u003Cbr \/\u003E |-- CRIM: double (nullable = true)\u003Cbr \/\u003E |-- ZN: double (nullable = true)\u003Cbr \/\u003E |-- INDUS: double (nullable = true)\u003Cbr \/\u003E |-- CHAS: double (nullable = true)\u003Cbr \/\u003E |-- NOX: double (nullable = true)\u003Cbr \/\u003E |-- RM: double (nullable = true)\u003Cbr \/\u003E |-- AGE: double (nullable = true)\u003Cbr \/\u003E |-- DIS: double (nullable = true)\u003Cbr \/\u003E |-- RAD: double (nullable = true)\u003Cbr \/\u003E |-- TAX: double (nullable = true)\u003Cbr \/\u003E |-- PTRATIO: double (nullable = true)\u003Cbr \/\u003E |-- B: double (nullable = true)\u003Cbr \/\u003E |-- LSTAT: double (nullable = true)\u003Cbr \/\u003E |-- target: double (nullable = true)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ETo combine all feature data and separate 'label' data in a dataset, we use VectorAnalyzer. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist()\u003Cbr \/\u003E\u003Cbr \/\u003Eva \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E VectorAssembler(inputCols\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003C\/span\u003Efeatures, outputCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(data)\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect([\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E, \u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E])\u003Cbr \/\u003Eva_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E+--------------------+------+\u003Cbr \/\u003E|            features|target|\u003Cbr \/\u003E+--------------------+------+\u003Cbr \/\u003E|[0.00632,18.0,2.3...|  24.0|\u003Cbr \/\u003E|[0.02731,0.0,7.07...|  21.6|\u003Cbr \/\u003E|[0.02729,0.0,7.07...|  34.7|\u003Cbr \/\u003E+--------------------+------+\u003Cbr \/\u003Eonly showing top 3 rows\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003EFitting and Accuracy Check\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; Next, we'll define the regressor model by using the LinearRegression\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Eclass. Here, we can change the parameters according to your data content\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E. After fitting the model we can check coefficients and intercept values.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Elr\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003ELinearRegression(featuresCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E, labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E,\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E                    regParam\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.3\u003C\/span\u003E, elasticNetParam\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.8\u003C\/span\u003E)\u003Cbr \/\u003Elr_model \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E lr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(va_df)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Coefficients: \"\u003C\/span\u003E, lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecoefficients\u003Cspan style=\"color: #666666;\"\u003E\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Intercept: \"\u003C\/span\u003E, lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eintercept)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003ECoefficients:  [-0.034024229130007695,0.009359015936752714,\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E0.0,2.247564189644528,-7.230786173732827,4.348712110587842,\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E0.0,-0.603564999831066,0.0,0.0,-0.8220712024477692,\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E0.00808024431913416,-0.5034480504252381]\u003Cbr \/\u003EIntercept:  20.025217329865892\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ENow, we check the accuracy scores of fitted data. The model provides easy summary report of accuracy metrics.\u0026nbsp; \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esummary\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EmeanSquaredError)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MAE: \"\u003C\/span\u003E, lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esummary\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EmeanAbsoluteError)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared: \"\u003C\/span\u003E, lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esummary\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Er2)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EMSE:  23.832602753248327\u003Cbr \/\u003EMAE:  3.3409807187310054\u003Cbr \/\u003ER-squared:  0.7176886039395777\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u0026nbsp;\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003EVisualizing the results\u003C\/b\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EWe can use 'matplotlib' library to visualize the original and predicted 'label' data. We'll extract those data from the lr_model object.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Emdata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(va_df)\u003Cbr \/\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E+--------------------+------+------------------+\u003Cbr \/\u003E|            features|target|        prediction|\u003Cbr \/\u003E+--------------------+------+------------------+\u003Cbr \/\u003E|[0.00632,18.0,2.3...|  24.0|30.554831691938382|\u003Cbr \/\u003E|[0.02731,0.0,7.07...|  21.6| 25.47215641847489|\u003Cbr \/\u003E|[0.02729,0.0,7.07...|  34.7|  31.3186615896002|\u003Cbr \/\u003E+--------------------+------+------------------+\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, mdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecount())\u003Cbr \/\u003Ey_pred\u003Cspan style=\"color: #666666;\"\u003E = \u003C\/span\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003Ey_orig\u003Cspan style=\"color: #666666;\"\u003E = \u003C\/span\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"target\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E  \u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EFinally, we'll visualize the original and predicted data in a plot.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, y_orig, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, y_pred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()  \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-TAvjRCrqbtA\/YKcWxHaRNBI\/AAAAAAAABGY\/fQKjWKzfy9ksOI_q-HnaliM9fPEtc-95ACLcBGAsYHQ\/s382\/pyspark_linear.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"278\" data-original-width=\"382\" height=\"466\" src=\"https:\/\/1.bp.blogspot.com\/-TAvjRCrqbtA\/YKcWxHaRNBI\/AAAAAAAABGY\/fQKjWKzfy9ksOI_q-HnaliM9fPEtc-95ACLcBGAsYHQ\/w640-h466\/pyspark_linear.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EIf you do new executions of your code, do not forget to close the spark context session.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# Stop session \u003C\/span\u003E\u003Cbr \/\u003Esc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Estop()\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E  \u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; In this tutorial, we've briefly learned how to fit and predict regression data by using PySpark and MLLib LinearRegression model\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E. The full  source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SparkContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.sql\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SQLContext\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.feature\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E VectorAssembler\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyspark.ml.regression\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E LinearRegression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Edf_boston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata,columns\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eboston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003Edf_boston[\u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ESeries(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(df_boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ehead())\u003Cbr \/\u003E\u003Cbr \/\u003Esc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparkContext()\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EgetOrCreate()\u003Cbr \/\u003EsqlContext \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SQLContext(sc)\u003Cbr \/\u003E\u003Cbr \/\u003Edata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sqlContext\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EcreateDataFrame(df_boston)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EprintSchema())\u003Cbr \/\u003E\u003Cbr \/\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist()\u003Cbr \/\u003E\u003Cbr \/\u003Eva \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E VectorAssembler(inputCols \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E features, outputCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(data)\u003Cbr \/\u003Eva_df \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E va_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect([\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E, \u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E])\u003Cbr \/\u003Eva_df\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Elr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E LinearRegression(featuresCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'features'\u003C\/span\u003E, labelCol\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'target'\u003C\/span\u003E,\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E                      regParam\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.3\u003C\/span\u003E, elasticNetParam\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.8\u003C\/span\u003E)\u003Cbr \/\u003Elr_model \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E lr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(va_df)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Coefficients: \"\u003C\/span\u003E, lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecoefficients)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Intercept: \"\u003C\/span\u003E, lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eintercept)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esummary\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EmeanSquaredError)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MAE: \"\u003C\/span\u003E, lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esummary\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EmeanAbsoluteError)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared: \"\u003C\/span\u003E, lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esummary\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Er2)\u003Cbr \/\u003E\u003Cbr \/\u003Emdata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E lr_model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(va_df)\u003Cbr \/\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, mdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecount())\u003Cbr \/\u003Ey_pred\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"prediction\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003Ey_orig\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Emdata\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eselect(\u003Cspan style=\"color: #4070a0;\"\u003E\"target\"\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecollect()\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, y_orig, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, y_pred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()  \u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# Stop session \u003C\/span\u003E\u003Cbr \/\u003Esc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Estop()\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/spark.apache.org\/docs\/latest\/ml-classification-regression.html#linear-regression\" target=\"_blank\"\u003ESpark Linear Regression\u003C\/a\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/2385747311802849247\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/05\/mllib-linear-regression-example-with.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/2385747311802849247"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/2385747311802849247"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/05\/mllib-linear-regression-example-with.html","title":"MLLib Linear Regression Example with PySpark"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-TAvjRCrqbtA\/YKcWxHaRNBI\/AAAAAAAABGY\/fQKjWKzfy9ksOI_q-HnaliM9fPEtc-95ACLcBGAsYHQ\/s72-w640-h466-c\/pyspark_linear.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-1041899010940809646"},"published":{"$t":"2021-04-22T18:42:00.006-07:00"},"updated":{"$t":"2021-04-22T18:56:20.692-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"feature-selection"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"SelectFromModel Feature Selection Example in Python"},"content":{"type":"html","$t":"\u003Cp\u003E\u0026nbsp;\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EScikit-learn  API provides \u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.feature_selection.SelectFromModel.html\" target=\"_blank\"\u003ESelectFromModel\u003C\/a\u003E class for extracting best features of given  dataset according to the importance of weights. The SelectFromModel is a meta-estimator that determines the weight importance by comparing to the given threshold value.\u0026nbsp; \u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn  this tutorial, we'll briefly learn how to select best features of regression data by using the SelectFromModel in Python. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe  tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESelectFromModel for regression data \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries and functions.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.feature_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SelectFromModel\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.ensemble\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E AdaBoostRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E array\u003C\/span\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESelectFromModel for regression data\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We use Boston housing price dataset in this tutorial. We'll load the dataset and check the dimensions of feature data.\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Feature data dimension: \"\u003C\/span\u003E, x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EFeature data dimension:  (506, 13)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ESelectFromModel requires an estimator and we can use AdaBoostRegressor class for this purpose. An estimator model must have attributes to provide the indexes of selected data like 'get_support()' function. We'll define model by default value which applies median method to set the threshold value and fit the model on x and y data. \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eestimator \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E AdaBoostRegressor(random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, n_estimators\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E50\u003C\/span\u003E)\u003Cbr \/\u003Eselector \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SelectFromModel(estimator)\u003Cbr \/\u003Eselector \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E selector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x, y)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EAfter the training, we'll get status of each feature data. To identify the selected features we can use  get_support() function and filter out them from the features list.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003C\/span\u003EFinally, we'll get selected features names and respective data from the x data.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Estatus \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E selector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_support()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Selection status: \"\u003C\/span\u003E, status)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003ESelection status:  [False False False False False  True False  True False False False False\u003Cbr \/\u003E  True]\u003C\/span\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"All features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Selected features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features[\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E])\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eselector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(x)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EAll features:\u003Cbr \/\u003E['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO'\u003Cbr \/\u003E 'B' 'LSTAT']\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ESelected features:\u003Cbr \/\u003E['RM' 'DIS' 'LSTAT']\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Earray([[6.575 , 4.09  , 4.98  ],\u003Cbr \/\u003E       [6.421 , 4.9671, 9.14  ],\u003Cbr \/\u003E       [7.185 , 4.9671, 4.03  ],\u003Cbr \/\u003E       ...,\u003Cbr \/\u003E       [6.976 , 2.1675, 5.64  ],\u003Cbr \/\u003E       [6.794 , 2.3889, 6.48  ],\u003Cbr \/\u003E       [6.03  , 2.505 , 7.88  ]])\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  In this tutorial, we've briefly learned how to select important features in a dataset by using sklearn SelectFromModel class in python.  \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe full  source code is listed below. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.feature_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SelectFromModel\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.ensemble\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E AdaBoostRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E array\u003Cbr \/\u003E\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Feature data dimension: \"\u003C\/span\u003E, x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Eestimator \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E AdaBoostRegressor(random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, n_estimators\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E50\u003C\/span\u003E)\u003Cbr \/\u003Eselector \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SelectFromModel(estimator)\u003Cbr \/\u003Eselector \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E selector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x, y)\u003Cbr \/\u003E\u003Cbr \/\u003Estatus \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E selector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_support()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Selection status: \"\u003C\/span\u003E, status)\u003Cbr \/\u003E\u003Cbr \/\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"All features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Selected features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features[status])\u003Cbr \/\u003Eselector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etransform(x)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.feature_selection.SelectFromModel.html\" target=\"_blank\"\u003EScikit learn API SelectFromModel\u003C\/a\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/1041899010940809646\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/04\/selectfrommodel-feature-selection.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/1041899010940809646"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/1041899010940809646"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/04\/selectfrommodel-feature-selection.html","title":"SelectFromModel Feature Selection Example in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-215451203715444817"},"published":{"$t":"2021-03-28T23:57:00.004-07:00"},"updated":{"$t":"2021-03-29T00:33:31.617-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"feature-selection"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"Recursive Feature Elimination (RFE) Example in Python"},"content":{"type":"html","$t":"\u003Cp\u003E\u0026nbsp;\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EExtracting influential features of dataset is essential part of data preparation to train model in machine learning. Scikit-learn  API provides RFE class that ranks features by recursive feature elimination to select best features. The method recursively eliminates the least important features based on specific attributes taken by estimator. \u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn  this tutorial, we'll briefly learn how to select best features of dataset by using the RFE in Python. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe  tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ERFE Example with Boston dataset \u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries and functions.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.feature_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E RFE\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.ensemble\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E AdaBoostRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E array\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003ERFE Example with Boston dataset\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; We'll load Boston housing price dataset and check the dimensions of features data. The 'data'  property of the boston object is considered a feature data.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Feature data dimension: \"\u003C\/span\u003E, x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EFeature data dimension:  (506, 13)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe feature data contains 13 columns of 506 rows, our purpose is to decrease those columns by selecting best 8 by their influence rank. \u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENext,  we'll define the model by using RFE class. The class requires estimator and we can use AdaBoostRegressor meta-estimator model for this purpose. The target number of  features to select is defined by n_feature_to_select parameter and step defines number of features to remove in each round. We'll fit the model on x and y training data.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eestimator \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E AdaBoostRegressor(random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, n_estimators\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E100\u003C\/span\u003E)\u003Cbr \/\u003Eselector \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E RFE(estimator, n_features_to_select\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E8\u003C\/span\u003E, step\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)\u003Cbr \/\u003Eselector \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E selector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x, y)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EAfter fitting we can obtain selected features and their ranking positions.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E selector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esupport_\u003Cbr \/\u003Eranking \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E selector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eranking_\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Mask data: \"\u003C\/span\u003E, \u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Ranking: \"\u003C\/span\u003E, ranking)\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EMask data:  [ True False False False  True  True False  True  True  True  True False\u003Cbr \/\u003E  True]\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003ERanking:  [1 5 3 6 1 1 4 1 1 1 1 2 1]\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ETo make it readable we'll filter out the selected features.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003E\u003C\/span\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"All features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Selected features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features[\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E])\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EAll features:\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO'\u003Cbr \/\u003E 'B' 'LSTAT']\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003ESelected features:\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E['CRIM' 'NOX' 'RM' 'DIS' 'RAD' 'TAX' 'PTRATIO' 'LSTAT']\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  In this tutorial, we've briefly learned how to get best features of dataset by using recursive feature elimination (RFE) model in Python.  \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe full  source code is listed below. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.feature_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E RFE\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.ensemble\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E AdaBoostRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E array\u003Cbr \/\u003E\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Feature data dimension: \"\u003C\/span\u003E, x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Eestimator \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E AdaBoostRegressor(random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, n_estimators\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E100\u003C\/span\u003E)\u003Cbr \/\u003Eselector \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E RFE(estimator, n_features_to_select\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E8\u003C\/span\u003E, step\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)\u003Cbr \/\u003Eselector \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E selector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x, y)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E selector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esupport_\u003Cbr \/\u003Eranking \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E selector\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eranking_\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Mask data: \"\u003C\/span\u003E, \u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Ranking: \"\u003C\/span\u003E, ranking)\u003Cbr \/\u003E\u003Cbr \/\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"All features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Selected features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features[\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E])\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.feature_selection.RFE.html#sklearn.feature_selection.RFE\" target=\"_blank\"\u003EScikit learn API RFE\u003C\/a\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/215451203715444817\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/03\/recursive-feature-elimination-rfe.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/215451203715444817"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/215451203715444817"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/03\/recursive-feature-elimination-rfe.html","title":"Recursive Feature Elimination (RFE) Example in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-8138531407182273371"},"published":{"$t":"2021-03-01T20:54:00.002-08:00"},"updated":{"$t":"2021-03-02T15:27:16.535-08:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"ocr"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"Reading Texts on Image by Using Tesseract and PyOCR in Python"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EOptical Character Recognition (OCR) is a conversion of typed or handwritten letters on an image into the machine encoded texts.\u0026nbsp; There are several methods and libraries that can be used to read text on image. \u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn  this tutorial, we'll briefly learn how to read letters in an image by using the Tesseract and PyOCR in Python. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe  tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EInstalling Tesseract and PyOCR\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EReading texts on image \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; Let's get started.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cp\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EInstalling Tesseract and PyOCR\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; My installation goes on MacBook and you can find similar installation methods on other OSs.\u0026nbsp; Here, we install Tesseract and python PyOCR library.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E% brew install tesseract\u003Cbr \/\u003E\u003Cbr \/\u003E% pip install pyocr\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EIf you want to use the Tesseract directly to read the texts on your image, you can run it as below. It parses the texts on your image and shows them on your terminal output.\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E% tesseract \/your\/path\/image.png stdout\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EReading texts on image\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EAfter installing Tesseract and PyOCR, we'll load the required libraries for this tutorial. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003EPIL\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E Image\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyocr\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyocr.builders\u003C\/span\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003EWe also need image file that contains texts and I prepared below image.\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-Em4_P6AOagQ\/YDwkeVwW1EI\/AAAAAAAABFQ\/Adc-BV_rg6gtJwQoLNCUifRhHMOxzQxaQCLcBGAsYHQ\/s1168\/image_to_read.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"840\" data-original-width=\"1168\" src=\"https:\/\/1.bp.blogspot.com\/-Em4_P6AOagQ\/YDwkeVwW1EI\/AAAAAAAABFQ\/Adc-BV_rg6gtJwQoLNCUifRhHMOxzQxaQCLcBGAsYHQ\/s320\/image_to_read.png\" width=\"320\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll get available tools and languages.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Etools \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pyocr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_available_tools()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(tools))\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Etool \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E tools[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Tools: \"\u003C\/span\u003E, tool\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_name())\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Available languages: \"\u003C\/span\u003E, tool\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_available_languages())\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E1\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003ETools:  Tesseract (sh)\u003Cbr \/\u003EAvailable languages:  ['eng', 'osd', 'snum']\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll load image file with PIL library's Image.\u0026nbsp; Then we'll parse text by using the image_to_string() function of the tools object. Finally, we'll print the output text.\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp; \u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Epath \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #4070a0;\"\u003E'\/my\/image\/path\/image_to_read.png'\u003C\/span\u003E\u003Cbr \/\u003Eimg \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E Image\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eopen(path)\u003Cbr \/\u003E\u003Cbr \/\u003Etxt \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E tool\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eimage_to_string(\u003Cbr \/\u003E    img,\u003Cbr \/\u003E    lang\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'eng'\u003C\/span\u003E,\u003Cbr \/\u003E    builder\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Epyocr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ebuilders\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ETextBuilder()\u003Cbr \/\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(txt)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EPresentation Subtitle\u003Cbr \/\u003E\u003Cbr \/\u003ECAN YOU READ IT\u003Cbr \/\u003E\u003Cbr \/\u003EONE TWO THREE\u003Cbr \/\u003E\u003Cbr \/\u003ETitle\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EIn this tutorial, we've briefly learned how to read text on image file by using Tesseract and PyOCR tools. Full source code is listed below.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003EPIL\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E Image\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyocr\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epyocr.builders\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Etools \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pyocr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_available_tools()\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(tools))\u003Cbr \/\u003E\u003Cbr \/\u003Etool \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E tools[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Tools: \"\u003C\/span\u003E, tool\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_name())\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Available languages: \"\u003C\/span\u003E, tool\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_available_languages())\u003Cbr \/\u003E\u003Cbr \/\u003Epath \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #4070a0;\"\u003E'\/my\/image\/path\/image_to_read.png'\u003C\/span\u003E\u003Cbr \/\u003Eimg \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E Image\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eopen(path)\u003Cbr \/\u003E\u003Cbr \/\u003Etxt \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E tool\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eimage_to_string(\u003Cbr \/\u003E    img,\u003Cbr \/\u003E    lang\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'eng'\u003C\/span\u003E,\u003Cbr \/\u003E    builder\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Epyocr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ebuilders\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ETextBuilder()\u003Cbr \/\u003E)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(txt)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/8138531407182273371\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/03\/reading-texts-on-image-with-tesseract-pyocr.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/8138531407182273371"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/8138531407182273371"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/03\/reading-texts-on-image-with-tesseract-pyocr.html","title":"Reading Texts on Image by Using Tesseract and PyOCR in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-Em4_P6AOagQ\/YDwkeVwW1EI\/AAAAAAAABFQ\/Adc-BV_rg6gtJwQoLNCUifRhHMOxzQxaQCLcBGAsYHQ\/s72-c\/image_to_read.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-7254026319070723175"},"published":{"$t":"2021-02-11T17:58:00.005-08:00"},"updated":{"$t":"2021-03-28T22:29:51.310-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"feature-selection"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"SelectKBest Feature Selection Example in Python"},"content":{"type":"html","$t":"\u003Cp\u003E\u0026nbsp;\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EScikit-learn API provides SelectKBest class for extracting best features of given dataset. The SelectKBest method selects the features according to the k highest score. By changing the 'score_func' parameter we can apply the method for both classification and regression data. Selecting best features is important process when we prepare a large dataset for training. It helps us to eliminate less important part of the data and reduce a training time. \u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn this tutorial, we'll briefly learn how to select best features of classification and regression data by using the SelectKBest in Python. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe  tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESelectKBest for classification data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESelectKBest for regression data \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries and functions.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.feature_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SelectKBest\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.feature_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E chi2, f_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E array\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003ESelectKBest for classification\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; First,  we'll apply the SelectKBest model to classification data, Iris dataset.\u0026nbsp; We'll load the dataset and check the feature data dimension. The 'data' property of the iris object is considered feature data.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Feature data dimension: \"\u003C\/span\u003E, x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EFeature data dimension:  (150, 4)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003ENext, we'll define the model by using SelectKBest class. For classification we'll set 'chi2'\u0026nbsp; method as a scoring function. The target number of features is defined by k parameter. Then we'll fit and transform method on training x and y data. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eselect \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SelectKBest(score_func\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Echi2, k\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E select\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x,y)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"After selecting best 3 features:\"\u003C\/span\u003E, z\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EAfter selecting best 3 features: (150, 3)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe've selected 3 best features in x data. To identify the selected features we use get_support() function and filter out them from the features name list.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp; The z object contains selected x data.\u0026nbsp; \u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E select\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_support()\u003Cbr \/\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"All features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Selected best 3:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features[\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E])\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(z)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EAll features:\u003Cbr \/\u003E['sepal length (cm)' 'sepal width (cm)' 'petal length (cm)' 'petal width (cm)']\u003Cbr \/\u003ESelected best 3:\u003Cbr \/\u003E['sepal length (cm)' 'petal length (cm)' 'petal width (cm)']\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESelectKBest for regression data\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We apply the same method for regression data only changing scoring function. We'll load the Boston housing data set and check the feature data dimensions. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Feature data dimension: \"\u003C\/span\u003E, x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EFeature data dimension:  (506, 13)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext,  we'll define the model by using SelectKBest class. For regression, we'll set 'f_regression'\u0026nbsp; method as a scoring function. The target number of  features to select is 8. We'll fit and transform the model on training x and y data. \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eselect \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SelectKBest(score_func\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Ef_regression, k\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E8\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E select\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x, y)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"After selecting best 8 features:\"\u003C\/span\u003E, z\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EAfter selecting best 8 features: (506, 8)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ETo identify the selected features we can use  get_support() function and filter out them from the features list.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E The z object contains selected x data.\u0026nbsp; \u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003E\u003C\/span\u003E\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E select\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_support()\u003Cbr \/\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"All features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Selected best 8:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features[\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(z)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EAll features:\u003Cbr \/\u003E['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO'\u003Cbr \/\u003E 'B' 'LSTAT']\u003Cbr \/\u003ESelected best 8:\u003Cbr \/\u003E['CRIM' 'INDUS' 'NOX' 'RM' 'RAD' 'TAX' 'PTRATIO' 'LSTAT']\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; In this tutorial, we've briefly learned how to get k best features in classification and regression data by using SelectKBest model in Python. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe full  source code is listed below. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.feature_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SelectKBest\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.feature_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E chi2, f_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E array\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Feature data dimension: \"\u003C\/span\u003E, x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Eselect \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SelectKBest(score_func\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Echi2, k\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E select\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x,y)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"After selecting best 3 features:\"\u003C\/span\u003E, z\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E select\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_support()\u003Cbr \/\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"All features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Selected best 3:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features[\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(z)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Feature data dimension: \"\u003C\/span\u003E, x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Eselect \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SelectKBest(score_func\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Ef_regression, k\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E8\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E select\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x, y)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"After selecting best 8 features:\"\u003C\/span\u003E, z\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E select\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_support()\u003Cbr \/\u003Efeatures \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"All features:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Selected best 8:\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(features[\u003Cspan style=\"color: #007020;\"\u003Efilter\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(z)\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.feature_selection.SelectKBest.html#sklearn.feature_selection.SelectKBest\" target=\"_blank\"\u003EScikit learn API SelectKBest\u003C\/a\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/7254026319070723175\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/02\/seleckbest-feature-selection-example-in-python.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/7254026319070723175"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/7254026319070723175"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/02\/seleckbest-feature-selection-example-in-python.html","title":"SelectKBest Feature Selection Example in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-6869210333538526284"},"published":{"$t":"2021-02-03T18:41:00.008-08:00"},"updated":{"$t":"2021-02-03T18:48:30.005-08:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"Dimensionality Reduction Example with Factor Analysis in Python "},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; Factor Analysis is a technique that used to express data with reduced number of variables. Reducing the number of variables in a data is helpful method to simplify large dataset by decreasing the variables without loosing the generality of it.\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EThe  Scikit-learn API provides the FactorAnalysis model that \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Eperforms a maximum likelihood estimate of\u0026nbsp; loading matrix using SVD based approach. In this tutorial, we'll briefly learn how to use FactorAnalysis model to reduce the data dimension and visualize  the output in Python. The tutorials covers:\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EMNIST\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E dataset \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EProjection with Factor Analysis\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EImage data \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EFactor Analysis\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E and visualizing\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESource code listing\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EWe'll start by loading the required libraries and functions. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.decomposition\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E FactorAnalysis \u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ekeras.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mnist\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E reshape\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eseaborn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esns\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E where\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EMNIST dataset projection with factor analysis\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe load \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EMNIST handwritten  digit dataset provided by Keras library. We'll check the dimensions of x part of data and\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E transform it into the two-dimensional data.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(x_train, y_train), (_ , _) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eload_data()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(60000, 28, 28)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex_mnist \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E reshape(x_train, [x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(60000, 784)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext,  we'll define the model by using the FactorAnalysis class, here the n_components  parameter defines the number of target dimensions. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Efa \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E FactorAnalysis(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E fa\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003ETo visualize the transformed data, we'll collect the output  component in a dataframe and plot it by using the 'seaborn' library's  scatterplot(). In color palette of scatter plot, we'll  set 10 because there are 10 type of categories in label data.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E \u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Edf \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E),\u003Cbr \/\u003E                data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"MNIST data projection with Factor Analysis\"\u003C\/span\u003E)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u0026nbsp;\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-yGnXA7-q8TA\/YBtZD4oZMRI\/AAAAAAAABEs\/p0soT83GjmooPplU4BDhk4345fUwilPMgCLcBGAsYHQ\/s640\/factor_analysi_projection.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"480\" data-original-width=\"640\" height=\"480\" src=\"https:\/\/1.bp.blogspot.com\/-yGnXA7-q8TA\/YBtZD4oZMRI\/AAAAAAAABEs\/p0soT83GjmooPplU4BDhk4345fUwilPMgCLcBGAsYHQ\/w640-h480\/factor_analysi_projection.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe  plot shows a two-dimensional visualization of the MNIST data. The colors define  the target digits and their feature data location in 2D space.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003EImage data Factor Analysis and visualizing\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENext,  we'll apply the factor analysis method to image data. Here, we use digit '3' x and y data. We can extract and reshape data as below.\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Edigit3_y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E where(y_train\u003Cspan style=\"color: #666666;\"\u003E==\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003Edigit3_x \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E x_train[digit3_y]\u003Cbr \/\u003E\u003Cbr \/\u003Ex_mnist \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E reshape(digit3_x, [digit3_x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], digit3_x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Edigit3_x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(6131, 784)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EHere,  we have 784 features and 6131 sample images. We'll fit FactorAnalysis model on x_mnist data and visualize the output images. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Efa \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E FactorAnalysis(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E, random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E fa\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x_mnist)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(z\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecomponents_\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esubplots_adjust(wspace\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, hspace\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etight_layout()\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egray()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efor\u003C\/span\u003E i \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ein\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E9\u003C\/span\u003E):\t\u003Cbr \/\u003E    plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esubplot(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E, i \u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E) \u003Cbr \/\u003E    plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etick_params(labelbottom\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u003Cbr \/\u003E    plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etick_params(labelleft\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u003Cbr \/\u003E    plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eimshow(z\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecomponents_[i]\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ereshape(\u003Cspan style=\"color: #40a070;\"\u003E28\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E28\u003C\/span\u003E), )\u003Cbr \/\u003E    \u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-T_k6tSq79fM\/YBtdCKdYMrI\/AAAAAAAABE4\/zha6BtW8tvgMXjDAaQXmYzSEAXRq87dRACLcBGAsYHQ\/s640\/imaga_factor_analysis.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"480\" data-original-width=\"640\" height=\"480\" src=\"https:\/\/1.bp.blogspot.com\/-T_k6tSq79fM\/YBtdCKdYMrI\/AAAAAAAABE4\/zha6BtW8tvgMXjDAaQXmYzSEAXRq87dRACLcBGAsYHQ\/w640-h480\/imaga_factor_analysis.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EThe  plot shows nine samples of output image data.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn this tutorial, we've briefly learned how\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E to \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Ehow to use Sklearn's FactorAnalysis model to reduce dimensions of data data in Python\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E The full source code is listed below.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.decomposition\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E FactorAnalysis \u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ekeras.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mnist\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E reshape\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eseaborn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esns\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E where\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(x_train, y_train), (_ , _) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eload_data()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape) \u003Cbr \/\u003E\u003Cbr \/\u003Ex_mnist \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E reshape(x_train, [x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Efa \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E FactorAnalysis(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E fa\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Edf \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E),\u003Cbr \/\u003E                data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"MNIST data projection with Factor Analysis\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Edigit3_y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E where(y_train\u003Cspan style=\"color: #666666;\"\u003E==\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003Edigit3_x \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E x_train[digit3_y]\u003Cbr \/\u003E\u003Cbr \/\u003Ex_mnist \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E reshape(digit3_x, [digit3_x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], digit3_x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Edigit3_x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Efa \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E FactorAnalysis(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E, random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E fa\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x_mnist)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(z\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecomponents_\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esubplots_adjust(wspace\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, hspace\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etight_layout()\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egray()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efor\u003C\/span\u003E i \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ein\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E9\u003C\/span\u003E):\t\u003Cbr \/\u003E    plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esubplot(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E, i \u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E) \u003Cbr \/\u003E    plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etick_params(labelbottom\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u003Cbr \/\u003E    plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etick_params(labelleft\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u003Cbr \/\u003E    plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eimshow(z\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecomponents_[i]\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ereshape(\u003Cspan style=\"color: #40a070;\"\u003E28\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E28\u003C\/span\u003E), )\u003Cbr \/\u003E    \u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.decomposition.FactorAnalysis.html\" target=\"_blank\"\u003EScikit-learn FactorAnalysis\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/6869210333538526284\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/02\/dimension-reducing-with-factor-analysis-in-python.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/6869210333538526284"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/6869210333538526284"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/02\/dimension-reducing-with-factor-analysis-in-python.html","title":"Dimensionality Reduction Example with Factor Analysis in Python "}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-yGnXA7-q8TA\/YBtZD4oZMRI\/AAAAAAAABEs\/p0soT83GjmooPplU4BDhk4345fUwilPMgCLcBGAsYHQ\/s72-w640-h480-c\/factor_analysi_projection.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-4781679362533711065"},"published":{"$t":"2021-01-19T16:18:00.004-08:00"},"updated":{"$t":"2021-01-19T20:24:05.461-08:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"PCA"},{"scheme":"http://www.blogger.com/atom/ns#","term":"projection"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"SparsePCA Projection Example in Python"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; Sparse Principal Component Analysis is a type of PCA analysis method. SparsePCA extracts sparse components to build the data. \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EThe  Scikit-learn API provides SparsePCA class to apply Sparse PCA method in Python. In this tutorial, we'll briefly learn how to project data by using SparsePCA and visualize  the projected data in Python. The tutorials covers:\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EIris dataset SparsePCA projection and visualizing\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EMNIST dataset SparsePCA projection and visualizing\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESource code listing\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EWe'll start by loading the required libraries and functions. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.decomposition\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SparsePCA \u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ekeras.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mnist\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E reshape\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eseaborn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esns\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EIris dataset SparsePCA projection and visualizing\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EAfter loading the Iris dataset, we'll get the data and label parts of the dataset.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EThen,  we'll define the model by using the SparsePCA class, here the n_components  parameter defines the number of target dimensions. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Espca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparsePCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E,\u003Cspan style=\"color: #666666;\"\u003E\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E\u003C\/span\u003E random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E spca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext,  we'll visualize the result in a plot. We'll collect the output  component data in a dataframe, then we use 'seaborn' library's  scatterplot() to plot the data. In color palette of scatter plot, we'll  set 3 because there are 3 types categories in label data.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E \u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Edf \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E),\u003Cbr \/\u003E                data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"Iris data SparsePCA projection\"\u003C\/span\u003E)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u0026nbsp;\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-1zGYeWWtRMc\/YAd1IsmPn6I\/AAAAAAAABEI\/6YWPCZ0vHHE2G5nEEYMhi03BnTPccgt3QCLcBGAsYHQ\/s394\/sparsepca_iris.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"278\" data-original-width=\"394\" height=\"452\" src=\"https:\/\/1.bp.blogspot.com\/-1zGYeWWtRMc\/YAd1IsmPn6I\/AAAAAAAABEI\/6YWPCZ0vHHE2G5nEEYMhi03BnTPccgt3QCLcBGAsYHQ\/w640-h452\/sparsepca_iris.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003Cspan\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003EMNIST \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003Edataset SparsePCA projection and visualizing\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENext,  we'll apply the same method to the larger dataset. MNIST handwritten  digit dataset works well for this purpose and we can use Keras API's  MNIST data. We extract only train part of the dataset because here it is  enough to test data with SparsePCA class. \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(x_train, y_train), (_ , _) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eload_data()\u003Cbr \/\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(60000, 28, 28)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EMNIST is a three-dimensional data, we'll reshape it into the two-dimensional one.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex_mnist \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E reshape(x_train, [x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(60000, 784)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EHere, we have 784 features and 60000 samples.\u0026nbsp; Now, we'll project it into two dimensions with Sparse PCA method and visualize it in a plot. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Espca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparsePCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E spca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003Cbr \/\u003Edf \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E),\u003Cbr \/\u003E                data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"MNIST data SparsePCA projection\"\u003C\/span\u003E)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-__JCbGKWX-A\/YAd07iKChrI\/AAAAAAAABEE\/8Q_AqCpSwLcUNFCR1r757yYr5gkHlEqowCLcBGAsYHQ\/s404\/sparse_pca_mnist.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"278\" data-original-width=\"404\" height=\"440\" src=\"https:\/\/1.bp.blogspot.com\/-__JCbGKWX-A\/YAd07iKChrI\/AAAAAAAABEE\/8Q_AqCpSwLcUNFCR1r757yYr5gkHlEqowCLcBGAsYHQ\/w640-h440\/sparse_pca_mnist.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EThe  plot shows a two-dimensional visualization of the MNIST data. The colors define  the target digits and their feature data location in 2D space.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn this tutorial, we've briefly learned how\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E to \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Ehow to project data with Sparse PCA method and visualize the projected data in Python\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E The full source code is listed below.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.decomposition\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SparsePCA \u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ekeras.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mnist\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E reshape\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eseaborn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esns\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Espca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparsePCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #666666;\"\u003E\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E\u003C\/span\u003Erandom_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E spca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003Cbr \/\u003Edf \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E),\u003Cbr \/\u003E                data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"Iris data SparsePCA projection\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003E(x_train, y_train), (_ , _) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eload_data()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape) \u003Cbr \/\u003E\u003Cbr \/\u003Ex_mnist \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E reshape(x_train, [x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Espca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparsePCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E spca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003Cbr \/\u003E\u003Cbr \/\u003Edf \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E),\u003Cbr \/\u003E                data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"MNIST data SparsePCA projection\"\u003C\/span\u003E)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.decomposition.SparsePCA.html#sklearn.decomposition.SparsePCA\" target=\"_blank\"\u003EScikit-learn SparsePCA\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/4781679362533711065\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/01\/sparsepca-projection-example-in-python.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/4781679362533711065"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/4781679362533711065"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2021\/01\/sparsepca-projection-example-in-python.html","title":"SparsePCA Projection Example in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-1zGYeWWtRMc\/YAd1IsmPn6I\/AAAAAAAABEI\/6YWPCZ0vHHE2G5nEEYMhi03BnTPccgt3QCLcBGAsYHQ\/s72-w640-h452-c\/sparsepca_iris.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-8515821636736025387"},"published":{"$t":"2020-12-15T01:51:00.008-08:00"},"updated":{"$t":"2020-12-15T17:39:22.894-08:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"anomaly detection"},{"scheme":"http://www.blogger.com/atom/ns#","term":"optics"}],"title":{"type":"text","$t":"Anomaly Detection Example With OPTICS Method in Python"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan\u003EOrdering Points To Identify the Clustering Structure (OPTICS) is an algorithm that estimates density-based clustering structure of a given data. It applies the clustering method similar to DBSCAN algorithm.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn this tutorial, we'll learn how to apply OPTICS method to detect anomalies in given data. Here, we use OPTIC class of Scikit-learn API. The tutorial covers: \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EAnomaly detection with OPTICS\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESource code listing\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIf you want to know other anomaly detection methods, please check out my\u003Cspan\u003E\u003Cspan\u003E \u003Ca href=\"https:\/\/www.datatechnotes.com\/2020\/05\/introduction-to-anomaly-detection-methods.html\" target=\"_blank\"\u003EA Brief Explanation of 8 Anomaly Detection Methods with Python\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E tutorial.\u0026nbsp;\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cp\u003E\u003C\/p\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EWe'll start by loading the required libraries and functions for this tutorial.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003E \u003C\/b\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"color: #007020;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5;\"\u003Esklearn.cluster\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Eimport\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EOPTICS\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"color: #007020;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Eimport\u003C\/span\u003E make_blobs\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"color: #007020;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Eimport\u003C\/span\u003E quantile, where, random\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"color: #007020;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5;\"\u003Eplt\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u0026nbsp;\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cfont\u003E\u003Cb\u003EPreparing the data\u003C\/b\u003E\u003C\/font\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003EWe'll generate simple data data for this tutorial by using the make_blob() function and visualize it in a plot. \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Erandom\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eseed(\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ex, _ \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_blobs(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E350\u003C\/span\u003E, centers\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, cluster_std\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E, center_box\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E20\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-85xpw_iMGbc\/X9iF9yQmCfI\/AAAAAAAABDQ\/7iisNF2VMQ0MApPsbc5tOEjmOFaLZslFgCLcBGAsYHQ\/s640\/data_for_optics.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"480\" data-original-width=\"640\" height=\"480\" src=\"https:\/\/1.bp.blogspot.com\/-85xpw_iMGbc\/X9iF9yQmCfI\/AAAAAAAABDQ\/7iisNF2VMQ0MApPsbc5tOEjmOFaLZslFgCLcBGAsYHQ\/w640-h480\/data_for_optics.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cspan\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cfont\u003E\u003Cb\u003EAnomaly detection with OPTICS\u003C\/b\u003E\u003C\/font\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EWe'll define the model by using OPTICS\u003C\/span\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003Eclass with its default parameters then we'll fit it on x data.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E You can  check the parameters of the class and change them according to your  analysis and target data.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Emodel \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E OPTICS()\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(model)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"color: #666666;\"\u003EOPTICS(algorithm='auto', cluster_method='xi', eps=None, leaf_size=30,\u003Cbr \/\u003E       max_eps=inf, metric='minkowski', metric_params=None,\u003Cbr \/\u003E       min_cluster_size=None, min_samples=5, n_jobs=None, p=2,\u003Cbr \/\u003E       predecessor_correction=True, xi=0.05)\u003Cbr \/\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ENext, we'll obtain the scores of each sample of x data by using core_distance_ property of the model.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Escores \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecore_distances_\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EThen, we'll extract the threshold value from the scores data by using quantile() function. You can set your target percentage to quantile, in this example we'll set 98% data as normal and remaining part of data the data becomes an outlier.\u003Cbr \/\u003E\u003Cb\u003E\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Ethresh \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E quantile(scores, \u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E98\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(thresh)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u003Cbr \/\u003E\u003Cspan style=\"color: #666666;\"\u003E0.35064484877392416\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u0026nbsp;\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EBy using threshold value, we'll find the samples with the scores that are equal to or higher than the threshold value. \u003Cbr \/\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Eindex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E where(scores \u003Cspan style=\"color: #666666;\"\u003E\u0026gt;=\u003C\/span\u003E thresh)\u003Cbr \/\u003Evalues \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E x[index]\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(values)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"color: #666666;\"\u003E[[ 9.45071447 14.58847433]\u003Cbr \/\u003E [ 8.500387   16.2113985 ]\u003Cbr \/\u003E [ 9.56481939 16.89136015]\u003Cbr \/\u003E [ 9.63176979 14.41548797]\u003Cbr \/\u003E [ 8.43771706 15.07302741]\u003Cbr \/\u003E [10.33672675 14.89789167]\u003Cbr \/\u003E [10.43533425 16.58262441]]\u003Cbr \/\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EFinally, we'll visualize the results in a plot by highlighting the anomalies with a color.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(values[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E],values[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'r'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend((\u003Cspan style=\"color: #4070a0;\"\u003E\"normal\"\u003C\/span\u003E, \u003Cspan style=\"color: #4070a0;\"\u003E\"anomal\"\u003C\/span\u003E), loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"best\"\u003C\/span\u003E, fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow() \u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px; text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cdiv style=\"text-align: center;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-T2N0Is-enBA\/X9iFzzh6YNI\/AAAAAAAABDM\/in2HYBVWVgw8vHb4M84_UFRzwDAPnpi3QCLcBGAsYHQ\/s640\/anomaly_det_optics.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"480\" data-original-width=\"640\" height=\"480\" src=\"https:\/\/1.bp.blogspot.com\/-T2N0Is-enBA\/X9iFzzh6YNI\/AAAAAAAABDM\/in2HYBVWVgw8vHb4M84_UFRzwDAPnpi3QCLcBGAsYHQ\/w640-h480\/anomaly_det_optics.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cspan\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: center;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;  In this tutorial, we've briefly learned how to detect the anomalies by  using the OPTICS method by using the Scikit-learn's OPTICS class in  Python. The full source code is listed below.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cfont\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/font\u003E\u003C\/span\u003E\u003C\/span\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cdiv style=\"background-color: whitesmoke; color: black; font-weight: normal; line-height: 19px; white-space: pre;\"\u003E\u003Cdiv\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"color: #007020;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5;\"\u003Esklearn.cluster\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Eimport\u003C\/span\u003E OPTICS\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Eimport\u003C\/span\u003E make_blobs\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Eimport\u003C\/span\u003E quantile, where, random\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Erandom\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eseed(\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ex, _ \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_blobs(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E350\u003C\/span\u003E, centers\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, cluster_std\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E, center_box\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E20\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow() \u003Cbr \/\u003E \u003Cbr \/\u003Emodel \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E OPTICS()\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(model)\u003Cbr \/\u003E\u003Cbr \/\u003Escores \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E model\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecore_distances_\u003Cbr \/\u003E\u003Cbr \/\u003Ethresh \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E quantile(scores, \u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E98\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(thresh) \u003Cbr \/\u003E\u003Cbr \/\u003Eindex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E where(scores \u003Cspan style=\"color: #666666;\"\u003E\u0026gt;=\u003C\/span\u003E thresh)\u003Cbr \/\u003Evalues \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E x[index]\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(values)\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(values[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E],values[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'r'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend((\u003Cspan style=\"color: #4070a0;\"\u003E\"normal\"\u003C\/span\u003E, \u003Cspan style=\"color: #4070a0;\"\u003E\"anomal\"\u003C\/span\u003E), loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"best\"\u003C\/span\u003E, fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u0026nbsp;\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.cluster.OPTICS.html#sklearn.cluster.OPTICS\" target=\"_blank\"\u003EScikit-learn API\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/8515821636736025387\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/12\/anomaly-detection-with-optics-in-python.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/8515821636736025387"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/8515821636736025387"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/12\/anomaly-detection-with-optics-in-python.html","title":"Anomaly Detection Example With OPTICS Method in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-85xpw_iMGbc\/X9iF9yQmCfI\/AAAAAAAABDQ\/7iisNF2VMQ0MApPsbc5tOEjmOFaLZslFgCLcBGAsYHQ\/s72-w640-h480-c\/data_for_optics.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-3989182544971421864"},"published":{"$t":"2020-12-01T20:45:00.005-08:00"},"updated":{"$t":"2020-12-01T22:55:02.826-08:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"clustering"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"Spectral Clustering Example in Python"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESpectral clustering is a technique to apply the spectrum of the similarity matrix of the data in dimensionality reduction. It is useful and easy to implement clustering method.\u0026nbsp;\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EThe  Scikit-learn API provides SpectralClustering class to implement spectral clustering method in Python. The SpectralClustering a\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Epplies the clustering to a projection of the normalized Laplacian. In this tutorial, we'll briefly learn how to cluster and visualize  data with SpectralClustering in Python. The tutorial covers:\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EClustering with the \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESpectralClustering \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003Eand visualizing \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESource code listing\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EWe'll start by importing the required libraries and functions. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.cluster\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SpectralClustering\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets.samples_generator\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_blobs\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E random\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll prepare a target data for this tutorial by generating a simple dataset using the make_blob() function and visualize it in a plot. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Erandom\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eseed(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)\u003Cbr \/\u003Ex, _ \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_blobs(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E400\u003C\/span\u003E, centers\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E, cluster_std\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1.5\u003C\/span\u003E)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-XyiV9aBlE9g\/X8cbEy-OAAI\/AAAAAAAABCE\/KpmMyrYXi7kbL20KLsvG1QVXKJ-g-JivQCLcBGAsYHQ\/s640\/cluster_data.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"480\" data-original-width=\"640\" height=\"480\" src=\"https:\/\/1.bp.blogspot.com\/-XyiV9aBlE9g\/X8cbEy-OAAI\/AAAAAAAABCE\/KpmMyrYXi7kbL20KLsvG1QVXKJ-g-JivQCLcBGAsYHQ\/w640-h480\/cluster_data.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EIt is an easy to understand data so we'll cluster it with spectral cluster method. \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003EClustering with the SpectralClustering and visualizing\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EWe'll define model by using \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESpectralClustering \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003Eclass then we'll fit it on x data.The \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESpectralClustering \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003Erequires the number of clusters so w'll set 4 to n_cluster parameter. You can check the parameters the class and change them according to your analysis and target data.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Esc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SpectralClustering(n_clusters\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(sc)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003ES\u003Cspan style=\"color: #666666;\"\u003ESpectralClustering(affinity='rbf', assign_labels='kmeans', coef0=1, degree=3,\u003Cbr \/\u003E                   eigen_solver=None, eigen_tol=0.0, gamma=1.0,\u003Cbr \/\u003E                   kernel_params=None, n_clusters=4, n_components=None,\u003Cbr \/\u003E                   n_init=10, n_jobs=None, n_neighbors=10, random_state=None)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003ENext, we'll visualize the clustered data in a plot. To separate the clusters by a color, we'll extract label data from the fitted model.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Elabels \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elabels_\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], c\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Elabels)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-VVS7DzAAj6Y\/X8cbL_4R7GI\/AAAAAAAABCI\/fUnoF095snsy0-aqMEixZnADy_XDmLGzACLcBGAsYHQ\/s640\/spectralclustering.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"480\" data-original-width=\"640\" height=\"480\" src=\"https:\/\/1.bp.blogspot.com\/-VVS7DzAAj6Y\/X8cbL_4R7GI\/AAAAAAAABCI\/fUnoF095snsy0-aqMEixZnADy_XDmLGzACLcBGAsYHQ\/w640-h480\/spectralclustering.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe can also check the clustering the result by changing the number of clusters. \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Ef \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efigure()\u003Cbr \/\u003Ef\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eadd_subplot(\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efor\u003C\/span\u003E i \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ein\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E6\u003C\/span\u003E):\u003Cbr \/\u003E sc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SpectralClustering(n_clusters\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Ei)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x)\u003Cbr \/\u003E f\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eadd_subplot(\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, i\u003Cspan style=\"color: #666666;\"\u003E-\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)\u003Cbr \/\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], s\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E, c\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elabels_, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"n_cluster-\"\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003E\u003Cspan style=\"color: #007020;\"\u003Estr\u003C\/span\u003E(i))\u003Cbr \/\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend()\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-IBi3a2bv8Us\/X8cbQHKdm2I\/AAAAAAAABCM\/fzBOWW__mn4-OkYW0tvd0fTSpGMjkr9NQCLcBGAsYHQ\/s640\/spectralclustering1.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"480\" data-original-width=\"640\" height=\"480\" src=\"https:\/\/1.bp.blogspot.com\/-IBi3a2bv8Us\/X8cbQHKdm2I\/AAAAAAAABCM\/fzBOWW__mn4-OkYW0tvd0fTSpGMjkr9NQCLcBGAsYHQ\/w640-h480\/spectralclustering1.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn this tutorial, we've briefly learned how\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E to \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Ehow to cluster and visualize the data by using the \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESpectralClustering \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003Eclass in Python\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E The full source code is listed below.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.cluster\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SpectralClustering\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets.samples_generator\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_blobs\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E random\u003Cbr \/\u003E\u003Cbr \/\u003Erandom\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eseed(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)\u003Cbr \/\u003Ex, _ \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_blobs(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E400\u003C\/span\u003E, centers\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E, cluster_std\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1.5\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003Esc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SpectralClustering(n_clusters\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(sc)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003Elabels \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elabels_\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], c\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Elabels)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003Ef \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efigure()\u003Cbr \/\u003Ef\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eadd_subplot(\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efor\u003C\/span\u003E i \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ein\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E6\u003C\/span\u003E):\u003Cbr \/\u003E sc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SpectralClustering(n_clusters\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Ei)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x)\u003Cbr \/\u003E f\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eadd_subplot(\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, i\u003Cspan style=\"color: #666666;\"\u003E-\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)\u003Cbr \/\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], s\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E, c\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elabels_, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"n_cluster-\"\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003E\u003Cspan style=\"color: #007020;\"\u003Estr\u003C\/span\u003E(i))\u003Cbr \/\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend()\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.cluster.SpectralClustering.html#sklearn.cluster.SpectralClustering\" target=\"_blank\"\u003EScikit-learn SpectralClustering\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/3989182544971421864\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/12\/spectral-clustering-example-in-python.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/3989182544971421864"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/3989182544971421864"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/12\/spectral-clustering-example-in-python.html","title":"Spectral Clustering Example in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-XyiV9aBlE9g\/X8cbEy-OAAI\/AAAAAAAABCE\/KpmMyrYXi7kbL20KLsvG1QVXKJ-g-JivQCLcBGAsYHQ\/s72-w640-h480-c\/cluster_data.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-6738925883255755405"},"published":{"$t":"2020-11-26T00:04:00.013-08:00"},"updated":{"$t":"2020-11-29T03:42:05.785-08:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"projection"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"},{"scheme":"http://www.blogger.com/atom/ns#","term":"tsne"}],"title":{"type":"text","$t":"TSNE Visualization Example in Python"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u0026nbsp;\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003ET-distributed Stochastic Neighbor Embedding (T-SNE) is a tool for visualizing high-dimensional data. T-SNE, based on stochastic neighbor embedding, is a nonlinear dimensionality reduction technique to visualize data in a two or three dimensional space.\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EThe Scikit-learn API provides TSNE class to visualize data with T-SNE method. In this tutorial, we'll briefly learn how to fit and visualize data with TSNE in Python. The tutorials covers:\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EIris dataset TSNE fitting and visualizing\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EMNIST dataset TSNE fitting and visualizing\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESource code listing\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EWe'll start by loading the required libraries and functions. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.manifold\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E TSNE\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ekeras.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mnist\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E reshape\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eseaborn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esns\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EIris dataset TSNE fitting and visualizing\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EAfter loading the Iris dataset, we'll get the data and label parts of the dataset.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EThen, we'll define the model by using the TSNE class, here the n_components parameter defines the number of target dimensions. The 'verbose=1' shows the log data so we can check it.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Etsne \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E TSNE(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, verbose\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E tsne\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003C\/span\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E[t-SNE] Computing 91 nearest neighbors...\u003Cbr \/\u003E[t-SNE] Indexed 150 samples in 0.001s...\u003Cbr \/\u003E[t-SNE] Computed neighbors for 150 samples in 0.006s...\u003Cbr \/\u003E[t-SNE] Computed conditional probabilities for sample 150 \/ 150\u003Cbr \/\u003E[t-SNE] Mean sigma: 0.509910\u003Cbr \/\u003E[t-SNE] KL divergence after 250 iterations with early exaggeration: 48.021526\u003Cbr \/\u003E[t-SNE] KL divergence after 1000 iterations: 0.122989\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll visualize the result in a plot. We'll collect the output component data in a dataframe, then we use 'seaborn' library's scatterplot() to plot the data. In color palette of scatter plot, we'll set 3 because there are 3 types categories in label data.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E \u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003Edf \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E),\u003Cbr \/\u003E                data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"Iris data T-SNE projection\"\u003C\/span\u003E)\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-sX7imo0tVoo\/X79ZmyxqSXI\/AAAAAAAABBk\/IyETgXAln_wF8KeNNULPs1o4EsSC10v-gCLcBGAsYHQ\/s640\/tsne-iris.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"480\" data-original-width=\"640\" height=\"480\" src=\"https:\/\/1.bp.blogspot.com\/-sX7imo0tVoo\/X79ZmyxqSXI\/AAAAAAAABBk\/IyETgXAln_wF8KeNNULPs1o4EsSC10v-gCLcBGAsYHQ\/w640-h480\/tsne-iris.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003EMNIST \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003Edataset TSNE fitting and visualizing\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll apply the same method to the larger dataset. MNIST handwritten digit dataset works well for this purpose and we can use Keras API's MNIST data. We extract only train part of the dataset because here it is enough to test data with TSNE. The TSNE requires too much time to process thus, I'll use only 3000 rows. \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(x_train, y_train), (_ , _) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eload_data()\u003Cbr \/\u003Ex_train \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E x_train[:\u003Cspan style=\"color: #40a070;\"\u003E3000\u003C\/span\u003E]\u003Cbr \/\u003Ey_train \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train[:\u003Cspan style=\"color: #40a070;\"\u003E3000\u003C\/span\u003E]\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(3000, 28, 28)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EMNIST is a three-dimensional data, we'll reshape it into the two-dimensional one.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003Ex_mnist \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E reshape(x_train, [x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E(3000, 784)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003EHere, we have 784 features data.\u0026nbsp; Now, we'll project it into two dimensions with TSNE and visualize it in a plot. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003Etsne \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E TSNE(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, verbose\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E tsne\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003Cbr \/\u003Edf \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E),\u003Cbr \/\u003E                data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"MNIST data T-SNE projection\"\u003C\/span\u003E)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E[t-SNE] Computing 91 nearest neighbors...\u003Cbr \/\u003E[t-SNE] Indexed 3000 samples in 0.922s...\u003Cbr \/\u003E[t-SNE] Computed neighbors for 3000 samples in 10.601s...\u003Cbr \/\u003E[t-SNE] Computed conditional probabilities for sample 1000 \/ 3000\u003Cbr \/\u003E[t-SNE] Computed conditional probabilities for sample 2000 \/ 3000\u003Cbr \/\u003E[t-SNE] Computed conditional probabilities for sample 3000 \/ 3000\u003Cbr \/\u003E[t-SNE] Mean sigma: 480.474473\u003Cbr \/\u003E[t-SNE] KL divergence after 250 iterations with early exaggeration: 78.815109\u003Cbr \/\u003E[t-SNE] KL divergence after 1000 iterations: 1.261612\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-lPRGyin_9PE\/X79c4SklGoI\/AAAAAAAABBw\/elm0fQLZCCY6AYUXx9NxNacyWFMog9X3wCLcBGAsYHQ\/s640\/mnist-tsne.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"480\" data-original-width=\"640\" height=\"480\" src=\"https:\/\/1.bp.blogspot.com\/-lPRGyin_9PE\/X79c4SklGoI\/AAAAAAAABBw\/elm0fQLZCCY6AYUXx9NxNacyWFMog9X3wCLcBGAsYHQ\/w640-h480\/mnist-tsne.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EThe  plot shows a two-dimensional visualization of the MNIST data. The colors define  the target digits and their feature data location in 2D space.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn this tutorial, we've briefly learned how\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E to \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Ehow to fit and visualize data with TSNE in Python\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E The full source code is listed below.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.manifold\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E TSNE\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ekeras.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mnist\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E reshape\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eseaborn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esns\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Etsne \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E TSNE(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, verbose\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E tsne\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003Cbr \/\u003Edf \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E),\u003Cbr \/\u003E                data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"Iris data T-SNE projection\"\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003E(x_train, y_train), (_ , _) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eload_data()\u003Cbr \/\u003Ex_train \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E x_train[:\u003Cspan style=\"color: #40a070;\"\u003E3000\u003C\/span\u003E]\u003Cbr \/\u003Ey_train \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train[:\u003Cspan style=\"color: #40a070;\"\u003E3000\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003Ex_mnist \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E reshape(x_train, [x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Etsne \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E TSNE(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, verbose\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, random_state\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E123\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E tsne\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003Edf \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E),\u003Cbr \/\u003E                data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf)\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"MNIST data T-SNE projection\"\u003C\/span\u003E)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.manifold.TSNE.html#sklearn.manifold.TSNE\" target=\"_blank\"\u003EScikit-learn TSNE\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/6738925883255755405\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/11\/tsne-visualization-example-in-python.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/6738925883255755405"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/6738925883255755405"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/11\/tsne-visualization-example-in-python.html","title":"TSNE Visualization Example in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-sX7imo0tVoo\/X79ZmyxqSXI\/AAAAAAAABBk\/IyETgXAln_wF8KeNNULPs1o4EsSC10v-gCLcBGAsYHQ\/s72-w640-h480-c\/tsne-iris.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-2964502470751605566"},"published":{"$t":"2020-11-23T19:08:00.012-08:00"},"updated":{"$t":"2020-12-01T20:55:05.850-08:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"PCA"},{"scheme":"http://www.blogger.com/atom/ns#","term":"projection"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"Dimensionality Reduction with Sparse, Gaussian Random Projection and PCA in Python  "},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EDimensionality reducing is used when we deal with large datasets, which contain too many feature data, to increase the calculation speed, to reduce the model size, and to visualize the huge datasets in a better way. The purpose of this method is to keep the most important data while removing the most of the feature data.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn this to tutorial, we'll briefly learn how to reduce data dimensions with Sparse and Gaussian random projection and PCA methods in Python. \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003EThe Scikit-learn API provides the SparseRandomProjection, GaussianRandomProjection classes and PCA transformer function to reduce data dimension. After reading this tutorial, you'll learn\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E how to reduce dimensionality of the dataset by using those methods. The tutorial covers:\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EGaussian\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E random projection\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESparse random projection\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EPCA projection\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EMNIST data projection\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESource code listing \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EWe'll start by loading the required libraries and functions.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cp\u003E\u003C\/p\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.random_projection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E GaussianRandomProjection\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.random_projection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SparseRandomProjection\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.decomposition\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E PCA\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ekeras.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mnist\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E reshape\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eseaborn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esns\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003C!--more--\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data \u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EFirst,  we'll generate simple random data for this tutorial. Here, we'll generate a dataset with 1000 features by using make_regression() function. To apply the dimension methods into the real dataset, we also use MNIST handwritten digit database of Keras API. The MNIST is three-dimensional dataset, here we'll reshape it into the two-dimensional.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E \u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex, _ \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_regression(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E50000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1000\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E(50000, 1000)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E(x_train, y_train), (_ , _) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eload_data()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E(60000, 28, 28)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex_mnist \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E reshape(x_train, [x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E(60000, 784)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003EGaussian Random Projection\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EGaussian random method projects\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E the original input space on a randomly generated matrix to reduce dimensions. We'll define the model by using the GaussionRandomProjection class by setting the components numbers. Here, we'll shrink the feature data from 1000 to 200.\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Egrp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E GaussianRandomProjection(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E200\u003C\/span\u003E)\u003Cbr \/\u003Egrp_data \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E grp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(grp_data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E(50000, 200)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EAccording to you analysis and target data you can set your target components.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003ESparse Random Projection\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ESparse random method projects\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E the original input space using a sparse random matrix to reduce dimensions. We'll  define the model by using the SparseRandomProjection class by setting the  components numbers. Here, we'll shrink the feature data from 1000 to  200.\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Esrp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparseRandomProjection(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E200\u003C\/span\u003E)\u003Cbr \/\u003Esrp_data \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E srp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(srp_data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E(50000, 200)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EAccording to you analysis and target data you can set your target components.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003EPCA Projection\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll  define the model by using the PCA decomposition function by setting the  components numbers. Here, we'll shrink the feature data from 1000 to  200.\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E PCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E200\u003C\/span\u003E)\u003Cbr \/\u003Epca_data \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(pca_data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E(50000, 200)\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EAccording to you analysis and target data you can set your target components.\u003Cbr \/\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003EMNIST data projection \u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;  After learning the dimension reduce by using Gaussian, Sparse random and PCA methods, now we can apply those methods into the MNIST dataset. For test purpose, we'll set 2 into the components and apply the projection. \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# Sparse random prejection on 2 components\u003C\/span\u003E\u003Cbr \/\u003Esrp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparseRandomProjection(n_components \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E srp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003Cbr \/\u003Edf_srp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf_srp[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf_srp[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf_srp[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# Gaussian random prejection on 2 components\u003C\/span\u003E\u003Cbr \/\u003Egrp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E GaussianRandomProjection(n_components \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E grp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003Cbr \/\u003Edf_grp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf_grp[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf_grp[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf_grp[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# PCA prejection on 2 components\u003C\/span\u003E\u003Cbr \/\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E PCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003Cbr \/\u003Edf_pca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf_pca[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf_pca[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf_pca[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EWe'll check the about projected results in a plot by visualizing them.\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Efig, ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esubplots(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, figsize\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E20\u003C\/span\u003E))\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_srp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E), data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_srp, \u003Cbr \/\u003E                ax\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eax[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E])\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'Sparse random projection'\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_grp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E), data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_grp, \u003Cbr \/\u003E                ax\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eax[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'Gaussian random projection'\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_pca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E), data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_pca, \u003Cbr \/\u003E                ax\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eax[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"PCA projection\"\u003C\/span\u003E)\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-0JIR_fsDmD0\/X7x2DXBNvpI\/AAAAAAAABBY\/Oe3K8rsE2fomulnzXTRuzSuHLylEK7W9ACLcBGAsYHQ\/s1148\/projection.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"1148\" data-original-width=\"635\" src=\"https:\/\/1.bp.blogspot.com\/-0JIR_fsDmD0\/X7x2DXBNvpI\/AAAAAAAABBY\/Oe3K8rsE2fomulnzXTRuzSuHLylEK7W9ACLcBGAsYHQ\/s16000\/projection.png\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe plot shows the changed dimensions of the MNIST data. The colors define the target digits and their feature data location in a plot.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn this tutorial, we've briefly learned how\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E to reduce data dimensions with Sparse and Gaussian random projection approaches and PCA method in Python.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E The full source code is listed below.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.random_projection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E GaussianRandomProjection\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.random_projection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SparseRandomProjection\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.decomposition\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E PCA\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ekeras.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mnist\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E reshape\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eseaborn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esns\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epandas\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Epd\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Ex, _ \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_regression(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E50000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1000\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003E(x_train, y_train), (_ , _) \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mnist\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eload_data()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003Ex_mnist \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E reshape(x_train, [x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], x_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex_train\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x_minst\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Egrp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E GaussianRandomProjection(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E200\u003C\/span\u003E)\u003Cbr \/\u003Egrp_data \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E grp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(grp_data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Esrp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparseRandomProjection(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E200\u003C\/span\u003E)\u003Cbr \/\u003Esrp_data \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E srp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(srp_data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003Cbr \/\u003E\u003Cbr \/\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E PCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E200\u003C\/span\u003E)\u003Cbr \/\u003Epca_data \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(pca_data\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshape)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# Sparse random prejection on 2 components\u003C\/span\u003E\u003Cbr \/\u003Esrp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SparseRandomProjection(n_components \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E srp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003Cbr \/\u003Edf_srp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf_srp[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf_srp[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf_srp[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# Gaussian random prejection on 2 components\u003C\/span\u003E\u003Cbr \/\u003Egrp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E GaussianRandomProjection(n_components \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E grp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003Cbr \/\u003Edf_grp \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf_grp[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf_grp[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf_grp[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# PCA prejection on 2 components\u003C\/span\u003E\u003Cbr \/\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E PCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Ez \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x_mnist)\u003Cbr \/\u003Edf_pca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pd\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003EDataFrame()\u003Cbr \/\u003Edf_pca[\u003Cspan style=\"color: #4070a0;\"\u003E\"y\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y_train\u003Cbr \/\u003Edf_pca[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003Edf_pca[\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E] \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E z[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Efig, ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Esubplots(\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, figsize\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E20\u003C\/span\u003E))\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_srp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E), data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_srp, \u003Cbr \/\u003E                ax\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eax[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E])\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'Sparse random projection'\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_grp\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E), data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_grp, \u003Cbr \/\u003E                ax\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eax[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'Gaussian random projection'\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatterplot(x\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-1\"\u003C\/span\u003E, y\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"comp-2\"\u003C\/span\u003E, hue\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_pca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ey\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etolist(),\u003Cbr \/\u003E                palette\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Esns\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecolor_palette(\u003Cspan style=\"color: #4070a0;\"\u003E\"hls\"\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E), data\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Edf_pca, \u003Cbr \/\u003E                ax\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Eax[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eset(title\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"PCA projection\"\u003C\/span\u003E)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.random_projection.SparseRandomProjection.html#sklearn.random_projection.SparseRandomProjection\" target=\"_blank\"\u003ESparse random projection\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.random_projection.GaussianRandomProjection.html#sklearn.random_projection.GaussianRandomProjection\" target=\"_blank\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EGaussian random projection\u003C\/span\u003E\u003C\/span\u003E\u003C\/a\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.decomposition.PCA.html?highlight=pca#sklearn.decomposition.PCA\" target=\"_blank\"\u003EPCA\u003C\/a\u003E\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/2964502470751605566\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/11\/dimension-reduce-with-sparse-gaussian-and-pca-methods.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/2964502470751605566"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/2964502470751605566"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/11\/dimension-reduce-with-sparse-gaussian-and-pca-methods.html","title":"Dimensionality Reduction with Sparse, Gaussian Random Projection and PCA in Python  "}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-0JIR_fsDmD0\/X7x2DXBNvpI\/AAAAAAAABBY\/Oe3K8rsE2fomulnzXTRuzSuHLylEK7W9ACLcBGAsYHQ\/s72-c\/projection.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-7862885788226892704"},"published":{"$t":"2020-11-16T20:34:00.006-08:00"},"updated":{"$t":"2020-11-16T22:06:50.011-08:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"curve-fitting"},{"scheme":"http://www.blogger.com/atom/ns#","term":"R"}],"title":{"type":"text","$t":"Curve Fitting Example With Nonlinear Least Squares in R"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EThe Nonlinear Least Squares (NLS) estimate the parameters of a nonlinear model. R provides 'nls' function to fit the nonlinear data. The 'nls' tries to find out the best parameters of a given function by iterating the variables.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EIn this tutorial, we'll briefly learn how to fit nonlinear data by using the 'nls' function in R.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E The 'nls' comes in a 'stats' base package. The  tutorial covers:\u003Cbr \/\u003E\u003C\/span\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EFitting the model and prediction\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cfont\u003E\u003Cspan\u003E\u003C\/span\u003E\u003C\/font\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cfont\u003E\u003C\/font\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cp\u003E\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cfont\u003E\u0026nbsp;\u003C\/font\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cp\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cfont\u003EPreparing the data\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  We'll start by generating simple test data for this tutorial as below. Here, I'll  generate x input and y output data. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Ep = function(x) x^3+2*x^2+5\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Ex = seq(-0.99, 1, by = .01)\u003Cbr \/\u003Ey = p(x) + runif(200)\u003Cbr \/\u003Edf = data.frame(x = x, y = y)\u003Cbr \/\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Ehead(df)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E      x        y\u003Cbr \/\u003E1 -0.99 6.183018\u003Cbr \/\u003E2 -0.98 6.611669\u003Cbr \/\u003E3 -0.97 6.762615\u003Cbr \/\u003E4 -0.96 6.594278\u003Cbr \/\u003E5 -0.95 5.990637\u003Cbr \/\u003E6 -0.94 6.048369\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003Cspan\u003E\u003Cspan\u003ENext,  we'll define multiple functions to fit the data with 'nls' function and compare their differences in fitting. You can also add or change the equations to get the best fitting parameters for your data.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EWe use below equations as the fitting functions.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003Ey = ax^2 + bx + c\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/i\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cb\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ey = ax^3 + bx^2 + c\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/i\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cb\u003Ey = a*exp(bx^2) + c\u003C\/b\u003E\u003C\/span\u003E\u003C\/i\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cfont\u003EFitting the model and prediction\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  We'll define the model by using the nls() function providing a fitting function, data, and start vector.\u0026nbsp; and fit on train data. We run the function to train the model with included data.  You can check the summary of the model by using the print() function.\u0026nbsp; \u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Efit = nls(y~a*x^2+b*x, data = df, start(a=0, b=0))\u003Cbr \/\u003Eprint(fit)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"color: #666666;\"\u003ENonlinear regression model\u003Cbr \/\u003E  model: y ~ a * x^2 + b * x + c\u003Cbr \/\u003E   data: df\u003Cbr \/\u003E     a      b      c \u003Cbr \/\u003E1.9545 0.5926 5.5061 \u003Cbr \/\u003E residual sum-of-squares: 20.39\u003Cbr \/\u003E\u003Cbr \/\u003ENumber of iterations to convergence: 1 \u003Cbr \/\u003EAchieved convergence tolerance: 4.515e-09\u003Cbr \/\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll predict the x data and visualize the result in a plot to check visually.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Epred = predict(fit, x)\u003Cbr \/\u003Eplot(x, y, pch = 20)\u003Cbr \/\u003Elines(x, pred, lwd = 3, col = \"blue\")\u003Cbr \/\u003Elegend(\"topleft\", legend = c(\"y~a*x^2+b*x\"), fill = c(\"blue\"))\u003Cbr \/\u003Egrid()\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-sdjQ8niYrdk\/X7M_7r8aLwI\/AAAAAAAABA4\/dswGpncK8mcs378HbaVWljF9SHGflDlVgCLcBGAsYHQ\/s1322\/nls_fit.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"1056\" data-original-width=\"1322\" height=\"512\" src=\"https:\/\/1.bp.blogspot.com\/-sdjQ8niYrdk\/X7M_7r8aLwI\/AAAAAAAABA4\/dswGpncK8mcs378HbaVWljF9SHGflDlVgCLcBGAsYHQ\/w640-h512\/nls_fit.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll apply the above function to fit the target data and check their differences in fitting. We'll fit each function on test data, predict x data, and visualize them in a plot. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Efit1 = nls(y~a*x^2+b*x+c, data=df, start=list(a=.5, b=0, c=1))\u003Cbr \/\u003Efit2 = nls(y~a*x^3+b*x^2+c, data=df, start=list(a=.1, b=.1, c=0))\u003Cbr \/\u003Efit3 = nls(y~a*exp(b*x^2)+c, data=df, start=list(a=1, b=1, c=0))\u003Cbr \/\u003E\u003Cbr \/\u003Eplot(x=df$x, y=df$y, pch=20, col=\"darkgray\", main = \"NLS fitting Example\")\u003Cbr \/\u003Elines(df$x, predict(fit1, df), type=\"l\", col=\"red\", lwd=2)\u003Cbr \/\u003Elines(df$x, predict(fit2, df), type=\"l\", col=\"green\", lwd=2)\u003Cbr \/\u003Elines(df$x, predict(fit3, df), type=\"l\", col=\"blue\", lwd=2)\u003Cbr \/\u003E\u003Cbr \/\u003Elegend(\"topleft\",  legend = c(\"y~ax^2+bx+c\", \"y~ax^3+bx^2+c\", \"y~a*exp(bx^2)+c\"), \u003Cbr \/\u003E       fill = c(\"red\", \"green\",\"blue\"), col = 2:3,  adj = c(0, 0.6))\u003Cbr \/\u003Egrid()\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-18fPQoOkNlM\/X7NQ16z4AiI\/AAAAAAAABBI\/5vJe454taGszbbacfdVeow-EV-ma5nwoQCLcBGAsYHQ\/s1322\/nls_fit_compare.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"1056\" data-original-width=\"1322\" height=\"512\" src=\"https:\/\/1.bp.blogspot.com\/-18fPQoOkNlM\/X7NQ16z4AiI\/AAAAAAAABBI\/5vJe454taGszbbacfdVeow-EV-ma5nwoQCLcBGAsYHQ\/w640-h512\/nls_fit_compare.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe plot shows the fitted results of each function. Based on the results, we can select the best function that works well with our target data.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EIn this tutorial, we've learned how to fit the target data with 'nls' nonlinear least squares function in R. The full source code is listed below.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cfont\u003ESource code listing\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cb\u003E\u003Cfont\u003E\u003Cbr \/\u003E\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Ep = function(x) x^3+2*x^2+5\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Ex = seq(-0.99, 1, by = .01)\u003Cbr \/\u003Ey = peq(x) + runif(200)\u003Cbr \/\u003Edf = data.frame(x = x, y = y)\u003Cbr \/\u003Ehead(df)\u003Cbr \/\u003E\u003Cbr \/\u003Efit = nls(y~a*x^2+b*x, data = df, start(a=0, b=0))\u003Cbr \/\u003Eprint(fit)\u003Cbr \/\u003E\u003Cbr \/\u003Epred = predict(fit, x)\u003Cbr \/\u003Eplot(x, y, pch = 20)\u003Cbr \/\u003Elines(x, pred, lwd = 3, col = \"blue\")\u003Cbr \/\u003Elegend(\"topleft\", legend = c(\"y~a*x^2+b*x\"), fill = c(\"blue\"))\u003Cbr \/\u003Egrid()\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Efit1 = nls(y~a*x^2+b*x+c, data=df, start=list(a=.5, b=0, c=1))\u003Cbr \/\u003Efit2 = nls(y~a*x^3+b*x^2+c, data=df, start=list(a=.1, b=.1, c=0))\u003Cbr \/\u003Efit3 = nls(y~a*exp(b*x^2)+c, data=df, start=list(a=1, b=1, c=0))\u003Cbr \/\u003E\u003Cbr \/\u003Eplot(x=df$x, y=df$y, pch=20, col=\"darkgray\", main = \"NLS fitting Example\")\u003Cbr \/\u003Elines(df$x, predict(fit1, df), type=\"l\", col=\"red\", lwd=2)\u003Cbr \/\u003Elines(df$x, predict(fit2, df), type=\"l\", col=\"green\", lwd=2)\u003Cbr \/\u003Elines(df$x, predict(fit3, df), type=\"l\", col=\"blue\", lwd=2)\u003Cbr \/\u003E\u003Cbr \/\u003Elegend(\"topleft\",  legend = c(\"y~ax^2+bx+c\", \"y~ax^3+bx^2+c\", \"y~a*exp(b*x^2)+c\"), \u003Cbr \/\u003E       fill = c(\"red\", \"green\",\"blue\"), col = 2:3,  adj = c(0, 0.6))\u003Cbr \/\u003Egrid()\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003C\/div\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/7862885788226892704\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/11\/curve-fitting-with-nonlinear-least-squares-in-r.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/7862885788226892704"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/7862885788226892704"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/11\/curve-fitting-with-nonlinear-least-squares-in-r.html","title":"Curve Fitting Example With Nonlinear Least Squares in R"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-sdjQ8niYrdk\/X7M_7r8aLwI\/AAAAAAAABA4\/dswGpncK8mcs378HbaVWljF9SHGflDlVgCLcBGAsYHQ\/s72-w640-h512-c\/nls_fit.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-1918887078846991548"},"published":{"$t":"2020-11-10T01:56:00.007-08:00"},"updated":{"$t":"2020-11-10T15:37:49.557-08:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"PCA"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"Principal Component Analysis (PCA) Example in Python"},"content":{"type":"html","$t":"\u003Cp style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-ofrJrl0jehA\/X6phyeOCX7I\/AAAAAAAABAs\/aO0upzlyyNYE0Gn0hvxjdpgRYzYEqV_NACLcBGAsYHQ\/s1048\/sepal%2Bpca.png\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"682\" data-original-width=\"1048\" height=\"130\" src=\"https:\/\/1.bp.blogspot.com\/-ofrJrl0jehA\/X6phyeOCX7I\/AAAAAAAABAs\/aO0upzlyyNYE0Gn0hvxjdpgRYzYEqV_NACLcBGAsYHQ\/w200-h130\/sepal%2Bpca.png\" width=\"200\" \/\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EPrincipal Component Analysis (PCA) is an \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Eunsupervised learning approach of the feature data by changing the dimensions and reducing the variables in a dataset. No\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E label or response data is considered in this analysis.\u003Cspan\u003E The Scikit-learn API provides the PCA transformer function \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003Ethat learns components of data and projects input data on learned components.\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn this tutorial, we'll briefly learn how to do principle components analysis by using the PCA function, change data dimensions, and visualize the projected data in Python. The tutorial covers:\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EExtracting principal components \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EDimension changing and visualizing\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003EIris PCA Example\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003ESource code listing\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cp style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EWe'll start by loading the required libraries and functions.\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E \u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.decomposition\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E PCA\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enp\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EExtracting principal components \u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EFirst,  we'll generate simple random data for this tutorial. Here, we'll generate 2 feature data and visualize it in a plot.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E \u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003En \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E100\u003C\/span\u003E\u003Cbr \/\u003Ex1 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E [i\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efor\u003C\/span\u003E i \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ein\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(n)]\u003Cbr \/\u003Ex2 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Erandom\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Euniform(\u003Cspan style=\"color: #666666;\"\u003E-\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E, n)\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003E x1\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Earray([x1, x2])\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ET\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E],x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-4PDqP5UjETA\/X6pfwTe4E0I\/AAAAAAAABAA\/L22qjc_CTDM8MW5ycUfxBgrd3u5vgBTTQCLcBGAsYHQ\/s1048\/data.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"682\" data-original-width=\"1048\" height=\"260\" src=\"https:\/\/1.bp.blogspot.com\/-4PDqP5UjETA\/X6pfwTe4E0I\/AAAAAAAABAA\/L22qjc_CTDM8MW5ycUfxBgrd3u5vgBTTQCLcBGAsYHQ\/w400-h260\/data.png\" width=\"400\" \/\u003E\u003C\/a\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EThen, we'll apply PCA function. First, we'll define the function with PCA() by setting 2 into the n_components parameter then fit it on x data. After the fitting, we can get component, mean, and covariance data.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Epc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E PCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Epc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"components:\"\u003C\/span\u003E, pc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecomponents_)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"mean:      \"\u003C\/span\u003E, pc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Emean_)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"covariance:\"\u003C\/span\u003E, pc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_covariance())\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ecomponents: [[ 0.99640834 -0.08467831]\u003Cbr \/\u003E             [ 0.08467831  0.99640834]]\u003Cbr \/\u003Emean:       [5.84333333 3.05733333]\u003Cbr \/\u003Ecovariance: [[ 0.68569351 -0.042434  ]\u003Cbr \/\u003E             [-0.042434    0.18997942]]\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003EDimension changing and visualizing\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ETo project target data by using principal components, we use fit_trasform() method and visualize both original and projected data in a plot.\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E PCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E],x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], label \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #4070a0;\"\u003E\"Original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(pca[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], pca[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], label \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #4070a0;\"\u003E\"Projected\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"best\"\u003C\/span\u003E, fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-ITumN_tSw3M\/X6pf1wUWiaI\/AAAAAAAABAI\/GtVm_Jo9BD0vsn2mflmvMLtpnyu5W0hAwCLcBGAsYHQ\/s1048\/pca%2Bprojection.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"682\" data-original-width=\"1048\" height=\"260\" src=\"https:\/\/1.bp.blogspot.com\/-ITumN_tSw3M\/X6pf1wUWiaI\/AAAAAAAABAI\/GtVm_Jo9BD0vsn2mflmvMLtpnyu5W0hAwCLcBGAsYHQ\/w400-h260\/pca%2Bprojection.png\" width=\"400\" \/\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe plot shows the changed dimensions and gives the idea about dimension changing concept. The dimension of the data is centered around (0, 0).\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003EIris PCA example \u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; Next, we'll do simple test with Iris dataset. To make it simple, we use only 'Sepal' width and length data. First, we'll load Iris dataset,\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E extract target parts, and visualize it in a plot.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata[:, (\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)]\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003Efeature \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Elabels \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget_names\u003Cbr \/\u003E\u003Cbr \/\u003Epcadata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ehstack((x, y\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ereshape(\u003Cspan style=\"color: #40a070;\"\u003E150\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)))\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efor\u003C\/span\u003E p1, p2, t \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ein\u003C\/span\u003E pcadata:\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eif\u003C\/span\u003E(t\u003Cspan style=\"color: #666666;\"\u003E==\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E):\u003Cbr \/\u003E        setosa \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'r'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eelif\u003C\/span\u003E(t\u003Cspan style=\"color: #666666;\"\u003E==\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E):\u003Cbr \/\u003E        versicolor \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'g'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eelse\u003C\/span\u003E:\u003Cbr \/\u003E        virginica \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'b'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend((setosa, versicolor, virginica), \u003Cbr \/\u003E           labels, loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(feature[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(feature[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-tRmKUyWsCPw\/X6pgh1ufbVI\/AAAAAAAABAY\/Zq5cA_oD9qsc1CMOLyl9bHX1jEHZl5g1QCLcBGAsYHQ\/s1048\/row%2Bdata.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"682\" data-original-width=\"1048\" height=\"260\" src=\"https:\/\/1.bp.blogspot.com\/-tRmKUyWsCPw\/X6pgh1ufbVI\/AAAAAAAABAY\/Zq5cA_oD9qsc1CMOLyl9bHX1jEHZl5g1QCLcBGAsYHQ\/w400-h260\/row%2Bdata.png\" width=\"400\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll project data with PCA function and visualize it in a plot.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E PCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003Cbr \/\u003Epcadata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ehstack((pca, y\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ereshape(\u003Cspan style=\"color: #40a070;\"\u003E150\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)))\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efor\u003C\/span\u003E p1, p2, t \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ein\u003C\/span\u003E pcadata:\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eif\u003C\/span\u003E(t\u003Cspan style=\"color: #666666;\"\u003E==\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E):\u003Cbr \/\u003E        setosa \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'r'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eelif\u003C\/span\u003E(t\u003Cspan style=\"color: #666666;\"\u003E==\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E):\u003Cbr \/\u003E        versicolor \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'g'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eelse\u003C\/span\u003E:\u003Cbr \/\u003E        virginica \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'b'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend((setosa, versicolor, virginica), \u003Cbr \/\u003E           labels, loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(feature[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(feature[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-2k_J2OPOm6k\/X6pgoXqrH4I\/AAAAAAAABAc\/eMAbMs4UVMM1EdyJkecrQ0G4Plzy7SMjwCLcBGAsYHQ\/s1048\/sepal%2Bpca.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"682\" data-original-width=\"1048\" height=\"260\" src=\"https:\/\/1.bp.blogspot.com\/-2k_J2OPOm6k\/X6pgoXqrH4I\/AAAAAAAABAc\/eMAbMs4UVMM1EdyJkecrQ0G4Plzy7SMjwCLcBGAsYHQ\/w400-h260\/sepal%2Bpca.png\" width=\"400\" \/\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EThe plot shows the changed dimensions of the data.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn this tutorial, we've briefly learned how to use PCA and changing dimension of the feature data in Python.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E The full source code is listed below.\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.decomposition\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E PCA\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enp\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003En \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #40a070;\"\u003E100\u003C\/span\u003E\u003Cbr \/\u003Ex1 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E [i\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efor\u003C\/span\u003E i \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ein\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(n)]\u003Cbr \/\u003Ex2 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Erandom\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Euniform(\u003Cspan style=\"color: #666666;\"\u003E-\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E, n)\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003E x1\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Earray([x1, x2])\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003ET\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E],x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003Epc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E PCA(n_components\u003Cspan style=\"color: #666666;\"\u003E = \u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Epc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(x)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"components:\"\u003C\/span\u003E, pc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ecomponents_)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"mean:      \"\u003C\/span\u003E, pc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Emean_)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"covariance:\"\u003C\/span\u003E, pc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eget_covariance())\u003Cbr \/\u003E\u003Cbr \/\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E PCA(n_components\u003Cspan style=\"color: #666666;\"\u003E = \u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(x[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E],x[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], label \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #4070a0;\"\u003E\"Original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(pca[:,\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], pca[:,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], label \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #4070a0;\"\u003E\"After PCA\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"best\"\u003C\/span\u003E, fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# Iris PCA example\u003C\/span\u003E\u003Cbr \/\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata[:, (\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)]\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003Efeature \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efeature_names[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Elabels \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget_names\u003Cbr \/\u003E\u003Cbr \/\u003Epcadata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ehstack((x, y\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ereshape(\u003Cspan style=\"color: #40a070;\"\u003E150\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)))\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efor\u003C\/span\u003E p1, p2, t \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ein\u003C\/span\u003E pcadata:\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eif\u003C\/span\u003E(t\u003Cspan style=\"color: #666666;\"\u003E==\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E):\u003Cbr \/\u003E        setosa \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'r'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eelif\u003C\/span\u003E(t\u003Cspan style=\"color: #666666;\"\u003E==\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E):\u003Cbr \/\u003E        versicolor \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'g'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eelse\u003C\/span\u003E:\u003Cbr \/\u003E        virginica \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'b'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend((setosa, versicolor, virginica), \u003Cbr \/\u003E           labels, loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(feature[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(feature[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E PCA(n_components\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E)\u003Cbr \/\u003Epca \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E pca\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit_transform(x)\u003Cbr \/\u003Epcadata \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E np\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ehstack((pca, y\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Ereshape(\u003Cspan style=\"color: #40a070;\"\u003E150\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)))\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efor\u003C\/span\u003E p1, p2, t \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ein\u003C\/span\u003E pcadata:\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eif\u003C\/span\u003E(t\u003Cspan style=\"color: #666666;\"\u003E==\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E):\u003Cbr \/\u003E        setosa \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'r'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eelif\u003C\/span\u003E(t\u003Cspan style=\"color: #666666;\"\u003E==\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E):\u003Cbr \/\u003E        versicolor \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'g'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eelse\u003C\/span\u003E:\u003Cbr \/\u003E        virginica \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E plt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escatter(p1, p2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'b'\u003C\/span\u003E)\u003Cbr \/\u003E    \u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend((setosa, versicolor, virginica), \u003Cbr \/\u003E           labels, loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(feature[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(feature[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E])\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.decomposition.PCA.html\" target=\"_blank\"\u003EScikit learn AP\u003C\/a\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/1918887078846991548\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/pca-analysis-in-python.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/1918887078846991548"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/1918887078846991548"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/pca-analysis-in-python.html","title":"Principal Component Analysis (PCA) Example in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-ofrJrl0jehA\/X6phyeOCX7I\/AAAAAAAABAs\/aO0upzlyyNYE0Gn0hvxjdpgRYzYEqV_NACLcBGAsYHQ\/s72-w200-h130-c\/sepal%2Bpca.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-2321655488935449567"},"published":{"$t":"2020-10-27T23:07:00.002-07:00"},"updated":{"$t":"2020-10-28T00:33:02.597-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"knn"},{"scheme":"http://www.blogger.com/atom/ns#","term":"R"},{"scheme":"http://www.blogger.com/atom/ns#","term":"regression"}],"title":{"type":"text","$t":"K-Nearest Neighbor Regression Example in R"},"content":{"type":"html","$t":"\u003Cp\u003E\u0026nbsp;\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EK-Nearest Neighbor (KNN) is a supervised machine learning algorithms that can be used for classification and regression problems. In this algorithm, k is a constant defined by user and nearest neighbors distances vector is calculated by using it.\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EThe 'caret'  package provides 'knnreg' function to apply KNN for regression problems. \u003C\/span\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EIn this tutorial, we'll briefly learn how to fit and predict regression data by using 'knnreg' function in R.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E The  tutorial covers:\u003Cbr \/\u003E\u003C\/span\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EFitting the model and prediction\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EAccuracy checking\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll start by loading the required libraries.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Elibrary(caret)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cfont\u003EPreparing the data\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  We use Boston house-price dataset as a target regression data in this  tutorial. After loading the dataset, first, we'll split them into the  train and test parts, and extract x-input and y-label parts. Here, I'll  extract 15 percent of the dataset as test data. It is better to scale x part of data to improve the accuracy. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Eboston = MASS::Boston\u003Cbr \/\u003Estr(boston)\u003Cbr \/\u003E\u003Cbr \/\u003Eset.seed(12)\u003Cbr \/\u003E\u003Cbr \/\u003Eindexes = createDataPartition(boston$medv, p = .85, list = F)\u003Cbr \/\u003Etrain = boston[indexes, ]\u003Cbr \/\u003Etest = boston[-indexes, ]\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003Etrain_x = train[, -14]\u003Cbr \/\u003Etrain_x = scale(train_x)[,]\u003Cbr \/\u003Etrain_y = train[,14]\u003Cbr \/\u003E\u003Cbr \/\u003Etest_x = test[, -14]\u003Cbr \/\u003Etest_x = scale(test[,-14])[,]\u003Cbr \/\u003Etest_y = test[,14]\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cfont\u003EFitting the model and prediction\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  We'll define the model by using the knnreg() function of the 'caret' package and fit on train data.\u0026nbsp; The  calling the function is enough to train the model with included data.  \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Eknnmodel = knnreg(train_x, train_y)\u003Cbr \/\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Estr(knnmodel)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"color: #666666;\"\u003EList of 3\u003Cbr \/\u003E $ learn  :List of 2\u003Cbr \/\u003E  ..$ y: num [1:458] 24 21.6 34.7 33.4 36.2 28.7 16.5 18.9 15 18.9 ...\u003Cbr \/\u003E  ..$ X: num [1:458, 1:13] -0.418 -0.416 -0.416 -0.416 -0.411 ...\u003Cbr \/\u003E  .. ..- attr(*, \"dimnames\")=List of 2\u003Cbr \/\u003E  .. .. ..$ : chr [1:458] \"1\" \"2\" \"3\" \"4\" ...\u003Cbr \/\u003E  .. .. ..$ : chr [1:13] \"crim\" \"zn\" \"indus\" \"chas\" ...\u003Cbr \/\u003E $ k      : num 5\u003Cbr \/\u003E $ theDots: list()\u003Cbr \/\u003E - attr(*, \"class\")= chr \"knnreg\"\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003ENow, we can predict the x test data with the trained model. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Epred_y = predict(knnmodel, data.frame(test_x))\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;;\"\u003E\u003Cb\u003E\u003Cfont\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003EAccuracy checking\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll check the prediction accuracy with MSE, MAE, and RMSE metrics. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Eprint(data.frame(test_y, pred_y))\u003Cbr \/\u003E\u003Cbr \/\u003Emse = mean((test_y - pred_y)^2)\u003Cbr \/\u003Emae = caret::MAE(test_y, pred_y)\u003Cbr \/\u003Ermse = caret::RMSE(test_y, pred_y)\u003Cbr \/\u003E\u003Cbr \/\u003Ecat(\"MSE: \", mse, \"MAE: \", mae, \" RMSE: \", rmse)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"color: #444444;\"\u003EMSE:  27.31944 MAE:  3.472917  RMSE:  5.2268\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: white; border: medium none; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003EFinally, we'll visualize original test and predicted data in a plot.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Ex = 1:length(test_y)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003Eplot(x, test_y, col = \"red\", type = \"l\", lwd=2,\u003Cbr \/\u003E     main = \"Boston housing test data prediction\")\u003Cbr \/\u003Elines(x, pred_y, col = \"blue\", lwd=2)\u003Cbr \/\u003Elegend(\"topright\",  legend = c(\"original-medv\", \"predicted-medv\"), \u003Cbr \/\u003E       fill = c(\"red\", \"blue\"), col = 2:3,  adj = c(0, 0.6))\u003Cbr \/\u003Egrid()\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-X0RO-30Z9g4\/X5kJwtbKXDI\/AAAAAAAAA_w\/dcfUALX0NGw6JWM53ZHbtVcXIOG0J4yZACLcBGAsYHQ\/s1478\/knnreg.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"978\" data-original-width=\"1478\" height=\"424\" src=\"https:\/\/1.bp.blogspot.com\/-X0RO-30Z9g4\/X5kJwtbKXDI\/AAAAAAAAA_w\/dcfUALX0NGw6JWM53ZHbtVcXIOG0J4yZACLcBGAsYHQ\/w640-h424\/knnreg.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  In this tutorial, we've learned how to fit and predict regression data  with 'knnreg' function of the 'caret' package in R. The full source code is listed below.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cfont\u003ESource code listing\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cb\u003E\u003Cfont\u003E\u003Cbr \/\u003E\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Elibrary(caret)\u003Cbr \/\u003E\u003Cbr \/\u003Eboston = MASS::Boston\u003Cbr \/\u003Eset.seed(12)\u003Cbr \/\u003Eindexes = createDataPartition(boston$medv, p = .9, list = F)\u003Cbr \/\u003Etrain = boston[indexes, ]\u003Cbr \/\u003Etest = boston[-indexes, ]\u003Cbr \/\u003E\u003Cbr \/\u003Etrain_x = train[, -14]\u003Cbr \/\u003Etrain_x = scale(train_x)[,]\u003Cbr \/\u003Etrain_y = train[,14]\u003Cbr \/\u003E\u003Cbr \/\u003Etest_x = test[, -14]\u003Cbr \/\u003Etest_x = scale(test[,-14])[,]\u003Cbr \/\u003Etest_y = test[,14]\u003Cbr \/\u003E\u003Cbr \/\u003Eknnmodel = knnreg(train_x, train_y)\u003Cbr \/\u003Estr(knnmodel)\u003Cbr \/\u003E\u003Cbr \/\u003Epred_y = predict(knnmodel, data.frame(test_x))\u003Cbr \/\u003E\u003Cbr \/\u003Emse = mean((test_y - pred_y)^2)\u003Cbr \/\u003Emae = caret::MAE(test_y, pred_y)\u003Cbr \/\u003Ermse = caret::RMSE(test_y, pred_y)\u003Cbr \/\u003E\u003Cbr \/\u003Ecat(\"MSE: \", mse, \"MAE: \", mae, \" RMSE: \", rmse)\u003Cbr \/\u003E\u003Cbr \/\u003Ex = 1:length(test_y)\u003Cbr \/\u003Eplot(x, test_y, col = \"red\", type = \"l\", lwd=2,\u003Cbr \/\u003E     main = \"Boston housing test data prediction\")\u003Cbr \/\u003Elines(x, pred_y, col = \"blue\", lwd=2)\u003Cbr \/\u003Elegend(\"topright\",  legend = c(\"original-medv\", \"predicted-medv\"), \u003Cbr \/\u003E       fill = c(\"red\", \"blue\"), col = 2:3,  adj = c(0, 0.6))\u003Cbr \/\u003Egrid()\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cbr \/\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/2321655488935449567\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/knn-regresion-example-in-r.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/2321655488935449567"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/2321655488935449567"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/knn-regresion-example-in-r.html","title":"K-Nearest Neighbor Regression Example in R"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-X0RO-30Z9g4\/X5kJwtbKXDI\/AAAAAAAAA_w\/dcfUALX0NGw6JWM53ZHbtVcXIOG0J4yZACLcBGAsYHQ\/s72-w640-h424-c\/knnreg.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-8086974535824259011"},"published":{"$t":"2020-10-21T02:51:00.004-07:00"},"updated":{"$t":"2020-10-26T22:54:38.570-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"python"},{"scheme":"http://www.blogger.com/atom/ns#","term":"regression"}],"title":{"type":"text","$t":"Regression Example With ARDRegression in Python"},"content":{"type":"html","$t":"\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EAutomatic Relevance Determination (ARD) is based on Bayesian inference method.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E Scikit-learn API provides ARDRegression class to fit the regression model by\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E using ARD method. The ARDRegression considers the model weights as a Gaussian distributed and\u0026nbsp; estimates the lambda and alpha parameters through the \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Eiteration.\u003C\/span\u003E\u003Cbr \/\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn this tutorial, we'll briefly learn how to fit and predict regression data by using ARDRegression class in Python. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll apply the model for a randomly generated  regression data and Boston housing price dataset to check the performance. The  tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ETraining the model\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPredicting and accuracy check\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EBoston housing dataset prediction \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.linear_model\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003EARDRegression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mean_squared_error\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E train_test_split\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.preprocessing\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E scale\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E set_config\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; First,  we'll generate random regression data with make_regression()  function. The dataset contains 10 features and 5000 samples. \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_regression(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E5000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(y[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[[ 1.773  2.534  0.693 -1.11   1.492  0.631 -0.577  0.085 -1.308  1.024]\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[ 1.953 -1.362  1.294  1.025  0.463 -0.485 -1.849  1.858  0.483 -0.52 ]]\u003Cbr \/\u003E[120.105 262.69 ]\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003ETo improve the model accuracy we'll scale both x and y data then, split  them into train and test parts. Here, we'll extract 10 percent of the  samples as test data.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Etrain_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.10\u003C\/span\u003E)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ETraining the model\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; Next, we'll define the regressor model by using the ARDRegression\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Eclass. Here, we can use default parameters of the ARDRegression\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E class. The default values can be seen in below.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eset_config(print_changed_only\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eardr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ARDRegression()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(ardr)\u003Cbr \/\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EARDRegression(alpha_1=1e-06, alpha_2=1e-06, compute_score=False, copy_X=True,\u003Cbr \/\u003E              fit_intercept=True, lambda_1=1e-06, lambda_2=1e-06, n_iter=300,\u003Cbr \/\u003E              normalize=False, threshold_lambda=10000.0, tol=0.001,\u003Cbr \/\u003E              verbose=False)\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EThen, we'll fit the model on train data and check the model accuracy score. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Edtr.fit(xtrain, ytrain)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ER-squared: 1.0\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003EPredicting and accuracy check\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENow, we can predict the test data by using the trained model. We can  check the accuracy of predicted data by using MSE and RMSE metrics.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Eardr\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EMSE:  1.0459020366671401e-22\u003Cbr \/\u003ERMSE:  5.2295101833357005e-23\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EFinally, we'll visualize the original and predicted data in a plot. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1.1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"y-test and y-predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E  \u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ERunning the above code provides a plot that shows the the original and predicted test data.\u003Cbr \/\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003EBoston housing dataset prediction\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll  apply the same method we've learned above to the Boston housing price  regression dataset. We'll load it by using load_boston() function, scale  and split into the train and test parts. Then, we'll define model by  changing some of the parameter values, check training accuracy, and  predict test data. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston housing dataset prediction.\"\u003C\/span\u003E)\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eardr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ARDRegression()\u003Cbr \/\u003Eardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow() \u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EBoston housing dataset prediction.\u003Cbr \/\u003ER-squared: 0.730951555514822\u003Cbr \/\u003EMSE:  0.1362112343271604\u003Cbr \/\u003ERMSE:  0.0681056171635802\u003C\/span\u003E\u0026nbsp;\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E  \u003Cdiv style=\"text-align: center;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-y1Ymbr2Fu98\/X5AD0PAtrRI\/AAAAAAAAA_c\/j4ZgjEDeoZcFsZjzc_6MNwaHvYJmagdHwCLcBGAsYHQ\/s1122\/ard_regression.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"896\" data-original-width=\"1122\" height=\"512\" src=\"https:\/\/1.bp.blogspot.com\/-y1Ymbr2Fu98\/X5AD0PAtrRI\/AAAAAAAAA_c\/j4ZgjEDeoZcFsZjzc_6MNwaHvYJmagdHwCLcBGAsYHQ\/w640-h512\/ard_regression.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; In this tutorial, we've briefly learned how to fit and predict regression data by using  Scikit-learn API's ARDRegression\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E class in Python. The full  source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.linear_model\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E ARDRegression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mean_squared_error\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E train_test_split\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.preprocessing\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E scale\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E set_config\u003Cbr \/\u003E\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_regression(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E5000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(y[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eset_config(print_changed_only\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u003Cbr \/\u003Eardr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ARDRegression()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(ardr)\u003Cbr \/\u003E\u003Cbr \/\u003Eardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1.1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"y-test and y-predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston housing dataset prediction.\"\u003C\/span\u003E)\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eardr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ARDRegression()\u003Cbr \/\u003Eardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E ardr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow() \u0026nbsp;\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.linear_model.ARDRegression.html#sklearn.linear_model.ARDRegression\" target=\"_blank\"\u003EScikit learn API\u003C\/a\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/8086974535824259011\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/regression-example-with-ardregression-in-python.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/8086974535824259011"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/8086974535824259011"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/regression-example-with-ardregression-in-python.html","title":"Regression Example With ARDRegression in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-y1Ymbr2Fu98\/X5AD0PAtrRI\/AAAAAAAAA_c\/j4ZgjEDeoZcFsZjzc_6MNwaHvYJmagdHwCLcBGAsYHQ\/s72-w640-h512-c\/ard_regression.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-8212610442666814886"},"published":{"$t":"2020-10-13T18:57:00.008-07:00"},"updated":{"$t":"2020-10-13T19:07:44.681-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"decision-tree"},{"scheme":"http://www.blogger.com/atom/ns#","term":"R"},{"scheme":"http://www.blogger.com/atom/ns#","term":"regression"}],"title":{"type":"text","$t":"Regression Example With RPART Tree Model in R"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EDecision trees can be implemented by using the 'rpart' package in R. The 'rpart' package extends to Recursive Partitioning and Regression Trees which applies the tree-based model for regression and classification problems. \u003C\/span\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EIn this tutorial, we'll briefly learn how to fit and predict regression data by using 'rpart' function in R.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E The  tutorial covers:\u003Cbr \/\u003E\u003C\/span\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EFitting the model and prediction\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EAccuracy checking\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll start by loading the required libraries.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Elibrary(rpart)\u003Cbr \/\u003Elibrary(caret)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cfont\u003EPreparing the data\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  We use Boston house-price dataset as a target regression data in this  tutorial. After loading the dataset, first, we'll split them into the  train and test parts, and extract x-input and y-label parts. Here, I'll  extract 15 percent of the dataset as test data. It is better to scale x part of data to improve the accuracy. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Eboston = MASS::Boston\u003Cbr \/\u003Estr(boston)\u003Cbr \/\u003E\u003Cbr \/\u003Eset.seed(12)\u003Cbr \/\u003E\u003Cbr \/\u003Eindexes = createDataPartition(boston$medv, p = .85, list = F)\u003Cbr \/\u003Etrain = boston[indexes, ]\u003Cbr \/\u003Etest = boston[-indexes, ]\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003Etrain_x = train[, -14]\u003Cbr \/\u003Etrain_x = scale(train_x)[,]\u003Cbr \/\u003Etrain_y = train[,14]\u003Cbr \/\u003E\u003Cbr \/\u003Etest_x = test[, -14]\u003Cbr \/\u003Etest_x = scale(test[,-14])[,]\u003Cbr \/\u003Etest_y = test[,14]\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cfont\u003EFitting the model and prediction\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  We'll define the model by using the rpart() function of the rpart package and fit on train data.\u0026nbsp; Here, we'll set 'control' parameters as shown below. The  calling the function is enough to train the model with included data.  You can check the summary of the model by using the print() or printcp() function.\u0026nbsp; \u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Efit = rpart(train_y~., data = data.frame(train_x, train_y), \u003Cbr \/\u003E            control = rpart.control(cp = 0.00001))\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Eprintcp(fit)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"color: #666666;\"\u003ERegression tree:\u003Cbr \/\u003Erpart(formula = train_y ~ ., data = data.frame(train_x, train_y), \u003Cbr \/\u003E    control = rpart.control(cp = 1e-05))\u003Cbr \/\u003E\u003Cbr \/\u003EVariables actually used in tree construction:\u003Cbr \/\u003E [1] age     black   crim    dis     lstat   nox     ptratio rad     rm      tax     zn     \u003Cbr \/\u003E\u003Cbr \/\u003ERoot node error: 38319\/458 = 83.667\u003Cbr \/\u003E\u003Cbr \/\u003En= 458 \u003Cbr \/\u003E\u003Cbr \/\u003E           CP nsplit rel error  xerror     xstd\u003Cbr \/\u003E1  0.45252258      0   1.00000 1.00371 0.087768\u003Cbr \/\u003E2  0.17808779      1   0.54748 0.62144 0.061139\u003Cbr \/\u003E3  0.06372523      2   0.36939 0.41766 0.049265\u003Cbr \/\u003E4  0.04001076      3   0.30566 0.34812 0.045449\u003Cbr \/\u003E5  0.03530233      4   0.26565 0.34948 0.046059\u003Cbr \/\u003E6  0.02585806      5   0.23035 0.31908 0.044292\u003Cbr \/\u003E7  0.00855071      6   0.20449 0.27123 0.039490\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E....\u003Cbr \/\u003E\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll apply prune function for fitted data. Then we can plot the trees.\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Efit.pruned = prune(fit, cp = 0.0001)\u003Cbr \/\u003E\u003Cbr \/\u003Eplot(fit.pruned)\u003Cbr \/\u003Etext(fit.pruned, cex = 0.9, xpd = TRUE)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-HshF4_43v0I\/X4ZVARiLMmI\/AAAAAAAAA-8\/iuebcLgtY54Ll7PswSkVtkuBXpdwoXPrwCLcBGAsYHQ\/s2048\/trees_rpart.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"1247\" data-original-width=\"2048\" height=\"390\" src=\"https:\/\/1.bp.blogspot.com\/-HshF4_43v0I\/X4ZVARiLMmI\/AAAAAAAAA-8\/iuebcLgtY54Ll7PswSkVtkuBXpdwoXPrwCLcBGAsYHQ\/w640-h390\/trees_rpart.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003ENow, we can predict the x test data with the trained model. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Epred_y = predict(fit.pruned, data.frame(test_x))\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cb\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;;\"\u003E\u003Cb\u003E\u003Cfont\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003EAccuracy checking\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003ENext, we'll check the prediction accuracy with MSE, MAE, and RMSE metrics. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Eprint(data.frame(test_y, pred_y))\u003Cbr \/\u003E\u003Cbr \/\u003Emse = mean((test_y - pred_y)^2)\u003Cbr \/\u003Emae = caret::MAE(test_y, pred_y)\u003Cbr \/\u003Ermse = caret::RMSE(test_y, pred_y)\u003Cbr \/\u003E\u003Cbr \/\u003Ecat(\"MSE: \", mse, \"MAE: \", mae, \" RMSE: \", rmse)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E\u003Cspan style=\"color: #444444;\"\u003EMSE:  20.28907 MAE:  2.979355  RMSE:  4.504339\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: white; border: medium none; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan class=\"GNKRCKGCGSB\" style=\"border: medium none; margin: 0px; outline: currentcolor none medium; user-select: text; white-space: pre-wrap; word-break: break-all;\"\u003E\u003Cbr \/\u003EMSE:  11.99942 MAE:  2.503739  RMSE:  3.464018\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003EFinally, we'll visualize original test and predicted data in a plot.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Ex = 1:length(test_y)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003Eplot(x, test_y, col = \"red\", type = \"l\", lwd=2,\u003Cbr \/\u003E     main = \"Boston housing test data prediction\")\u003Cbr \/\u003Elines(x, pred_y, col = \"blue\", lwd=2)\u003Cbr \/\u003Elegend(\"topright\",  legend = c(\"original-medv\", \"predicted-medv\"), \u003Cbr \/\u003E       fill = c(\"red\", \"blue\"), col = 2:3,  adj = c(0, 0.6))\u003Cbr \/\u003Egrid()\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-nUF-BYHjt0s\/X4ZZM5QIO8I\/AAAAAAAAA_I\/XL0vJtKlOj8rHeiVbrvY29TXWGaBRY2VACLcBGAsYHQ\/s1600\/rpart_prediction.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"1196\" data-original-width=\"1600\" height=\"478\" src=\"https:\/\/1.bp.blogspot.com\/-nUF-BYHjt0s\/X4ZZM5QIO8I\/AAAAAAAAA_I\/XL0vJtKlOj8rHeiVbrvY29TXWGaBRY2VACLcBGAsYHQ\/w640-h478\/rpart_prediction.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;  In this tutorial, we've learned how to fit and predict regression data  with rpart function in R. The full source code is listed below.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: georgia; font-size: large;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003Cspan\u003E\u003Cb\u003E\u003Cfont\u003ESource code listing\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cb\u003E\u003Cfont\u003E\u003Cbr \/\u003E\u003C\/font\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003Elibrary(rpart)\u003Cbr \/\u003Elibrary(caret)\u003Cbr \/\u003E\u003Cbr \/\u003Eboston = MASS::Boston\u003Cbr \/\u003Estr(boston)\u003Cbr \/\u003E\u003Cbr \/\u003Eset.seed(12)\u003Cbr \/\u003Eindexes = createDataPartition(boston$medv, p = .9, list = F)\u003Cbr \/\u003Etrain = boston[indexes, ]\u003Cbr \/\u003Etest = boston[-indexes, ]\u003Cbr \/\u003E\u003Cbr \/\u003Etrain_x = train[, -14]\u003Cbr \/\u003Etrain_x = scale(train_x)[,]\u003Cbr \/\u003Etrain_y = train[,14]\u003Cbr \/\u003E\u003Cbr \/\u003Etest_x = test[, -14]\u003Cbr \/\u003Etest_x = scale(test[,-14])[,]\u003Cbr \/\u003Etest_y = test[,14]\u003Cbr \/\u003E\u003Cbr \/\u003Efit = rpart(train_y~., data = data.frame(train_x, train_y), \u003Cbr \/\u003E            control = rpart.control(cp = 0.00001))\u003Cbr \/\u003Eprintcp(fit)\u003Cbr \/\u003E\u003Cbr \/\u003Efit.pruned = prune(fit, cp = 0.0001)\u003Cbr \/\u003E\u003Cbr \/\u003Eplot(fit.pruned)\u003Cbr \/\u003Etext(fit.pruned, cex = 0.9, xpd = TRUE)\u003Cbr \/\u003E\u003Cbr \/\u003Epred_y = predict(fit.pruned, data.frame(test_x))\u003Cbr \/\u003Eprint(data.frame(test_y, pred_y))\u003Cbr \/\u003E\u003Cbr \/\u003Emse = mean((test_y - pred_y)^2)\u003Cbr \/\u003Emae = caret::MAE(test_y, pred_y)\u003Cbr \/\u003Ermse = caret::RMSE(test_y, pred_y)\u003Cbr \/\u003E\u003Cbr \/\u003Ecat(\"MSE: \", mse, \"MAE: \", mae, \" RMSE: \", rmse)\u003Cbr \/\u003E\u003Cbr \/\u003Ex = 1:length(test_y)\u003Cbr \/\u003E\u003Cbr \/\u003Eplot(x, test_y, col = \"red\", type = \"l\", lwd=2,\u003Cbr \/\u003E     main = \"Boston housing test data prediction\")\u003Cbr \/\u003Elines(x, pred_y, col = \"blue\", lwd=2)\u003Cbr \/\u003Elegend(\"topright\",  legend = c(\"original-medv\", \"predicted-medv\"), \u003Cbr \/\u003E       fill = c(\"red\", \"blue\"), col = 2:3,  adj = c(0, 0.6))\u003Cbr \/\u003Egrid()\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre class=\"GNKRCKGCGSB\" id=\"rstudio_console_output\" style=\"-webkit-text-stroke-width: 0px; background-color: whitesmoke; border: medium none; color: black; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 15px; margin: 0px; outline: currentcolor none medium; text-indent: 0px; text-transform: none; user-select: text; white-space: pre-wrap; word-break: break-all; word-spacing: 0px;\" tabindex=\"0\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan class=\"GNKRCKGCMRB ace_keyword\" style=\"color: blue;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cb\u003E\u003Cfont\u003EReference:\u003C\/font\u003E\u003C\/b\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/cran.r-project.org\/web\/packages\/rpart\/rpart.pdf\" target=\"_blank\"\u003ERPART package\u003C\/a\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/8212610442666814886\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/regression-example-with-rpart-tree.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/8212610442666814886"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/8212610442666814886"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/regression-example-with-rpart-tree.html","title":"Regression Example With RPART Tree Model in R"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-HshF4_43v0I\/X4ZVARiLMmI\/AAAAAAAAA-8\/iuebcLgtY54Ll7PswSkVtkuBXpdwoXPrwCLcBGAsYHQ\/s72-w640-h390-c\/trees_rpart.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-5277813888902132012"},"published":{"$t":"2020-10-06T21:01:00.003-07:00"},"updated":{"$t":"2020-10-07T21:43:46.856-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Classification"},{"scheme":"http://www.blogger.com/atom/ns#","term":"k-neighbors"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"Classification Example with KNeighborsClassifier in Python"},"content":{"type":"html","$t":"\u003Cp style=\"text-align: left;\"\u003E\u0026nbsp;\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003EThe k-neighbors\u003C\/span\u003E\u003C\/span\u003E is commonly used and easy to apply classification method which implements the k neighbors queries to classify data. \u003Cspan\u003E\u003Cspan\u003EIt is an instant-based and non-parametric learning method. \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit;\"\u003E\u003Cspan\u003E\u003Cspan\u003EIn this method, the classifier \u003C\/span\u003E\u003C\/span\u003Elearns from the instances in the training dataset and classifies new input by using the previously measured scores.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E \u003C\/span\u003E\u003C\/p\u003E\u003Cp style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EScikit-learn  API provides the KNeighborsClassifier class to implement k-neighbors method for  classification problems.\u003Cspan style=\"font-family: inherit;\"\u003E \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003EIn this tutorial, we'll briefly learn how to classify data by using \u003C\/span\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003Ethe \u003C\/span\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EKNeighborsClassifier\u003C\/span\u003E class in Python. The tutorial  covers:\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003ETraining the model\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003EPredicting and accuracy check\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003EIris dataset classification example\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries and functions.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.neighbors\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E KNeighborsClassifier\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_classification\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E train_test_split\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E confusion_matrix\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E classification_report\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EFirst,  we'll generate random classification dataset with make_classification()  function. The dataset contains 4 classes with 10 features and the  number of samples is 10000. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E \u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_classification(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E, \u003Cbr \/\u003E                           n_classes\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E, \u003Cbr \/\u003E                           n_clusters_per_class\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003EThen, we'll split the data into train and test parts. Here, we'll extract 15 percent of it as test data.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Extrain, xtest, ytrain, ytest\u003Cspan style=\"color: #666666;\"\u003E =\u003C\/span\u003E train_test_split(x, y, test_size \u003Cspan style=\"color: #666666;\"\u003E= \u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.15\u003C\/span\u003E)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003ETraining the model\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENext,  we'll define the classifier by using the \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EKNeighborsClassifier\u003C\/span\u003E\u003C\/span\u003E class. The neighbors number is important in this method. Selecting the right number of neighbors provides the more accurate results. Here, we'll set 4 into the n_neighbors parameter of the class.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eknc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EKNeighborsClassifier\u003C\/span\u003E\u003C\/span\u003E(\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003En_neighbors \u003Cspan style=\"color: #666666;\"\u003E= \u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(knc)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EKNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\u003Cbr \/\u003E                     metric_params=None, n_jobs=None, n_neighbors=4, p=2,\u003Cbr \/\u003E                     weights='uniform')\u0026nbsp;\u003Cbr \/\u003E\u0026nbsp;  \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll fit the model on the train data. \u003C\/span\u003EAfter the training the classifier, we'll check the model accuracy score. \u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eknc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E knc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Training score: \"\u003C\/span\u003E, score)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ETraining Score:  0.8647058823529412\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003EPredicting and accuracy check\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENow, we can predict the test data by using the trained model. After the  prediction, we'll check the accuracy level by using the confusion matrix  function.\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eypred \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E knc\u003Cspan style=\"color: #333333;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Ecm \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E confusion_matrix(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eprint\u003C\/span\u003E(cm)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[[342  19   2   3]\u003Cbr \/\u003E [ 27 289  16  39]\u003Cbr \/\u003E [ 16   9 318  46]\u003Cbr \/\u003E [  5  62  59 248]]\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp; \u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe can also create a classification report by using  classification_report() function on predicted data to check the other  accuracy metrics. \u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ecr \u003Cspan style=\"color: #333333;\"\u003E=\u003C\/span\u003E classification_report(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #008800; font-weight: bold;\"\u003Eprint\u003C\/span\u003E(cr)\u003Cbr \/\u003E\u003Cbr \/\u003E \u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E precision    recall  f1-score   support\u003Cbr \/\u003E\u003Cbr \/\u003E           0       0.88      0.93      0.90       366\u003Cbr \/\u003E           1       0.76      0.78      0.77       371\u003Cbr \/\u003E           2       0.81      0.82      0.81       389\u003Cbr \/\u003E           3       0.74      0.66      0.70       374\u003Cbr \/\u003E\u003Cbr \/\u003E    accuracy                           0.80      1500\u003Cbr \/\u003E   macro avg       0.80      0.80      0.80      1500\u003Cbr \/\u003Eweighted avg       0.80      0.80      0.80      1500\u003Cbr \/\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan face=\"\" style=\"font-size: medium;\"\u003E\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E \u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003EIris dataset classification example\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll  load the Iris dataset with load_iris() function, extract the x and y  parts, then split into the train and test parts.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"color: #60a0b0; font-size: medium; font-style: italic;\"\u003E# Iris dataset example\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cbr \/\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003Extrain, xtest, ytrain, ytest\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Etrain_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.10\u003C\/span\u003E)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003C\/div\u003E \u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EThen, we'll use the same method mentioned above. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eknc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E KNeighborsClassifier(n_neighbors\u003Cspan style=\"color: #666666;\"\u003E = \u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(knc)\u003Cbr \/\u003E\u003Cbr \/\u003Eknc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E knc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Score: \"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E knc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Ecm \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E confusion_matrix(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(cm)\u003Cbr \/\u003E\u003Cbr \/\u003Ecr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E classification_report(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(cr)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EKNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\u003Cbr \/\u003E                     metric_params=None, n_jobs=None, n_neighbors=3, p=2,\u003Cbr \/\u003E                     weights='uniform')\u003Cbr \/\u003EScore:  0.9555555555555556\u003Cbr \/\u003E[[4 0 0]\u003Cbr \/\u003E [0 8 0]\u003Cbr \/\u003E [0 0 3]]\u003Cbr \/\u003E              precision    recall  f1-score   support\u003Cbr \/\u003E\u003Cbr \/\u003E           0       1.00      1.00      1.00         4\u003Cbr \/\u003E           1       1.00      1.00      1.00         8\u003Cbr \/\u003E           2       1.00      1.00      1.00         3\u003Cbr \/\u003E\u003Cbr \/\u003E    accuracy                           1.00        15\u003Cbr \/\u003E   macro avg       1.00      1.00      1.00        15\u003Cbr \/\u003Eweighted avg       1.00      1.00      1.00        15\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn this tutorial, we've briefly learned how to classify data by using  Scikit-learn's \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EKNeighborsClassifier\u003C\/span\u003E\u003C\/span\u003E class in Python. The full source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.neighbors\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E KNeighborsClassifier\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_iris\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_classification\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E train_test_split\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E confusion_matrix\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E classification_report\u003Cbr \/\u003E\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_classification(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E, \u003Cbr \/\u003E                           n_classes\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E, n_clusters_per_class\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Extrain, xtest, ytrain, ytest\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Etrain_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.15\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eknc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E KNeighborsClassifier(n_neighbors\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(knc)\u003Cbr \/\u003E\u003Cbr \/\u003Eknc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E knc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Training score: \"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E knc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003Ecm \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E confusion_matrix(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(cm)\u003Cbr \/\u003E\u003Cbr \/\u003Ecr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E classification_report(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(cr)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# Iris dataset example\u003C\/span\u003E\u003Cbr \/\u003Eiris \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_iris()\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, iris\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Extrain, xtest, ytrain, ytest\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Etrain_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eknc \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E KNeighborsClassifier(n_neighbors\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(knc)\u003Cbr \/\u003E\u003Cbr \/\u003Eknc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E knc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Score: \"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E knc\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Ecm \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E confusion_matrix(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(cm)\u003Cbr \/\u003E\u003Cbr \/\u003Ecr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E classification_report(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(cr)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.neighbors.KNeighborsClassifier.html#sklearn.neighbors.KNeighborsClassifier\" target=\"_blank\"\u003EScikit learn API\u003C\/a\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E \u003C!--more--\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/5277813888902132012\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/classification-with-kneighbors-classifier-in-python.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/5277813888902132012"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/5277813888902132012"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/classification-with-kneighbors-classifier-in-python.html","title":"Classification Example with KNeighborsClassifier in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-4022588890174471013"},"published":{"$t":"2020-10-03T13:39:00.005-07:00"},"updated":{"$t":"2020-10-03T15:40:55.260-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"decision-tree"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"},{"scheme":"http://www.blogger.com/atom/ns#","term":"regression"}],"title":{"type":"text","$t":"Regression Example With DecisionTreeRegressor in Python"},"content":{"type":"html","$t":"\u003Cp\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EDecision tree is one of the well known and powerful supervised machine learning algorithms that can be used for classification and regression problems. The model is based on decision rules extracted from the training data. In regression problem, the model uses the value instead of class and mean squared error is used to for a decision accuracy.\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EDecision tree model is not good in generalization and sensitive to the changes in training data. A small change in a training dataset may effect the model predictive accuracy.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003EScikit-learn  API provides the DecisionTreeRegressor class to apply decision tree method for regression task.\u0026nbsp; \u003C\/span\u003E\u003C\/p\u003E\u003Cp style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003C\/span\u003EIn this tutorial, we'll briefly learn how to fit and predict regression data by using the \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EDecisionTreeRegressor\u003C\/span\u003E\u003C\/span\u003E class in Python. We'll apply the model for a randomly generated regression data and Boston housing dataset to check the performance. The tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ETraining the model\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPredicting and accuracy check\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EBoston housing dataset prediction \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.ensemble\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003EDecisionTreeRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mean_squared_error\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E train_test_split\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.preprocessing\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E scale\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E set_config\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; First,  we'll generate random regression data with make_regression()  function. The dataset contains 10 features and 5000 samples. \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_regression(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E5000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(y[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[[ 1.773  2.534  0.693 -1.11   1.492  0.631 -0.577  0.085 -1.308  1.024]\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[ 1.953 -1.362  1.294  1.025  0.463 -0.485 -1.849  1.858  0.483 -0.52 ]]\u003Cbr \/\u003E[120.105 262.69 ]\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003ETo improve the model accuracy we'll scale both x and y data then, split  them into train and test parts. Here, we'll extract 10 percent of the  samples as test data.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Etrain_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.10\u003C\/span\u003E)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ETraining the model\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; Next, we'll define the regressor model by using the \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EDecisionTreeRegressor\u003C\/span\u003Eclass. Here, we can use default parameters of the \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EDecisionTreeRegressor\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E class. The default values can be seen in below.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eset_config(print_changed_only\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Edtr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E DecisionTreeRegressor()\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(dtr)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EDecisionTreeRegressor(ccp_alpha=0.0, criterion='mse', max_depth=None,\u003Cbr \/\u003E                      max_features=None, max_leaf_nodes=None,\u003Cbr \/\u003E                      min_impurity_decrease=0.0, min_impurity_split=None,\u003Cbr \/\u003E                      min_samples_leaf=1, min_samples_split=2,\u003Cbr \/\u003E                      min_weight_fraction_leaf=0.0, presort='deprecated',\u003Cbr \/\u003E                      random_state=None, splitter='best')\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EThen, we'll fit the model on train data and check the model accuracy score. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Edtr.fit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ER-squared: 0.9796146270086489\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003EPredicting and accuracy check\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENow, we can predict the test data by using the trained model. We can  check the accuracy of predicted data by using MSE and RMSE metrics.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Edtr\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EMSE:  0.130713987032462\u003Cbr \/\u003ERMSE:  0.065356993516231\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EFinally, we'll visualize the original and predicted data in a plot. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1.1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"y-test and y-predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E  \u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ERunning the above code provides a plot that shows the the original and predicted test data.\u003Cbr \/\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003EBoston housing dataset prediction\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll  apply the same method we've learned above to the Boston housing price  regression dataset. We'll load it by using load_boston() function, scale  and split into the train and test parts. Then, we'll define model by  changing some of the parameter values, check training accuracy, and  predict test data. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston housing dataset prediction.\"\u003C\/span\u003E)\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Edtr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E DecisionTreeRegressor()\u003Cbr \/\u003Edtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow() \u0026nbsp;\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EBoston housing dataset prediction.\u003Cbr \/\u003ER-squared: 0.9834125970221356\u003Cbr \/\u003EMSE:  0.2157465095558568\u003Cbr \/\u003ERMSE:  0.1078732547779284\u0026nbsp;\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E  \u003Cdiv style=\"text-align: center;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-o_FYUP0LdSc\/X3jg6roPkiI\/AAAAAAAAA-k\/1Sk8kY0sYuoDp2oSqkgrTEWOZb0xkyD6QCLcBGAsYHQ\/s1126\/decisiontreeson_bostonhd.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"892\" data-original-width=\"1126\" height=\"506\" src=\"https:\/\/1.bp.blogspot.com\/-o_FYUP0LdSc\/X3jg6roPkiI\/AAAAAAAAA-k\/1Sk8kY0sYuoDp2oSqkgrTEWOZb0xkyD6QCLcBGAsYHQ\/w640-h506\/decisiontreeson_bostonhd.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: center;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; In this tutorial, we've briefly learned how to fit and predict regression data by using  Scikit-learn API's \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EDecisionTreeRegressor\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E class in Python. The full  source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.tree\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E DecisionTreeRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mean_squared_error\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E train_test_split\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.preprocessing\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E scale\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E set_config\u003Cbr \/\u003E\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_regression(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E5000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(y[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eset_config(print_changed_only\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u003Cbr \/\u003Edtr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EDecisionTreeRegressor\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(dtr)\u003Cbr \/\u003E\u003Cbr \/\u003Edtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1.1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"y-test and y-predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston housing dataset prediction.\"\u003C\/span\u003E)\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Edtr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E DecisionTreeRegressor()\u003Cbr \/\u003Edtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E dtr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow() \u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.tree.DecisionTreeRegressor.html\" target=\"_blank\"\u003EScikit learn API\u003C\/a\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/4022588890174471013\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/regression-example-with-decisiontreeregressor.html#comment-form","title":"2 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/4022588890174471013"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/4022588890174471013"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/10\/regression-example-with-decisiontreeregressor.html","title":"Regression Example With DecisionTreeRegressor in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-o_FYUP0LdSc\/X3jg6roPkiI\/AAAAAAAAA-k\/1Sk8kY0sYuoDp2oSqkgrTEWOZb0xkyD6QCLcBGAsYHQ\/s72-w640-h506-c\/decisiontreeson_bostonhd.png","height":"72","width":"72"},"thr$total":{"$t":"2"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-7941257837592864058"},"published":{"$t":"2020-09-29T13:53:00.005-07:00"},"updated":{"$t":"2020-09-29T14:00:32.273-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"python"},{"scheme":"http://www.blogger.com/atom/ns#","term":"randomforest"},{"scheme":"http://www.blogger.com/atom/ns#","term":"regression"}],"title":{"type":"text","$t":"Regression Example with RandomForestRegressor in Python"},"content":{"type":"html","$t":"\u003Cp style=\"text-align: left;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003ERandom forest is an ensemble learning algorithm based on decision tree learners. The estimator fits multiple decision trees on randomly extracted subsets from the dataset and averages their prediction.\u003C\/span\u003E\u003C\/p\u003E\u003Cp style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E \u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003EScikit-learn  API provides the RandomForestRegressor class included in ensemble module to implement the random forest for regression problem.\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003C\/span\u003EIn this tutorial, we'll briefly learn how to fit and predict regression data by using the \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ERandomForestRegressor\u003C\/span\u003E class in Python. The tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ETraining the model\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPredicting and accuracy check\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EBoston dataset prediction \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.ensemble\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E RandomForestRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mean_squared_error\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E train_test_split\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.preprocessing\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E scale\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E set_config\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; First,  we'll generate random regression data with make_regression()  function. The dataset contains 10 features and 5000 samples. \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_regression(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E5000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(y[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[[ 1.773  2.534  0.693 -1.11   1.492  0.631 -0.577  0.085 -1.308  1.024]\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[ 1.953 -1.362  1.294  1.025  0.463 -0.485 -1.849  1.858  0.483 -0.52 ]]\u003Cbr \/\u003E[120.105 262.69 ]\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003ETo improve the model accuracy we'll scale both x and y data then, split  them into train and test parts. Here, we'll extract 10 percent of the  samples as test data.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Etrain_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.10\u003C\/span\u003E)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ETraining the model\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; Next, we'll define the regressor model by using the \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ERandomForestRegressor\u003C\/span\u003Eclass. Here, we can use default parameters of the \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003ERandomForestRegressor\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E class. The default values can be seen in below.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eset_config(print_changed_only\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Erfr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E RandomForestRegressor()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(rfr)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ERandomForestRegressor(bootstrap=True, ccp_alpha=0.0, criterion='mse',\u003Cbr \/\u003E                      max_depth=None, max_features='auto', max_leaf_nodes=None,\u003Cbr \/\u003E                      max_samples=None, min_impurity_decrease=0.0,\u003Cbr \/\u003E                      min_impurity_split=None, min_samples_leaf=1,\u003Cbr \/\u003E                      min_samples_split=2, min_weight_fraction_leaf=0.0,\u003Cbr \/\u003E                      n_estimators=100, n_jobs=None, oob_score=False,\u003Cbr \/\u003E                      random_state=None, verbose=0, warm_start=False)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EThen, we'll fit the model on train data and check the model accuracy score. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Erfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ER-squared: 0.9796146270086489\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003EPredicting and accuracy check\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENow, we can predict the test data by using the trained model. We can  check the accuracy of predicted data by using MSE and RMSE metrics.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Erfr\u003Cspan style=\"color: #666666;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EMSE:  0.130713987032462\u003Cbr \/\u003ERMSE:  0.065356993516231\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EFinally, we'll visualize the original and predicted data in a plot. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1.1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"y-test and y-predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E  \u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/--t1YblyCqkI\/X3ObubaZ0qI\/AAAAAAAAA-Q\/WGseFWiKvxgf0gmuxiJ_XRrFvTSe5uN2ACLcBGAsYHQ\/s1134\/randomforestregressor_pred.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"898\" data-original-width=\"1134\" height=\"507\" src=\"https:\/\/1.bp.blogspot.com\/--t1YblyCqkI\/X3ObubaZ0qI\/AAAAAAAAA-Q\/WGseFWiKvxgf0gmuxiJ_XRrFvTSe5uN2ACLcBGAsYHQ\/w640-h507\/randomforestregressor_pred.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003EBoston housing dataset prediction\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll  apply the same method we've learned above to the Boston housing price  regression dataset. We'll load it by using load_boston() function, scale  and split into train and test parts. Then, we'll define model by  changing some of the parameter values, check training accuracy, and  predict test data. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston housing dataset prediction.\"\u003C\/span\u003E)\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Erfr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E RandomForestRegressor()\u003Cbr \/\u003Erfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EBoston housing dataset prediction.\u003Cbr \/\u003ER-squared: 0.9834125970221356\u003Cbr \/\u003EMSE:  0.2157465095558568\u003Cbr \/\u003ERMSE:  0.1078732547779284\u0026nbsp;\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E  \u003Cdiv style=\"text-align: center;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: center;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-xUqw_3A8Qiw\/X3OcUhEIMwI\/AAAAAAAAA-Y\/brh27jA8rKQ0ZS4ssI6tfFeHPW2uVF4vgCLcBGAsYHQ\/s1134\/boston_housing_pred.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"898\" data-original-width=\"1134\" height=\"506\" src=\"https:\/\/1.bp.blogspot.com\/-xUqw_3A8Qiw\/X3OcUhEIMwI\/AAAAAAAAA-Y\/brh27jA8rKQ0ZS4ssI6tfFeHPW2uVF4vgCLcBGAsYHQ\/w640-h506\/boston_housing_pred.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; In this tutorial, we've briefly learned how to fit and predict regression data by using  Scikit-learn API's \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ERandomForestRegressor\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E class in Python. The full  source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.ensemble\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E RandomForestRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mean_squared_error\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E train_test_split\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.preprocessing\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E scale\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E set_config\u003Cbr \/\u003E\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_regression(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E5000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(y[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Eset_config(print_changed_only\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003EFalse\u003C\/span\u003E)\u003Cbr \/\u003Erfr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E RandomForestRegressor()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(rfr)\u003Cbr \/\u003E\u003Cbr \/\u003Erfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1.1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"y-test and y-predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston housing dataset prediction.\"\u003C\/span\u003E)\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Erfr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E RandomForestRegressor()\u003Cbr \/\u003Erfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E rfr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.ensemble.RandomForestRegressor.html#sklearn.ensemble.RandomForestRegressor\" target=\"_blank\"\u003EScikit learn API\u003C\/a\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/7941257837592864058\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/09\/regression-example-with-randomforestregressor.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/7941257837592864058"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/7941257837592864058"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/09\/regression-example-with-randomforestregressor.html","title":"Regression Example with RandomForestRegressor in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/--t1YblyCqkI\/X3ObubaZ0qI\/AAAAAAAAA-Q\/WGseFWiKvxgf0gmuxiJ_XRrFvTSe5uN2ACLcBGAsYHQ\/s72-w640-h507-c\/randomforestregressor_pred.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-788117102938812372"},"published":{"$t":"2020-09-22T18:41:00.010-07:00"},"updated":{"$t":"2020-10-13T19:11:51.225-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"curve-fitting"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"Fitting Example With SciPy curve_fit Function in Python"},"content":{"type":"html","$t":"\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EThe SciPy API provides a 'curve_fit' function in its optimization library to fit the data with a given function. This method applies non-linear least squares to fit the data and extract the optimal parameters out of it.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn this tutorial, we'll learn how to fit the curve with the curve_fit() function by using various fitting functions in Python.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EWe'll start by loading the required libraries.\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E array, exp\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Escipy.optimize\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E curve_fit\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u0026nbsp; \u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cbr \/\u003E\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EWe  need a test data for this tutorial to implement curve fitting and we  can define a simple x input and y output data for this tutorial. You may  apply the same method for your target data.\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array([\u003Cspan style=\"color: #40a070;\"\u003E12\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E11\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E13\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E16\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E16\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E14\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E12\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E11\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E12\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E8\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E9\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E7\u003C\/span\u003E,\u003Cspan style=\"color: #40a070;\"\u003E6\u003C\/span\u003E])\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(\u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(y)))\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENext,  we'll define multiple functions to use in curve_fit() function and check their differences in fitting. You can also add or change the equations to get the best fitting parameters for your data.\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EWe use below equations as the fitting functions.\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003Cb\u003Ey = ax^2 + bx + c\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/i\u003E\u003C\/p\u003E\u003Cp\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cb\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ey = ax^3 + bx + c\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/i\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cb\u003Ey = ax^3 + bx^2 + c\u003C\/b\u003E\u003C\/span\u003E\u003C\/i\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cb\u003Ey = a*exp(bx) + c\u003C\/b\u003E\u003C\/span\u003E\u003C\/i\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-family: times;\"\u003EWe can write them in python as below.\u003C\/span\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cb\u003E \u003Cbr \/\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/i\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc1\u003C\/span\u003E(x, a, b, c):\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc2\u003C\/span\u003E(x, a, b, c):\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc3\u003C\/span\u003E(x, a, b, c):\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc4\u003C\/span\u003E(x, a, b, c):\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Eexp(b\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex)\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E \u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EFitting the data with curve_fit is easy, providing fitting function,\u0026nbsp; x and y data is enough to fit the data. The curve_fit() function returns an optimal parameters and estimated covariance values as an output.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eparams, covs \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E curve_fit(func1, x, y)\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"params: \"\u003C\/span\u003E, params) \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[-0.08139835  0.86364809 12.13622291]\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"covariance: \"\u003C\/span\u003E, covs) \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[ 2.38376129e-04 -3.81401808e-03  9.53504521e-03]\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[-3.81401808e-03  6.55534359e-02 -1.88793896e-01]\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E[ 9.53504521e-03 -1.88793896e-01  7.79966703e-01]] \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E \u003Cdiv\u003E\u0026nbsp; \u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENow,  we'll start fitting the data by setting the target function, and x, y data into  the curve_fit() function and get the output data which contains \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Ea, b, and c parameter values. Here, we don't use covariance values so we can skip it.\u0026nbsp; Then we'll calculate y fitted by using derived a, b, and c values for each function. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003Eparams, _ \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E curve_fit(func1, x, y)\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit1 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eparams, _  \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E curve_fit(func2, x, y)\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit2 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eparams, _  \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E curve_fit(func3, x, y)\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit3 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eparams, _  \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E curve_fit(func4, x, y)\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit4 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Eexp(x\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Eb)\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EFinally, we'll visualize the results in a plot to check the deference visually.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, y, \u003Cspan style=\"color: #4070a0;\"\u003E'bo'\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y-original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit1, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=a*x^2+b*x+c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit2, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=a*x^3+b*x+c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit3, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=a*x^3+b*x^2*c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit4, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=a*exp(b*x)+c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'x'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'y'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E, fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow() \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E \u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-LSjMAWMCAPs\/X2qp0gguUYI\/AAAAAAAAA-E\/14sGSZrek34YovP3Y2OPjrUNplnsNASHQCLcBGAsYHQ\/s1160\/curve_fit_example.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"808\" data-original-width=\"1160\" height=\"446\" src=\"https:\/\/1.bp.blogspot.com\/-LSjMAWMCAPs\/X2qp0gguUYI\/AAAAAAAAA-E\/14sGSZrek34YovP3Y2OPjrUNplnsNASHQCLcBGAsYHQ\/w640-h446\/curve_fit_example.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn  this tutorial, we've briefly learned how to fit curve with SciPy API's  curve_fit() function in Python. The full source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E array, exp\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Escipy.optimize\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E curve_fit\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array([\u003Cspan style=\"color: #40a070;\"\u003E12\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E11\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E13\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E16\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E16\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E14\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E12\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E11\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E12\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E8\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E9\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E7\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E6\u003C\/span\u003E])\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(\u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(y)))\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc1\u003C\/span\u003E(x, a, b, c):\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc2\u003C\/span\u003E(x, a, b, c):\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc3\u003C\/span\u003E(x, a, b, c):\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc4\u003C\/span\u003E(x, a, b, c):\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Eexp(b\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex)\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eparams, covs \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E curve_fit(func1, x, y)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"params: \"\u003C\/span\u003E, params)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"covariance: \"\u003C\/span\u003E, covs)\u003Cbr \/\u003E\u003Cbr \/\u003Eparams, _ \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E curve_fit(func1, x, y)\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit1 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eparams, _  \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E curve_fit(func2, x, y)\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit2 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eparams, _  \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E curve_fit(func3, x, y)\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit3 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eparams, _  \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E curve_fit(func4, x, y)\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit4 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Eexp(x\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Eb)\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, y, \u003Cspan style=\"color: #4070a0;\"\u003E'bo'\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y-original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit1, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=a*x^2+b*x+c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit2, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=a*x^3+b*x+c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit3, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=a*x^3+b*x^2*c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit4, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=a*exp(b*x)+c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'x'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'y'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E, fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow() \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/docs.scipy.org\/doc\/scipy\/reference\/generated\/scipy.optimize.curve_fit.html\" target=\"_blank\"\u003ESciPy API curve_fit\u003C\/a\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C!--more--\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/788117102938812372\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/09\/curve-fitting-with-curve-fit-function-in-python.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/788117102938812372"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/788117102938812372"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/09\/curve-fitting-with-curve-fit-function-in-python.html","title":"Fitting Example With SciPy curve_fit Function in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-LSjMAWMCAPs\/X2qp0gguUYI\/AAAAAAAAA-E\/14sGSZrek34YovP3Y2OPjrUNplnsNASHQCLcBGAsYHQ\/s72-w640-h446-c\/curve_fit_example.png","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-168212815833966849"},"published":{"$t":"2020-09-15T18:59:00.011-07:00"},"updated":{"$t":"2021-08-08T17:54:35.918-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"python"},{"scheme":"http://www.blogger.com/atom/ns#","term":"regression"},{"scheme":"http://www.blogger.com/atom/ns#","term":"sgd"}],"title":{"type":"text","$t":"Regression Example with SGDRegressor in Python"},"content":{"type":"html","$t":"\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EApplying the\u0026nbsp;\u003Cspan\u003E\u003C\/span\u003EStochastic  Gradient Descent (SGD) method to the linear classifier or regressor provides the efficient estimator for classification and regression problems.\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EScikit-learn  API provides the SGDRegressor class to implement SGD method for regression problems. The  SGD regressor applies regularized linear model with SGD learning to  build an estimator. \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EA regularizer is a penalty (L1, L2, or Elastic Net) added to the loss function to shrink the model parameters. \u003C\/span\u003EThe SGD regressor works well with large-scale  datasets.\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EIn previous post, we learned how to classify data with SGD classifier in Python and you can find it \u003Ca href=\"https:\/\/www.datatechnotes.com\/2020\/09\/sgd-classification-example-with-sgdclassifier-in-python.html\" target=\"_blank\"\u003Ehere\u003C\/a\u003E.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv style=\"text-align: justify;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cdiv style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EIn this tutorial, we'll briefly learn how to fit and predict regression data by using  Scikit-learn's SGDRegressor class in Python. The tutorial  covers:\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/div\u003E\u003Col\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPreparing the data\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ETraining the model\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EPredicting and accuracy check\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003EBoston dataset prediction \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003ESource code listing\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; We'll start by loading the required libraries.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.linear_model\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SGDRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mean_squared_error\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E train_test_split\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E cross_val_score\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.preprocessing\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E scale\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003E\u003Cspan\u003EPreparing the data\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; First,  we'll generate random regression data with make_regression()  function. The dataset contains 30 features and 1000 samples. \u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_regression(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E30\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003ETo improve the model accuracy we'll scale both x and y data then, split  them into train and test parts. Here, we'll extract 15 percent of the  samples as test data.\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003Etrain_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.15\u003C\/span\u003E)\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ETraining the model\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; Next, we'll define the regressor model by using the SGDRegressor class. Here, we can use default parameters of the \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ESGDRegressor\u003C\/span\u003E class. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Esgdr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SGDRegressor()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(sgdr)\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EThen, we'll fit the model on train data and check the model accuracy score. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Esgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003ER-squared: 0.9999999253180197\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe can also apply a cross-validation method to the model and check the training accuracy.\u0026nbsp; \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ecv_score \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E cross_val_score(sgdr, x, y, cv\u003Cspan style=\"color: #666666;\"\u003E = \u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"CV mean score: \"\u003C\/span\u003E, cv_score\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Emean())\u003Cbr \/\u003E\u003Cbr \/\u003ECV mean score:  0.9999999624822019\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003EPredicting and accuracy check\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENow, we can predict the test data by using the trained model. We can  check the accuracy of predicted data by using MSE and RMSE metrics.\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E nsvr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EMSE:  1.1979434697284535e-07\u003Cbr \/\u003ERMSE:  5.989717348642267e-08 \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EFinally, we'll visualize the original and predicted data in a plot. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1.1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"y-test and y-predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E  \u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003C\/div\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-lOnGjV2XD0s\/X2Fv5jxya5I\/AAAAAAAAA9c\/73VE8yV9Or43E2Nq1X2szRShQyQ8tjA8gCLcBGAsYHQ\/s1132\/sgdregressor_pred.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"904\" data-original-width=\"1132\" height=\"512\" src=\"https:\/\/1.bp.blogspot.com\/-lOnGjV2XD0s\/X2Fv5jxya5I\/AAAAAAAAA9c\/73VE8yV9Or43E2Nq1X2szRShQyQ8tjA8gCLcBGAsYHQ\/w640-h512\/sgdregressor_pred.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cb\u003EBoston housing dataset prediction\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EWe'll  apply the same method we've learned above to the Boston housing price  regression dataset. We'll load it by using load_boston() function, scale  and split into train and test parts. Then, we'll define model by changing some of the parameter values, check training accuracy, and predict test data. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cbr \/\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston housing dataset prediction.\"\u003C\/span\u003E)\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Esgdr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SGDRegressor(alpha\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.0001\u003C\/span\u003E, epsilon\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.01\u003C\/span\u003E, eta0\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.1\u003C\/span\u003E,penalty\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'elasticnet'\u003C\/span\u003E)\u003Cbr \/\u003Esgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# cv_score = cross_val_score(lsvr, x, y, cv=5)\u003C\/span\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #60a0b0; font-style: italic;\"\u003E# print(\"CV mean score: \", cv_score.mean())\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E*(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EBoston housing dataset prediction.\u003Cbr \/\u003ER-squared: 0.7239074887406243\u003Cbr \/\u003EMSE:  0.2200176437469965\u003Cbr \/\u003ERMSE:  0.11000882187349825 \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E  \u003Cdiv style=\"text-align: center;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: center;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-5GEaZbxnQRY\/X2FwoWW9cQI\/AAAAAAAAA9k\/0J3DZpubsdYW4F1FZZFW8c1bZ2M0LKvdwCLcBGAsYHQ\/s1132\/sgdregressor_pred1.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"904\" data-original-width=\"1132\" height=\"512\" src=\"https:\/\/1.bp.blogspot.com\/-5GEaZbxnQRY\/X2FwoWW9cQI\/AAAAAAAAA9k\/0J3DZpubsdYW4F1FZZFW8c1bZ2M0LKvdwCLcBGAsYHQ\/w640-h512\/sgdregressor_pred1.png\" width=\"640\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; In this tutorial, we've briefly learned how to fit and predict regression data by using  Scikit-learn API's \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003ESGDRegressor\u003C\/span\u003E class in Python. The full  source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: \u0026quot;georgia\u0026quot;; font-size: large;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.linear_model\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E SGDRegressor\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E load_boston\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.datasets\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E make_regression\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.metrics\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E mean_squared_error\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E train_test_split\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.model_selection\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E cross_val_score\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Esklearn.preprocessing\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E scale\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E make_regression(n_samples\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1000\u003C\/span\u003E, n_features\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E30\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(x[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(y[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E:\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E])\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Esgdr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SGDRegressor()\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(sgdr)\u003Cbr \/\u003E\u003Cbr \/\u003Esgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003Ecv_score \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E cross_val_score(sgdr, x, y, cv\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E10\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"CV mean score: \"\u003C\/span\u003E, cv_score\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Emean())\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E*(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, linewidth\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E1.1\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"y-test and y-predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston housing dataset prediction.\"\u003C\/span\u003E)\u003Cbr \/\u003Eboston \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E load_boston()\u003Cbr \/\u003Ex, y \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Edata, boston\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etarget\u003Cbr \/\u003E\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(x)\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E scale(y)\u003Cbr \/\u003Extrain, xtest, ytrain, ytest \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E train_test_split(x, y, test_size\u003Cspan style=\"color: #666666;\"\u003E=.\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E15\u003C\/span\u003E)\u003Cbr \/\u003E\u003Cbr \/\u003Esgdr \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E SGDRegressor(alpha\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.0001\u003C\/span\u003E, epsilon\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.01\u003C\/span\u003E, eta0\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E0.1\u003C\/span\u003E,penalty\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'elasticnet'\u003C\/span\u003E)\u003Cbr \/\u003Esgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Efit(xtrain, ytrain)\u003Cbr \/\u003E\u003Cbr \/\u003Escore \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Escore(xtrain, ytrain)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"R-squared:\"\u003C\/span\u003E, score)\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Eypred \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E sgdr\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Epredict(xtest)\u003Cbr \/\u003E\u003Cbr \/\u003Emse \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E mean_squared_error(ytest, ypred)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"MSE: \"\u003C\/span\u003E, mse)\u003Cbr \/\u003E\u003Cspan style=\"color: #007020;\"\u003Eprint\u003C\/span\u003E(\u003Cspan style=\"color: #4070a0;\"\u003E\"RMSE: \"\u003C\/span\u003E, mse\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003E*(\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E\/\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2.0\u003C\/span\u003E))\u003Cbr \/\u003E\u003Cbr \/\u003Ex_ax \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E \u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(ytest))\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ytest, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x_ax, ypred, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"predicted\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Etitle(\u003Cspan style=\"color: #4070a0;\"\u003E\"Boston test and predicted data\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'X-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'Y-axis'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E,fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow() \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.linear_model.SGDRegressor.html\" target=\"_blank\"\u003EScikit learn API\u003C\/a\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/168212815833966849\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/09\/regression-example-with-sgdregressor-in-python.html#comment-form","title":"2 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/168212815833966849"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/168212815833966849"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/09\/regression-example-with-sgdregressor-in-python.html","title":"Regression Example with SGDRegressor in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-lOnGjV2XD0s\/X2Fv5jxya5I\/AAAAAAAAA9c\/73VE8yV9Or43E2Nq1X2szRShQyQ8tjA8gCLcBGAsYHQ\/s72-w640-h512-c\/sgdregressor_pred.png","height":"72","width":"72"},"thr$total":{"$t":"2"}},{"id":{"$t":"tag:blogger.com,1999:blog-3884463987282087684.post-4378238453424551095"},"published":{"$t":"2020-09-09T01:57:00.014-07:00"},"updated":{"$t":"2020-09-22T18:53:48.463-07:00"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"curve-fitting"},{"scheme":"http://www.blogger.com/atom/ns#","term":"python"}],"title":{"type":"text","$t":"Curve Fitting Example with leastsq() Function in Python"},"content":{"type":"html","$t":"\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EThe \u003Ca href=\"https:\/\/docs.scipy.org\/doc\/scipy\/reference\/generated\/scipy.optimize.leastsq.html\" target=\"_blank\"\u003ESciPy API\u003C\/a\u003E provides a 'leastsq()' function in its optimization library to implement the least-square method to fit the curve data with a given function. The leastsq() function applies the least-square minimization to fit the data.\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn this tutorial, we'll learn how to fit the data with the leastsq() function by using various fitting function functions in Python.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EWe'll start by loading the required libraries.\u003C\/span\u003E\u003C\/p\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E array\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Escipy.optimize\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E leastsq\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cspan\u003E\u0026nbsp; \u003Cbr \/\u003E\u003Cspan\u003E\u003Ca name='more'\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EWe need a test data for this tutorial to implement curve fitting and we can define a simple x input and y output data for this tutorial. You may apply the same method for your target data.\u003C\/span\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array([\u003Cspan style=\"color: #40a070;\"\u003E12\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E8\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E11\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E7\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E6\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E7\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E8\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E13\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E19\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E22\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E25\u003C\/span\u003E])\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(\u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(y)))\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003Cp style=\"text-align: justify;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENext, we'll define the functions to use in leastsq() function and check the differences in fitting. Below code, I defined three types of function to fit. You can also add or change the formulas in the functions to observe the fitting differences.\u0026nbsp;\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003EWe use below equations as a fitting function.\u003C\/span\u003E\u003C\/p\u003E\u003Cp\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003Cb\u003Ey = ax^2 + bc + c\u003C\/b\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/i\u003E\u003C\/p\u003E\u003Cp\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cb\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Ey = ax^3 + bx + c\u003C\/span\u003E\u003C\/span\u003E\u003C\/b\u003E\u003C\/i\u003E\u003C\/p\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ci\u003E\u003Cspan style=\"font-family: times;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cb\u003Ey = ax^2 + bx\u003C\/b\u003E\u003C\/span\u003E\u003C\/i\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc1\u003C\/span\u003E(params, x, y):\u003Cbr \/\u003E    a, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003E    residual \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y\u003Cspan style=\"color: #666666;\"\u003E-\u003C\/span\u003E(a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E residual\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc2\u003C\/span\u003E(params, x, y):\u003Cbr \/\u003E    a, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003E    residual \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y\u003Cspan style=\"color: #666666;\"\u003E-\u003C\/span\u003E(a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E residual\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc3\u003C\/span\u003E(params, x, y):\u003Cbr \/\u003E    a, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003E    residual \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y\u003Cspan style=\"color: #666666;\"\u003E-\u003C\/span\u003E(a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E residual\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E \u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EInitial parameters are required for the method and we can start with 0 values.\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E \u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eparams\u003Cspan style=\"color: #666666;\"\u003E = \u003C\/span\u003E[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E \u003Cdiv\u003E\u0026nbsp; \u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003ENow, we'll set target function, initial parameters, and x and y data into the leastsq() function and get the output data that contains \u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003Ea, b, and c values. Then we'll calculate y fitted by using derived a, b, and c values for each function. \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eresult \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E leastsq(func1, params, (x, y))\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit1 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eresult \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E leastsq(func2, params, (x, y))\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit2 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eresult \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E leastsq(func3, params, (x, y))\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit3 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003C\/span\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cp\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003EFinally, we'll visualize the results in a plot to check the deference visually.\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/p\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, y, \u003Cspan style=\"color: #4070a0;\"\u003E'bo'\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y-original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit1, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"red\"\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=ax^2+bx+c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"orange\"\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=ax^2+b+c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit3, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"green\"\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=ax^2+bx\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'x'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'y'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E, fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E \u003Cdiv\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv\u003E\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-azmg6Dusxng\/X1iYOwShJrI\/AAAAAAAAA9Q\/CE40LL3TARsHMw32zn849YDO5Cm7O57WQCLcBGAsYHQ\/s1230\/leastsq-curve-fit.png\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" data-original-height=\"980\" data-original-width=\"1230\" height=\"319\" src=\"https:\/\/1.bp.blogspot.com\/-azmg6Dusxng\/X1iYOwShJrI\/AAAAAAAAA9Q\/CE40LL3TARsHMw32zn849YDO5Cm7O57WQCLcBGAsYHQ\/w400-h319\/leastsq-curve-fit.png\" width=\"400\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\u003Cbr \/\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp;\u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: medium;\"\u003EIn this tutorial, we've briefly learned curve fitting with SciPy API's leastsq() function in Python. The full source code is listed below. \u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cspan\u003E\u003Cb\u003ESource code listing\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Enumpy\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E array\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Efrom\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Escipy.optimize\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E leastsq\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eimport\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Ematplotlib.pyplot\u003C\/span\u003E \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Eas\u003C\/span\u003E \u003Cspan style=\"color: #0e84b5; font-weight: bold;\"\u003Eplt\u003C\/span\u003E\u003Cbr \/\u003E\u003Cbr \/\u003Ey \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array([\u003Cspan style=\"color: #40a070;\"\u003E12\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E8\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E11\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E7\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E6\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E4\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E5\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E7\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E8\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E13\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E19\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E22\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E25\u003C\/span\u003E])\u003Cbr \/\u003Ex \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E array(\u003Cspan style=\"color: #007020;\"\u003Erange\u003C\/span\u003E(\u003Cspan style=\"color: #007020;\"\u003Elen\u003C\/span\u003E(y)))\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc1\u003C\/span\u003E(params, x, y):\u003Cbr \/\u003E    a, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003E    residual \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y\u003Cspan style=\"color: #666666;\"\u003E-\u003C\/span\u003E(a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E residual\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc2\u003C\/span\u003E(params, x, y):\u003Cbr \/\u003E    a, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003E    residual \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y\u003Cspan style=\"color: #666666;\"\u003E-\u003C\/span\u003E(a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E residual\u003Cbr \/\u003E\u003Cbr \/\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Edef\u003C\/span\u003E \u003Cspan style=\"color: #06287e;\"\u003Efunc3\u003C\/span\u003E(params, x, y):\u003Cbr \/\u003E    a, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E params[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], params[\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003E    residual \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E y\u003Cspan style=\"color: #666666;\"\u003E-\u003C\/span\u003E(a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex)\u003Cbr \/\u003E    \u003Cspan style=\"color: #007020; font-weight: bold;\"\u003Ereturn\u003C\/span\u003E residual\u003Cbr \/\u003E\u003Cbr \/\u003Eparams\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E, \u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E]\u003Cbr \/\u003E\u003Cbr \/\u003Eresult \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E leastsq(func1, params, (x, y))\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit1 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eresult \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E leastsq(func2, params, (x, y))\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit2 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E3\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Ec\u003Cbr \/\u003E\u003Cbr \/\u003Eresult \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E leastsq(func3, params, (x, y))\u003Cbr \/\u003Ea, b, c \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E1\u003C\/span\u003E], result[\u003Cspan style=\"color: #40a070;\"\u003E0\u003C\/span\u003E][\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E]\u003Cbr \/\u003Eyfit3 \u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E a\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cspan style=\"color: #666666;\"\u003E**\u003C\/span\u003E\u003Cspan style=\"color: #40a070;\"\u003E2\u003C\/span\u003E\u003Cspan style=\"color: #666666;\"\u003E+\u003C\/span\u003Eb\u003Cspan style=\"color: #666666;\"\u003E*\u003C\/span\u003Ex\u003Cbr \/\u003E\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, y, \u003Cspan style=\"color: #4070a0;\"\u003E'bo'\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y-original\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit1, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"red\"\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=ax^2+bx+c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit2, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"orange\"\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=ax^2+b+c\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eplot(x, yfit3, color\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"green\"\u003C\/span\u003E, label\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E\"y=ax^2+bx\"\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Exlabel(\u003Cspan style=\"color: #4070a0;\"\u003E'x'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eylabel(\u003Cspan style=\"color: #4070a0;\"\u003E'y'\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Elegend(loc\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #4070a0;\"\u003E'best'\u003C\/span\u003E, fancybox\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E, shadow\u003Cspan style=\"color: #666666;\"\u003E=\u003C\/span\u003E\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Egrid(\u003Cspan style=\"color: #007020; font-weight: bold;\"\u003ETrue\u003C\/span\u003E)\u003Cbr \/\u003Eplt\u003Cspan style=\"color: #666666;\"\u003E.\u003C\/span\u003Eshow()\u003C\/span\u003E\u003C\/pre\u003E\u003Cpre style=\"background-color: whitesmoke; line-height: 125%; margin: 0px;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp; \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/pre\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Cspan style=\"color: #007020;\"\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003C\/span\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Cspan style=\"font-family: georgia; font-size: medium;\"\u003E\u003Cb\u003E\u003Cspan\u003EReferences:\u003C\/span\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003Col style=\"text-align: left;\"\u003E\u003Cli\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"https:\/\/docs.scipy.org\/doc\/scipy\/reference\/generated\/scipy.optimize.leastsq.html\" target=\"_blank\"\u003ESciPy API leastsq\u003C\/a\u003E \u003Cbr \/\u003E\u003C\/span\u003E\u003C\/li\u003E\u003C\/ol\u003E\u003C\/div\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C!--more--\u003E\u003C\/div\u003E\u003Cdiv style=\"text-align: left;\"\u003E\u003C\/div\u003E\u003Cspan style=\"font-family: inherit; font-size: medium;\"\u003E\u003C\/span\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.datatechnotes.com\/feeds\/4378238453424551095\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/09\/curve-fitting-with-leastsq-function-in-python.html#comment-form","title":"1 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/4378238453424551095"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/3884463987282087684\/posts\/default\/4378238453424551095"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.datatechnotes.com\/2020\/09\/curve-fitting-with-leastsq-function-in-python.html","title":"Curve Fitting Example with leastsq() Function in Python"}],"author":[{"name":{"$t":"Unknown"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-azmg6Dusxng\/X1iYOwShJrI\/AAAAAAAAA9Q\/CE40LL3TARsHMw32zn849YDO5Cm7O57WQCLcBGAsYHQ\/s72-w400-h319-c\/leastsq-curve-fit.png","height":"72","width":"72"},"thr$total":{"$t":"1"}}]}});