<jupyter_start><jupyter_text>Numpy 3D arrays<jupyter_code>import numpy as np

d3 = np.arange(50).reshape(2,5,5)
d3<jupyter_output><empty_output><jupyter_text>Indexing 3D<jupyter_code>d3[1,2,1]
d3[1]
d3[0:,2:,1:3]
#d3[1:2,4:5,0:5]
d3[1:,4:,:]
### d3[0:,2:4,1:3]
d3[1,:,3:4]<jupyter_output><empty_output><jupyter_text>BroadCasting<jupyter_code>x = np.array([[1,2,3]])  #(1,3)
y = np.array([[1],[2],[3]])#(3,1)
x+y
x.shape, y.shape
x,y=np.broadcast_arrays(x,y)
x 
y
x+y<jupyter_output><empty_output><jupyter_text>Pandas  Pandas data structureSeriesDataFrame  Series<jupyter_code>import pandas as pd
s = pd.Series([111,222,333,444,555,666,777])
s  # pandas series object
# labels/index and values
#getting a single series value

s[4]
#getting a single series value
s[[5,0,3,1]]
# updating a series value
s[3]= 1000
# updating multiple series value

s[[2,4,0]]=[9999,7777,1111]
s
# deleting a value in series

del s[4]
s
# deleting multiple values in a series
del s[[0,2,6]]

# not allowed
s[1234]= 45676878
s[[123,345,678]]=[23,23,23]
#not allowed
s
s1 = pd.Series([100,200,300,400], index=["apples", "oranges","bananas", "grapes"])
s1<jupyter_output><empty_output><jupyter_text>Creating a series using a dictionary<jupyter_code>aDic = {"name":"<NAME>", "Class":"AI", "Time":"5-9"}
aDic
s2 = pd.Series(aDic)
s2
s3 = pd.Series(s1, index=["mango", "oranges","bananas", "grapes"])
s3
s3["mango"]=100
s3
s3.values
s3.index
s3[s3>200]
s3>200
"Mango" in s3
s4= pd.Series(s3.values, index=[22,33,44,55])
s4
s3
s3+s4<jupyter_output><empty_output><jupyter_text>DataFrame<jupyter_code>df = pd.DataFrame([123,234,345,456])
df
df = pd.DataFrame(["Red","Green", "Blue"],index=['v1','v2','v3'])
df
df = pd.DataFrame(["Red","Green", "Blue"],index=['v1','v2','v3'],columns=["Colors"])
df<jupyter_output><empty_output><jupyter_text>Creating a Data Frame from a dictionary<jupyter_code>score = {"Ai for EO":[76,78,89,90,83,82,87,89,90,99],
         "Python1"  :[67,78,76,75,89,90,54,78,87,45],
         "Python2"  :[78,76,79,56,34,89,45,78,91,89]
        }
df = pd.DataFrame(score,index=["Nasir", "Asad","Ahmed",
                               "Abdullah","Faraz", "Zaid",
                               "Ali", "Khan", 'Faisal', 
                               "Fahad"])
df
df.head(3)
df.tail(3)
df
df.info()
df.describe()
#data = pd.read_csv("banknotes.csv")
df['Python1']
df['Ai for EO']
df.loc[['Abdullah','Fahad']]['Python1']
df
df["extra"]=range(10)
df
del df["extra"]
df
df["Total"]=df["Ai for EO"] + df["Python1"] + df['Python2']
df
# df["Status"]=np.where(df['Total']>200,"Pass","Fail")
# df


df["Status"]=["Pass" if marks>200 else "Fails" for marks in df['Total']]
df
df['RevisedStatus']=np.where(((df['Ai for EO']>=50) & (df["Python1"]>=50) & (df["Python2"]>=50)),"Pass","Fail")
df
df['newStatus']=["Pass" if (ai>=50) & (p1>=50) & (p2>=50) else "Fail" for ai,p1,p2 in zip(df["Ai for EO"], df['Python1'],df["Python2"])]
df
x = [1,2,3,4]
y = ['a','b','c','d']
z = zip(x,y)

for x,y in z:
    print(x,y)<jupyter_output><empty_output><jupyter_text>13-10-2019<jupyter_code>pop = {'Nevada': {2001: 2.4, 2002: 2.9},
       'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}
frame3 = pd.DataFrame(pop)
frame3
pdata = {'Ohio': frame3['Ohio'][:-1],
         
         'Nevada': frame3['Nevada'][:2]}
pdata = pd.DataFrame(pdata)
pdata
frame3.index.name = 'year'; frame3.columns.name = 'state'
frame3
frame3.values<jupyter_output><empty_output><jupyter_text>Index Objects<jupyter_code>obj = pd.Series(range(3), index=['a', 'b', 'c'])
obj
index = obj.index
index
index[1:]<jupyter_output><empty_output><jupyter_text>5.2 Essential Functionality<jupyter_code>Reindexing Series<jupyter_output><empty_output><jupyter_text>obj = pd.Series([4.5, 7.2, -5.3, 3.6], index=['d', 'b', 'a', 'c']) obj obj2 = obj.reindex(['a', 'b', 'c', 'd', 'e']) obj2['e']=100obj2 bj3 = pd.Series(['blue', 'purple', 'yellow'], index=[0,  4,9]) obj3 obj3.reindex(range(6)) obj3.reindex(range(6), method='ffill') obj3.reindex(range(6), method='bfill')<jupyter_code>###########Reindexing Frame#############

            Row indexing<jupyter_output><empty_output><jupyter_text>frame = pd.DataFrame(np.arange(9).reshape((3, 3)),                    index=['a', 'c', 'd'],                    columns=['Ohio', 'Texas', 'California'])frame frame2 = frame.reindex(['a', 'b', 'c', 'd'])frame2<jupyter_code>Column Reindexing<jupyter_output><empty_output><jupyter_text>states = ['Texas', 'Utah', 'California'] frame.reindex(columns=states)<jupyter_code>Row and Coulumn Idexing<jupyter_output><empty_output><jupyter_text>newFrame = frame.reindex(index=['a', 'b', 'c', 'd'],                        columns=['Texas', 'Utah', 'California',                                 "NewYork"])newFrame newFrame['NewYork']['b']=800 newFrame frame.loc[['a', 'b', 'c', 'd'], states]<jupyter_code># Dropping Entries from an Axis<jupyter_output><empty_output><jupyter_text>obj = pd.Series(np.arange(5.), index=['a', 'b', 'c', 'd', 'e']) obj.drop('c',inplace=True) obj data = pd.DataFrame(np.arange(16).reshape((4, 4)),                    index=['Ohio', 'Colorado', 'Utah', 'New York'],                    columns=['one', 'two', 'three', 'four'])data data.drop(['Colorado', 'Ohio']) data data.drop('two', axis=1) df = pd.read_csv('data.csv') df df.shape df.columns df[df['Mid']>20].count() df[(df['Mid']>20) & (df['Best of Quizzes']>7) & (df['Best of Assignments']>7)] df.info() df df1=df.copy() df1['Quiz 1'].fillna(df1['Quiz 1'].mean(),inplace=True) df1 df.mean<jupyter_code># 19-10-2019<jupyter_output><empty_output><jupyter_text>Function Application and Mapping<jupyter_code>frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'),
                        index=['Utah', 'Ohio', 'Texas', 'Oregon'])
frame
frame = np.abs(frame)
frame
f = lambda x: x.max() - x.min()
frame.apply(f)
frame.apply(f, axis='columns')<jupyter_output><empty_output><jupyter_text>Sorting and Ranking<jupyter_code>obj = pd.Series(range(4), index=['d', 'a', 'b', 'c'])
obj
obj.sort_index()
frame = pd.DataFrame(np.arange(8).reshape((2, 4)),
                    index=['three', 'one'],
                    columns=['d', 'a', 'b', 'c'])
frame

frame.sort_index()
frame.sort_index(axis=1)
obj = pd.Series([4, 7, -3, 2])
obj
obj.sort_values()
frame = pd.DataFrame({'b': [4, 7, -3, 2], 'a': [1,2,3,4]})
frame
frame.sort_values(by='b')
frame
frame.sort_values(by=['a', 'b'])
obj = pd.Series([7, -5, 7, 4, 2, 4,0, 4,])
obj
obj.rank()
df = pd.DataFrame([[1.4, np.nan], [7.1, -4.5],
                    [np.nan, np.nan], [0.75, -1.3]],
                        index=['a', 'b', 'c', 'd'],
                            columns=['one', 'two'])
df
df.sum()
df.sum(axis='columns')
df.mean(axis='columns', skipna=True)
frame = pd.DataFrame({'b': [1,0], 'a': [0,1]})

frame
frame.corr()
frame.cov()
frame
import pandas_datareader.data as web
all_data = {ticker: web.get_data_yahoo(ticker)
                    for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']}<jupyter_output><empty_output><jupyter_text>CorrelationCorrelation is another way to determine how two variables are related. In addition to telling you whether variables are positively or inversely related, correlation also tells you the degree to which the variables tend to move together.As stated above,  covariance measures variables that have different units of measurement. Using covariance, you could determine whether units were increasing or decreasing, but it was impossible to measure the degree to which the variables moved together because covariance does not use one standard unit of measurement. To measure the degree to which variables move together, you must use correlation.Correlation standardizes the measure of interdependence between two variables and, consequently, tells you how closely the two variables move. The correlation measurement, called a correlation coefficient, will always take on a value between 1 and – 1:If the correlation coefficient is one, the variables have a perfect positive correlation. This means that if one variable moves a given amount, the second moves proportionally in the same direction. A positive correlation coefficient less than one indicates a less than perfect positive correlation, with the strength of the correlation growing as the number approaches one.If correlation coefficient is zero, no relationship exists between the variables. If one variable moves, you can make no predictions about the movement of the other variable; they are uncorrelated.If correlation coefficient is –1, the variables are perfectly negatively correlated (or inversely correlated) and move in opposition to each other. If one variable increases, the other variable decreases proportionally. A negative correlation coefficient greater than –1 indicates a less than perfect negative correlation, with the strength of the correlation growing as the number approaches –1.<jupyter_code>all_data
price = pd.DataFrame({ticker: data['Adj Close'] for ticker, data in all_data.items()})
price
volume = pd.DataFrame({ticker: data['Volume']
for ticker, data in all_data.items()})
volume<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18420182879806105, 'fraction_numerical': 0.05012669384157761, 'mean_word_length': 5.113131313131313, 'pattern_counts': {'":': 4, '<': 74, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '468762', 'license': 'MIT', 'n_tokens_mistral': 3588, 'n_tokens_neox': 3332, 'n_words': 798, 'path': 'Q2/NUMPY/chapter 4.2.ipynb', 'repo_name': 'qasim1020/PIAIC-Artificial-Intelligence', 'size': '9084'}