Pythan的功能

上传人:碎****木 文档编号:220862214 上传时间:2021-12-09 格式:DOCX 页数:7 大小:19.94KB
返回 下载 相关 举报
Pythan的功能_第1页
第1页 / 共7页
Pythan的功能_第2页
第2页 / 共7页
Pythan的功能_第3页
第3页 / 共7页
亲,该文档总共7页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Pythan的功能》由会员分享,可在线阅读,更多相关《Pythan的功能(7页珍藏版)》请在金锄头文库上搜索。

1、下载程序包:Numpy Scipy Matplotlib Scikit-learn根本学问:1、“.”从左到右由大到小;“.”后的为属性 2、indent 缩进期末考试1、用 python 书写名字首字母,课下完成,最终一节课带过来2、选择代码并进展解释FudanNLP,当堂解释1、Numpy数值计算 Pythan 模块numpyimport numpy检验是否安装成功 numpy.version.full_version检验版本”1.9.2”import numpy as np改名字,将之简写为 a=np.array(0,1,2,3,4,5) aarray(0, 1, 2, 3, 4, 5)

2、 a.ndim 1 a.shape (6,) b=a.reshape(3,2) barray(0, 1,2, 3,4, 5) b.ndim 2 b.shape (3, 2)2、Matplotlib 可视化Matpoltlib 没有属性,它是面对对象的,这在import 中会消灭报错 from matplotlib import pyplot as plt命名空间,只能命名里面其中一个包,由于数量太多需要分开 from pylab import randn,hist从 pylab 这个模块中随机生成随机数一般呈正态分布; hist 为直方图 _x=randn(1000)赋值于x,产生 1000

3、个随机数 hist(x,100)分成 100 个等距的组(array(1.,0.,1.,0.,0.,0.,1.,0.,0.,2.,0.,2.,1.,2.,2.,1.,1.,2.,2.,3.,6.,2.,3.,4.,6.,6.,9.,9.,6.,9.,11.,10.,12.,15.,20.,14.,20.,17.,23.,11.,28.,25.,19.,22.,22.,21.,29.,20.,36.,30.,23.,28.,32.,26.,30.,23.,22.,27.,18.,20.,30.,23.,23.,16.,14.,17.,19.,11.,21.,8.,8.,14.,8.,11.,9.

4、,3.,3.,1.,4.,4.,1.,3.,1.,2.,0.,1.,2.,0.,2.,3.,0.,0.,0.,0.,1.,0.,0.,1.,0.,1.), array(-3.26652977, -3.20213814, -3.13714652, -3.0724549 , -3.00776327,-2.94307165, -2.87838003, -2.8136884 , -2.74899678, -2.68430516,-2.61961353, -2.55492191, -2.49023029, -2.42553866, -2.36084704,-2.29615542, -2.23146379

5、, -2.16677217, -2.10208055, -2.03738892,-1.9726973 , -1.90800568, -1.84331405, -1.77862243, -1.71393081,-1.64923918, -1.58454756, -1.51985594, -1.45516431, -1.39047269,-1.32578107, -1.26108944, -1.19639782, -1.13170619, -1.06701457,-1.00232295, -0.93763132, -0.8729397 , -0.80824808, -0.74355645,-0.6

6、7886483, -0.61417321, -0.54948158, -0.48478996, -0.42021834,-0.35540671, -0.29071509, -0.22602347, -0.16133184, -0.09664022,-0.0319486 ,0.03274303,0.09743465,0.16212627,0.2268179 ,0.29150952,0.35620214,0.42089277,0.48558439,0.55027601,0.61496764,0.93842575,0.67965926,1.00311738,0.74435088,1.067809,0

7、.80904251,1.13250062,0.87373413,1.19719225,1.26188387,1.32657549,1.39126712,1.45595874,1.52065036,1.58534199,1.65003361,1.71472523,1.77941686,1.84410848,1.9088001 ,1.97349173,2.03818335,2.10287497,2.1675666 ,2.23225822,2.29694984,2.36164147,2.42633309,2.49102471,2.55571634,2.62040796,2.68509958,2.74

8、979121,2.81448283,2.87917445,2.94386608,3.0085577 ,3.07324932,3.13794095,3.20263257), ) plt.show()plt 指 pyplot,刚刚命名了,用作画图消灭直方图 import numpy as np命名 y=np.array(0,1,2,3,4,5)array 为向量,把同类的数聚在一起 x=randn(1000) y=randn(1000) plt.scatter(x,y)scatter 是散布的意思 plt.show()消灭散点图 plt.title(“No trend“)命名图的标题 plt.xl

9、abel(“A“)命名 x 轴 plt.ylabel(“B“)命名y 轴 plt.grid()形成网格 plt.show()消灭图,但是没有数据,被清空了,要重新设定xy plt.xticks(w*7*24 for w in range(10),weak %i %w for w in range(10)调整点的大小3、安装 pip检验程序:cd 为转变路径pip list 检验你下载的程序包4、sklearniris 数据:表示一个花的全部属性import sklearnfrom sklearn import datasetsdatasets 为自带的数据库,scikit 自带的供测试用iri

10、s=datasets.load_iris()data=iris.datadata.shape (150,4)150 行 4 列digits=datasets.load_digits()digits.images.shape (1797,8,8)import pylab as plpl.imshow(digits.images-1,cmap=pl .gray_r)5、线性模型 from sklearn import linear_modelimport 线性模型 clf=linear_model.LinearRegression()定义 clf 为一个线性模型 clf.fit(0,0,1,1,2

11、,2,0,1,2)在里面设置点,像设置地点一样LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False) clf.coef_求 a、b 系数,前面两个集的相关系数array( 0.5,0.5)得出相关系数 import matplotlib.pyplot as pltmatplotlib 负责画图 import numpy as npnumpy 是科学计数的模块 from sklearn import datasets,linear_model diabetes=datasets.load_diabe

12、tes()把 diabetes 糖尿病的数据下载下来 diabetes_X=diabetes.data:,np.newaxis定义X 轴,newaxis 为 numpy 中定义X轴 的 功能 diabetes_X_temp=diabetes_X:,:,2首先,数据被分成两局部:训练数据和测试数据,中括号里表示糖尿病的三个属性,第三个属性有两个变量,temp 为临时文件 diabetes_X_train=diabetes_X_temp:-20-20 以前的数据用作训练数据 diabetes_X_test=diabetes_X_temp-20:-20 后面的数据用作测试,X 为自变量 diabet

13、es_Y_train=diabetes.target:-20Y 为因变量目标变量,处理同 Xdiabetes_Y_test=diabetes.target-20:处理同X regr=linear_model.LinearRegression()形成线性回归 regr.fit(diabetes_X_train,diabetes_Y_train) fit表示拟合,在 X、Y 的训练集里取出数据形成线性回归LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False) print(”Coefficients: n”,regr.coef_)求其回归系数Coefficients: 938.23786125 print(“Residuals sum of squares:%.2f“2f 代表字体大小,求残差平方和%np.mean(regr.predict(diabetes_X_test)-diabetes_Y_test)*2)使用测试数据

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号