(1-7)sklearn库的补充---如何生成数据?

生成数据,本应当在(1-1)sklearn库的 数据处理里面的内容,但由于现在不是我研究的重点,故单独摘出来备用




1.2 创建数据集
我们除了可以使用sklearn自带的数据集,还可以自己去创建训练样本,具体用法可以参考: https://scikit-learn.org/stable/datasets/
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
1.2.1 生成回归数据 make_regression()

from sklearn.datasets import make_regression
X, y, coef = make_regression(n_samples=200, n_features=1, n_informative=1, n_targets=1,
                       bias = 0, effective_rank=None, noise = 20,
                        tail_strength=0,random_state=0, coef = True)
  • n_samples 样本数量
  • n_features 特征数量
  • n_informative 对回归有效的特征数量
    (该项为对本次回归有用的特征数量,举个例子,我们想要预测房价,手上的特征有:房子面积、房龄、地段和房主的名字,显然前3项特征时有效特征,而房主的名字属于无效特征,改变其值并不影响回归效果。这里的n_informative就是3,总特征数为4.)
  • n_targets y的维度
  • bias 底层线性模型中的偏差项。相当于y的中位数
  • effective_rank 有效等级
  • noise 设置高斯噪声的标准偏差加到数据上。(noise:其值是高斯分布的偏差值,其值越大数据越分散,其值越小数据越集中。)
  • shuffle 是否洗牌(设置是否洗牌,如果为False,会按照顺序创建样本,一般设置为True。)
  • coef 如果为真,则返回权重值
  • random_state 设置随机种子
from sklearn.datasets import make_regression
from matplotlib import pyplot

X, y, coef = make_regression(n_samples=200, n_features=1, n_informative=1, n_targets=1,
                       bias = 0, effective_rank=None, noise = 20,
                        tail_strength=0,random_state=0, coef = True)


print(X[:10])#输出前十个
print(y[:10])#输出前十个
print(coef)

pyplot.scatter(X,y)
pyplot.show()

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
1.2.2 生成分类数据 make_classification()

from sklearn.datasets.samples_generator import make_classification
 
X, y = make_classification(n_samples=6, n_features=5, n_informative=2,
    n_redundant=2, n_classes=2, n_clusters_per_class=2, scale=1.0,
    random_state=20)
 
# n_samples:指定样本数
# n_features:指定特征数
#n_informative有意义的特征数,对本次分类有利的特征数
#n_redundant冗余信息
#n_repeated重复信息,随机提取n_informative和n_redundant 特征
# n_classes:指定几分类
#n_clusters_per_class某一个类别是由几个cluster构成的(子类别)
# random_state:随机种子,使得随机状可重

测试①:

>>> for x_,y_ in zip(X,y):
    print(y_,end=': ')
    print(x_)
 
     
0: [-0.6600737  -0.0558978   0.82286793  1.1003977  -0.93493796]
1: [ 0.4113583   0.06249216 -0.90760075 -1.41296696  2.059838  ]
1: [ 1.52452016 -0.01867812  0.20900899  1.34422289 -1.61299022]
0: [-1.25725859  0.02347952 -0.28764782 -1.32091378 -0.88549315]
0: [-3.28323172  0.03899168 -0.43251277 -2.86249859 -1.10457948]
1: [ 1.68841011  0.06754955 -1.02805579 -0.83132182  0.93286635]

测试②:

from sklearn.datasets import make_classification
import matplotlib.pyplot as plt

X, y = make_classification(n_samples=200, n_features=2, n_informative=2,n_redundant=0,
                           n_repeated=0, n_classes=2, n_clusters_per_class=1,random_state = 1)

print(X[:10])
print(y[:10])

plt.scatter(X[y == 0, 0], X[y == 0, 1], c = 'red', s = 100, label='0')
plt.scatter(X[y == 1, 0], X[y == 1, 1], c = 'blue', s = 100, label='1')
plt.scatter(X[y == 2, 0], X[y == 2, 1], c = 'green', s = 100, label='2')
plt.legend()
plt.show()

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import numpy as np
data = [[1,2,6],
        [3,4,7]]
data = np.array(data)
print(data[:,0])#第一列所有数据
print(data[:,1])#第二列所有数据
print(data[:,2])#第三列所有数据
print(data[1,:])#第一行所有数据
print(data[:, 1:])#表示从第2列开始所有数据

'''
[1 3]
[2 4]
[6 7]
[3 4 7]
[[2 6]
 [4 7]]
'''

在这里插入图片描述


import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets.samples_generator import make_classification

X,labels=make_classification(n_samples=200,n_features=2,n_redundant=0,n_informative=2,
                             random_state=1,n_clusters_per_class=2)
'''
labels=[1 0 1 0 1 .....]
set(labels)={0, 1}
np.linspace(0,1,len(set(labels)))为[0.  1. ]
#np.linspace(0,1,2))就是返回(0,1)内均匀间隔的数字,即[0.  1. ]
plt.cm.Spectral([0.  1. ])产生两种颜色
X[labels==0]就是y==0的X,即[[-1.23576237 -3.99795241] [ 0.72073196 -0.02628244]...]
'''

unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))
for k,col in zip(unique_lables,colors):
    x_k=X[labels==k]
    plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor="k",
             markersize=14)
plt.title('data by make_classification()')
plt.show()

在这里插入图片描述
1.2.3 用sklearn.datasets.make_blobs来生成数据
scikit中的make_blobs方法常被用来生成聚类算法的测试数据,直观地说,make_blobs会根据用户指定的特征数量,中心点数量,范围等来生成几类数据,这些数据可用于测试聚类算法的效果。

sklearn.datasets.make_blobs(n_samples=100, n_features=2, centers=3,
cluster_std=1.0, center_box=(-10.0, 10.0), shuffle=True,
 random_state=None)[source]

输入:

  • n_samples表示产生多少个数据
  • n_features表示数据是几维
  • centers表示数据点中心,可以输入int数字,代表有多少个中心,也可以输入几个坐标(fixed center locations)
  • cluster_std表示分布的标准差
  • center_box 每个样本的取值范围

返回值:

  • X,[n_samples, n_features]形状的数组,代表产生的样本
  • y,[n_samples]形状的数组,代表每个点的标签(类别)

实例(生成三类数据用于聚类(100个样本,每个样本2个特征)):

from sklearn.datasets import make_blobs 
data,label = make_blobs(n_samples=100,n_features=2,centers=5)
print(data)
print(label)

在这里插入图片描述

from sklearn.datasets import make_blobs
from matplotlib import pyplot
 
data,label = make_blobs(n_samples=100,n_features=2,centers=5)
 
# 绘制样本显示
pyplot.scatter(data[:,0],data[:,1],c=label)#c是color属性,data和label见下面
pyplot.show()

在这里插入图片描述

为每个类别设置不同的方差,只需要在上述代码中加入cluster_std参数即可:

from sklearn.datasets import make_blobs
import matplotlib.pylab as plt
 
data,target = make_blobs(n_samples=100,n_features=2,centers=3,cluster_std=[1.0,2.0,3.0])

# 在2D图中绘制样本,每个样本颜色不同
plt.scatter(data[:,0],data[:,1],c=target)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=200, n_features=2,centers = 4, cluster_std=0.5, center_box=(-5,5) ,random_state=0, shuffle= False)

plt.scatter(X[y == 0, 0], X[y == 0, 1], c = 'red', s = 100, label='0')
plt.scatter(X[y == 1, 0], X[y == 1, 1], c = 'blue', s = 100, label='1')
plt.scatter(X[y == 2, 0], X[y == 2, 1], c = 'green', s = 100, label='2')
plt.scatter(X[y == 3, 0], X[y == 3, 1], c = 'yellow', s = 100, label='3')
plt.legend()
plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobs
center=[[1,1],[-1,-1],[1,-1]] #centers表示数据点中心,可以输入int数字,代表有多少个中心,也可以输入几个坐标
cluster_std=0.3 #表示分布的标准差
X,labels=make_blobs(n_samples=200,centers=center,n_features=2,
                    cluster_std=cluster_std,random_state=0)

'''
X.shape为(200, 2)即200个样本,2个特征值
labels为[0 2 0 1 1 ....]
set(labels)为{0, 1, 2}
np.linspace(0,1,len(set(labels)))为[0.  0.5  1. ]
#np.linspace(0,1,3))就是返回(0,1)内均匀间隔的数字,即[0.  0.5  1. ]
plt.cm.Spectral([0.  0.5  1. ])绘制三种不同的颜色
X[labels==0]就是y==0的X,即[[1.68092639 0.5636903 ] [1.01995517 1.09074157] ...]
'''

unique_lables=set(labels)#即unique_lables={0, 1, 2}
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))#即colors=三种颜色
print(list(zip(unique_lables,colors)))
for k,col in zip(unique_lables,colors):
    x_k=X[labels==k]
    plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor="k",
             markersize=14)

plt.title('data by make_blob()')
plt.show()

在这里插入图片描述
1.2.4用sklearn.datasets.make_classification来生成数据

sklearn.datasets.make_classification(n_samples=100, n_features=20,
n_informative=2, n_redundant=2,n_repeated=0, n_classes=2,
n_clusters_per_class=2, weights=None,flip_y=0.01, class_sep=1.0,
 hypercube=True,shift=0.0, scale=1.0, shuffle=True, random_state=None)

输入:

  1. n_features :特征个数= n_informative() + n_redundant + n_repeated
  2. n_informative:多信息特征的个数
  3. n_redundant:冗余信息,informative特征的随机线性组合
  4. n_repeated :重复信息,随机提取n_informative和n_redundant 特征
  5. n_classes:分类类别
  6. n_clusters_per_class :某一个类别是由几个cluster构成的

1.2.5 用sklearn.datasets.make_gaussian和make_hastie_10_2来生成数据

sklearn.datasets.make_gaussian_quantiles(mean=None, cov=1.0, n_samples=100,
 n_features=2, n_classes=3,shuffle=True, random_state=None)

利用高斯分位点区分不同数据

sklearn.datasets.make_hastie_10_2(n_samples=12000, random_state=None)

实例:利用Hastie算法,生成二分类数据

import matplotlib.pyplot as plt
  
from sklearn.datasets import make_classification
from sklearn.datasets import make_blobs
from sklearn.datasets import make_gaussian_quantiles
from sklearn.datasets import make_hastie_10_2
  
plt.figure(figsize=(8, 8))
plt.subplots_adjust(bottom=.05, top=.9, left=.05, right=.95)
  
plt.subplot(421)
plt.title("One informative feature, one cluster per class", fontsize='small')
X1, Y1 = make_classification(n_samples=1000,n_features=2, n_redundant=0, n_informative=1,
                             n_clusters_per_class=1)
plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1)
  
plt.subplot(422)
plt.title("Two informative features, one cluster per class", fontsize='small')
X1, Y1 = make_classification(n_samples=1000,n_features=2, n_redundant=0, n_informative=2,
                             n_clusters_per_class=1)
plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1)
  
plt.subplot(423)
plt.title("Two informative features, two clusters per class", fontsize='small')
X2, Y2 = make_classification(n_samples=1000,n_features=2, n_redundant=0, n_informative=2)
plt.scatter(X2[:, 0], X2[:, 1], marker='o', c=Y2)
  
  
plt.subplot(424)
plt.title("Multi-class, two informative features, one cluster",
          fontsize='small')
X1, Y1 = make_classification(n_samples=1000,n_features=2, n_redundant=0, n_informative=2,
                             n_clusters_per_class=1, n_classes=3)
plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1)
  
plt.subplot(425)
plt.title("Three blobs", fontsize='small')
X1, Y1 = make_blobs(n_samples=1000,n_features=2, centers=3)
plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1)
  
plt.subplot(426)
plt.title("Gaussian divided into four quantiles", fontsize='small')
X1, Y1 = make_gaussian_quantiles(n_samples=1000,n_features=2, n_classes=4)
plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1)
  
plt.subplot(427)
plt.title("hastie data ", fontsize='small')
X1, Y1 = make_hastie_10_2(n_samples=1000)
plt.scatter(X1[:, 0], X1[:, 1], marker='o', c=Y1)
plt.show()

在这里插入图片描述
1.2.6 用sklearn.datasets.make_circles和make_moons来生成数据
生成环线数据,factor:外圈和内圈的比例

from sklearn.datasets import make_circles
X,y=make_circles(n_samples=100, shuffle=True, noise=None,random_state=None, factor=0.8)

'''
n_samples 样本的数量
shuffle 是否洗牌
noise 设置高斯噪声的标准偏差加到数据上。
random_state随机种子
factor (default=.8)	外圈和内圈的比例
'''

生成半环图

from sklearn.datasets import make_moons
X,y=make_moons(n_samples=100, shuffle=True, noise=None,random_state=None)

实例:

from sklearn.datasets import make_circles
X,y=make_circles(n_samples=100, shuffle=True, noise=None,random_state=None, factor=0.8)
print(X[:10])
print(y[:10])

在这里插入图片描述

from sklearn.datasets import make_circles
from sklearn.datasets import make_moons
import matplotlib.pyplot as plt
import numpy as np
  
fig=plt.figure(1)
x1,y1=make_circles(n_samples=1000,factor=0.5,noise=0.1)
plt.subplot(121)
plt.title('make_circles function example')
plt.scatter(x1[:,0],x1[:,1],marker='o',c=y1)
  
plt.subplot(122)
x1,y1=make_moons(n_samples=1000,noise=0.1)
plt.title('make_moons function example')
plt.scatter(x1[:,0],x1[:,1],marker='o',c=y1)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
from sklearn.datasets import make_circles
X, y = make_circles(n_samples=200, shuffle=True, noise = 0.05, factor=0.4, random_state=0)

plt.scatter(X[y == 0, 0], X[y == 0, 1], c = 'red', s = 100, label='0')#用red绘制当y=0时的x
plt.scatter(X[y == 1, 0], X[y == 1, 1], c = 'blue', s = 100, label='1')#用blue绘制当y=1时的x
plt.scatter(X[y == 2, 0], X[y == 2, 1], c = 'green', s = 100, label='2')#用绿色绘制当y=2时的x(事实上不存在,因为y只有0和1这两种状态)
plt.legend()
plt.show()

在这里插入图片描述
在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets.samples_generator import make_circles

X,labels=make_circles(n_samples=200,noise=0.2,factor=0.2,random_state=1)

'''
print("X.shape:",X.shape)  #(200, 2)即200个数据集,2个特征量{0, 1}
print("labels:",set(labels)) #set(labels)={0, 1}
'''

unique_lables=set(labels) 
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))
for k,col in zip(unique_lables,colors):
    x_k=X[labels==k]
    plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor="k",
             markersize=14)
plt.title('data by make_moons()')
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的甲乙丙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值