Escape The Brain

  • Home

  • Tags

  • Archives

scikit-learn 笔记 - 监督学习

Posted on 2018-10-19 | Edited on 2019-03-14

线性回归 (Linear Regression)

线性模型的数学表示:

通常使用最小二乘法来解决线性回归问题,从而得到合适的系数。

在sklearn中,通过线性回归得到的系数有 coef 和 intercept 。coef 表示向量w(w1,…,wp), intercept 表示截距w0。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# create linear regression model
reg = LinearRegression()

# fit the model on the training set
clf.fit(X_train, y_train)

# use the trained model to predict result for the test set
pred = clf.predict(X_test)

# The coefficients
print('Coefficients: ', reg.coef_)
print('Intercept: ', reg.intercept_)

# The mean squared error
print("Mean squared error: %.2f"
% mean_squared_error(y_test, pred))

# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(y_test, pred))
# also can use the below code
# reg.score(X_test, y_test)

R^2 Score

线性回归模型可视化

1
2
3
4
5
plt.scatter(X, Y)
plt.plot(X, reg.predict(X), color='red', linewidth=2)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
Read more »

scikit-learn 笔记 - 数据集转换

Posted on 2018-10-18 | Edited on 2019-03-14

特征缩放 (Feature Scaling)

在使用机器学习算法前,经常需要进行特征缩放(feature scaling),将所有特征都缩放到相同的尺度 ([0,1]的范围)。

下面是进行特征缩放的一种方式:

1
2
3
4
def featureScaling(arr):
minval = min(arr)
maxval = max(arr)
return [1.0 * (val - minval) / (maxval - minval) for val in arr]

使用sklearn:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy
from sklearn.preprocessing import MinMaxScaler

train_data = numpy.array([[-1, 2], [-0.5, 6], [0, 10], [1, 18]])
test_data = numpy.array([[2, 2]])

# create the scaler
scaler = MinMaxScaler()

# compute the minimum and maximum to be used for later scaling.
scaler.fit(train_data)

# scaling features
print(scaler.transform(train_data))
print(scaler.transform(test_data))

对许多机器学习算法,特征缩放将影响最终的效果,如SVM和K-Means等(影响计算距离时各特征的比重)。

此外,特征缩放通常可以加速(大部分)机器学习算法的训练过程。

文本特征

文本分析是机器学习算法的一个重要应用领域,然而原始的文本数据很难直接作为大部分算法的输入,为此 scikit-learn 提供了许多方法从文本中提取特征向量。

Read more »

高效学习技巧

Posted on 2018-10-11 | Edited on 2018-10-12

美国加州威廉格拉瑟学院(William Glasser Institute)的一项研究表明:

  • 如果通过阅读获取信息,最终只有大概10%被长期存储下来;看到或听到的信息能保留50%;
  • 亲身体验(动用各种感官)的信息可以被保留80%;
  • 如果把学到的知识积极地教给他人,我们就可以记住至少95%。

有效学习知识四大关键:

  1. 有效吸收知识
  2. 做好笔记
  3. 记忆
  4. 复习

高效阅读

只要方法正确,阅读的速度越快,越容易记住相关信息。最好的方法就是一边读,一边在重点信息旁做标记,只要有笔或干脆用食指就可以。用笔或手指辅助阅读能大大提高阅读时的专注度,并极大地提高你的阅读速度。

关键点笔记

建议每专心阅读20分钟左右就停下来做些笔记,在刚刚读过的内容中找出关键点,然后在纸上写下这些关键信息。思维导图可以说是完美的信息储存工具。理想情况下应当凭记忆从刚刚读过的信息中抽取关键信息,而不用再回头翻一遍,不过也不必强求。

Read more »
12

WeiJun

If you are depressed, you are living in the past. If you are anxious, you are living in the future. If you are at peace, you are living in the present.

13 posts
12 tags
GitHub
Creative Commons
© 2019 WeiJun
Powered by Hexo v3.7.1
|
Theme – NexT.Mist v6.4.2