Update on r codes
This commit is contained in:
@@ -112,9 +112,11 @@ either Python or C++ as programming languages.
|
||||
To add more entropy, _cython_ can also be used when running your notebooks. It means that Python with the Jupyter/IPython notebook
|
||||
setup allows you to integrate widely popular softwares and tools for scientific computing. With its versatility,
|
||||
including symbolic operations, Python offers a unique computational environment. Your Jupyter/IPython notebook
|
||||
can easily be converted into a nicely rendered _PDF_ file or a Latex file for further processing.
|
||||
can easily be converted into a nicely rendered _PDF_ file or a Latex file for further processing. For example, convert to latex as
|
||||
!bc pycod
|
||||
jupyter nbconvert filename.ipynb --to latex
|
||||
!ec
|
||||
|
||||
This never ends.
|
||||
If you use the light mark-up language _doconce_ you can convert a standard ascii text file into various HTML
|
||||
formats, ipython notebooks, latex files, pdf files etc.
|
||||
|
||||
@@ -445,7 +447,7 @@ plt.show()
|
||||
!split
|
||||
===== Linear Least squares in R =====
|
||||
!bblock
|
||||
!bc pycod
|
||||
!bc r
|
||||
HudsonBay = read.csv("src/Hudson_Bay.csv",header=T)
|
||||
fix(HudsonBay)
|
||||
dim(HudsonBay)
|
||||
@@ -471,7 +473,7 @@ predict(linearMod,data.frame(Year=c(1910,1914,1920)),interval="confidence")
|
||||
!split
|
||||
===== Non-Linear Least squares in R =====
|
||||
!bblock
|
||||
!bc pycod
|
||||
!bc r
|
||||
set.seed(1485)
|
||||
len = 24
|
||||
x = runif(len)
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
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()
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from IPython.display import display
|
||||
import pandas as pd
|
||||
|
||||
data = pd.read_csv('src/Hudson_Bay.csv', delimiter=',', skiprows=1)
|
||||
|
||||
|
||||
data_pandas = pd.DataFrame(data)
|
||||
display(data_pandas)
|
||||
@@ -5,12 +5,29 @@ import sklearn
|
||||
from sklearn.linear_model import LinearRegression
|
||||
from sklearn.tree import DecisionTreeRegressor
|
||||
from sklearn.model_selection import train_test_split
|
||||
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
|
||||
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)
|
||||
#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")
|
||||
|
||||
Reference in New Issue
Block a user