关于Numpy、Pandas、matplotlib笔记 关于Numpy的学习 关于Pandas的学习 关于Matplotlib的学习
关于Numpy的学习
Numpy的代码练习
import numpy as np
a= np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
print ( a)
print ( a. dtype)
a. astype( np. float128)
print ( a. dtype)
b= a. astype( np. float128)
print ( a. dtype)
print ( b. dtype) import numpy as np
x= np. empty( [ 3 , 2 ] )
print ( x)
print ( '..............' )
y= np. empty( [ 2 , 3 ] )
print ( y)
print ( '..............' )
z= np. zeros( ( 5 , 2 ) , dtype= int )
print ( z)
print ( '..............' )
x= np. ones( [ 2 , 2 ] , dtype= int )
print ( x)
print ( '..............' )
b= [ ( 1 , 2 , 3 ) , ( 4 , 5 ) ]
a= np. asarray( b)
print ( a)
print ( '..............' )
a= np. asarray( [ 1 , 2 , 3 , 4 ] ) . reshape( ( 2 , 2 ) )
print ( a)
print ( '..............' )
x= np. arange( 5 )
print ( x) import numpy as np
print ( np. logspace( 0 , 2 , 5 ) ) print ( np. random. random( ( 3 , 2 , 3 ) ) ) print ( '________________________' ) a= np. arange( 10 )
b= a[ 5 ]
c= a[ 3 ]
print ( a)
print ( b)
print ( c)
print ( '________________________' )
a= np. array( [ [ 1 , 2 , 3 ] , [ 3 , 4 , 5 ] , [ 4 , 5 , 6 ] ] )
print ( a)
print ( a[ . . . , 1 ] ) print ( a[ 1 , . . . ] ) print ( a[ . . . , 1 : ] ) import numpy as np
print ( np. logspace( 0 , 2 , 5 ) ) print ( np. random. random( ( 3 , 2 , 3 ) ) ) print ( '________________________' ) a= np. arange( 10 )
b= a[ 5 ]
c= a[ 3 ]
print ( a)
print ( b)
print ( c)
print ( '________________________' )
a= np. array( [ [ 1 , 2 , 3 ] , [ 3 , 4 , 5 ] , [ 4 , 5 , 6 ] ] )
print ( a)
print ( a[ . . . , 1 ] ) print ( a[ 1 , . . . ] ) print ( a[ . . . , 1 : ] ) print ( '________________________' )
import numpy as np
x= np. array( [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] )
y= x[ [ 0 , 1 , 2 ] , [ 0 , 1 , 0 ] ]
print ( x)
print ( y) print ( '________________________' ) import numpy as np
a = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
print ( a) b= a[ 1 : 3 , 1 : 3 ]
print ( b) c= a[ 1 : 3 , [ 1 , 2 ] ]
print ( c)
d= a[ . . . , 1 : ]
print ( d) print ( '________________________' ) x= np. arange( 32 ) . reshape( ( 8 , 4 ) )
print ( x)
print ( x[ [ - 4 , - 2 , - 1 , - 7 ] ] )
import numpy as np
a1= np. array( [ 1 , 2 , 6 , 4 ] )
a2= np. array( [ 4 , 5 , 6 , 0 ] )
print ( 'MOD:' , np. mod( a2, a1) )
print ( 'DOT:' , np. dot( a1, a2) )
print ( "a1>a2?" , np. greater( a1, a2) )
print ( "a1>=a2?" , np. greater_equal( a1, a2) )
print ( "a1<a2?" , np. less( a1, a2) )
print ( "a1<=a2?" , np. less_equal( a1, a2) )
print ( 'a1!=a2?' , np. not_equal( a1, a2) )
print ( 'a1&a2:' , np. logical_and( a1, a2) )
print ( 'a1|a2:' , np. logical_or( a1, a2) )
print ( 'a1^a2:' , np. logical_xor( a1, a2) )
print ( 'max(a1,a2):' , np. maximum( a1, a2) )
print ( 'max(a1,a2):' , np. fmax( a1, a2) )
print ( 'min(a1,a2):' , np. minimum( a1, a2) )
print ( 'min(a1,a2):' , np. fmin( a1, a2) )
print ( 'a1*a2:' , np. multiply( a1, a2) )
print ( '------numpy的统计函数--------' )
import numpy as np
a= np. array( [ [ 3 , 7 , 8 ] , [ 7 , 4 , 2 ] , [ 36 , 5 , 7 ] ] )
print ( a)
print ( np. amin( a, 1 ) )
print ( np. amax( a, axis= 0 ) )
print ( np. amin( a, 0 ) )
print ( '------1--------' )
print ( np. mean( a, axis= 0 ) )
print ( np. std( [ 1 , 2 , 3 , 4 ] ) )
print ( np. var( [ 1 , 2 , 3 , 4 ] ) )
print ( '------排序、条件刷选函数--------' )
import numpy as np
a= np. array( [ [ 3 , 7 ] , [ 9 , 1 ] ] )
print ( a)
print ( np. sort( a, axis= 0 ) )
print ( np. argsort( a, axis= 0 ) )
print ( '-----' )
x= np. array( [ 3 , 1 , 2 ] )
y= np. argsort( x, axis= 0 )
print ( 'x' , x)
print ( 'y' , y) print ( 'np.argmax/np.argmin' )
a = np. array( [ [ 30 , 40 , 70 ] , [ 80 , 20 , 10 ] , [ 50 , 90 , 60 ] ] )
print ( a)
print ( 'max:' , np. max ( a, axis= 0 ) )
print ( 'argmax:' , np. argmax( a, axis= 0 ) )
print ( 'argmax:' , np. argmax( a, axis= 1 ) )
print ( 'argmin:' , np. argmin( a, axis= 0 ) ) import numpy as np
x= np. arange( 9. ) . reshape( 3 , 3 )
print ( '我们的数组是:' )
print ( x)
print ( '大于3的元素的索引:' )
y= np. where( x> 3 )
print ( y)
print ( '利用索引提取元素!' )
print ( x[ y] ) print ( 'numpy.nonzero:返回所用输出数组中非零元素的索引!' )
a= np. array( [ [ 30 , 40 , 0 ] , [ 0 , 20 , 10 ] , [ 50 , 0 , 60 ] ] )
print ( '我们的数组是:' )
print ( a)
print ( '\n' )
print ( '调用nonzero()函数:' )
print ( np. nonzero( a) ) print ( '______________np.unique函数' )
a= [ 1 , 2 , 2 , 5 , 3 , 4 , 3 ]
print ( a)
print ( np. unique( a) )
b= ( 1 , 2 , 2 , 5 , 3 , 4 , 3 )
print ( np. unique( b) )
c= [ 'fgfh' , 'asd' , 'fgfh' , 'asdfds' , 'wrh' ]
print ( np. unique( c) )
关于Pandas的学习
Pandas代码的练习
import pandas as pd
import numpy as npobj= pd. Series( [ 4 , 7 , - 5 , 3 ] )
print ( obj)
print ( '______________' )
obj2= pd. Series( [ 4 , 7 , - 5 , 3 ] , index= [ 'd' , 'b' , 'a' , 'c' ] )
print ( obj2) obj1= pd. Series( [ 0 , 1 , 2 , 3 , 4 ] , index= [ 'a' , 'b' , 'c' , 'd' , 'e' ] )
print ( obj1) mylist= list ( 'zxcvbnnmasdfghjklqwertyuiop' )
myarr= np. arange( 26 )
mydict= dict ( zip ( mylist, myarr) )
print ( 'mylist' , mylist)
print ( 'myarr' , myarr)
print ( 'mydict' , mydict)
s1= pd. Series( mylist)
s2= pd. Series( myarr)
s3= pd. Series( mydict)
print ( 's1' , s1)
print ( 's2' , s2)
print ( 's3' , s3) print ( s3. head( ) )
print ( s3. head( 15 ) )
print ( s3. head( 3 ) ) print ( '----------------------------' ) obj2= pd. Series( [ 4 , 7 , - 5 , 3 ] , index= [ 'd' , 'b' , 'a' , 'c' ] )
print ( obj2)
sdata = { 'Ohio' : 35000 , 'Texas' : 71000 , 'Oregon' : 16000 , 'Utah' : 5000 }
print ( '1:' , sdata)
obj= pd. Series( sdata)
obj= obj. astype( int )
print ( '2' , obj)
states= [ 'California' , 'Ohio' , 'Oregon' , 'Texas' ]
obj3= pd. Series( sdata, index= states)
print ( pd. isnull( obj3) )
print ( pd. notnull( obj3) )
print ( 'obj3:' , obj3)
print ( obj3. isnull)
print ( '查找Series:--------------' )
obj1= pd. Series( [ 4 , 7 , - 5 , 3 ] , index= [ 'd' , 'b' , 'a' , 'c' ] )
print ( '1' , obj1)
print ( '2' , obj1. values)
print ( '3' , obj1. index)
print ( '4' , obj1. dtype) obj= pd. Series( [ 4 , 7 , - 5 , 3 , 2 ] , index= [ 'd' , 'b' , 'a' , 'c' , 'a' ] )
print ( obj[ 'a' ] )
print ( obj[ - 1 ] )
print ( obj[ 0 ] )
print ( '多个值' , obj[ : 3 ] ) print ( obj/ 2 )
obj= obj+ 2
print ( obj* 2 ) print ( '----DataaFrame------' )
print ( '表格型的数据结构,含有一组有序的列,' ) data = { 'state' : [ 'Ohio' , 'Ohio' , 'Ohio' , 'Nevada' , 'Nevada' , 'Nevada' ] , 'year' : [ 2000 , 2001 , 2002 , 2001 , 2002 , 2003 ] , 'pop' : [ 1.5 , 1.7 , 3.6 , 2.4 , 2.9 , 3.2 ] }
print ( data)
frame= pd. DataFrame( data)
print ( frame) print ( pd. DataFrame( data, columns= [ 'year' , 'state' , 'pop' ] ) )
frame2 = pd. DataFrame( data, columns= [ 'year' , 'state' , 'pop' , 'debt' ] , index= [ 'one' , 'two' , 'three' ,
'four' , 'five' , 'six' ] )
print ( '缺失' , frame2)
print ( frame2. columns)
print ( frame2. year)
print ( frame2. loc[ 'three' ] ) obj = pd. Series( [ 4.5 , 7.2 , - 5.3 , 3.6 ] , index= [ 'd' , 'b' , 'a' , 'c' ] )
print ( 'obj' , obj)
obj2= obj. reindex( [ 'a' , 'b' , 'c' , 'd' , 'e' ] )
print ( obj2)
obj3= pd. Series( [ 'blue' , 'purple' , 'yellow' ] , index= [ 0 , 2 , 4 ] )
print ( obj3)
aa= [ 'blue' , 'purple' , 'yellow' ]
bb= [ 0 , 2 , 4 ]
obj4= pd. Series( aa, index= bb)
print ( '3:' , obj3. reindex( range ( 6 ) , method= 'ffill' ) )
print ( '4:' , obj4. reindex( range ( 6 ) , method= 'ffill' ) ) print ( '修改行和列' ) frame= pd. DataFrame( np. arange( 9 ) . reshape( ( 3 , 3 ) ) , index= [ 'a' , 'c' , 'd' ] , columns= [ 'ohio' , 'Texas' , 'California' ]
)
print ( frame) frame2= frame. reindex( [ 'a' , 'b' , 'c' , 'd' ] )
print ( frame2)
data= frame2. drop( [ 'a' ] )
print ( data)
print ( frame2)
data= frame2. drop( 'ohio' , axis= 1 )
print ( data)
print ( frame2) data= pd. DataFrame( np. arange( 24 ) . reshape( ( 6 , 4 ) ) , index= [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] , columns= [ 'one' , 'two' , 'three' , 'four' ] )
print ( data) print ( '2' , data[ 'two' ] )
print ( data[ [ 'three' , 'one' , 'two' ] ] ) print ( data[ : 2 ] )
print ( data)
print ( data[ data[ 'three' ] > 5 ] )
print ( data. iloc[ 2 , [ 3 , 0 , 1 ] ] )
print ( data< 5 )
print ( data)
print ( data. loc[ 'c' , [ 'two' , 'one' ] ] )
print ( data. iloc[ 2 , [ 3 , 0 , 1 ] ] )
print ( data. iloc[ [ 1 , 2 ] , [ 3 , 0 , 1 ] ] )
print ( data. iloc[ 2 ] ) print ( '索引选取和过滤:' )
obj= pd. Series( np. arange( 4. ) , index= [ 'a' , 'b' , 'c' , 'd' ] )
print ( obj)
print ( obj[ 'b' ] )
print ( obj[ 2 : 4 ] ) o1= obj[ [ 'b' , 'd' ] ] = 5
print ( obj) o1= obj[ 'b' : 'd' ] = 5
print ( obj) print ( data)
print ( '-------------' )
print ( data) a= data. loc[ : 'b' , 'two' ]
b= data. iloc[ : , : 3 ] [ data. three> 5 ]
print ( '-----' , a)
print ( '-0--------' , b) df1 = pd. DataFrame( np. arange( 9. ) . reshape( ( 3 , 3 ) ) , columns= list ( 'bcd' ) , index= [ 'O' , 'T' , 'C' ] )
df2 = pd. DataFrame( np. arange( 12. ) . reshape( ( 4 , 3 ) ) , columns= list ( 'bde' ) , index= [ 'U' , 'O' ,
'T' , 'O' ] )
print ( '1------' , df1)
print ( '2---------' , df2)
print ( '3--------' , df1+ df2)
print ( '4----' , df1- df2)
frame = pd. DataFrame( np. random. randn( 4 , 3 ) , columns= list ( 'bde' ) , index= [ 'Utah' , 'Ohio' , 'Texas' ,
'Oregon' ] )
print ( frame)
f= lambda x: x. max ( ) - x. min ( )
print ( frame. apply ( f) )
print ( frame. apply ( f, axis= 'columns' ) ) def f1 ( x) : return pd. Series( [ x. min ( ) , x. max ( ) ] , index= [ 'min' , 'max' ] )
print ( frame. apply ( f1) )
关于Matplotlib的学习
Matplotlib代码练习
import matplotlib. pyplot as plt
import numpy as np
import time
'''
根据前两个numpy和pandas的编程初步可以这样简要概括:
numpy主要负责一些矩阵的创建和矩阵的一些基本运算
pandas基于numpy运用了一些表格、列表的制作和增删改查的功能
接下来我们看看这个matplotlib的使用方法和方式
''' s_t1= time. time( )
plt. plot( [ 1 , 2 , 3 ] , [ 5 , 7 , 4 ] )
plt. show( ) plt. plot( [ 3.75 , 4.58 , 55.99 ] , [ - 59 , 5 , - 21 ] )
plt. show( ) e_t1= time. time( ) f_t = e_t1 - s_t1
print ( "所用时间为:{}s" . format ( abs ( f_t) ) ) print ( '---------------------' ) figure= plt. figure( )
axes1= figure. add_subplot( 2 , 1 , 1 , label= 'a1' )
axes2= figure. add_subplot( 2 , 1 , 1 , label= 'a2' )
print ( axes1, axes2)
figure. show( ) print ( '---------------------' )
import tkinter as tk
import matplotlib
import numpy as np
import matplotlib. pyplot as pltfig1= plt. figure( num= 'fig1' , figsize= ( 6 , 9 ) )
plt. xlim( 0 , 20 )
plt. ylim( 0 , 10 ) plt. show( )
print ( '---------------------' )
plt. figure( '正弦曲线' )
x= np. linspace( - 1 , 1 , 100 )
y1= x* 2 + 1
y2= x** 2 plt. plot( x, y1)
plt. plot( x, y2)
plt. show( )
import matplotlib. pyplot as plt
import numpy as np
x= np. linspace( - 3 , 3 , 20 )
y1= 2 * x* 3 + 3
y2= x** 3
plt. figure( num= 1 , figsize= ( 8 , 5 ) )
plt. plot( x, y1, color= 'c' , marker= '+' , linewidth= 2 , linestyle= '-.' )
plt. plot( x, y2, color= 'y' , marker= '*' , linestyle= ':' , linewidth= 1 , markersize= 6 )
plt. show( )
x= np. linspace( - 7 , 7 , 50 )
y1= - x+ x** 2
y2= 2 * x+ ( x- 1 ) ** 2
y3= 2 ** - x
y4= x** x
plt. figure( num= 2 , figsize= ( 10 , 10 ) )
plt. plot( x, y1, color= 'y' , linewidth= 2 , linestyle= '-' , label= 'y1=-x+x**2' )
plt. plot( x, y2, color= 'r' , label= 'y2=2*x+(x-1)**2' )
plt. plot( x, y3, color= 'b' , marker= '+' , linewidth= 2 , linestyle= '-.' , label= 'y3=2**-x' )
plt. plot( x, y4, color= 'k' , label= 'y4=x**x' )
plt. xlim( - 2 , 2 )
plt. ylim( - 3 , 3 )
plt. xlabel( 'this is x' ) plt. ylabel( 'This is y' ) plt. legend( loc= 'best' )
plt. show( )
x= np. linspace( - 20 , 20 , 50 ) y1= x** 2
y2= x* y1plt. figure( '自创的曲线图' , figsize= ( 8 , 8 ) ) plt. plot( x, y1, color= 'y' )
plt. plot( x, y2, color= 'r' ) plt. xlim( - 20 , 40 )
plt. ylim( - 20 , 20 )
plt. xlabel( 'This is X' )
plt. ylabel( 'This is Y' )
new_ticks= np. linspace( - 20 , 20 , 15 ) plt. xticks( new_ticks) plt. yticks( [ - 20 , - 10 , 0 , 10 , 20 ] , [ r'$very\ bad$' , '$bad$' , '$so...$' , '$well$' , '$really\ well$' ] ) ax= plt. gca( )
ax. spines[ 'right' ] . set_color( 'none' )
ax. spines[ 'top' ] . set_color( 'none' )
plt. show( ) x= np. linspace( - 20 , 20 , 50 ) y1= x** 2
y2= x* y1plt. figure( '添加图例' , figsize= ( 8 , 8 ) ) l1, = plt. plot( x, y1, color= 'y' , label= 'linear line' )
l2, = plt. plot( x, y2, color= 'r' , label= 'square line' ) plt. xlim( - 20 , 40 )
plt. ylim( - 20 , 20 ) new_ticks= np. linspace( - 20 , 20 , 15 ) plt. xticks( new_ticks) plt. yticks( [ - 20 , - 10 , 0 , 10 , 20 ] , [ r'$very\ bad$' , '$bad$' , '$so...$' , '$well$' , '$really\ well$' ] ) ax= plt. gca( )
ax. spines[ 'right' ] . set_color( 'none' )
ax. spines[ 'top' ] . set_color( 'none' ) plt. legend( loc= '0' )
plt. show( )
n= 1024
X= np. random. normal( 0 , 1 , n)
Y= np. random. normal( 0 , 1 , n)
T= np. arctan2( Y, X)
plt. scatter( X, Y, s= 75 , c= T, alpha= 0.5 )
plt. xlim( - 1.5 , 1.5 )
plt. xticks( ( ) )
plt. ylim( - 1.5 , 1.5 )
plt. yticks( ( ) )
plt. show( )
n= 12
X= np. arange( n)
Y1= ( 1 - X/ float ( n) ) * np. random. uniform( 0.5 , 1 , n) Y2= ( 1 - X/ float ( n) ) * np. random. uniform( 0.5 , 1 , n) plt. bar( X, + Y1, facecolor= '#9999ff' , edgecolor= 'white' )
plt. bar( X, - Y2, facecolor= '#ff9999' , edgecolor= 'white' ) for x, y in zip ( X, Y1) : plt. text( x+ 0.4 , y+ 0.05 , '%.2f' % y, ha= 'center' , va= 'bottom' )
for x, y in zip ( X, Y2) : plt. text( x+ 0.4 , - y- 0.05 , '%.2f' % y, ha= 'center' , va= 'top' ) plt. xlim( - 0.5 , n)
plt. xticks( ( ) ) plt. ylim( - 1.25 , 1.25 )
plt. yticks( ( ) ) plt. show( ) plt. figure( '柱状图' )
x_index= np. arange( 5 )
x_data= ( 'A' , 'B' , 'C' , 'D' , 'E' )
y1_data= ( 20 , 35 , 30 , 35 , 27 )
y2_data= ( 25 , 32 , 34 , 20 , 25 )
bar_width= 0.35 plt. bar( x_index, y1_data, width= bar_width, alpha= 0.4 , color= 'b' , label= 'legend1' ) plt. bar( x_index+ bar_width, y2_data, width= bar_width, alpha= 0.5 , color= 'r' , label= 'legend2' ) plt. xticks( x_index+ bar_width/ 2 , x_data) plt. legend( loc= 0 ) plt. tight_layout( )
plt. show( ) n= 256
x= np. linspace( - 3 , 3 , n)
y= np. linspace( - 3 , 3 , n)
X, Y= np. meshgrid( x, y)
def f ( x, y) : return ( 1 - x/ 2 + x** 5 + y** 3 ) * np. exp( - x** 2 - y** 2 )
plt. contour( X, Y, f( X, Y) , 8 , alpha= 0.75 , cmap= plt. cm. hot)
C= plt. contour( X, Y, f( X, Y) , 8 , colors= 'black' , linewidth= 0.5 )
plt. clabel( C, inline= True , fontsize= 10 )
plt. xticks( ( ) )
plt. yticks( ( ) )
plt. show( ) plt. figure( '子图' )
plt. subplot( 2 , 1 , 1 )
plt. plot( [ 0 , 1 ] , [ 0 , 1 ] )
plt. subplot( 2 , 3 , 4 )
plt. plot( [ 0 , 1 ] , [ 0 , 2 ] )
plt. subplot( 2 , 3 , 4 )
plt. plot( [ 0 , 1 ] , [ 0 , 3 ] )
plt. subplot( 3 , 3 , 8 )
plt. plot( [ 0 , 1 ] , [ 0 , 4 ] )
plt. show( )
fig= plt. figure( '来个图中图' )
x= np. arange( 7 )
y= [ 1 , 3 , 4 , 2 , 5 , 8 , 6 ]
print ( 'x-----------' , x)
print ( 'y-----------' , y)
left, bottom, width, height= 0.1 , 0.1 , 0.8 , 0.8
appp= [ left, bottom, width, height]
ax1= fig. add_axes( appp)
ax1. plot( x, y, 'r' )
ax1. set_xlabel( 'x' )
ax1. set_ylabel( 'y' )
ax1. set_title( '大图' ) ax2= fig. add_axes( [ 0.2 , 0.6 , 0.25 , 0.25 ] )
ax2. plot( y, x, 'b' )
ax2. set_xlabel( 'x' )
ax2. set_ylabel( 'y' )
ax2. set_title( 'title inside 1' )
plt. axes( [ 0.6 , 0.2 , 0.25 , 0.25 ] )
plt. plot( y[ : : 1 ] , x, 'y' ) plt. show( )
x= np. arange( 0 , 10 , 0.1 )
y1= 0.5 * x** 2
y2= - 1 * y1
fig, ax1= plt. subplots( ) ax2= ax1. twinx( )
ax1. plot( x, y1, 'g-' )
ax2. plot( x, y2, 'b-' ) ax1. set_xlabel( 'X data' )
ax1. set_ylabel( 'Y1 data' , color= 'g' )
ax2. set_ylabel( 'Y2 data' , color= 'b' )
plt. show( )