前言 Python在机器学习方面有天然的优势,那么我们今天也来涉足一下机器学习方面的技术,以下是在学习过程中的一些笔记,里面有大量的注释说明,用于理解为什么这样操作。 代码实现如下:NumpyPandasMatplotlibIpythonNumPy(NumericalPython)是Python语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。importnumpyasnpPandas可以对各种数据进行运算操作,比如归并、再成形、选择,还有数据清洗和数据加工特征importpandasaspdMatplotlib是Python的绘图库。它可与NumPy一起使用,提供了一种有效的MatLab开源替代方案importmatplotlib。pyplotaspltIpython。display的库是用来展示图片的fromIPython。displayimportImagefromsklearn。modelselectionimporttraintestsplitimportwarningswarnings。filterwarnings(ignore)datapd。readcsv(train。csv)print(type(data))print(data。info())print(data。shape)print(data。head())print(data〔〔MSSubClass,LotArea〕〕)数据集合缺失值选择数据集合中的几个重要特征dataselectdata〔〔BedroomAbvGr,LotArea,Neighborhood,SalePrice〕〕对数据集中的字段进行重命名dataselectdataselect。rename(columns{BedroomAbvGr:room,LotArea:area})print(dataselect)print(dataselect。shape)print(100)判断缺失值一般采用isnull(),然而生成的却是所有数据的truefalse矩阵print(dataselect。isnull())df。isnull()。any()则会判断哪些列存在缺失值print(dataselect。isnull()。any())只显示存在缺失值的行列,清楚的确定缺失值的位置print(dataselect。isnull()。valuesTrue)对缺失的数据进行过滤dataselectdataselect。dropna(axis0)print(dataselect。shape)print(dataselect。head())print(np。take(dataselect。columns,〔0,1,3〕))print(type(np。take(dataselect。columns,〔0,1,3〕)))归一化处理数太大,归一化,让数据的分布处于同一区间,咱们选择一种最简单的数据调整方法,每一个数除以其最大值forcolinnp。take(dataselect。columns,〔0,1,1〕):print(col)print(dataselect〔col〕)dataselect〔col〕dataselect〔col〕。max()print(dataselect。head())分配测试数据和训练数据train,testtraintestsplit(dataselect。copy(),testsize0。9)print(train。shape)print(test。shape)print(test。describe())numpy里面axis0andaxis1的使用示例说明:print(50)datanp。array(〔〔1,2,3,4〕,〔5,6,7,8〕,〔9,10,11,12〕〕)print(data)print(data。shape)shape〔3,4〕即为3行4列print(np。sum(data))在numpy中若没有指定axis,默认对所有的数据相加print(np。sum(data,axis0))若指定了axis0,则沿着第一个维度的方向进行计算,即为3按列中的3个数据进行计算,得到4组列数据计算结果print(np。sum(data,axis1))若指定了axis1,则沿着第二个维度的方向进行计算,即为4按行中的4个数据进行计算,得到3组行数据计算结果print(50)pandas里面axis0andaxis1的使用示例说明:如果我们调用df。mean(axis1),我们将得到按行计算的均值dfpd。DataFrame(np。arange(12)。reshape(3,4))print(df)print(df。mean())在pandas中,如果没有指定axis,则默认按axis0来计算print(df。mean(axis0))若指定了axis0,则按照第一个维度的变化方向来计算,即为3按列中的3个数据进行计算,得到4组列数据计算结果print(df。mean(axis1))若指定了axis1,则按照第二个维度的变化方向来计算,即为4按行中的4个数据进行计算,得到3组行数据计算结果线性回归模型线性回归模型,假设h(x)wxb是线性的。deflinear(features,pars):print(theparsis:,pars)print(pars〔:1〕)pricenp。sum(featurespars〔:1〕,axis1)pars〔1〕returnpriceprint(100)train〔predict〕linear(train〔〔room,area〕〕。values,np。array(〔0。1,0。1,0。0〕))能够看到,在该参数下,模型的预测价格和真实价格有较大的差距。那么寻找合适的参数值是咱们须要作的事情print(train。head())预测函数为h(x)wxb偏差的平方和函数:defmeansquarederror(predy,realy):returnsum(np。array(predyrealy)2)损失函数:deflostfunction(df,features,pars):df〔predict〕linear(df〔features〕。values,pars)costmeansquarederror(df。predict,df。SalePrice)len(df)returncostcostlostfunction(train,〔room,area〕,np。array(〔0。1,0。1,0。1〕))print(cost)linspace函数原型:linspace(start,stop,num50,endpointTrue,retstepFalse,dtypeNone)作用为:在指定的大间隔内,返回固定间隔的数据。他将返回num个等间距的样本,在区间〔start,stop〕中。其中,区间的结束端点可以被排除在外,默认是包含的。num100Xsnp。linspace(0,1,num)Ysnp。linspace(0,1,num)print(Xs)如果num5〔0。0。250。50。751。〕print(Ys)如果num5〔0。0。250。50。751。〕zeros函数原型:zeros(shape,dtypefloat,orderC)作用:通常是把数组转换成想要的矩阵;示例:np。zeros((2,3),dtypenp。int)Zsnp。zeros(〔num,num〕)100100的矩阵,值全为0。print(Zs)meshgrid从坐标向量中返回坐标矩阵Xs,Ysnp。meshgrid(Xs,Ys)print(Xs。shape,Ys。shape)print(Xs)如果num5则处理后的矩阵为:〔〔0。0。250。50。751。〕〔0。0。250。50。751。〕〔0。0。250。50。751。〕〔0。0。250。50。751。〕〔0。0。250。50。751。〕〕print(Ys)如果num5则处理后的矩阵为:〔〔0。0。0。0。0。〕〔0。250。250。250。250。25〕〔0。50。50。50。50。5〕〔0。750。750。750。750。75〕〔1。1。1。1。1。〕〕W1〔〕W2〔〕Costs〔〕foriinrange(100):forjinrange(100):W1。append(0。01i)W2。append(0。01j)Costs。append(lostfunction(train,〔room,area〕,np。array(〔0。01i,0。01j,0。〕)))numpy。argmin(a,axisNone,outNone)a:一个矩阵axis:整数,可选(没选择的话就是整个数组的展开)(0:行,1列)返回小值的下标indexnp。array(lostfunction)。argmin()print(W1〔index〕,W2〔index〕,Costs〔index〕)frommpltoolkits。mplot3dimportAxes3Dfigplt。figure()axfig。addsubplot(111,projection3d)ax。viewinit(5,15)ax。scatter(W1,W2,Costs,s10)ax。scatter(0。58,0。28,zslostfunction(train,〔room,area〕,np。array(〔0。58,0。28,0。0〕)),s100,colorred)plt。xlabel(rooms)plt。ylabel(llotArea)plt。show()