39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
from IPython.display import display
|
|
import sklearn
|
|
from sklearn.linear_model import LinearRegression
|
|
from sklearn.tree import DecisionTreeRegressor
|
|
from sklearn.model_selection import train_test_split
|
|
import mglearn
|
|
X, y = mglearn.datasets.make_forge()
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
|
|
from sklearn.neighbors import KNeighborsClassifier
|
|
clf = KNeighborsClassifier(n_neighbors=3)
|
|
clf.fit(X_train, y_train)
|
|
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',metric_params=None, n_jobs=1, n_neighbors=3, p=2,weights='uniform')
|
|
|
|
clf.predict(X_test)
|
|
|
|
clf.score(X_test, y_test)
|
|
fig, axes = plt.subplots(1, 3, figsize=(10, 3))
|
|
for n_neighbors, ax in zip([1, 3, 9], axes):
|
|
clf = KNeighborsClassifier(n_neighbors=n_neighbors).fit(X, y)
|
|
mglearn.plots.plot_2d_separator(clf, X, fill=True, eps=0.5, ax=ax, alpha=.4)
|
|
ax.scatter(X[:, 0], X[:, 1], c=y, s=60, cmap=mglearn.cm2)
|
|
ax.set_title("%d neighbor(s)" % n_neighbors)
|
|
|
|
|
|
data = np.loadtxt('src/Hudson_Bay.csv', delimiter=',', skiprows=1)
|
|
x = data[:,0]
|
|
y = data[:,1]
|
|
#x_train, y_train = train_test_split(x, y, random_state=0)
|
|
line = np.linspace(1900,1930,1000,endpoint=False).reshape(-1,1)
|
|
reg = DecisionTreeRegressor(min_samples_split=3).fit(x.reshape(-1,1),y.reshape(-1,1))
|
|
plt.plot(line, reg.predict(line), label="decision tree")
|
|
regline = LinearRegression().fit(x.reshape(-1,1),y.reshape(-1,1))
|
|
plt.plot(line, regline.predict(line), label= "Linear Regression")
|
|
plt.plot(x, y, label= "Linear Regression")
|
|
plt.show()
|
|
|