1.np.expand_dims(np.linspace(0.0, 10.0, num=100), axis=1)
#linspace()可以生成元素为50的等间隔数列。而前两个参数分别是数列的开头与结尾。如果写入第三个参数,可以制定数列的元素个数。
#expand_dims(input,axis)指定在第一维处为输入张量添加维数;
2.np.random.uniform(1.0, 1.5, size=(100, 1))与np.random.normal(0.0, 3.5, size=(100, 1)
#random.uniform()返回一个均匀分布的采样结果,左闭右开区间[low, high)。返回数组的shape与size相同
#torch.normal(means, std, out=None)返回一个张量,包含从给定参数means=1.0
,std=1.5
的离散正态分布中抽取随机数;
官网给例子——torch.normal(means=torch.arange(1, 11), std=torch.arange(1, 0, -0.1))
1.5104#是从均值为1,标准差为1的正态分布中随机生成的
1.6955#是从均值为2,标准差为0.9的正态分布中随机生成的
3.pd.DataFrame(np.concatenate([T, X], axis=1), columns=['t', 'x'])
#numpy.concatenate((a1,a2,…), axis=0)函数,能够一次完成多个数组的拼接。其中a1,a2,…是数组类型的参数,分为三种情况:
情况一:对于一维数组拼接,axis的值不影响最后的结果
情况二: axis=0为按列拼接,也是默认情况:
a=np.array([[1,2,3],[4,5,6]])
b=np.array([[11,21,31],[7,8,9]])
np.concatenate((a,b),axis=0)
array([[ 1, 2, 3],
[ 4, 5, 6],
[11, 21, 31],
[ 7, 8, 9]])
原文链接:https://blog.csdn.net/qq_40689236/article/details/106849775
情况三:axis=1或者-1为按行拼接:
np.concatenate((a,b),axis=1) #axis=1表示对应行的数组进行拼接
array([[ 1, 2, 3, 11, 21, 31],
[ 4, 5, 6, 7, 8, 9]])
#columns=['t', 'x'] DataFrame.columns
属性返回给定Dataframe的列标签。猜测,在这里应该是指给横纵坐标标记为‘t',‘x’
#Pandas DataFrame带有标签轴(行和列)的二维大小可变的,可能是异构的表格数据结构。算术运算在行和列标签上对齐。可以将其视为Series对象的dict-like容器。这是 Pandas 的主要数据结构;
4.lr = LinearRegression()
lr.fit(T, X)
sklearn数据预处理中fit()Method calculates the parameters μ and σ and saves them as internal objects.返回一个History的对象,其History.history属性记录了损失函数和其他指标的数值随epoch变化的情况,如果有验证集的话,也包含了验证集的这些指标变化情况;
5.lr.coef_[0][0], lr.intercept_[0])
#g(x) = w1x1 + w2x2 + w3x3 + w4x4 + w0
coef_为w1到w4
intercept_为w0;
6.>>>print('test{0:.3f}'.format (3.1415962789))
test3.142
7.sns.lmplot(data=df, x='t', y='x', height=8)
#sns.lmplot()是绘制回归图的,它的参数很多,在这里“data”是指3.的“DataFrame”数据,“x”和“y”是横纵坐标,他俩的值来自“DataFrame”数据中的行‘t’和列'x'
二维数据的线性回归
# Create the dataset of the 100 sample points
T = np.expand_dims(np.linspace(0.0, 10.0, num=100), axis=1)
X = (T * np.random.uniform(1.0, 1.5, size=(100, 1))) + np.random.normal(0.0, 3.5, size=(100, 1))
df = pd.DataFrame(np.concatenate([T, X], axis=1), columns=['t', 'x'])
# Perform the linear regression
lr = LinearRegression()
lr.fit(T, X)
# Print the equation
print('x(t) = {0:.3f}t + {1:.3f}'.format(lr.coef_[0][0], lr.intercept_[0]))