Computer Science > STUDY GUIDE > kmeans_clusters.cpp University of Michigan ENGR 151 (All)
#include <iostream> #include <fstream> #include <string> #include <vector> #include <math.h> using namespace std; // structure for holding data from cars.csv struct car_data { vector<string>... headers; // stores the headers int N_rows; // stores number of rows // data in the 8 columns in cars.csv vector<double> mpg; vector<double> cylinders; vector<double> cubicinches; vector<double> hp; vector<double> weightlbs; vector<double> time260; vector<double> year; vector<string> region; vector<int> clusters; // cluster 1,2,3 that car is assigned to }; void read_csv(ifstream &fin, car_data &dat); // read data from cars.csv into struct car_data void normalize(car_data &data_norm);// normalize data void unnormalize(car_data &data_norm,car_data &data); // take normalized data_norm and unnormalize it using max/min values from data void initialize(car_data ¢roids); // give random initial values for centroids double distance(car_data data, car_data centroids, int carIndex, int centroidIndex); double check_convergence(const car_data ¢roids, const car_data ¢roids_old); // find sum of squares difference between old and new centroids void assign_clusters(const car_data ¢roids, car_data &data_norm); // for each mpg, etc. find nearest cluster void find_new_centroids(car_data ¢roids, const car_data &data_norm); // find average position for each cluster to define new centroids void print(ostream &fout, car_data &data); // print to cout or file (ostream can be either) double minVec(vector<double> a); double maxVec(vector<double> a); int main() { car_data data; ifstream fin("cars.csv"); // Step 1 - read in data and verify read_csv(fin, data); data.headers.pop_back(); data.headers.push_back(" cluster"); //print(cout,data); // good to check output for debugging fin.close(); // Step 2 - Normalize vectors - only columns 1-7 car_data data_norm = data; normalize(data_norm); //print(data_no [Show More]
Last updated: 2 years ago
Preview 1 out of 6 pages
Buy this document to get the full access instantly
Instant Download Access after purchase
Buy NowInstant download
We Accept:
Can't find what you want? Try our AI powered Search
Connected school, study & course
About the document
Uploaded On
Nov 17, 2022
Number of pages
6
Written in
This document has been written for:
Uploaded
Nov 17, 2022
Downloads
0
Views
94
In Scholarfriends, a student can earn by offering help to other student. Students can help other students with materials by upploading their notes and earn money.
We're available through e-mail, Twitter, Facebook, and live chat.
FAQ
Questions? Leave a message!
Copyright © Scholarfriends · High quality services·