<?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 Yana Santos on Medium]]></title>
        <description><![CDATA[Stories by Yana Santos on Medium]]></description>
        <link>https://medium.com/@yanasantos?source=rss-cab279bb0eaf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DyOBmnw01lm5oeVSqBjszA.jpeg</url>
            <title>Stories by Yana Santos on Medium</title>
            <link>https://medium.com/@yanasantos?source=rss-cab279bb0eaf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 13:21:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yanasantos/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[Breast Cancer Dataset Classification Using KNN Algorithm]]></title>
            <link>https://medium.com/suzano-digitaltech/breast-cancer-dataset-classification-using-knn-algorithm-696ce1b39b3f?source=rss-cab279bb0eaf------2</link>
            <guid isPermaLink="false">https://medium.com/p/696ce1b39b3f</guid>
            <category><![CDATA[knn]]></category>
            <category><![CDATA[classification]]></category>
            <category><![CDATA[classification-algorithms]]></category>
            <category><![CDATA[eda]]></category>
            <dc:creator><![CDATA[Yana Santos]]></dc:creator>
            <pubDate>Sun, 25 Dec 2022 15:19:15 GMT</pubDate>
            <atom:updated>2023-01-12T19:45:39.843Z</atom:updated>
            <content:encoded><![CDATA[<p>This article will discuss how KNN Classification Algorithm works and also present an example of classification using Breast Cancer Wisconsin Dataset. At the end, the algorithm performance will be analyzed.</p><h4>Dataset</h4><p>The dataset features are computed from a digitized image of a fine needle aspirate (FNA) of a breast mass. They describe characteristics of the cell nuclei present in the image. Check the <a href="https://www.kaggle.com/datasets/uciml/breast-cancer-wisconsin-data">dataset</a> on Kaggle to download and to read more information about.</p><figure><img alt="Breast Cancer Dataset" src="https://cdn-images-1.medium.com/max/950/0*_b2pW8NHRsUikamx.jpg" /></figure><h4>Exploratory Data Analysis</h4><p>This section contains only graphical representations of EDA. If you are interested to check the code I wrote to data treatment and to achieve the results, feel free to acess the original <a href="https://github.com/Yanasants/breast_cancer/blob/main/breast_cancer_analysis.ipynb">file</a>.</p><p>Checking the number of occurences by diagnosis, it is clear to see that that there are more occurrences classified as benign than malignant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*nVoinGJ2itKSNTqbYbSoIQ.png" /></figure><p>Looking to the correlation between the features and the diagnosis is also an important part to understand data and find patterns.</p><figure><img alt="Correlation Between Features and Diagnosis" src="https://cdn-images-1.medium.com/max/820/1*TT-tN86E0p9XDBf2HL4BsA.png" /></figure><p>As presented in the bar chart above, top 4 features with highest correlation with diagnosis are <em>Mean Concave Points, Mean Perimeter, Mean Radius </em>and<em> Mean Area.</em> But how are their values distributed by diagnosis?</p><figure><img alt="Mean Concave Points" src="https://cdn-images-1.medium.com/max/820/1*QyWTy_1P2ihOSE5kEEyEiA.png" /></figure><p>Once perimeter and area are calculated as a function of radius, the distribution of radius values was set to be analyzed.</p><figure><img alt="Mean Radius Distribution by Diagnosis" src="https://cdn-images-1.medium.com/max/820/1*E043_bPIxp7ThjIAaumu2w.png" /></figure><p>The main conclusions of EDA are a) dataset contains more benign occurences; b) number of concave points and radius presented the highest correlation with diagnosis; c) benign occurences are concentrated in lower radius values and lower number of concave points than malignant ones. Then, after the general understanding of features, correlations and patterns, the classification of the dataset can begin.</p><h4>K-Nearest Neighbors</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3fGTY0FIHPHx2_Nm0X_qwQ.png" /></figure><p>K-Nearest Neighbors (KNN) corresponds to a supervised learning algorithm used for both classification and regression. It is necessary to define features by which KNN will find the classification. During training, the algorithm will learn how to relate the features and their respective classification. As presented in the flowchart above, KNN receives an unclassified data and calculates the distance between test data and the training occurrences. The most popular calculation in this step is the Euclidian distance, which has the formula shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*BMLXR-CILkUuYauDq_PjkA.png" /></figure><p>Then, a <em>K </em>number of nearest neighbors (hyperparameter) needs to be set. If number 5 was set, for example, the algorithm will focus on the 5 nearest neighbors’ classes. Considering that 3 of these neighbors were classified as malignant, then KNN will classify the analyzed point as malignant, the most recurrent classe between the neighbors. Because of this methodology, odd numbers presents better results than the even ones which can cause draws.</p><h4>Breast Cancer Dataset Classification</h4><p>Loading Dataset</p><pre># Loading Filtered Dataset (link available on EDA section)<br>mean_df = pd.read_csv(&#39;breast_cancer_data.csv&#39;)</pre><p>Setting independent and dependent variables</p><pre># Independent variables (features) <br>x = mean_df.drop(&#39;diagnosis&#39;, axis=1)<br># Dependent variables (classification)<br>y = mean_df[&#39;diagnosis&#39;]</pre><p>Splitting dataset: Training and Testing</p><pre># Subdividing training and testing data<br>x_train,x_test,y_train,y_test = train_test_split(x,y, test_size=0.2)</pre><p>Scale Standardizing</p><pre># standardizing the scale<br>scaler = StandardScaler()<br>scaler.fit(x_train)<br>x_train = scaler.transform(x_train)<br>x_test = scaler.transform(x_test)</pre><p>Implementation</p><p>At this part of the code, the classifier was trained using different K-Values in order to test which one would present the best results and this process was repeated over different executions. All results of the each execution were saved on an external file of <em>csv </em>format, in order to register and simplify later comparisons. This methodology was used after the realization that the results presented variations during different executions <strong>if you restart the kernel</strong>. It means that after each execution, kernel must be restarted and the <em>number_of_execution </em>variable must be manually changed.</p><p>In addition, the report function used to generate a dataframe with the values of the metrics to analyze performance and the function to generate csv file were also created by me and they are available <a href="https://github.com/Yanasants/breast_cancer/blob/main/analysis_functions.py">here</a>.</p><pre># Number of execution must be manually changed after kernel restarting<br>number_of_execution = 1<br><br>all_exec_df = []<br><br>for k in range(1,10):<br>    knn = KNeighborsClassifier(n_neighbors=k)<br>    knn.fit(x_train, y_train)<br>    y_pred = knn.predict(x_test)<br>    df_report = report(K_value=k, y_test=y_test, y_pred=y_pred)<br>    all_exec_df.append(df_report)<br><br>generate_csv(all_exec_df=all_exec_df, number_of_execution=number_of_execution)</pre><p>If you are looking for a simpler implementation, ignore the part of multiple executions and I also recommend you to see the metrics values and check algorithm performance using<a href="https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html"> classification_report</a> from <strong>Sklearn</strong>.</p><pre>from sklearn.metrics import classification_report</pre><p>Finding best K-Value</p><p>This section contains only graphical representations of the results, but feel free to read the complete <a href="https://github.com/Yanasants/breast_cancer/blob/main/results_analysis.ipynb">notebook</a> I wrote to get them. I also recommend you to read about the most popular metrics to evaluate machine learning models in case you are not familiarized with them.</p><p>K-Values between 1 and 9 were used during 10 executions. Looking to the mean values of all executions, the results were as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sxk0JGBLCEdLCa3uWrrcUQ.png" /></figure><p>The minimum error (0.0605) was obtained using K-Value equal to 5. Now looking for the F1-Score:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qnkbYW9szzTuhTy-Zb2F3A.png" /></figure><p>The maximum F1-Score (0.9360) was obtained using K-Value equal to 5. Same behavior occured to Recall (0.9325), but checking Precision, K-Value equal to 2 presented the largest value (0.9484).</p><p>It is possible to observe the distribution of F1-Score to each K-Value during executions, represented by the boxplots below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sBobMwb-HJ36Qekk3OilMg.png" /></figure><p>In this way, once K-Value 5 presented the minimum Mean Error and the maximum Mean F1-Score Value with a considerable distance from the others, it was set as the best K-Value to the classification. If during the executions the outputs were not clear enough to recognize which number was the best hiperparameter, some non-parametric tests would be applied, like Kruskal-Wallis one. It is important to priorize the smallest number in these cases, focusing on reducing computational effort.</p><h4>Classification Report</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*KX63ok4jy4sWGjyTWpxFWw.png" /></figure><p>As presented by the boxplots, the algorithm had largest F1-Score to Benign occurences (Median equal to 0.9491, Standard-Deviation equal to 0.0175) than Malignant ones (Median equal to 0.9208, Standard-Deviation equal to 0.0240). Analyzing mean values (considering both diagnosis), the results obtained were: a) Precision: 0.9413; b) Recall: 0.9326. The Mean Error was equivalent to 0.0605.</p><h4>Conclusion</h4><p>Therefore, KNN Algorithm is a simple algorithm to implement and in many cases presents satisfactory results. In this implementation, all dataset features were used to predict the diagnosis, but testing classifying using only features with largest correlation with diagnosis, as found during EDA section, would also be a good alternative. By the other hand, KNN is not recommend to use with large data volumes. The dataset used in this article contains only 569 entries and it is a factor that contributes to the good performance. It is also interesting to highlight that most of biomedical datasets present small data volumes due to the difficult acquisition, so exploring KNN Algorithm to applications of Machine Learning into Health Sector may be a promising way forward.</p><h4>GitHub Repository</h4><p>All files used to the classification are available on my GitHub profile. Explore the <a href="https://github.com/Yanasants/breast_cancer">repository</a> if you want to.</p><h4>References</h4><p>KNN (K-Nearest Neighbors). Medium, 2021. Available on:<br>&lt;<a href="https://medium.com/swlh/k-nearest-neighbor-ca2593d7a3c4">https://medium.com/brasil-ai/knn-k-nearest-neighbors-1-e140c82e9c4e</a>&gt;.</p><p>O Algorítmo K-Nearest Neighbors (KNN) Em Machine Learning. Portal Data Science, 2018. Available on: &lt;<a href="https://portaldatascience.com/o-algoritmo-k-nearest-neighbors-knn-em-machine-learning/#:~:text=A%20dist%C3%A2ncia%20euclidiana%20%C3%A9%20calculada,os%20atributos%20de%20entrada%20j.&amp;text=Outras%20medidas%20populares%20de%20dist%C3%A2ncia,os%20vetores%20bin%C3%A1rios%20(%20mais%20).">acess_link</a>&gt;</p><p><strong><em>Your feedback is extremely important to me! Please comment if the article was helpful. Suggestions are also welcome!</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=696ce1b39b3f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/suzano-digitaltech/breast-cancer-dataset-classification-using-knn-algorithm-696ce1b39b3f">Breast Cancer Dataset Classification Using KNN Algorithm</a> was originally published in <a href="https://medium.com/suzano-digitaltech">Suzano DigitalTech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>