---
# System prepended metadata

title: ML HW 2

---

# ML HW 2


![](https://hackmd.io/_uploads/Skkd62UGp.png)
![](https://hackmd.io/_uploads/Hk8dTnIfp.png)
![](https://hackmd.io/_uploads/r1iu62IM6.png)
![](https://hackmd.io/_uploads/S1gtphLza.png)
![](https://hackmd.io/_uploads/ByHKThUf6.png)
![](https://hackmd.io/_uploads/Sk9KphUfT.png)


#### 4

```python
import os
import sys
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix


def read_csv():
	df = pd.read_csv('IRIS.csv')
	return df

def train_test(df):
	'''
	Part a : Train test split 7 : 3 
	'''
	X = df[['sepal_length','sepal_width', 'petal_length', 'petal_width']].values
	Y = df['species'].values

	X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.3)

	return X_train, X_test, y_train, y_test


def train_model(X_train, y_train):
	'''
	Part b : Train model
	'''
	model = SVC(gamma='auto').fit(X_train,y_train)
	return model


def predict(model, X_test, y_test):
	'''
	Part c : Predict and Results
	'''

	pred = model.predict(X_test)
	confusion_result = confusion_matrix(y_test,pred)
	classifcation_result = classification_report(y_test,pred)
	return pred, confusion_result, classifcation_result


def main():
	df = read_csv()
	X_train, X_test, y_train, y_test = train_test(df)
	model = train_model(X_train, y_train)

	pred, confusion_result, classifcation_result = predict(model, X_test, y_test)


	print(">>>>>>>>>>>>>>>> Confusion Matrix : <<<<<<<<<<<\n", confusion_result)
	print(">>>>>>>>>>>>>>>> Classification Report:<<<<<<<<<\n", classifcation_result)



if __name__ == '__main__':
	main()
```

![](https://hackmd.io/_uploads/By1CanLza.png)
