<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Garvit Joshi on Medium]]></title>
        <description><![CDATA[Stories by Garvit Joshi on Medium]]></description>
        <link>https://medium.com/@joshigarvit.work?source=rss-6bdc538db06f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*kut_U5BZ32RIAvzm.</url>
            <title>Stories by Garvit Joshi on Medium</title>
            <link>https://medium.com/@joshigarvit.work?source=rss-6bdc538db06f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 07:20:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@joshigarvit.work/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[RR_Lyrae Star Prediction and analysis of efficiency of various algorithm]]></title>
            <link>https://medium.com/@joshigarvit.work/rr-lyrae-star-prediction-and-analysis-of-efficiency-of-various-algorithm-a38e441d4321?source=rss-6bdc538db06f------2</link>
            <guid isPermaLink="false">https://medium.com/p/a38e441d4321</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[k-neighbours]]></category>
            <category><![CDATA[astroml]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[rr-lyrae]]></category>
            <dc:creator><![CDATA[Garvit Joshi]]></dc:creator>
            <pubDate>Tue, 30 Jan 2018 20:48:56 GMT</pubDate>
            <atom:updated>2018-02-02T06:34:12.895Z</atom:updated>
            <content:encoded><![CDATA[<p>A guide to create a prediction module in python.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/298/1*ppkk4Tot9hWZL4DvNyJs_A.png" /><figcaption>Illustration of the typical light curve for RR Lyrae</figcaption></figure><h3>A brief information about RR_Lyrae Stars</h3><p>RR Lyrae variables are periodic variable stars, commonly found in globular clusters and are used as standard candles to measure (extra)galactic distances.<br>RR Lyraes are pulsating horizontal branch ageing stars of spectral class A or F, with a mass of around half the Sun’s. They are thought to have previously shed mass during the Red-giant branch phase, and consequently, they were once stars with similar or slightly less mass than the Sun, around 0.8 solar masses.</p><ul><li>RRab variables are the most common, making up 91% of all observed RR Lyrae, and display the steep rises in brightness typical of RR Lyrae</li><li>RRc are less common, making up 9% of observed RR Lyrae, and have shorter periods and more sinusoidal variation</li><li>RRd are rare, making up between &lt;1% and 30% of RR Lyrae in a system, and are double-mode pulsators, unlike RRab and RRc.</li></ul><h3>Algorithms that are being compared</h3><ol><li>Decision Tree</li><li>SVM</li><li>Linear Discriminator</li><li>K-Neighbours</li><li>Logistic Regression</li></ol><h3>How to do the experiment</h3><p>This module is developed using python and following libraries are used you can install most of them with conda and then install astroML and it’s add-ons.</p><p>First, create a <a href="http://jupyter.org/">jupyter notebook</a> and give it an apt name.</p><ol><li><a href="https://pandas.pydata.org/">Pandas</a></li><li><a href="http://scikit-learn.org/stable/">scikit-learn</a></li><li><a href="http://www.numpy.org/">numpy</a></li><li><a href="https://matplotlib.org/">matplotlib</a></li><li><a href="https://seaborn.pydata.org/">seaborn</a></li><li><a href="http://www.astroml.org/">astroML</a></li></ol><p>Install astroML and addons via pip</p><pre>pip install astroML</pre><pre>pip install astroML_addons</pre><p>For importing the linear Discriminator there is an alternative code too</p><pre>from sklearn.lda import LDA</pre><p>This works in latest version through my jupyter notebook at the time of experiment was running an older version of the library so the old version of the code is being included too.</p><pre>import numpy as np<br>import pandas as pd<br>from matplotlib import pyplot as plt<br>import seaborn as sns</pre><pre>from astroML.datasets import fetch_rrlyrae_combined<br>from astroML.utils import completeness_contamination<br>from astroML.utils import split_samples<br>from astroML.plotting import setup_text_plots</pre><pre>from sklearn.tree import DecisionTreeClassifier<br>from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA<br>from sklearn.linear_model import LogisticRegression<br>from sklearn.neighbors import KNeighborsClassifier<br>from sklearn.metrics import precision_recall_curve, roc_curve<br>from sklearn.svm import SVC<br>from sklearn.model_selection import cross_val_score</pre><p>Let’s define the text properties for the graphs you can skip this</p><pre>setup_text_plots(fontsize=8, usetex=True)</pre><p>We will now import and split data into test and train.</p><p>Whenever we use an ML module we divide the data into two sets so that we can train the module and then get the prediction efficiency also, we are converting the data into two pandas dataframe this will allow proper data manipulation later in the notebook. This library makes the data visualization better.</p><p>You can keep the random state to 0 but since data is limited and we want to avoid overfitting we will set it 1.</p><pre>X, y = fetch_rrlyrae_combined()<br>xd = pd.DataFrame(data=X)<br>yd = pd.DataFrame(data=y)<br>(X_train, X_test), (y_train, y_test) = split_samples(X, y, [0.75, 0.25], random_state=1)<br></pre><p>We will construct a Decision tree this is a type of classification algorithm</p><pre>clf = DecisionTreeClassifier(random_state=1, criterion=&#39;entropy&#39;)</pre><p>We will now fit our data to train the decision tree there are two data sets we have made x_train and y_train we have the variable in x and we need to generate output in form of y. basically we have x as inputs and we need to predict the value of the y (1,0) ie rr_lyare or not.</p><pre>clf.fit(X_train, y_train)</pre><p>you output should be like</p><pre>DecisionTreeClassifier(class_weight=None, criterion=&#39;entropy&#39;, max_depth=None,<br>            max_features=None, max_leaf_nodes=None,<br>            min_impurity_decrease=0.0, min_impurity_split=None,<br>            min_samples_leaf=1, min_samples_split=2,<br>            min_weight_fraction_leaf=0.0, presort=False, random_state=1,<br>            splitter=&#39;best&#39;)</pre><p>We will now generate a prediction score</p><pre>y_out = clf.predict(X_test)<br>clf.predict_proba(X_test, check_input=True)</pre><pre><strong><em>output will be</em></strong><br>array([[ 1.,  0.],<br>       [ 1.,  0.],<br>       [ 1.,  0.],<br>       ..., <br>       [ 1.,  0.],<br>       [ 1.,  0.],<br>       [ 1.,  0.]])</pre><p>We will now generate average accuracy score</p><pre>clf.score(X_test,y_test)</pre><pre><strong><em>output will be </em></strong><br>0.99484668899768103</pre><p>We will now gene the accuracy score that defines how well the algorithm can predict the data values also we can see how many mistakes have been made by the algorithm.</p><pre>tot_neg = np.sum(y_test == 0)<br>tot_pos = np.sum(y_test == 1)</pre><pre>fn = np.sum((y_test == 1) &amp; (y_out == 0))<br>tn = np.sum((y_test == 0) &amp; (y_out == 0))</pre><pre>fp = np.sum((y_test == 0) &amp; (y_out == 1))<br>tp = np.sum((y_test == 1) &amp; (y_out == 1))</pre><pre>print( &quot;----------------------------------------------------------------&quot;)<br>print (&quot;partial training set: (%i non-variable, %i RR Lyrae)&quot;<br>       % (np.sum(y_train == 0), np.sum(y_train == 1)))<br>print( &quot;positive = RR Lyrae, negative = non-variable&quot;)<br>print (&quot;false positives: %i (%.1f%%)&quot; % (fp, fp * 100. / (fp + tp)))<br>print (&quot;false negatives: %i (%.1f%%)&quot; % (fn, fn * 100. / (fn + tn)))</pre><pre><strong><em>output generated is </em></strong><br>----------------------------------------------------------------<br>partial training set: (69499 non-variable, 356 RR Lyrae)<br>positive = RR Lyrae, negative = non-variable<br>false positives: 65 (47.4%)<br>false negatives: 55 (0.2%)</pre><p>We will genrate a SVM and also get the crossvalidation the accuracy which is quite well in the given instance.</p><pre>clf =SVC(random_state=1)<br>clf.fit(X_train, y_train) <br>clf.score(X_test,y_test)<br>print(clf.predict(X_test))</pre><pre><strong><em>output gernerated is </em></strong><em><br></em>0.99454607918921245</pre><pre>[ 0.  0.  0. ...,  0.  0.  0.]</pre><p>Scatter plot of the data shows that which ones are stars and rr_lyrae stars in which photometric domain. Matplotlib is used here and full data is utilised</p><pre>stars = (y == 0)<br>rrlyrae = (y == 1)<br>ax = plt.axes()</pre><pre>ax.plot(X[stars, 0], X[stars, 1], ‘.’, ms=5, c=’b’, label=’stars’)<br>ax.plot(X[rrlyrae, 0], X[rrlyrae, 1], ‘.’, ms=5, c=’r’, label=’RR-Lyrae’)</pre><pre>ax.legend(loc=10)</pre><pre>ax.set_xlabel(‘$u-g$’)<br>ax.set_ylabel(‘$g-r$’)</pre><pre>ax.set_xlim(0.7, 1.4)<br>ax.set_ylim(-0.2, 0.4)</pre><pre>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*IE0X2jQKQybn3jtiIvZ79Q.png" /></figure><p>We will now show the plot via seaborn. We have various subplots explaining the distribution of photo-metric domains.that define a star in multiple distributions.</p><pre>sns.set(style=&quot;ticks&quot;, color_codes=True)<br>sns.pairplot(xd, diag_kind=&quot;kde&quot;, markers=&quot;+&quot;,plot_kws=dict(s=50, edgecolor=&quot;b&quot;, linewidth=1),diag_kws=dict(shade=True))<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/713/1*Vt_9GtqhA5sO_qi4k0cfsw.png" /></figure><p>The sample is further constrained to a smaller region of the overall color–color space following [8] (0.7 &lt; u − g &lt; 1.35,−0.15 &lt; g − r &lt; 0.4, −0.15 &lt; r − i &lt; 0.22, and −0.21 &lt; i − z &lt; 0.25)</p><pre>sns.set(style=&quot;whitegrid&quot;, color_codes=True)<br>sns.stripplot( data=xd, jitter=True)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*sQzLNm79jdIY10ncp-McXA.png" /></figure><p>We can make a plot that shows the distribution of data in y this shows which category the star is this is not much help but this is integral part of the system.</p><pre>sns.pairplot(yd, diag_kind=&quot;kde&quot;, markers=&quot;+&quot;,plot_kws=dict(s=50, edgecolor=&quot;b&quot;, linewidth=1),diag_kws=dict(shade=True))<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/160/1*yJuJeTLInUtS4gKBewp_HQ.png" /></figure><p>The data inside the pandas dataframe can be viewed directly.</p><pre>print(xd)<br>print(yd)</pre><pre><strong><em>output generated is <br></em></strong>              0         1         2         3<br>0      1.250999  0.394000  0.137000  0.061999<br>1      1.048000  0.339001  0.151999  0.023001<br>2      1.008001  0.341999  0.129000  0.203001<br>3      0.965000  0.392000  0.149000  0.150000<br>4      1.040001  0.333000  0.125999  0.101999<br>5      1.154001  0.373999  0.145000  0.121000<br>...         ...       ...       ...       ...<br>93136  0.962999  0.059000 -0.025999 -0.025000<br>93137  1.059999  0.185001  0.050999 -0.023998<br>93138  1.044001  0.212000  0.035000  0.002001<br>93139  1.064999  0.172001  0.042000  0.003000<br>93140  1.125999  0.065001 -0.017000 -0.057999<br><br>[93141 rows x 4 columns]</pre><pre>        0<br>0      0.0<br>1      0.0<br>2      0.0<br>3      0.0<br>4      0.0<br>5      0.0<br>...    ...<br>93136  1.0<br>93137  1.0<br>93138  1.0<br>93139  1.0<br>93140  1.0<br><br>[93141 rows x 1 columns]</pre><p>We will define other algorithms like the previous SVM but we will be trying regression to linear one the kernel used in SVM was linear and the regression is logistics. then we have other classification algorithms our goal as mentioned above will be to plot curves to see which one is most suitable for the data.</p><p>A hint the Logistic regression doesn’t support class weights as auto if you try out dict else it will be none but the balanced setting should be used to avoid any bias towards any classes.</p><pre>def compute_models(*args):<br>    names = []<br>    probs = []<br>    for classifier, kwargs in args:<br>        print (classifier.__name__)<br>        clf = classifier(**kwargs)<br>        clf.fit(X_train, y_train)<br>        y_probs = clf.predict_proba(X_test)[:, 1]</pre><pre>names.append(classifier.__name__)<br>        probs.append(y_probs)</pre><pre>return names, probs</pre><pre>names, probs = compute_models(<br>                              (LDA, {}),<br>                              <br>                              (LogisticRegression,<br>                               dict(class_weight=&#39;balanced&#39;)),<br>                              (KNeighborsClassifier,<br>                               dict(n_neighbors=10)),<br>                              (DecisionTreeClassifier,<br>                               dict(random_state=0, max_depth=12,<br>                                    criterion=&#39;entropy&#39;)),<br>                             )<br><strong><em>output generated is<br></em></strong>LinearDiscriminantAnalysis<br>LogisticRegression<br>KNeighborsClassifier<br>DecisionTreeClassifier</pre><p>Finally, this segment will test different algo’s efficiency and see the ROC curve to decide which one is most suitable. Custom Labels for the algorithms are defined please check underlying code for full form the abbreviations used.</p><pre>fig = plt.figure(figsize=(12, 8.5))<br>fig.subplots_adjust(left=0.1, right=0.95, bottom=0.15, top=0.9, wspace=0.25)</pre><pre># ax2 will show roc curves<br>ax1 = plt.subplot(121)</pre><pre># ax1 will show completeness/efficiency<br>ax2 = plt.subplot(122)</pre><pre>labels = dict(LinearDiscriminantAnalysis=&#39;LDA&#39;,<br>              KNeighborsClassifier=&#39;KNN&#39;,<br>              DecisionTreeClassifier=&#39;DT&#39;,<br>              LogisticRegression=&#39;LR&#39;)</pre><p>Now we will generate ROC curves.</p><pre>thresholds = np.linspace(0, 1, 1001)[:-1]</pre><pre># iterate through and show results<br>for name, y_prob in zip(names, probs):<br>    fpr, tpr, thresh = roc_curve(y_test, y_prob)</pre><pre># add (0, 0) as first point<br>    fpr = np.concatenate([[0], fpr])<br>    tpr = np.concatenate([[0], tpr])</pre><pre>ax1.plot(fpr, tpr, label=labels[name])</pre><pre>comp = np.zeros_like(thresholds)<br>    cont = np.zeros_like(thresholds)<br>    for i, t in enumerate(thresholds):<br>        y_pred = (y_prob &gt;= t)<br>        comp[i], cont[i] = completeness_contamination(y_pred, y_test)<br>    ax2.plot(1 - cont, comp, label=labels[name])</pre><pre>ax1.set_xlim(0, 0.04)<br>ax1.set_ylim(0, 1.02)<br>ax1.xaxis.set_major_locator(plt.MaxNLocator(5))<br>ax1.set_xlabel(&#39;false positive rate&#39;)<br>ax1.set_ylabel(&#39;true positive rate&#39;)<br>ax1.legend(loc=4)</pre><pre>ax2.set_xlabel(&#39;efficiency&#39;)<br>ax2.set_ylabel(&#39;completeness&#39;)<br>ax2.set_xlim(0, 1.0)<br>ax2.set_ylim(0.2, 1.02)</pre><pre>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*kSoirrvzv6ssH-0L99QRyQ.png" /></figure><p>We can conclude that the slopes of the KNN and DT are quite good and fits the criterion for a good algorithm thus we can say that these two are good ones. The curves for LR and LDA are quite steep.</p><p>The neural network may have higher performance but the time taken will be a negative factor and we have not covered random decision tree and neural networks, they will be covered in subsequent posts.</p><h3>our team</h3><ol><li><a href="https://www.linkedin.com/in/joshigarvitgh/">Garvit Joshi</a></li><li><a href="https://www.linkedin.com/in/jasvinder04/">Jasvinder Singh</a></li><li><a href="https://www.linkedin.com/in/shivani-joshi-565219124/">Shivani Joshi</a></li></ol><p>sources: Wikipedia and astroml</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a38e441d4321" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>