how to calculate the best fit to a plane in 3D, and how to find the corresponding statistical parameters
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程)
https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campaign=commission&utm_source=cp-400000000398149&utm_medium=share


# -*- coding: utf-8 -*-
'''
python入门/爬虫/人工智能/机器学习/自然语言/数据统计分析视频教程网址
https://pythoner.taobao.com/ https://github.com/thomas-haslwanter/statsintro_python/tree/master/ISP/Code_Quantlets/12_Multivariate/multipleRegression
Multiple Regression
- Shows how to calculate the best fit to a plane in 3D, and how to find the
corresponding statistical parameters.
- Demonstrates how to make a 3d plot.
- Example of multiscatterplot, for visualizing correlations in three- to
six-dimensional datasets.
'''
# Import standard packages
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns # additional packages
import sys
import os
sys.path.append(os.path.join('..', '..', 'Utilities')) try:
# Import formatting commands if directory "Utilities" is available
from ISP_mystyle import showData except ImportError:
# Ensure correct performance otherwise
def showData(*options):
plt.show()
return # additional packages ...
# ... for the 3d plot ...
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm # ... and for the statistic
from statsmodels.formula.api import ols def generateData():
''' Generate and show the data: a plane in 3D '''
#随机产生101个数据,取值范围从(-5到5)
x = np.linspace(-5,5,101)
(X,Y) = np.meshgrid(x,x)
# To get reproducable values, I provide a seed value
np.random.seed(987654321)
#np.random.randn产生随机的正太分布数,np.shape(X)表示X的size(101,101)
#np.random.randn(np.shape(X)[0], np.shape(X)[1])表示产生(101,101)个随机数
Z = -5 + 3*X-0.5*Y+np.random.randn(np.shape(X)[0], np.shape(X)[1]) # 绘图
#Set the color
myCmap = cm.GnBu_r
# If you want a colormap from seaborn use:
#from matplotlib.colors import ListedColormap
#myCmap = ListedColormap(sns.color_palette("Blues", 20)) # Plot the figure
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X,Y,Z, cmap=myCmap, rstride=2, cstride=2,
linewidth=0, antialiased=False)
ax.view_init(20,-120)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
fig.colorbar(surf, shrink=0.6) outFile = '3dSurface.png'
showData(outFile)
#X.flatten()把多维数据展开,弄成一维数据
return (X.flatten(),Y.flatten(),Z.flatten()) def regressionModel(X,Y,Z):
'''Multilinear regression model, calculating fit, P-values, confidence intervals etc.''' # Convert the data into a Pandas DataFrame
df = pd.DataFrame({'x':X, 'y':Y, 'z':Z}) # --- >>> START stats <<< ---
# Fit the model
model = ols("z ~ x + y", df).fit()
# Print the summary
print((model.summary()))
# --- >>> STOP stats <<< ---
return model._results.params # should be array([-4.99754526, 3.00250049, -0.50514907]) #用numpy的线性回归模型,和上面regressionModel函数计算结果一致
def linearModel(X,Y,Z):
'''Just fit the plane, using the tools from numpy''' # --- >>> START stats <<< ---
M = np.vstack((np.ones(len(X)), X, Y)).T
bestfit = np.linalg.lstsq(M,Z)
# --- >>> STOP stats <<< ---
print(('Best fit plane:', bestfit))
return bestfit if __name__ == '__main__':
(X,Y,Z) = generateData()
regressionModel(X,Y,Z)
linearModel(X,Y,Z)
python风控评分卡建模和风控常识(博客主亲自录制视频教程)
how to calculate the best fit to a plane in 3D, and how to find the corresponding statistical parameters的更多相关文章
- (转)Markov Chain Monte Carlo
Nice R Code Punning code better since 2013 RSS Blog Archives Guides Modules About Markov Chain Monte ...
- What is an eigenvector of a covariance matrix?
What is an eigenvector of a covariance matrix? One of the most intuitive explanations of eigenvector ...
- kaggle入门项目:Titanic存亡预测(四)模型拟合
原kaggle比赛地址:https://www.kaggle.com/c/titanic 原kernel地址:A Data Science Framework: To Achieve 99% Accu ...
- Course Machine Learning Note
Machine Learning Note Introduction Introduction What is Machine Learning? Two definitions of Machine ...
- [C2P3] Andrew Ng - Machine Learning
##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...
- AI-IBM-cognitive class --Liner Regression
Liner Regression import matplotlib.pyplot as plt import pandas as pd import pylab as pl import numpy ...
- OpenCASCADE PCurve of Topological Face
OpenCASCADE PCurve of Topological Face eryar@163.com Abstract. OpenCASCADE provides a class BRepBuil ...
- The Model Complexity Myth
The Model Complexity Myth (or, Yes You Can Fit Models With More Parameters Than Data Points) An oft- ...
- 中国澳门sinox很多平台CAD制图、PCB电路板、IC我知道了、HDL硬件描述语言叙述、电路仿真和设计软件,元素分析表
中国澳门sinox很多平台CAD制图.PCB电路板.IC我知道了.HDL硬件描述语言叙述.电路仿真和设计软件,元素分析表,可打开眼世界. 最近的研究sinox执行windows版protel,powe ...
随机推荐
- Linux内核分析 读书笔记 (第五章)
第五章 系统调用 5.1 与内核通信 1.调用在用户空间进程和硬件设备之间添加了一个中间层.该层主要作用有三个: 为用户空间提供了硬件的抽象接口. 系统调用保证了系统的稳定和安全. 实现多任务和虚拟内 ...
- 软件项目第一次Sprint总结
成果评分表: 组名 分数 原因 9-652 6 界面和谐生动,可运行,在目前阶段可时间基本操作 hzsy -2 代码下载,但实现安卓和相机调用 JYJe族 -1 实现安卓界面,完成一项功能,做得少 结 ...
- 小学四则运算APP 第二次冲刺-第二天
团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第二次冲刺阶段时间:11.29~12.09 本次发布的判断题功能界面的设置: activity_panduan_set.xml: < ...
- DOM的基本操作
什么是DOM 1:文档对象模型(DocumentObjectModel,DOM) 2:DOM定义了访问和操作HTML文档的标准方法. 3:DOM将HTML 文档表达为树结构. 其他查询元素的方法: d ...
- [工作相关] 一个虚拟机上面的SAP4HANA的简单使用维护
1.公司组织竞品分析, 选择了SAP的 SAP4HANA作为竞品 这边协助同事搭建了SAP4HANA的测试环境: 备注 这个环境 应该是同事通过一些渠道获取到的. 里面是基于这个虚拟机进行的说明:: ...
- 搭建Spark所遇过的坑
一.经验 1.Spark Streaming包含三种计算模式:nonstate .stateful .window 2.kafka可通过配置文件使用自带的zookeeper集群 3.Spark一切操作 ...
- Angular $broadcast和$emit和$ond实现父子通信
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...
- using 的三种使用方式
using 关键字有两个主要用途: 作为指令:用于为命名空间创建别名或导入在其他命名空间中定义的类型. 作为语句:用于定义一个范围,在此范围的末尾将释放对象. 此外,使用 using 静态指令可定义一 ...
- mysql学习笔记三 —— 数据恢复与备份
要点: 1.存储引擎2.导入导出3.备份与恢复 查看当前数据库中的所有表use db1:show tables: 1.存储引擎 不同的发动机(引擎)适用的汽车类型不一样. 存储和处理的不同方式.不同的 ...
- Lodop在页面获取打印机列表 选择打印机预览
利用GET_PRINTER_COUNT,获取打印机个数,然后用GET_PRINTER_NAME(intPrinterIndex);循环获取打印机名称,添加到list列表里,可以让用户在页面就选择打印机 ...
