markdown stringlengths 0 1.02M | code stringlengths 0 832k | output stringlengths 0 1.02M | license stringlengths 3 36 | path stringlengths 6 265 | repo_name stringlengths 6 127 |
|---|---|---|---|---|---|
A simple check for the featurization would be to count the different atomic numbers present in the features. | atomic_numbers = features[:, 0]
from collections import Counter
unique_numbers = Counter(atomic_numbers)
print(unique_numbers) | Counter({0.0: 9, 1.0: 8, 6.0: 3})
| MIT | examples/tutorials/06_Going_Deeper_on_Molecular_Featurizations.ipynb | patrickphatnguyen/deepchem |
For propane, we have $3$ `C-atoms` and $8$ `H-atoms`, and these numbers are in agreement with the results shown above. There's also the additional padding of 9 atoms, to equalize with `max_atoms`. CoulombMatrix `CoulombMatrix`, featurizes a molecule by computing the coulomb matrices for different conformers of the mol... | example_smile = "CCC"
example_mol = Chem.MolFromSmiles(example_smile)
engine = conformers.ConformerGenerator(max_conformers=1)
example_mol = engine.generate_conformers(example_mol)
print("Number of available conformers for propane: ", len(example_mol.GetConformers()))
coulomb_mat = CoulombMatrix(max_atoms=20, randomi... | _____no_output_____ | MIT | examples/tutorials/06_Going_Deeper_on_Molecular_Featurizations.ipynb | patrickphatnguyen/deepchem |
A simple check for the featurization is to see if the feature list has the same length as the number of conformers | print(len(example_mol.GetConformers()) == len(features)) | True
| MIT | examples/tutorials/06_Going_Deeper_on_Molecular_Featurizations.ipynb | patrickphatnguyen/deepchem |
CoulombMatrixEig `CoulombMatrix` is invariant to molecular rotation and translation, since the interatomic distances or atomic numbers do not change. However the matrix is not invariant to random permutations of the atom's indices. To deal with this, the `CoulumbMatrixEig` featurizer was introduced, which uses the eig... | example_smile = "CCC"
example_mol = Chem.MolFromSmiles(example_smile)
engine = conformers.ConformerGenerator(max_conformers=1)
example_mol = engine.generate_conformers(example_mol)
print("Number of available conformers for propane: ", len(example_mol.GetConformers()))
coulomb_mat_eig = CoulombMatrixEig(max_atoms=20, ... | True
| MIT | examples/tutorials/06_Going_Deeper_on_Molecular_Featurizations.ipynb | patrickphatnguyen/deepchem |
Neural NetworksIn the previous part of this exercise, you implemented multi-class logistic re gression to recognize handwritten digits. However, logistic regression cannot form more complex hypotheses as it is only a linear classifier.In this part of the exercise, you will implement a neural network to recognize handw... | # import libraries
import scipy.io
import numpy as np
data = scipy.io.loadmat("ex3data1")
weights = scipy.io.loadmat('ex3weights') | _____no_output_____ | MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
Now we extract X and y variables from the .mat file and save them into .csv file for further usage. After running the below code you should see X.csv and y.csv files in your directory. | for i in data:
if '__' not in i and 'readme' not in i:
np.savetxt((i+".csv"),data[i],delimiter=',')
for i in weights:
if '__' not in i and 'readme' not in i:
np.savetxt((i+".csv"),weights[i],delimiter=',') | _____no_output_____ | MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
1.B Loading Dataset and Trained Neural Network WeightsFirst we import .csv files into pandas dataframes then save them into numpy arrays.There are 5000 training examples in ex3data1.mat, where each training example is a 20 pixel by 20 pixel grayscale image of the digit. Each pixel is represented by a floating point nu... | # import library
import pandas as pd
# saving .csv files to pandas dataframes
x_df = pd.read_csv('X.csv',names= np.arange(0,400))
y_df = pd.read_csv('y.csv',names=['label'])
# saving .csv files to pandas dataframes
Theta1_df = pd.read_csv('Theta1.csv',names = np.arange(0,401))
Theta2_df = pd.read_csv('Theta2.csv',name... | _____no_output_____ | MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
1.C Plotting DataYou will begin by visualizing a subset of the training set. In first part, the code randomly selects selects 100 rows from X and passes those rows to the display_data function. This function maps each row to a 20 pixel by 20 pixel grayscale image and displays the images together.After plotting, you sh... | import numpy as np
import matplotlib.pyplot as plt
import random
amount = 100
lines = 10
columns = 10
image = np.zeros((amount, 20, 20))
number = np.zeros(amount)
for i in range(amount):
rnd = random.randint(0,4999)
image[i] = x[rnd].reshape(20, 20)
y_temp = y.reshape(m,)
number[i] = y_temp[rnd]
fig =... | _____no_output_____ | MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
2. Model RepresentationOur neural network is shown in below figure. It has 3 layers an input layer, a hidden layer and an output layer. Recall that our inputs are pixel values of digit images. Since the images are of size 20×20, this gives us 400 input layer units (excluding the extra bias unit which always outputs +1... | print('theta1 shape = {}, theta2 shape = {}'.format(theta1.shape,theta2.shape)) | theta1 shape = (25, 401), theta2 shape = (10, 26)
| MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
It seems our weights are transposed, so we transpose them to have them in a way our neural network is. | theta1 = theta1.transpose()
theta2 = theta2.transpose()
print('theta1 shape = {}, theta2 shape = {}'.format(theta1.shape,theta2.shape)) | theta1 shape = (401, 25), theta2 shape = (26, 10)
| MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
3. Feedforward Propagation and PredictionNow you will implement feedforward propagation for the neural network.You should implement the feedforward computation that computes hθ(x(i)) for every example i and returns the associated predictions. Similar to the one-vs-all classification strategy, the prediction from the n... | # adding column of 1's to x
x = np.append(np.ones(shape=(m,1)),x,axis = 1) | _____no_output_____ | MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
h = hypothesis(x,theta) will compute sigmoid function on θTX and return a number which 0.You can use this library for calculating sigmoid. | def sigmoid(z):
return 1/(1+np.exp(-z))
def lr_hypothesis(x,theta):
return np.dot(x,theta) | _____no_output_____ | MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
predict(theta1, theta2, x): outputs the predicted label of x given the trained weights of a neural network (theta1, theta2). | layers = 3
num_labels = 10 | _____no_output_____ | MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
Becuase the initial dataset has changed and mapped 0 to "10", so the weights also are changed. So we just rotate columns one step to right, to predict correct values.Recall we have changed mapping 0 to "10" to 0 to "0" but we cannot detect this mapping in weights of neural netwrok. So we have to this rotation on final ... | def rotate_column(array):
array_ = np.zeros(shape=(m,num_labels))
temp = np.zeros(num_labels,)
temp= array[:,9]
array_[:,1:10] = array[:,0:9]
array_[:,0] = temp
return array_
def predict(theta1,theta2,x):
z2 = np.dot(x,theta1) # hidden layer
a2 = sigmoid(z2) # hidden layer
# adding ... | _____no_output_____ | MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
Now we will compare our predicted result to the true one with confusion_matrix of numpy library. | from sklearn.metrics import confusion_matrix
# Function for accuracy
def acc(confusion_matrix):
t = 0
for i in range(num_labels):
t += confusion_matrix[i][i]
f = m-t
ac = t/(m)
return (t,f,ac)
#import library
from sklearn.metrics import confusion_matrix
cm_train = confusion_matrix(y.reshape... | _____no_output_____ | MIT | Week 4 - Multi-Class Classification and Neural Networks/Neural Networks.ipynb | Nikronic/Coursera-Machine-Learning |
[View in Colaboratory](https://colab.research.google.com/github/neoaksa/IMDB_Spider/blob/master/Movie_Analysis.ipynb) | # I've already uploaded three files onto googledrive, you can use uploaded function blew to upload the files.
# # upload
# uploaded = files.upload()
# for fn in uploaded.keys():
# print('User uploaded file "{name}" with length {length} bytes'.format(
# name=fn, length=len(uploaded[fn])))
import pandas as pd
... | stars_id stars avg_rating num_movie
330 nm0000134 Robert De Niro 8.375 8
352 nm0000148 Harrison Ford 8.34286 7
172 nm0000138 Leonardo DiCaprio 8.3 6
250 nm0000158 Tom Hanks 8.38333 6
588 nm0000142 Clint Eastwood 8.28 ... | Apache-2.0 | Movie_Analysis.ipynb | neoaksa/IMDB_Spider |
Accordig this breif table, we can find **Robert De Niro** took the most movies in top 250 list. Followed by **Harrison**,**Tom** and **Leonardo** . | # visual stars
import matplotlib.pyplot as plt
# figure = plt.figure()
ax1 = plt.subplot()
df_aggbyMovie = df_star[df_star['num_movie']>0].groupby(['num_movie']).agg({'stars_id':np.size})
df_aggbyMovie.columns.values[0] ='freq'
df_aggbyMovie = df_aggbyMovie.sort_values(['freq'])
acc_numMovie = np.cumsum(df_aggbyMovie['... | _____no_output_____ | Apache-2.0 | Movie_Analysis.ipynb | neoaksa/IMDB_Spider |
**165** movies in top 250 movies are performed by the **100** best stars who is defined that took more than one movies in the list. We picked up these 100 movie stars for future star research | # movie star relationship analysis
df_movie_star_plus = df_star[df_star['num_movie']>2][['stars_id','stars']]
# transfer star list to relationship list
def starlist2network(list):
bi_list = []
i = 0
while i<len(list):
j = 1
while j<len(list)-i:
bi_list.append((list[i],list[i+j])... | Requirement already satisfied: networkx in /usr/local/lib/python3.6/dist-packages (2.1)
Requirement already satisfied: decorator>=4.1.0 in /usr/local/lib/python3.6/dist-packages (from networkx) (4.3.0)
| Apache-2.0 | Movie_Analysis.ipynb | neoaksa/IMDB_Spider |
I picked up a few stars who took more than 2 movies in the top 250 list, and create a relationship netwrok for them.We can find the major 5 blocks, if we loose the filter, maybe we can find more. | # pick 100 stars for age analysis
# rebin the year by 10 years
df_movieStar_bin = df_movieStar.copy()
df_movieStar_bin['name'] = df_movieStar_bin['name'].str[2:-2]
df_movieStar_bin['born_year'] = df_movieStar_bin['born_year'].str[2:-2]
df_movieStar_bin['born_area'] = df_movieStar_bin['born_area'].str[2:-2]
df_movieSta... | _____no_output_____ | Apache-2.0 | Movie_Analysis.ipynb | neoaksa/IMDB_Spider |
From picked 100 movie stars, most of them are born between **1930s to 1970s**. **California, Illinois, New Jersey ** are the states with most movie stars. Even so, none of state or regions is predominant. | # review analysis
!pip install wordcloud
!pip install multidict
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import string
import multidict as multidict
nltk.download('stopwords')
nltk.download('punkt')
nltk.downl... | _____no_output_____ | Apache-2.0 | Movie_Analysis.ipynb | neoaksa/IMDB_Spider |
²⁹Si 1D MAS spinning sideband (CSA) After acquiring an NMR spectrum, we often require a least-squares analysis todetermine site populations and nuclear spin interaction parameters. Generally, thiscomprises of two steps:- create a fitting model, and- determine the model parameters that give the best fit to the spectrum... | import csdmpy as cp
import matplotlib.pyplot as plt
from lmfit import Minimizer, Parameters
from mrsimulator import Simulator, SpinSystem, Site
from mrsimulator.methods import BlochDecaySpectrum
from mrsimulator import signal_processing as sp
from mrsimulator.utils import spectral_fitting as sf | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
Import the datasetUse the `csdmpy `_module to load the synthetic dataset as a CSDM object. | file_ = "https://sandbox.zenodo.org/record/835664/files/synthetic_cuspidine_test.csdf?"
synthetic_experiment = cp.load(file_).real
# standard deviation of noise from the dataset
sigma = 0.03383338
# convert the dimension coordinates from Hz to ppm
synthetic_experiment.x[0].to("ppm", "nmr_frequency_ratio")
# Plot of ... | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
Create a fitting modelBefore you can fit a simulation to an experiment, in this case, the synthetic dataset,you will first need to create a fitting model. We will use the ``mrsimulator`` objectsas tools in creating a model for the least-squares fitting.**Step 1:** Create initial guess sites and spin systems.The initia... | # the guess model comprising of a single site spin system
site = Site(
isotope="29Si",
isotropic_chemical_shift=-82.0, # in ppm,
shielding_symmetric={"zeta": -63, "eta": 0.4}, # zeta in ppm
)
spin_system = SpinSystem(
name="Si Site",
description="A 29Si site in cuspidine",
sites=[site], # fr... | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
**Step 2:** Create the method object.The method should be the same as the one usedin the measurement. In this example, we use the `BlochDecaySpectrum` method. Note,when creating the method object, the value of the method parameters must match therespective values used in the experiment. | MAS = BlochDecaySpectrum(
channels=["29Si"],
magnetic_flux_density=7.1, # in T
rotor_frequency=780, # in Hz
spectral_dimensions=[
{
"count": 2048,
"spectral_width": 25000, # in Hz
"reference_offset": -5000, # in Hz
}
],
experiment=synthetic... | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
**Step 3:** Create the Simulator object, add the method and spin system objects, andrun the simulation. | sim = Simulator(spin_systems=[spin_system], methods=[MAS])
sim.run() | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
**Step 4:** Create a SignalProcessor class and apply post simulation processing. | processor = sp.SignalProcessor(
operations=[
sp.IFFT(), # inverse FFT to convert frequency based spectrum to time domain.
sp.apodization.Exponential(FWHM="200 Hz"), # apodization of time domain signal.
sp.FFT(), # forward FFT to convert time domain signal to frequency spectrum.
sp... | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
**Step 5:** The plot the spectrum. We also plot the synthetic dataset for comparison. | plt.figure(figsize=(4.25, 3.0))
ax = plt.subplot(projection="csdm")
ax.plot(synthetic_experiment, "k", linewidth=1, label="Experiment")
ax.plot(processed_data, "r", alpha=0.75, linewidth=1, label="guess spectrum")
ax.set_xlim(50, -200)
plt.legend()
plt.grid()
plt.tight_layout()
plt.show() | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
Setup a Least-squares minimizationNow that our model is ready, the next step is to set up a least-squares minimization.You may use any optimization package of choice, here we show an application usingLMFIT. You may read more on the LMFIT`documentation page `_. Create fitting parametersNext, you will need a list of par... | site1 = spin_system.sites[0]
params = Parameters()
params.add(name="iso", value=site1.isotropic_chemical_shift)
params.add(name="eta", value=site1.shielding_symmetric.eta, min=0, max=1)
params.add(name="zeta", value=site1.shielding_symmetric.zeta)
params.add(name="FWHM", value=processor.operations[1].FWHM)
params.add(... | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
Create a minimization functionNote, the above set of parameters does not know about the model. You will need toset up a function that will- update the parameters of the `Simulator` and `SignalProcessor` object based on the LMFIT parameter updates,- re-simulate the spectrum based on the updated values, and- return the... | def minimization_function(params, sim, processor, sigma=1):
values = params.valuesdict()
# the experiment data as a Numpy array
intensity = sim.methods[0].experiment.y[0].components[0].real
# Here, we update simulation parameters iso, eta, and zeta for the site object
site = sim.spin_systems[0].si... | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
NoteTo automate the fitting process, we provide a function to parse the ``Simulator`` and ``SignalProcessor`` objects for parameters and construct an *LMFIT* ``Parameters`` object. Similarly, a minimization function, analogous to the above `minimization_function`, is also included in the *mrsimulator* ... | minner = Minimizer(minimization_function, params, fcn_args=(sim, processor, sigma))
result = minner.minimize()
result | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
The plot of the fit, measurement and the residuals is shown below. | best_fit = sf.bestfit(sim, processor)[0]
residuals = sf.residuals(sim, processor)[0]
plt.figure(figsize=(4.25, 3.0))
ax = plt.subplot(projection="csdm")
ax.plot(synthetic_experiment, "k", linewidth=1, label="Experiment")
ax.plot(best_fit, "r", alpha=0.75, linewidth=1, label="Best Fit")
ax.plot(residuals, alpha=0.75, l... | _____no_output_____ | BSD-3-Clause | docs/notebooks/fitting/1D_fitting/plot_1_29Si_cuspidine.ipynb | pjgrandinetti/mrsimulator |
PytorchPytorch is a framework that challenge you to build a ANN almost from scratch.This tutorial aims to explain how load non-iamges data in Pytorch, and create classification models.1. Learn how to generate synthetic data for classification. The more complex the bidimentional patern, the larger the high dimentional... | import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
# plotlib and sklearn modules
import numpy as np
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.metrics import accuracy_score, f1_score
from sklearn.model_selection im... | _____no_output_____ | MIT | pytorch.ipynb | lamahechag/pytorch_tensorflow |
Data loaderWe create a custom dataset class to iterate our data in the dataloader from Pytorch.`trainData(torch.FloatTensor(X_train), torch.FloatTensor(y_train))`Then we use `DataLoader` to allow auto batching. The function `loader_data()` gather all the pipeline to load tha data in a Pytorch tensor. | class trainData(Dataset):
def __init__(self, X_data, y_data):
self.X_data = X_data
self.y_data = y_data
def __getitem__(self, index):
return self.X_data[index], self.y_data[index]
def __len__ (self):
return len(self.X_data)
class testData(Dataset):
def __init__(s... | _____no_output_____ | MIT | pytorch.ipynb | lamahechag/pytorch_tensorflow |
Pytorch ModelTo build a model in Pytorch, you should define a `Class`. The class has two parts:1. `__init__` defines the different elements of calculation, like: hidden layers, activation functions, dropouts, etc.1. `foward` method where you define how the input going through each calculation element.You will see tha... | class LogisClassifier(nn.Module):
def __init__(self, num_input=2):
super(LogisClassifier, self).__init__()
self.num_input = num_input
# Number of input features
self.layer_1 = nn.Linear(self.num_input, 1)
def forward(self, inputs):
x = self.layer_1(inputs)
... | _____no_output_____ | MIT | pytorch.ipynb | lamahechag/pytorch_tensorflow |
Training loopIn a neural network the process of learning is as follow: calculate the output, calculate the gradient, do the backward pass and update the weights.Within the training loop, you should do this in each iteration.1. reset gradient to zero.1. perform backward step.1. update parameters.Also before to measure ... | def binary_acc(y_pred, y_test):
y_pred_tag = torch.round(torch.sigmoid(y_pred))
correct_results_sum = (y_pred_tag == y_test).sum().float()
acc = correct_results_sum/y_test.shape[0]
acc = torch.round(acc * 100)
return acc
def eval_testdata(model, test_loader):
y_pred_list = []
model.eval()
... | _____no_output_____ | MIT | pytorch.ipynb | lamahechag/pytorch_tensorflow |
Declare model and trainWe have defined a training loop, but we need a loss function and an optimizer to perform gradient desent step.In the first line the data are loaded, followed by the model declaration and send to the `GPU` device in this case. First experiment: Logistic classifier. | train_loader, test_loader, y_test = loader_data(X_moon, y_moon, BATCH_SIZE=10)
model = LogisClassifier()
model.to(device)
# define loss function
criterion = nn.BCEWithLogitsLoss()
LEARNING_RATE = 0.001
# define gradient decent optimizer
optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)
print(model)
# now tr... | _____no_output_____ | MIT | pytorch.ipynb | lamahechag/pytorch_tensorflow |
Drawing Conclusions Using Groupby | # Load `winequality_edited.csv`
import pandas as pd
df = pd.read_csv('winequality_edited.csv') | _____no_output_____ | MIT | .ipynb_checkpoints/conclusions_groupby-checkpoint.ipynb | Siddharth1698/Data-Analyst-Nanodegree |
Is a certain type of wine associated with higher quality? | # Find the mean quality of each wine type (red and white) with groupby
df.groupby('color').mean().quality | _____no_output_____ | MIT | .ipynb_checkpoints/conclusions_groupby-checkpoint.ipynb | Siddharth1698/Data-Analyst-Nanodegree |
What level of acidity receives the highest average rating? | # View the min, 25%, 50%, 75%, max pH values with Pandas describe
df.describe().pH
# Bin edges that will be used to "cut" the data into groups
bin_edges = [2.72, 3.11, 3.21, 3.32, 4.01] # Fill in this list with five values you just found
# Labels for the four acidity level groups
bin_names = ['high', 'mod_high', 'mediu... | _____no_output_____ | MIT | .ipynb_checkpoints/conclusions_groupby-checkpoint.ipynb | Siddharth1698/Data-Analyst-Nanodegree |
Write a function that takes in a string and returns its longest substring without duplicate characters. Assume that there will only be one longest substring without duplication.For example, longest_substring("zaabcde") == 'abcde' | # longest substring function.
def longest_substring(s):
lastSeen = {} # dictionary of where the last of a character is.
longest = [0, 1] # index of beginning and end of longest substring.
startIdx = 0 # index of beginning of current substring
for i, char in enumerate(s):
if char in lastSeen:
# start o... | abcde
| MIT | Challenges/LongestSubstringChallenge.ipynb | CVanchieri/LambdaSchool-DS-Challenges |
Crimes Svetozar Mateev Putting Crime in the US in Context First I am going to calculate the total crimes by dividing the population by 100 000 and then multiplying it by the crimes percapita.Then I am going to remove the NaN values. | crime_reports=pd.read_csv("report.csv")
crime_reports=crime_reports.dropna()
crime_reports=crime_reports.reset_index()
crime_reports["total_crimes"]=(crime_reports.population/100000*crime_reports.crimes_percapita)
#crime_reports[["population",'crimes_percapita','total_crimes']]
| _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
• Have a look at the “months_reported” column values. What do they mean? What percent of the rows have less than 12 months? How significant is that? | crime_reports["months_reported"].unique()
less_than_twelve=crime_reports[crime_reports.months_reported<12]
print(str(len(less_than_twelve)/len(crime_reports.months_reported)*100)+'%') | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
The months reported column indicates how much months from the year have been reported and only 1.9% of the rows have less than 12 months reported per year whichn on a 5% level isn't significant. • Overall crime popularity: Create a bar chart of crime frequencies (total, not per capita). Display the type of crime and to... | homicides_total_sum=crime_reports.homicides.sum()
rapes_total_sum=crime_reports.rapes.sum()
assaults_total_sum=crime_reports.assaults.sum()
robberies_total_sum=crime_reports.robberies.sum()
total_crimes_total_sum= crime_reports.total_crimes.sum()
homicides_frequency=homicides_total_sum/total_crimes_total_sum
rapes_freq... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
The most frequent crimes are the assaults and i can see from the diagram that crimes which are less serious are committed more often. • Crime popularity by year: Break down the analysis of the previous graph by year. What is the most common crime (total, not per capita) for each year? What is the least common one? | homicides_sum=0
rapes_sum=0
assaults_sum=0
robberies_sum=0
for year in crime_reports.report_year.unique():
year_df=crime_reports[crime_reports.report_year==year]
homicides_sum_year=year_df.homicides.sum()
rapes_sum_year=year_df.rapes.sum()
assaults_sum_year=year_df.assaults.sum()
robberies_sum_year=... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
I can see from the bar chart that assault were the most popular crime for a year almost thirty time and that the homicides and rapes were never the most popular crime for a year. • Crime evolution (e. g. crime rates as a function of time): How do crime rates per capita evolve over the years? Create a plot (or a series)... | rapes_per_capita=[]
homicides_per_capita=[]
assaults_per_capita=[]
robberies_per_capita=[]
for year in crime_reports.report_year.unique():
year_df=crime_reports[crime_reports.report_year==year]
homicides_mean_year=year_df.homicides_percapita.mean()
rapes_mean_year=year_df.rapes_percapita.mean()
assaults... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
From the plots we can see that each crime has significanttly lower rate per capita and that for all of them the peak was between 1990 and 1995. | rapes_per_year=[]
homicides_per_year=[]
assaults_per_year=[]
robberies_per_year=[]
for year in crime_reports.report_year.unique():
year_df=crime_reports[crime_reports.report_year==year]
homicides_mean_year=year_df.homicides.sum()
rapes_mean_year=year_df.rapes.sum()
assaults_mean_year=year_df.assaults.su... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
Again our observations are confirmed that the peak of the crimes is around 1990 and that in present there are a lot less crimes except the rapes which between 2010 and 2015 have begun raise slightly. Crimes by States • “Criminal” jurisdictions: Plot the sum of all crimes (total, not per capita) for each jurisdiction. ... | #agency_jurisdiction
jurisdicitons=[]
counter=0
crimes_per_jurisdiction=[]
agencies_df=crime_reports.sort_values('violent_crimes',ascending=False)
for jurisdiciton in agencies_df.agency_jurisdiction.unique():
jurisdicition_df=agencies_df[agencies_df.agency_jurisdiction==jurisdiciton]
all_crimes=jurisdicition_d... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
From the bar chart we can see that the New York City,Ny jurisdiction has the most crimes. • “Criminal” jurisdictions, part 2: Create the same type of chart as above, but use the crime rates per capita this time. Are you getting the same distribution? Why? You may need data from the “population” column to answer this. D... | jurisdicitons=[]
counter=0
crimes_per_jurisdiction=[]
population=[]
agencies_df=crime_reports
agencies_df=crime_reports.sort_values('crimes_percapita',ascending=False)
for a in agencies_df.agency_jurisdiction.unique():
agencies_df["crimes_percapita_per_agency"]=agencies_df[agencies_df.agency_jurisdiction==jurisdic... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
We can see that the crime per capita in Miami is the biggest contary to the previous plot. However it appears to have little correlation between the number of crimes per capita and the population. • “Criminal states”: Create the same type of chart as in the first subproblem, but use the states instead. You can get the ... | parts=crime_reports['agency_jurisdiction'].str.extract("(\w+), (\w+)", expand = True)
parts.columns=['something_else','state']
parts['state']
crime_reports['state']=parts['state']
crime_states=[]
total_crimes=[]
counter=0
gencies_df=crime_reports.sort_values('violent_crimes',ascending=False)
for state in crime_reports... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
From the chart we can see that New York has the biggest number of crimes. • Hypothesis testing: Are crime rates per capita related to population, e. g. does a more densely populated community produce more crime (because there are more people), or less crime (because there is a better police force)? Plot the total numbe... | total_crimes=[]
agency_jurisdiction=[]
population=[]
counter=0
for jurisdiction in crime_reports.agency_jurisdiction.unique():
jurisdicition_df=crime_reports[crime_reports.agency_jurisdiction==jurisdiction]
all_crimes=jurisdicition_df.violent_crimes.sum()
total_crimes.append(all_crimes)
counter+=1
... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
We can see that there isn't a corelation between the population and the crimes because some places like Atlanta,GA shows that there might be but others like Baltimore Country,MD show us totaly different results Additional data First I am droping some of the unnecessary columns and then I am tranforming the dates to d... | crimes=pd.read_csv("crimes.csv")
crimes=crimes.drop(['x','y','OBJECTID','ESRI_OID','Time'],axis=1)
crimes.columns=['publicaddress', 'controlnbr', 'CCN', 'precinct', 'reported_date',
'begin_date', 'offense', 'description', 'UCRCode', 'entered_date',
'long', 'lat', 'neighborhood', 'lastchanged', 'last_updat... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
• Total number of crimes per year: Count all crimes for years in the dataset (2010-2016). Print the total number. | print(str(len(crimes))+" "+"crimes between 2010 and 2016") | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
• Plot how crimes evolve each year | year_10=0
year_11=0
year_12=0
year_13=0
year_14=0
year_15=0
year_16=0
for date in crimes.begin_date:
if date.year==2010:
year_10+=1
elif date.year==2011:
year_11+=1
elif date.year==2012:
year_12+=1
elif date.year==2013:
year_13+=1
elif date.year==2014:
year_1... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
From 2010 to 2012 ther is a sligth raise in the number of crimes.However from 2012 to 2016 there is a drop in the number of crimes committed. • Compare the previous plot to the plots in the previous exercise.Note: In order to make comparison better, plot the data for all states again, but this time filter only years 20... | crime_states=[]
total_crimes=[]
counter=0
gencies_df=crime_reports.sort_values('violent_crimes',ascending=False)
for state in crime_reports.state.unique():
jurisdicition_df=crime_reports[crime_reports.state==state]
right_year=jurisdicition_df[jurisdicition_df.report_year>2009]
all_crimes=right_year.viol... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
• Cross-dataset matching: Get data from the previous dataset (crime rates in the US) again. This time, search only for MN and only for years 2010-2016. Do you have any results? If so, the results for total crime in MN should match in both datasets. Do they match? | year_10n=4064.0
year_11n=3722.0
year_12n=3872.0
year_13n=4038.0
year_14n=4093.0
year_15n=0
year_16n=0
MN=crime_reports[crime_reports.state=="MN"]
MN=MN[MN.report_year>2009]
number_crimes=sum(MN.violent_crimes)
print(str(int(number_crimes))+" from the first data set")
print(str(len(crimes))+" "+"from the second data set... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
The values in the first data set are until 2014 and they are much smaller than those in the second.There is a big difference between the two. Temporal Analysis • Look at the crime categories. Which is the most popular crime category in MN overall? | crimes.description.unique()
d={'Shoplifting':1, 'Theft From Motr Vehc':1, 'Other Theft':1,
'Theft From Building':1, 'Crim Sex Cond-rape':1, 'Burglary Of Dwelling':1,
'Theft From Person':1, 'Motor Vehicle Theft':1, 'Robbery Of Business':1,
'Aslt-police/emerg P':1, 'Domestic Assault/Strangulation':1,... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
The most common type is Other theft but since it si do unclear we can say that Burglary of Dwelling is the most commnon type of theft. • Break down the data by months. Plot the total number of crimes for each month, summed over the years. Is there a seasonal component? Which month has the highest crime rate? Which has ... | january=0
february=0
march=0
april=0
may=0
june=0
july=0
august=0
september=0
october=0
november=0
december=0
for date in crimes.begin_date:
if(date.month==1):
january+=1
elif(date.month==2):
february+=1
elif(date.month==3):
march+=1
elif(date.month==4):
april+=1
elif... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
We can see that most of the crimes are in june and that there is seasonal tendency that most of the crimes are committer in the summer. • Break the results by weekday. You can get the weekday from the date (there are functions for this). Do more crimes happen on the weekends? | Monday=0
Tuesday=0
Wednesday=0
Thursday=0
Friday=0
Saturday=0
Sunday=0
for date in crimes.begin_date:
if(date.weekday()==0):
Monday+=1
elif(date.weekday()==1):
Tuesday+=1
elif(date.weekday()==2):
Wednesday+=1
elif(date.weekday()==3):
Thursday+=1
elif(date.weekday()==4... | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
Most crimes are committed on Fridays.On the second place are Thursdays. • Break the weekday data by crime type. Are certain types of crime more likely to happen on a given day? Comment your findings.I have no time to complete this because I have a Programming Fundamentals Exam to take but I would make 7 plots one for e... | communities= pd.read_table("communities.data",sep=',',header=None)
communities.columns
communities_names= pd.read_table('communities.names',header=None)
communities_names | _____no_output_____ | MIT | DataScienceExam/Exam.ipynb | SvetozarMateev/Data-Science |
fastdot.core> Drawing graphs with graphviz. | #export
from fastcore.all import *
import pydot
from matplotlib.colors import rgb2hex, hex2color
#export
_all_ = ['pydot']
#hide
from nbdev.showdoc import * | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
Nodes | #export
def Dot(defaults=None, rankdir='LR', directed=True, compound=True, **kwargs):
"Create a `pydot.Dot` graph with fastai/fastdot style defaults"
return pydot.Dot(rankdir=rankdir, directed=directed, compound=compound, **kwargs)
#export
def uniq_name(o): return 'n'+(uuid4().hex)
def quote(x, q='"'):
'Su... | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
`pydot` uses the same name-based approach to identifying graph items as `graphviz`. However we would rather use python objects. Therefore, we patch `pydot` to use unique names. | g = Dot()
a = Node('a')
g.add_node(a)
g | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
If a 2-tuple is passed to `add_node`, then the 2nd element becomes the tooltip. You can also pass any `kwargs` that are accepted by `graphviz`. | g = Dot()
g.add_node(Node(['a', "My tooltip"], fillcolor='pink'))
g | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
Keyword args can also be arbitrary functions, which will called with the node's label. | g = Dot()
o = 'a'
g.add_node(Node(o, fillcolor=lambda o:'pink'))
g
#export
def object2graph(o):
"Get graph item representing `o`"
return graph_objects[object_names[id(o)]]
object2graph(o).get_fillcolor() | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
Colors The callable kwargs functionality can be used to map labels to colors in a consistent way.. | #export
def obj2node_color(cm, minalpha, rangealpha, o):
"Create a consistent mapping from objects to colors, using colormap `cm`"
h = hash(o)
i = float(h % 256) / 256
alpha = (h^hash('something')) % rangealpha + minalpha
return rgb2hex(cm(i)) + f'{alpha:02X}'
#exports
graph_colors1 = partial(obj2no... | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
These predefined color mapping functions provide a good range of colors and readable text. | g = Dot()
g.add_node(Node('a', fillcolor=graph_colors1))
g.add_node(Node('b', fillcolor=graph_colors1))
g
g = Dot()
g.add_node(Node('a', fillcolor=graph_colors2))
g.add_node(Node('b', fillcolor=graph_colors2))
g | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
We'll use the former color function as our default. You can change it by simply modifying `node_defaults`. | #export
node_defaults['fillcolor'] = graph_colors1 | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
Clusters and Items | #export
cluster_defaults = dict(label=str, tooltip=str, graph_name=uniq_name, style='rounded, filled', fillcolor='#55555522')
#export
def Cluster(obj='', **kwargs):
"Create a `pydot.Cluster` with a unique name"
kwargs = merge(cluster_defaults, kwargs)
return _pydot_create(pydot.Cluster, obj, **kwargs)
g = D... | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
You can subscript into a `Graph`'s `Node`s by index: | print(sg[0].get_label())
#export
@patch
def add_item(self:pydot.Graph, item, **kwargs):
"Add a `Cluster`, `Node`, or `Edge` to the `Graph`"
if not isinstance(item, (pydot.Edge,pydot.Node,pydot.Graph)): item = Node(item, **kwargs)
f = self.add_node if isinstance(item, pydot.Node ) else \
self.add... | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
There's no good reason to have different methods for adding clusters vs nodes (as `pydot` requires), so we provide a single method. | g = Dot()
sg = Cluster('clus')
g.add_item(sg)
sg.add_item('a')
g
#export
@patch
def add_items(self:pydot.Graph, *items, **kwargs):
"Add `items` the `Graph`"
return L(self.add_item(it, **kwargs) for it in items)
#export
def graph_items(*items, **kwargs):
"Add `items` to a new `pydot.Dot`"
g = Dot()
g... | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
Edges | #export
@patch
def first(self:pydot.Graph):
"First node in `Graph`, searching subgraphs recursively as needed"
nodes = self.nodes()
if nodes: return nodes[0]
for subg in self.get_subgraphs():
res = subg.first()
if res: return res
#export
@patch
def last(self:pydot.Graph):
"Lastt node... | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
This is a shortcut for creating connections between objects that are already in a graph. | a,b = 'a','b'
g = graph_items(a, b)
g.add_items(*object_connections([(a,b)]))
g | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
Sequential Since it's common to want to connect a series sequentially, we provide some simple shortcuts for this functionality. | #export
def graph_edges_seq(items):
"Add edges between each pair of nodes in `items`"
return L(items[i].connect(items[i+1]) for i in range(len(items)-1))
#export
@patch
def add_edges_seq(self:pydot.Graph, items):
"Add edges between each pair of nodes in `items`"
return self.add_items(*graph_edges_seq(it... | _____no_output_____ | Apache-2.0 | 00_core.ipynb | noklam/fastdot |
Export - | #hide
from nbdev.export import notebook2script
notebook2script() | Converted 00_core.ipynb.
Converted index.ipynb.
| Apache-2.0 | 00_core.ipynb | noklam/fastdot |
Data Science Boot Camp Introduction to Pandas Part 1 * __Pandas__ is a Python package providing fast, flexible, and expressive data structures designed to work with *relational* or *labeled* data both.* It is a fundamental high-level building block for doing practical, real world data analysis in Python.* Python has ... | %matplotlib inline
import pandas as pd
import numpy as np
print(pd.__version__) | 0.22.0
| Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* Let's set some options for `Pandas` | pd.set_option('display.notebook_repr_html', False)
pd.set_option('max_columns', 10)
pd.set_option('max_rows', 10) | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
Pandas Objects * At the very basic level, Pandas objects can be thought of as enhanced versions of NumPy structured arrays in which the rows and columns are identified with labels rather than simple integer indices.* There are three fundamental Pandas data structures: the Series, DataFrame, and Index. Series * A __Se... | counts = pd.Series([15029231, 7529491, 7499740, 5445026, 2702492, 2742534, 4279677, 2133548, 2146129])
counts | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* If an index is not specified, a default sequence of integers is assigned as the index. A NumPy array comprises the values of the `Series`, while the index is a pandas `Index` object. | counts.values
counts.index | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* We can assign meaningful labels to the index, if they are available: | population = pd.Series([15029231, 7529491, 7499740, 5445026, 2702492, 2742534, 4279677, 2133548, 2146129],
index=['Istanbul Total', 'Istanbul Males', 'Istanbul Females', 'Ankara Total', 'Ankara Males', 'Ankara Females', 'Izmir Total', 'Izmir Males', 'Izmir Females'])
population | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* These labels can be used to refer to the values in the `Series`. | population['Istanbul Total']
mask = [city.endswith('Females') for city in population.index]
mask
population[mask] | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* As you noticed that we can masking in series.* Also we can still use positional indexing even we assign meaningful labels to the index, if we wish. | population[0] | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* We can give both the array of values and the index meaningful labels themselves: | population.name = 'population'
population.index.name = 'city'
population | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* Also, NumPy's math functions and other operations can be applied to Series without losing the data structure. | np.ceil(population / 1000000) * 1000000 | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* We can also filter according to the values in the `Series` like in the Numpy's: | population[population>3000000] | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* A `Series` can be thought of as an ordered key-value store. In fact, we can create one from a `dict`: | populationDict = {'Istanbul Total': 15029231, 'Ankara Total': 5445026, 'Izmir Total': 4279677}
pd.Series(populationDict) | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* Notice that the `Series` is created in key-sorted order.* If we pass a custom index to `Series`, it will select the corresponding values from the dict, and treat indices without corrsponding values as missing. Pandas uses the `NaN` (not a number) type for missing values. | population2 = pd.Series(populationDict, index=['Istanbul Total','Ankara Total','Izmir Total','Bursa Total', 'Antalya Total'])
population2
population2.isnull() | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* Critically, the labels are used to **align data** when used in operations with other Series objects: | population + population2 | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* Contrast this with NumPy arrays, where arrays of the same length will combine values element-wise; adding Series combined values with the same label in the resulting series. Notice also that the missing values were propogated by addition. DataFrame * A `DataFrame` represents a tabular, spreadsheet-like data structur... | areaDict = {'Istanbul': 5461, 'Ankara': 25632, 'Izmir': 11891,
'Bursa': 10813, 'Antalya': 20177}
area = pd.Series(areaDict)
area
populationDict = {'Istanbul': 15029231, 'Ankara': 5445026, 'Izmir': 4279677, 'Bursa': 2936803, 'Antalya': 2364396}
population3 = pd.Series(populationDict)
population3 | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* Now that we have 2 Series population by cities and areas by cities, we can use a dictionary to construct a single two-dimensional object containing this information: | cities = pd.DataFrame({'population': population3, 'area': area})
cities | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* Or we can create our cities `DataFrame` with lists and indexes. | cities = pd.DataFrame({
'population':[15029231, 5445026, 4279677, 2936803, 2364396],
'area':[5461, 25632, 11891, 10813, 20177],
'city':['Istanbul', 'Ankara', 'Izmir', 'Bursa', 'Antalya']
})
cities | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
Notice the `DataFrame` is sorted by column name. We can change the order by indexing them in the order we desire: | cities[['city','area', 'population']] | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* A `DataFrame` has a second index, representing the columns: | cities.columns | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* If we wish to access columns, we can do so either by dictionary like indexing or by attribute: | cities['area']
cities.area
type(cities.area)
type(cities[['area']]) | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* Notice this is different than with `Series`, where dictionary like indexing retrieved a particular element (row). If we want access to a row in a `DataFrame`, we index its `iloc` attribute. | cities.iloc[2]
cities.iloc[0:2] | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
Alternatively, we can create a `DataFrame` with a dict of dicts: | cities = pd.DataFrame({
0: {'city': 'Istanbul', 'area': 5461, 'population': 15029231},
1: {'city': 'Ankara', 'area': 25632, 'population': 5445026},
2: {'city': 'Izmir', 'area': 11891, 'population': 4279677},
3: {'city': 'Bursa', 'area': 10813, 'population': 2936803},
4: {'city': 'Antalya', 'area': 2... | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* We probably want this transposed: | cities = cities.T
cities | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* It's important to note that the Series returned when a DataFrame is indexted is merely a **view** on the DataFrame, and not a copy of the data itself. * So you must be cautious when manipulating this data just like in the Numpy. | areas = cities.area
areas
areas[3] = 0
areas
cities | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* It's a usefull behavior for large data sets but for preventing this you can use copy method. | areas = cities.area.copy()
areas[3] = 10813
areas
cities | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* We can create or modify columns by assignment: | cities.area[3] = 10813
cities
cities['year'] = 2017
cities | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
* But note that, we can not use the attribute indexing method to add a new column: | cities.projection2020 = 20000000
cities | _____no_output_____ | Apache-2.0 | iPython Notebooks/Introduction to Pandas Part 1.ipynb | AlpYuzbasioglu/Zeppelin-Notebooks |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.