pythonlibraries[1]
pythonlibraries[1]
Installation of NumPy
If you have Python and PIP already installed on a system,
then installation of NumPy is very easy.
import numpy
print(arr)
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
*2-D Arrays
import numpy as np
print(arr)
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
print(arr.shape)
SciPy stands for Scientific Python.
SciPy is a scientific computation library that uses NumPy
underneath.
These constants can be helpful when you are working with Data
Science.
print(constants.kibi) #1024
print(constants.mebi) #1048576
print(constants.gibi) #1073741824
print(constants.tebi) #1099511627776
print(constants.pebi) #1125899906842624
print(constants.exbi) #1152921504606846976
print(constants.zebi) #1180591620717411303424
print(constants.yobi) #1208925819614629174706176
*Matplotlib is a low level graph plotting library in python that
serves as a visualization utility.
plt.plot(xpoints, ypoints)
plt.show()
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.grid()
plt.show()
*Creating Bars
With Pyplot, you can use the bar() function to draw bar graphs:
plt.bar(x,y)
plt.show()
plt.pie(y)
plt.show()
mydataset = {
'cars': ["BMW", "Volvo", "Ford"],
'passings': [3, 7, 2]
}
myvar = pandas.DataFrame(mydataset)
print(myvar)
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)
import pandas as pd
myvar = pd.Series(calories)
print(myvar)
DataFrames
Data sets in Pandas are usually multi-dimensional tables, called
DataFrames.
Example
Create a DataFrame from two Series:
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
myvar = pd.DataFrame(data)
print(myvar)
What is a DataFrame?
A Pandas DataFrame is a 2 dimensional data structure, like a 2
dimensional array, or a table with rows and columns.
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
print(df)
CSV files contains plain text and is a well know format that can
be read by everyone including Pandas.
In our examples we will be using a CSV file called 'data.csv'.
import pandas as pd
df = pd.read_csv('data.csv')
print(df.to_string())
Read JSON
Big data sets are often stored, or extracted as JSON.
JSON is plain text, but has the format of an object, and is well
known in the world of programming, including Pandas.
Load the JSON file into a DataFrame:
import pandas as pd
df = pd.read_json('data.json')
print(df.to_string())
Dictionary as JSON
JSON = Python Dictionary
data = {
"Duration":{
"0":60,
"1":60,
"2":60,
"3":45,
"4":45,
"5":60
},
"Pulse":{
"0":110,
"1":117,
"2":103,
"3":109,
"4":117,
"5":102
},
"Maxpulse":{
"0":130,
"1":145,
"2":135,
"3":175,
"4":148,
"5":127
},
"Calories":{
"0":409,
"1":479,
"2":340,
"3":282,
"4":406,
"5":300
}
}
df = pd.DataFrame(data)
print(df)