Computer Science > STUDY GUIDE > kmeans_clusters.cpp University of Michigan ENGR 151 (All)

kmeans_clusters.cpp University of Michigan ENGR 151

Document Content and Description Below

#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 &centroids); // give random initial values for centroids double distance(car_data data, car_data centroids, int carIndex, int centroidIndex); double check_convergence(const car_data &centroids, const car_data &centroids_old); // find sum of squares difference between old and new centroids void assign_clusters(const car_data &centroids, car_data &data_norm); // for each mpg, etc. find nearest cluster void find_new_centroids(car_data &centroids, 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 Now

Instant download

We Accept:

We Accept
document-preview

Buy this document to get the full access instantly

Instant Download Access after purchase

Buy Now

Instant download

We Accept:

We Accept

Reviews( 0 )

$6.00

Buy Now

We Accept:

We Accept

Instant download

Can't find what you want? Try our AI powered Search

94
0

Document information


Connected school, study & course


About the document


Uploaded On

Nov 17, 2022

Number of pages

6

Written in

Seller


seller-icon
Browsegrades

Member since 2 years

0 Documents Sold

Additional information

This document has been written for:

Uploaded

Nov 17, 2022

Downloads

 0

Views

 94

Document Keyword Tags


$6.00
What is Scholarfriends

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 are here to help

We're available through e-mail, Twitter, Facebook, and live chat.
 FAQ
 Questions? Leave a message!

Follow us on
 Twitter

Copyright © Scholarfriends · High quality services·