楼主: ReneeBK
1995 18

[GitHub]Python Machine Learning [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4900份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49655 个
通用积分
55.9937
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2017-4-22 04:40:37 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Python Machine Learning

本帖隐藏的内容

https://github.com/rasbt/python-machine-learning-book


What you can expect are 400 pages rich in useful material just about everything you need to know to get started with machine learning ... from theory to the actual code that you can directly put into action! This is not yet just another "this is how scikit-learn works" book. I aim to explain all the underlying concepts, tell you everything you need to know in terms of best practices and caveats, and we will put those concepts into action mainly using NumPy, scikit-learn, and Theano.

You are not sure if this book is for you? Please checkout the excerpts from the Foreword and Preface, or take a look at the FAQ section for further information.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝


沙发
ReneeBK 发表于 2017-4-22 04:42:54
  1. First steps with scikit-learn
  2. Loading the Iris dataset from scikit-learn. Here, the third column represents the petal length, and the fourth column the petal width of the flower samples. The classes are already converted to integer labels where 0=Iris-Setosa, 1=Iris-Versicolor, 2=Iris-Virginica.
  3. In [4]:
  4. from sklearn import datasets
  5. import numpy as np

  6. iris = datasets.load_iris()
  7. X = iris.data[:, [2, 3]]
  8. y = iris.target

  9. print('Class labels:', np.unique(y))


  10. In [5]:
  11. if Version(sklearn_version) < '0.18':
  12.     from sklearn.cross_validation import train_test_split
  13. else:
  14.     from sklearn.model_selection import train_test_split

  15. X_train, X_test, y_train, y_test = train_test_split(
  16.     X, y, test_size=0.3, random_state=0)
  17. Standardizing the features:

  18. In [6]:
  19. from sklearn.preprocessing import StandardScaler

  20. sc = StandardScaler()
  21. sc.fit(X_train)
  22. X_train_std = sc.transform(X_train)
  23. X_test_std = sc.transform(X_test)
复制代码

藤椅
ReneeBK 发表于 2017-4-22 04:44:07
  1. Training a perceptron via scikit-learn
  2. Redefining the plot_decision_region function from chapter 2:
  3. In [7]:
  4. from sklearn.linear_model import Perceptron

  5. ppn = Perceptron(n_iter=40, eta0=0.1, random_state=0)
  6. ppn.fit(X_train_std, y_train)

  7. In [8]:
  8. y_test.shape

  9. In [9]:
  10. y_pred = ppn.predict(X_test_std)
  11. print('Misclassified samples: %d' % (y_test != y_pred).sum())

  12. In [10]:
  13. from sklearn.metrics import accuracy_score

  14. print('Accuracy: %.2f' % accuracy_score(y_test, y_pred))

  15. In [11]:
  16. from matplotlib.colors import ListedColormap
  17. import matplotlib.pyplot as plt
  18. import warnings


  19. def versiontuple(v):
  20.     return tuple(map(int, (v.split("."))))


  21. def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):

  22.     # setup marker generator and color map
  23.     markers = ('s', 'x', 'o', '^', 'v')
  24.     colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
  25.     cmap = ListedColormap(colors[:len(np.unique(y))])

  26.     # plot the decision surface
  27.     x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
  28.     x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
  29.     xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
  30.                            np.arange(x2_min, x2_max, resolution))
  31.     Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
  32.     Z = Z.reshape(xx1.shape)
  33.     plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
  34.     plt.xlim(xx1.min(), xx1.max())
  35.     plt.ylim(xx2.min(), xx2.max())

  36.     for idx, cl in enumerate(np.unique(y)):
  37.         plt.scatter(x=X[y == cl, 0],
  38.                     y=X[y == cl, 1],
  39.                     alpha=0.6,
  40.                     c=cmap(idx),
  41.                     edgecolor='black',
  42.                     marker=markers[idx],
  43.                     label=cl)

  44.     # highlight test samples
  45.     if test_idx:
  46.         # plot all samples
  47.         if not versiontuple(np.__version__) >= versiontuple('1.9.0'):
  48.             X_test, y_test = X[list(test_idx), :], y[list(test_idx)]
  49.             warnings.warn('Please update to NumPy 1.9.0 or newer')
  50.         else:
  51.             X_test, y_test = X[test_idx, :], y[test_idx]

  52.         plt.scatter(X_test[:, 0],
  53.                     X_test[:, 1],
  54.                     c='',
  55.                     alpha=1.0,
  56.                     edgecolor='black',
  57.                     linewidths=1,
  58.                     marker='o',
  59.                     s=55, label='test set')
  60. Training a perceptron model using the standardized training data:
  61. In [12]:
  62. X_combined_std = np.vstack((X_train_std, X_test_std))
  63. y_combined = np.hstack((y_train, y_test))

  64. plot_decision_regions(X=X_combined_std, y=y_combined,
  65.                       classifier=ppn, test_idx=range(105, 150))
  66. plt.xlabel('petal length [standardized]')
  67. plt.ylabel('petal width [standardized]')
  68. plt.legend(loc='upper left')

  69. plt.tight_layout()
  70. # plt.savefig('./figures/iris_perceptron_scikit.png', dpi=300)
  71. plt.show()
复制代码

板凳
ReneeBK 发表于 2017-4-22 04:45:22
  1. Logistic regression intuition and conditional probabilities
  2. In [13]:
  3. import matplotlib.pyplot as plt
  4. import numpy as np


  5. def sigmoid(z):
  6.     return 1.0 / (1.0 + np.exp(-z))

  7. z = np.arange(-7, 7, 0.1)
  8. phi_z = sigmoid(z)

  9. plt.plot(z, phi_z)
  10. plt.axvline(0.0, color='k')
  11. plt.ylim(-0.1, 1.1)
  12. plt.xlabel('z')
  13. plt.ylabel('$\phi (z)$')

  14. # y axis ticks and gridline
  15. plt.yticks([0.0, 0.5, 1.0])
  16. ax = plt.gca()
  17. ax.yaxis.grid(True)

  18. plt.tight_layout()
  19. # plt.savefig('./figures/sigmoid.png', dpi=300)
  20. plt.show()
复制代码

报纸
ReneeBK 发表于 2017-4-22 04:46:18
  1. Learning the weights of the logistic cost function
  2. In [15]:
  3. def cost_1(z):
  4.     return - np.log(sigmoid(z))


  5. def cost_0(z):
  6.     return - np.log(1 - sigmoid(z))

  7. z = np.arange(-10, 10, 0.1)
  8. phi_z = sigmoid(z)

  9. c1 = [cost_1(x) for x in z]
  10. plt.plot(phi_z, c1, label='J(w) if y=1')

  11. c0 = [cost_0(x) for x in z]
  12. plt.plot(phi_z, c0, linestyle='--', label='J(w) if y=0')

  13. plt.ylim(0.0, 5.1)
  14. plt.xlim([0, 1])
  15. plt.xlabel('$\phi$(z)')
  16. plt.ylabel('J(w)')
  17. plt.legend(loc='best')
  18. plt.tight_layout()
  19. # plt.savefig('./figures/log_cost.png', dpi=300)
  20. plt.show()
复制代码

地板
ReneeBK 发表于 2017-4-22 04:47:08
  1. Training a logistic regression model with scikit-learn
  2. In [16]:
  3. from sklearn.linear_model import LogisticRegression

  4. lr = LogisticRegression(C=1000.0, random_state=0)
  5. lr.fit(X_train_std, y_train)

  6. plot_decision_regions(X_combined_std, y_combined,
  7.                       classifier=lr, test_idx=range(105, 150))
  8. plt.xlabel('petal length [standardized]')
  9. plt.ylabel('petal width [standardized]')
  10. plt.legend(loc='upper left')
  11. plt.tight_layout()
  12. # plt.savefig('./figures/logistic_regression.png', dpi=300)
  13. plt.show()
复制代码

7
cemcem 发表于 2017-4-22 11:41:25
Thanks for sharing

8
fengyg 企业认证  发表于 2017-4-22 13:27:35
kankan

9
franky_sas 发表于 2017-4-22 14:07:09

10
h2h2 发表于 2017-4-22 14:38:37
谢谢分享

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-24 17:23