Predicting using a dataset
Without much talking, let's take a look at the following code:
import numpy as np import pandas as pd from sklearn.neighbors import KNeighborsClassifier knn = KNeighborsClassifier(n_neighbors=5) data = pd.read_csv('dataset.csv') x = np.array(data[['Time', 'Temp']]) y = np.array(data[['State']]).ravel() knn.fit(x,y) time = raw_input("Enter time") temp = raw_input("Enter temp") data =. [] data.append(float(time)) data.append(float(temp)) a = knn.predict([data]) print(a[0])}
So, let's see what we are doing here:
import numpy as np
We are importing numpy
to our program; this helps us handle lists and matrices:
import pandas as pd
Here, we are importing a library named pandas
; this helps us read files in comma-separated values or in other words, CSV files. We will be using CSV files to store our data and access it for learning process:
from sklearn.neighbors import KNeighborsClassifier
Here, we are importing KneighborsClassifier
from the library sklearn
. sklearn
itself...