ADVANCED P Y T HON JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE NumPy Cheat Sheet D EC ODI N G D A T A S C I E N C E JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 1 . Basi c Commands Importing NumPy and checking its version: ```python import numpy as np print(np.__version__) ``` JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 2. Array Creat i on Creating NumPy arrays from lists and with initial placeholders: ```python # From a list arr = np.array([1, 2, 3, 4, 5]) # Array of zeros arr = np.zeros((3, 3)) # Array of ones arr = np.ones((3, 3)) # Array with a range of values arr = np.arange(0, 10) # Array of random values arr = np.random.rand(3, 3) ``` JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 3. Array At t ri but es Getti ng an array' s shape and data type: ```python arr = np. array([ [ 1, 2, 3] , [ 4, 5, 6] ] ) # Shape pri nt(arr. shape) # Data type pri nt(arr. dtype) ``` JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 4. I ndexi ng and Sl i ci ng Indexing and slicing one-dimensional and multi-dimensional arrays: ```python arr = np.array([1, 2, 3, 4, 5]) # Get the first element print(arr[0]) # Get the last element print(arr[-1]) # Get a slice from the second to the fourth element print(arr[1:4]) ``` JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 5. Array Mani pul at i on Vari ous ways to mani pul ate arrays such as reshapi ng, stacki ng, and spl i tti ng: ```python arr = np.array([ [ 1, 2, 3] , [ 4, 5, 6] ] ) # Reshape arr_reshaped = arr.reshape((3, 2)) # Verti cal stack arr_stack = np.vstack([ arr, arr] ) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 6. Ari t hmet i c Operat i ons Performing addition, subtraction, multiplication, division, and dot product on arrays: ```python arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # Addition print (arr1 + arr2) # Subtraction print (arr1 - arr2) # Multiplication print (arr1 * arr2) # Division print (arr1 / arr2) ``` JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 7. St at i st i cal Operat i ons Calculating the mean, median, and standard deviation of an array: ```python arr = np.array([1, 2, 3, 4, 5]) # Mean print(np.mean(arr)) # Median print(np.median(arr)) # Standard deviation print(np.std(arr)) ``` JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE Mat pl ot l i b Cheat Sheet D EC ODI N G D A T A S C I E N C E JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 1 . Basi c Commands Matplot l i b i s a p l o t t i n g l i b rary for the P y t hon p r o g r a m m i n g l a n g uage and its n u m eri c a l m a t h e m a t i c s e x t ens i o n N u m P y . - I mport i n g Ma t p l o t l i b : i mport ma t p l o t l i b . p y p l o t a s plt - C hecki n g Ma t p l o t l i b v e r s ion: i mport ma t p l o t l i b p r i nt(ma t p l o tl i b . _ _ v e r s i o n __) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 2. Basi c Pl ot t i ng Matplot l i b p r ov i d e s f u n c t i onalities for various types of plots. - L ine P l o t : p lt . p l o t ( [ 1 , 2 , 3 , 4], [1, 4, 9, 16]) - S catte r P l o t: p l t . s c a t t e r ( [1, 2, 3, 4], [1, 4, 9, 16]) - B ar Pl o t : p l t. b a r ( [ ' g r o u p _ a', 'group_b', 'group_c'], [1, 10, 5]) - H istog r a m: p l t . h i s t ( [ 1 , 2 , 2, 3, 4, 4, 4, 5, 5, 5, 5]) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 3. Fi gure and Axes A f igure i n ma t p l o t l i b m e a ns the whole window in the user i n t erfac e . A x is a r e t h e n u mber-line-like objects and they t a k e car e o f ge n e r a t i n g t h e graph limits. - C reati n g F i gu r e a n d A x e s : fig, ax = plt.subplots() - S ettin g F i g ur e S i z e : f i g , ax = plt.subplots(figsize=( 6, 4)) - S ettin g A x i s L a b e l s a n d Title: a x . set_x l a b e l( ' x ' ) a x . set_y l a b e l( ' y ' ) a x . set_t i t l e ( ' Ti t l e ' ) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 4. Cust omi zi ng Pl ot s Matplot l i b a l lo w s y o u t o c ustomize various aspects of y o u r plo t s . - C hangi n g L in e S t y l e a n d Color: p l t .plot ( [ 1 , 2 , 3 , 4 ] , [ 1 , 4 , 9, 16], linestyle='--', color='r') - A dding G r i d: p l t .grid ( T r u e ) - S ettin g A x i s L i m i t s : p l t .xlim( 0 , 5 ) p l t .ylim( 0 , 2 0 ) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 5. Mul t i pl e Pl ot s Matplot l i b p r ov i d e s f u n c t i onalities to create multiple p l o ts in a s i n gl e f i g u r e . - S ubplo t s : f i g , axs = p l t .s u b p l o t s ( 2 ) - S harin g A x i s: f i g , axs = p l t .s u b p l o t s ( 2 , s harex=True, sharey=True) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 6. Text and Annot at i ons Matplot l i b p r ov i d e s f u n c t i onalities to add text and a n n ota t i o n s t o t h e p l o t s . - A dding T e x t: p l t .text ( 0 . 5 , 0. 5 , ' H e l l o ' ) - A dding A n n ot a t i o n s : p l t .anno t a t e ( 'H e l l o ' , x y = ( 0 . 5, 0.5), xytext=(0.6, 0.6), a r r owp r o p s = d i c t ( f a c e c o l o r ='black', shrink=0.05)) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 7. Savi ng Fi gures Matplot l i b p r ov i d e s t h e s a vefig() function to save the c u r rent f i g u r e t o a f i l e . - S aving F i g ur e s a s P N G , P DF, SVG, and more: p l t .save f i g ( ' f ig u r e . p n g ' ) p l t .save f i g ( ' f ig u r e . p d f ' ) p l t .save f i g ( ' f ig u r e . s v g ' ) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE Pandas Cheat Sheet D EC ODI N G D A T A S C I E N C E JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 1 . Basi c Commands Pandas is a software library for Python that provides tools for data manipulation and analysis. It' s important to ensure that the correct version of pandas is installed for compatibility with your code. - Importing pandas: import pandas as pd - Checking pandas version: print(pd.__version__) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 2. Dataframe Creation Dataframes are two-dimensional labeled data structures with columns potentially of different types. You can think of it like a spreadsheet or SQL table. - From a list: my_list = [1, 2, 3, 4, 5] df = pd.DataFrame(my_list, columns=[' column_name' ]) - From a dictionary: my_dict = {' A' : [1, 2, 3], ' B' : [4, 5, 6]} df = pd.DataFrame(my_dict) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 3. Dat a Sel ect i on Pandas provides different methods for data selection. - Selecting a column: df[' A' ] - Selecting multiple columns: df[[' A' , ' B' ]] - Selecting rows: df.loc[0] # row label df.iloc[0] # row index - Selecting specific value: df.at[0, ' A' ] # row label and column name df.iat[0, 0] # row index and column index JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 4. Dat a Mani pul at i on P a n das p r o v i d e v a r i o u s w a ys to manipulate a dataset. - A dding a c ol u m n : d f [ 'C'] = p d . S e r i e s ( [ 7 , 8 , 9 ]) - D eleti n g a co l u m n : d f . d r op('C', axis=1, inplace=True) - R enami n g co l u m n s : d f . r ename(columns={'A': 'new_A '}, i n p lace= T r u e ) - A pplyi n g a fu n c t i o n t o a column: d f [ 'A']. a p p l y ( la m b d a x : x * 2) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 5. Dat a Cl eani ng D a t a cle a n i n g i s t h e p r o c e ss of detecting and correcting ( o r remo v i n g ) c o r r u p t o r i naccurate records from a d a t ase t . - C hecki n g f or n u l l v a l u e s : d f . isnul l ( ) - D roppi n g n ul l v a l u e s : d f . dropn a ( i n pl a c e = T r u e ) - F illin g n u l l va l u e s : d f . filln a ( v a l u e= 0 , i n p l a c e =True) - R eplac i n g va l u e s : d f . repla c e ( 1 , 1 0 , i n p l a c e = True) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 6. Groupi ng & Aggregat i on G r o upi n g i n v o l v e s c o m b i n i ng data based on some c r i teria , w h i l e a g g r e g a t i o n is the process of turning the r e s ults o f a qu e r y i n t o a s i ngle row. - G roup b y : d f . group b y ( ' A' ) - A ggreg a t i o n: d f . agg({ ' A ' : [ ' mi n ' , ' m a x ' , ' m ean', 'sum']}) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 7. Mergi ng, Joi ni ng, and Concat enat i ng Pandas provides various ways to combine DataFrames including merge and join. - Concatenating: df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]}) df = pd.concat([df1, df2]) - Merging: df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) df2 = pd.DataFrame({'A': [1, 2, 3], 'C': [7, 8, 9]}) df = pd.merge(df1, df2, on='A') - Joining: df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) df2 = pd.DataFrame({'C': [7, 8, 9]}) df = df1.join(df2) JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 8. Worki ng wi t h Dat es Pandas provides powerful functionalities for working with dates. - Convert to datetime: df['date'] = pd.to_datetime(df['date']) - Extracting date parts: df['year'] = df['date'].dt.year df['month'] = df['date'].dt.month df['day'] = df['date'].dt.day JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE 9. Fi l e I /O Pandas can seamlessly read from and write to a variety of file formats. - Reading a CSV file: df = pd.read_csv('file.csv') - Writing to a CSV file: df.to_csv('file.csv', index=False) - Similarly for other file formats like Excel (read_excel, to_excel), JSON (read_json, to_json), SQL (read_sql, to_sql), etc. JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE Joi n Our AI Communi t y Being part of an AI community like ours offers multiple benefits. You can network with likeminded individuals, learn from experienced professionals, and stay up-to-date with the latest AI trends and developments. Whether you're a beginner looking to start your journey in AI, or an experienced professional looking to enhance your skills, our community has something to offer. Join us and be a part of this exciting journey. Learn more, Grow more! Click the link in the footer to join us today! JOIN OUR AI COMMUNITY: HTTPS://NAS.IO/ARTIFICIALINTELLIGENCE