2016.12.07
Finally got in touch with the legendary machine learning.
first look at a picture
For us humans, we can quickly see the type of movie, but the machine is different. At this time, we will use machine learning.
This is where the Euler distance formula is used.
The following is a piece of code to implement.
The following is the class of the main function:
package RobotStudy; public class test { public static void main(String[] args) { movie[] movies = new movie[6];// Existing template data movies[0] = new movie(3, 104, "love"); movies[1] = new movie(2, 100, "love"); movies[2] = new movie(1, 81, "love"); movies[3] = new movie(101, 10, "action"); movies[4] = new movie(99, 5, "action"); movies[5] = new movie(98, 2, "action"); movie new_movie = new movie(18, 90);// New data to be classified double[] distances = new double[6];// Calculate Euclidean distance: the distance between the new data and each template data for (int i = 0; i < 6; i++) { distances[i] = Math.sqrt(Math.pow((new_movie.Action_Count - movies[i].Action_Count),2) + Math.pow( (new_movie.Kiss_Count - movies[i].Kiss_Count), 2)); System.out.println(new_movie.Action_Count); System.out.println(new_movie.Kiss_Count); System.out.println("The distance between the new movie and the " + i + " movie is: " + distances[i]); } double min_distance = distances[0];// find the minimum distance int min = 0;// Template number corresponding to the minimum distance for (int i = 1; i < 6; i++) { if(min_distance > distances[i]){ min_distance = distances[i]; min = i; } } System.out.println("The type of the new movie is: " + movies[min].type); } }
Define another class to hold the properties:
package RobotStudy; public class movie { public int Action_Count; public int Kiss_Count; public String type; public movie(int action_Count, int kiss_Count, String type) { super(); this.Action_Count = action_Count; this.Kiss_Count = kiss_Count; this.type = type; } public movie(int action_Count, int kiss_Count) { super(); this.Action_Count = action_Count; this.Kiss_Count = kiss_Count; } }