Scikit 中的乐趣

简介: Scikit 中的乐趣# 来源:NumPy Cookbook 2e Ch10加载示例数据集from __future__ import print_function from sklearn import datasets# datasets.

Scikit 中的乐趣

# 来源:NumPy Cookbook 2e Ch10

加载示例数据集

from __future__ import print_function 
from sklearn import datasets

# datasets.load_? 用于加载不同的数据集
print filter(lambda s: s.startswith('load_'), dir(datasets))
'''
['load_boston', 'load_breast_cancer', 'load_diabetes', 'load_digits', 'load_files', 'load_iris', 'load_lfw_pairs', 'load_lfw_people', 'load_linnerud', 'load_mlcomp', 'load_sample_image', 'load_sample_images', 'load_svmlight_file', 'load_svmlight_files']
'''

# 这里加载波士顿房价数据集
# 波士顿房价数据集是连续模型
boston_prices = datasets.load_boston() 

# 对于离散型数据集来说,data 是属性,target 是标签
# 对于连续型数据集来说,data 是自变量,target 是因变量
# data 是二维数组,行为记录,列为属性/自变量
print("Data shape", boston_prices.data.shape) 
# Data shape (506, 13) 

print("Data max=%s min=%s" % (boston_prices.data.max(), boston_prices. data.min())) 
# Data max=711.0 min=0.0 

# target 是标签/因变量的一维数组
print("Target shape", boston_prices.target.shape) 
# Target shape (506,)

print("Target max=%s min=%s" % (boston_prices.target.max(), boston_ prices.target.min())) 
# Target max=50.0 min=5.0

道琼斯股票聚类

# 2011 到 2012 
start = datetime.datetime(2011, 01, 01) 
end = datetime.datetime(2012, 01, 01)

# 这里是股票代码
symbols = ["AA", "AXP", "BA", "BAC", "CAT",
    "CSCO", "CVX", "DD", "DIS", "GE", "HD",
    "HPQ", "IBM", "INTC", "JNJ", "JPM",
    "KO", "MCD", "MMM", "MRK", "MSFT", "PFE",
    "PG", "T", "TRV", "UTX", "VZ", "WMT", "XOM"]

# 下载每只股票 2011 ~ 2012 的所有数据
quotes = []
for symbol in symbols:
    try:
        quotes.append(finance.quotes_historical_yahoo(symbol, start, end, asobject=True))
    except urllib2.HTTPError as e:
        print(symbol, e)

# 每只股票只取收盘价
close = np.array([q.close for q in quotes]).astype(np.float) 
print(close.shape) 
# (29, 252)

# 计算每只股票的对数收益
logreturns = np.diff(np.log(close)) 
print(logreturns.shape)
# (29, 251)

# 计算对数收益的平方和
logreturns_norms = np.sum(logreturns ** 2, axis=1)
# np.dot(logreturns, logreturns.T) 的矩阵
# 每项是 logret[i] · logret[j]
# logreturns_norms[:, np.newaxis]
# 每项是 sqsum[i]
# logreturns_norms[np. newaxis, :]
# 每项是 sqsum[j]
# S 的每一项就是 logret[i] 和 logret[j] 的欧氏距离
S = - logreturns_norms[:, np.newaxis] - logreturns_norms[np. newaxis, :] + 2 * np.dot(logreturns, logreturns.T)

# 使用 AP 算法进行聚类
# AffinityPropagation 用于创建聚类器
# 向 fit 传入距离矩阵可以对其聚类
# 用于聚类的属性是每个向量到其它向量的距离
aff_pro = sklearn.cluster.AffinityPropagation().fit(S)
# labels_ 获取聚类结果
labels = aff_pro.labels_
# 打印每只股票的类别
for symbol, label in zip(symbols, labels):
    print('%s in Cluster %d' % (symbol, label)) 
'''
AA in Cluster 0 
AXP in Cluster 6 
BA in Cluster 6 
BAC in Cluster 1 
CAT in Cluster 6 
CSCO in Cluster 2 
CVX in Cluster 7 
DD in Cluster 6 
DIS in Cluster 6 
GE in Cluster 6 
HD in Cluster 5 
HPQ in Cluster 3 
IBM in Cluster 5 
INTC in Cluster 6 
JNJ in Cluster 5 
JPM in Cluster 4 
KO in Cluster 5 
MCD in Cluster 5 
MMM in Cluster 6
MRK in Cluster 5 
MSFT in Cluster 5 
PFE in Cluster 7 
PG in Cluster 5 
T in Cluster 5 
TRV in Cluster 5 
UTX in Cluster 6 
VZ in Cluster 5 
WMT in Cluster 5 
XOM in Cluster 7

使用 statsmodels 执行正态性测试

from __future__ import print_function 
import datetime 
import numpy as np 
from matplotlib import finance 
from statsmodels.stats.adnorm import normal_ad


# 下载 2011 到 2012 的收盘价数据
start = datetime.datetime(2011, 01, 01) 
end = datetime.datetime(2012, 01, 01)
quotes = finance.quotes_historical_yahoo('AAPL', start, end, asobject=True)
close = np.array(quotes.close).astype(np.float) 
print(close.shape)
# (252,) 

# 对对数收益执行正态性测试
# 也就是是否满足正态分布
# normal_ad 使用 Anderson-Darling 测试
# 请见 http://en.wikipedia.org/wiki/Anderson%E2%80%93Darling_test
print(normal_ad(np.diff(np.log(close))))
# (0.57103805516803163, 0.13725944999430437)
# p-value,也就是概率为 0.13

角点检测


from skimage.feature import corner_peaks 
from skimage.color import rgb2gray

# 加载示例图片(亭子那张)
dataset = load_sample_images() 
img = dataset.images[0] 

# 将 RGB 图像转成灰度
gray_img = rgb2gray(img) 

# 使用 Harris 角点检测器
# http://en.wikipedia.org/wiki/Corner_detection
harris_coords = corner_peaks(corner_harris(gray_img))
# harris_coords 第一列是 y,第二列是 x
y, x = np.transpose(harris_coords) 
plt.axis('off') 
# 绘制图像和角点
plt.imshow(img) 
plt.plot(x, y, 'ro') 
plt.show()

边界检测

from sklearn.datasets import load_sample_images 
import matplotlib.pyplot as plt 
import skimage.feature

# 加载示例图片(亭子那张)
dataset = load_sample_images() 
img = dataset.images[0] 

# 使用 Canny 过滤器检测边界
# 基于高斯分布的标准差
# http://en.wikipedia.org/wiki/Edge_detection
edges = skimage.feature.canny(img[..., 0]) 

# 绘制图像
plt.axis('off') 
plt.imshow(edges) 
plt.show()

相关文章
|
6月前
|
C语言 Python
初学python的感受
初学python的感受
40 0
|
7天前
|
机器学习/深度学习 数据采集 算法
聚类分析实战:scikit-learn助你轻松上手
【4月更文挑战第17天】使用scikit-learn进行聚类分析,包括K-Means、DBSCAN、Mean Shift和Hierarchical Clustering等算法。实战步骤涉及数据预处理、选择算法、确定簇数量、训练模型和评估结果。以鸢尾花数据集为例,展示如何应用K-Means进行聚类,并强调理解结果的重要性。
|
8月前
|
机器学习/深度学习 数据采集
揭秘乳腺癌预测黑科技:R语言和支持向量机的奇妙之旅!
本文旨在探讨乳腺癌预测的重要性和挑战。首先,我们描述了乳腺癌作为全球常见癌症的背景和重要性。随后,我们引入了乳腺癌预测的重要性,并强调了早期检测和准确预测对于提高患者生存率的重要性。然而,乳腺癌预测面临着多方面的挑战,包括乳腺癌复杂的发病机制、数据获取和处理的困难,以及模型选择和解释的问题。为了克服这些挑战,我们需要深入研究乳腺癌预测领域,并不断改进和优化预测方法和技术。通过准确的乳腺癌预测,我们可以提供更有效的医疗护理,为患者提供更好的预防和治疗策略。
126 0
|
机器学习/深度学习 数据挖掘 领域建模
机器学习神器Scikit-Learn入门教程
Scikit-learn是一个非常知名的Python机器学习库,它广泛地用于统计分析和机器学习建模等数据科学领域。
|
机器学习/深度学习
强化学习快餐教程(1) - gym环境搭建
# 强化学习快餐教程(1) - gym环境搭建 欲练强化学习神功,首先得找一个可以操练的场地。 两大巨头OpenAI和Google DeepMind都不约而同的以游戏做为平台,比如OpenAI的长处是DOTA2,而DeepMind是AlphaGo下围棋。 下面我们就从OpenAI为我们提供的gym为入口,开始强化学习之旅。 ## OpenAI gym平台安装 安装方法很简
2101 0
|
机器学习/深度学习 Python
|
Web App开发 机器学习/深度学习 存储
教程:用强化学习玩转恐龙跳跳
本文用强化学习做一个类似障碍跑的小游戏
2305 0
|
机器学习/深度学习 程序员 TensorFlow
|
机器学习/深度学习 资源调度 TensorFlow
Tensorflow快餐教程(6) - 矩阵分解
特征分解,奇异值分解,Moore-Penrose广义逆
10394 0

热门文章

最新文章