Detailed description of the k-means clustering algorithm implemented by Java, k-means clustering

Source: Internet
Author: User

Detailed description of the k-means clustering algorithm implemented by Java, k-means clustering

Requirement

Execute the k-means algorithm for a field in a table in the MySQL database to write the processed data to the new table.

Source code and driver

Kmeans_jb51.rar

Source code

Import java. SQL. *; import java. util. *;/*** @ author tianshl * @ version 2018/1/13 am */public class Kmeans {// source data private List <Integer> origins = new ArrayList <> (); // private Map of grouped data <Double, List <Integer> grouped; // The initial centroid List private List <Double> cores; // the data source private String tableName; private String colName; /*** constructor ** @ param tableName source data table name * @ param colName source data column name * @ param cores center list * /Private Kmeans (String tableName, String colName, List <Double> cores) {this. cores = cores; this. tableName = tableName; this. colName = colName;}/*** recalculate the centroid ** @ return New centroid List */private List <Double> newCores () {List <Double> newCores = new ArrayList <> (); for (List <Integer> v: grouped. values () {newCores. add (v. stream (). reduce (0, (sum, num)-> sum + num)/(v. size () + 0.0);} Collections. sort (newCo Res); return newCores;}/*** determine whether to end ** @ return bool */private Boolean isOver () {List <Double> _ cores = newCores (); for (int I = 0, len = cores. size (); I <len; I ++) {if (! Cores. get (I ). toString (). equals (_ cores. get (I ). toString () {// use the new centroid cores = _ cores; return false;} return true;}/*** Data Group */private void setGrouped () {grouped = new HashMap <> (); Double core; for (Integer origin: origins) {core = getCore (origin); if (! Grouped. containsKey (core) {grouped. put (core, new ArrayList <> ();} grouped. get (core ). add (origin) ;}}/*** select the center of mass ** @ param num the data to be grouped * @ return center of mass */private Double getCore (Integer num) {// difference List <Double> diffs = new ArrayList <> (); // calculate the difference for (Double core: cores) {diffs. add (Math. abs (num-core);} // Minimum deviation-> index-> return cores of the center of mass. get (diffs. indexOf (Collections. min (diffs);}/*** create a database connection * @ Return connection */private Connection getConn () {try {// URL points to the name of the database to be accessed mydata String url = "jdbc: mysql: // localhost: 3306/data_analysis_dev "; // MySQL username String user = "root"; // MySQL password String password = "root"; // load the driver Class. forName ("com. mysql. jdbc. driver "); // declare the Connection object Connection conn = DriverManager. getConnection (url, user, password); if (conn. isClosed () {System. out. println ("Connections Failed to database! "); Return null;} System. out. println (" database connected successfully! "); Return conn;} catch (Exception e) {System. out. println (" failed to connect to the database! "); E. printStackTrace ();} return null;}/*** close database Connection ** @ param conn Connection */private void close (Connection conn) {try {if (conn! = Null &&! Conn. isClosed () conn. close ();} catch (Exception e) {e. printStackTrace () ;}}/*** obtain source data */private void getOrigins () {Connection conn = null; try {conn = getConn (); if (conn = null) return; Statement statement = conn. createStatement (); ResultSet rs = statement.exe cuteQuery (String. format ("select % s from % s", colName, tableName); while (rs. next () {origins. add (rs. getInt (1);} conn. close ();} cat Ch (Exception e) {e. printStackTrace () ;}finally {close (conn) ;}/ *** write data to the new table */private void write () {Connection conn = null; try {conn = getConn (); if (conn = null) return; // Create a table Statement statement = conn. createStatement (); // Delete the old data TABLE statement.exe cute ("drop table if exists k_means ;"); // CREATE a new TABLE statement.exe cute ("create table if not exists k_means ('core' DECIMAL (11, 7), 'col' INTEGER (11 )); "); // Disable automatic submission of conn. setAutoCommit (false); PreparedStatement ps = conn. prepareStatement (" insert into k_means VALUES (?, ?) "); For (Map. entry <Double, List <Integer> entry: grouped. entrySet () {Double core = entry. getKey (); for (Integer value: entry. getValue () {ps. setDouble (1, core); ps. setInt (2, value); ps. addBatch () ;}/// Batch Execute ps.exe cuteBatch (); // submit the transaction conn. commit (); // close the connection conn. close ();} catch (Exception e) {e. printStackTrace () ;}finally {close (conn) ;}/ *** process data */private void run () {System. out. println ("Get Source data "); // get source data getOrigins (); // stop grouping Boolean isOver = false; System. out. println (" data group processing "); while (! IsOver) {// data group setGrouped (); // determine whether to stop the group isOver = isOver ();} System. out. println ("write processed data to the database"); // write grouped data to the new table write (); System. out. println ("Data Writing completed");} public static void main (String [] args) {List <Double> cores = new ArrayList <> (); cores. add (1, 260.0); cores. add (600.0); // table name, column name, centroid list new Kmeans ("attributes", "attr_length", cores ). run ();}}

Source File

Kmeans.java

Compile

javac Kmeans.java 

Run

# Specify the dependent library java-Djava. ext. dirs =./lib Kmeans

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.