Skip to content

Commit 2f3df4c

Browse files
committed
ch01
1 parent 31ede40 commit 2f3df4c

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

Diff for: Iris_test/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#

Diff for: Iris_test/__main__.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Aug 1 17:07:30 2018
4+
5+
@author: Administrator
6+
"""
7+
8+
import pandas as pd
9+
from sklearn.datasets import load_iris
10+
from sklearn.model_selection import train_test_split
11+
from sklearn.neighbors import KNeighborsClassifier
12+
13+
if __name__ == '__main__':
14+
iris_data = load_iris()
15+
print(iris_data.keys())
16+
17+
print(iris_data['target_names'],iris_data['data'])
18+
19+
X_train, X_test, y_train, y_test = train_test_split(
20+
iris_data['data'], iris_data['target'],random_state=0)
21+
22+
print(X_train.shape,y_train.shape)
23+
24+
iris_data_df = pd.DataFrame(X_train, columns=iris_data.feature_names)
25+
scatter = pd.scatter_matrix(iris_data_df, c=y_train,figsize=(15,15),
26+
marker='o', hist_kwds={'bins':20},s=60,
27+
alpha=0.8)
28+
29+
knn = KNeighborsClassifier(n_neighbors=1)
30+
knn.fit(X_train,y_train)
31+
32+
y_pred = knn.predict(X_test)
33+
print("test score is :{:.2f}".format(knn.score(X_test,y_test)))
34+

Diff for: Supervised_learn/KNN.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Aug 1 17:39:03 2018
4+
5+
@author: Administrator
6+
"""
7+
import mglearn
8+
import pandas as pd
9+
from sklearn.datasets import load_iris
10+
from sklearn.model_selection import train_test_split
11+
from sklearn.neighbors import KNeighborsClassifier
12+
13+
14+
def Knn_clf(k):
15+
mglearn.plots.plot_knn_classification(n_neighbors=k)
16+
X, y = mglearn.datasets.make_forge()
17+
18+
X_train, X_test, y_train, y_test = train_test_split(
19+
X, y ,random_state=0)
20+
clf = KNeighborsClassifier(n_neighbors=k)
21+
22+
clf.fit(X_train,y_train)
23+
print("test accuracy: {:.2f}".format(clf.score(X_test, y_test)))
24+
25+
def Knn_reg(k):
26+
mglearn.plots.plot_knn_regression(n_neighbors=k)
27+

Diff for: Supervised_learn/Linear_reg.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Thu Aug 2 16:24:58 2018
4+
5+
@author: Administrator
6+
"""
7+
from sklearn.linear_model import LinearRegression
8+
from sklearn.linear_model import Ridge
9+
from sklearn.linear_model import Lasso
10+
11+
import mglearn
12+
import numpy as np
13+
14+
def Linear_reg():
15+
X, y = mglearn.datasets.load_extended_boston()
16+
17+
X_train, X_test, y_train, y_test = train_test_split(
18+
X, y ,random_state=0)
19+
20+
lr = LinearRegression().fit(X_train,y_train)
21+
print("lr.coef: {}, bias: {}".format(lr.coef_, lr.intercept_))
22+
print("lr.test: {:.2f}".format(lr.score(X_test,y_test)))
23+
24+
def Rigde_reg():
25+
X, y = mglearn.datasets.load_extended_boston()
26+
27+
X_train, X_test, y_train, y_test = train_test_split(
28+
X, y ,random_state=0)
29+
ridge = Ridge(alpha=0.5).fit(X_train,y_train)
30+
print("lr.test: {:.2f}".format(ridge.score(X_test,y_test)))
31+
32+
def Lasso_reg():
33+
X, y = mglearn.datasets.load_extended_boston()
34+
35+
X_train, X_test, y_train, y_test = train_test_split(
36+
X, y ,random_state=0)
37+
lasso = Lasso(alpha=0.0015, max_iter=100000).fit(X_train,y_train)
38+
print("lr.test: {:.2f}".format(lasso.score(X_test,y_test)))
39+
print("the number of features is:" , np.sum(lasso.coef_ != 0))

Diff for: Supervised_learn/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#

Diff for: Supervised_learn/__main__.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Aug 1 17:38:57 2018
4+
5+
@author: Administrator
6+
"""
7+
from load_data import *
8+
from knn import *
9+
import matplotlib.pyplot as plt
10+
11+
if __name__ == '__main__':
12+
13+
cancer, bos = Create_data()
14+
X_train, X_test, y_train, y_test = train_test_split(
15+
cancer.data, cancer.target , stratify=cancer.target
16+
, random_state=0)
17+
train_acurracy = []
18+
test_acurracy = []
19+
20+
setting = range(1,10)
21+
22+
for k in setting:
23+
clf = KNeighborsClassifier(n_neighbors=k)
24+
clf.fit(X_train, y_train)
25+
26+
train_acurracy.append(clf.score(X_train, y_train))
27+
test_acurracy.append(clf.score(X_test, y_test))
28+
29+
plt.plot(setting, train_acurracy,label='train')
30+
plt.plot(setting, test_acurracy,label='test')
31+
plt.legend()
32+

Diff for: Supervised_learn/load_data.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Aug 1 17:38:47 2018
4+
5+
@author: Administrator
6+
"""
7+
8+
import mglearn
9+
import matplotlib.pyplot as plt
10+
from sklearn.datasets import load_breast_cancer, load_boston
11+
12+
def Create_data():
13+
X, y = mglearn.datasets.make_forge()
14+
15+
#mglearn.discrete_scatter(X[:,0], X[:,1],y)
16+
plt.legend(["Class 0", "Class 1"], loc=4)
17+
18+
cancer = load_breast_cancer()
19+
print("cancer key: \n{}".format(cancer.keys()))
20+
boston = load_boston()
21+
return cancer, boston

0 commit comments

Comments
 (0)