mirror of
https://github.com/wahyd4/code-sandbox.git
synced 2026-08-08 21:03:28 +10:00
add home macbook code snippets
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
.vscode
|
||||
.mypy_cache
|
||||
@@ -0,0 +1,16 @@
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import seaborn as sns
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib as mpl
|
||||
|
||||
sns.set()
|
||||
|
||||
births = pd.read_csv("data/births.csv")
|
||||
births["decade"] = 10 * (births["year"] // 10)
|
||||
|
||||
|
||||
births.pivot_table("births", index="year", columns="gender", aggfunc="sum").plot()
|
||||
plt.ylabel("total births per year")
|
||||
|
||||
plt.show(block=True)
|
||||
+15548
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,52 @@
|
||||
"state","abbreviation"
|
||||
"Alabama","AL"
|
||||
"Alaska","AK"
|
||||
"Arizona","AZ"
|
||||
"Arkansas","AR"
|
||||
"California","CA"
|
||||
"Colorado","CO"
|
||||
"Connecticut","CT"
|
||||
"Delaware","DE"
|
||||
"District of Columbia","DC"
|
||||
"Florida","FL"
|
||||
"Georgia","GA"
|
||||
"Hawaii","HI"
|
||||
"Idaho","ID"
|
||||
"Illinois","IL"
|
||||
"Indiana","IN"
|
||||
"Iowa","IA"
|
||||
"Kansas","KS"
|
||||
"Kentucky","KY"
|
||||
"Louisiana","LA"
|
||||
"Maine","ME"
|
||||
"Montana","MT"
|
||||
"Nebraska","NE"
|
||||
"Nevada","NV"
|
||||
"New Hampshire","NH"
|
||||
"New Jersey","NJ"
|
||||
"New Mexico","NM"
|
||||
"New York","NY"
|
||||
"North Carolina","NC"
|
||||
"North Dakota","ND"
|
||||
"Ohio","OH"
|
||||
"Oklahoma","OK"
|
||||
"Oregon","OR"
|
||||
"Maryland","MD"
|
||||
"Massachusetts","MA"
|
||||
"Michigan","MI"
|
||||
"Minnesota","MN"
|
||||
"Mississippi","MS"
|
||||
"Missouri","MO"
|
||||
"Pennsylvania","PA"
|
||||
"Rhode Island","RI"
|
||||
"South Carolina","SC"
|
||||
"South Dakota","SD"
|
||||
"Tennessee","TN"
|
||||
"Texas","TX"
|
||||
"Utah","UT"
|
||||
"Vermont","VT"
|
||||
"Virginia","VA"
|
||||
"Washington","WA"
|
||||
"West Virginia","WV"
|
||||
"Wisconsin","WI"
|
||||
"Wyoming","WY"
|
||||
|
@@ -0,0 +1,53 @@
|
||||
state,area (sq. mi)
|
||||
Alabama,52423
|
||||
Alaska,656425
|
||||
Arizona,114006
|
||||
Arkansas,53182
|
||||
California,163707
|
||||
Colorado,104100
|
||||
Connecticut,5544
|
||||
Delaware,1954
|
||||
Florida,65758
|
||||
Georgia,59441
|
||||
Hawaii,10932
|
||||
Idaho,83574
|
||||
Illinois,57918
|
||||
Indiana,36420
|
||||
Iowa,56276
|
||||
Kansas,82282
|
||||
Kentucky,40411
|
||||
Louisiana,51843
|
||||
Maine,35387
|
||||
Maryland,12407
|
||||
Massachusetts,10555
|
||||
Michigan,96810
|
||||
Minnesota,86943
|
||||
Mississippi,48434
|
||||
Missouri,69709
|
||||
Montana,147046
|
||||
Nebraska,77358
|
||||
Nevada,110567
|
||||
New Hampshire,9351
|
||||
New Jersey,8722
|
||||
New Mexico,121593
|
||||
New York,54475
|
||||
North Carolina,53821
|
||||
North Dakota,70704
|
||||
Ohio,44828
|
||||
Oklahoma,69903
|
||||
Oregon,98386
|
||||
Pennsylvania,46058
|
||||
Rhode Island,1545
|
||||
South Carolina,32007
|
||||
South Dakota,77121
|
||||
Tennessee,42146
|
||||
Texas,268601
|
||||
Utah,84904
|
||||
Vermont,9615
|
||||
Virginia,42769
|
||||
Washington,71303
|
||||
West Virginia,24231
|
||||
Wisconsin,65503
|
||||
Wyoming,97818
|
||||
District of Columbia,68
|
||||
Puerto Rico,3515
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,17 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
import seaborn
|
||||
import time
|
||||
|
||||
seaborn.set()
|
||||
|
||||
rainfall = pd.read_csv(
|
||||
"https://raw.githubusercontent.com/jakevdp/PythonDataScienceHandbook/master/notebooks/data/Seattle2014.csv"
|
||||
)["TMAX"].values
|
||||
print(rainfall)
|
||||
# inches = rainfall / 254.0 # 1/10mm -> inches
|
||||
rainfall.shape
|
||||
plt.hist(rainfall, 40)
|
||||
plt.show(block=True)
|
||||
@@ -0,0 +1,27 @@
|
||||
# name = "John"
|
||||
# print("Hello, %s \n !" % name, "555")
|
||||
|
||||
# a = 1.2345
|
||||
|
||||
# print("%.2f" % a)
|
||||
|
||||
# b = 1245
|
||||
|
||||
# print("%x/%X" % (b, b))
|
||||
|
||||
# data = ["John", "Doe", 53.44]
|
||||
# print(data[2])
|
||||
|
||||
# # reverse a string
|
||||
|
||||
# astring = "Hello world!"
|
||||
# print(astring[::-1])
|
||||
|
||||
# m = True
|
||||
# if m != True:
|
||||
# print("hahah")
|
||||
# else:
|
||||
# print("wooooo")
|
||||
|
||||
for i in range(10):
|
||||
print(i)
|
||||
@@ -0,0 +1,31 @@
|
||||
30,4,setosa,versicolor,virginica
|
||||
5.9,3.0,4.2,1.5,1
|
||||
6.9,3.1,5.4,2.1,2
|
||||
5.1,3.3,1.7,0.5,0
|
||||
6.0,3.4,4.5,1.6,1
|
||||
5.5,2.5,4.0,1.3,1
|
||||
6.2,2.9,4.3,1.3,1
|
||||
5.5,4.2,1.4,0.2,0
|
||||
6.3,2.8,5.1,1.5,2
|
||||
5.6,3.0,4.1,1.3,1
|
||||
6.7,2.5,5.8,1.8,2
|
||||
7.1,3.0,5.9,2.1,2
|
||||
4.3,3.0,1.1,0.1,0
|
||||
5.6,2.8,4.9,2.0,2
|
||||
5.5,2.3,4.0,1.3,1
|
||||
6.0,2.2,4.0,1.0,1
|
||||
5.1,3.5,1.4,0.2,0
|
||||
5.7,2.6,3.5,1.0,1
|
||||
4.8,3.4,1.9,0.2,0
|
||||
5.1,3.4,1.5,0.2,0
|
||||
5.7,2.5,5.0,2.0,2
|
||||
5.4,3.4,1.7,0.2,0
|
||||
5.6,3.0,4.5,1.5,1
|
||||
6.3,2.9,5.6,1.8,2
|
||||
6.3,2.5,4.9,1.5,1
|
||||
5.8,2.7,3.9,1.2,1
|
||||
6.1,3.0,4.6,1.4,1
|
||||
5.2,4.1,1.5,0.1,0
|
||||
6.7,3.1,4.7,1.5,1
|
||||
6.7,3.3,5.7,2.5,2
|
||||
6.4,2.9,4.3,1.3,1
|
||||
|
@@ -0,0 +1,121 @@
|
||||
120,4,setosa,versicolor,virginica
|
||||
6.4,2.8,5.6,2.2,2
|
||||
5.0,2.3,3.3,1.0,1
|
||||
4.9,2.5,4.5,1.7,2
|
||||
4.9,3.1,1.5,0.1,0
|
||||
5.7,3.8,1.7,0.3,0
|
||||
4.4,3.2,1.3,0.2,0
|
||||
5.4,3.4,1.5,0.4,0
|
||||
6.9,3.1,5.1,2.3,2
|
||||
6.7,3.1,4.4,1.4,1
|
||||
5.1,3.7,1.5,0.4,0
|
||||
5.2,2.7,3.9,1.4,1
|
||||
6.9,3.1,4.9,1.5,1
|
||||
5.8,4.0,1.2,0.2,0
|
||||
5.4,3.9,1.7,0.4,0
|
||||
7.7,3.8,6.7,2.2,2
|
||||
6.3,3.3,4.7,1.6,1
|
||||
6.8,3.2,5.9,2.3,2
|
||||
7.6,3.0,6.6,2.1,2
|
||||
6.4,3.2,5.3,2.3,2
|
||||
5.7,4.4,1.5,0.4,0
|
||||
6.7,3.3,5.7,2.1,2
|
||||
6.4,2.8,5.6,2.1,2
|
||||
5.4,3.9,1.3,0.4,0
|
||||
6.1,2.6,5.6,1.4,2
|
||||
7.2,3.0,5.8,1.6,2
|
||||
5.2,3.5,1.5,0.2,0
|
||||
5.8,2.6,4.0,1.2,1
|
||||
5.9,3.0,5.1,1.8,2
|
||||
5.4,3.0,4.5,1.5,1
|
||||
6.7,3.0,5.0,1.7,1
|
||||
6.3,2.3,4.4,1.3,1
|
||||
5.1,2.5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0 +1,52 @@
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
|
||||
print(tf.__version__)
|
||||
|
||||
from tensorflow.contrib.learn.python.learn.datasets import base
|
||||
|
||||
# Data files
|
||||
IRIS_TRAINING = "iris_training.csv"
|
||||
IRIS_TEST = "iris_test.csv"
|
||||
|
||||
# Load datasets.
|
||||
training_set = base.load_csv_with_header(filename=IRIS_TRAINING,
|
||||
features_dtype=np.float32,
|
||||
target_dtype=np.int)
|
||||
test_set = base.load_csv_with_header(filename=IRIS_TEST,
|
||||
features_dtype=np.float32,
|
||||
target_dtype=np.int)
|
||||
|
||||
# Specify that all features have real-value data
|
||||
feature_name = "flower_features"
|
||||
feature_columns = [tf.feature_column.numeric_column(feature_name,
|
||||
shape=[4])]
|
||||
classifier = tf.estimator.LinearClassifier(
|
||||
feature_columns=feature_columns,
|
||||
n_classes=3,
|
||||
model_dir="/tmp/iris_model")
|
||||
|
||||
def input_fn(dataset):
|
||||
def _fn():
|
||||
features = {feature_name: tf.constant(dataset.data)}
|
||||
label = tf.constant(dataset.target)
|
||||
return features, label
|
||||
return _fn
|
||||
|
||||
# Fit model.
|
||||
classifier.train(input_fn=input_fn(training_set),
|
||||
steps=1000)
|
||||
print('fit done')
|
||||
|
||||
# Evaluate accuracy.
|
||||
accuracy_score = classifier.evaluate(input_fn=input_fn(test_set),
|
||||
steps=100)["accuracy"]
|
||||
print('\nAccuracy: {0:f}'.format(accuracy_score))
|
||||
|
||||
# Export the model for serving
|
||||
feature_spec = {'flower_features': tf.FixedLenFeature(shape=[4], dtype=np.float32)}
|
||||
|
||||
serving_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)
|
||||
|
||||
classifier.export_savedmodel(export_dir_base='/tmp/iris_model' + '/export',
|
||||
serving_input_receiver_fn=serving_fn)
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
# Create 2 new lists height and weight
|
||||
import numpy as np
|
||||
height = [1.87, 1.87, 1.82, 1.91, 1.90, 1.85]
|
||||
weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]
|
||||
|
||||
# Import the numpy package as np
|
||||
|
||||
# Create 2 numpy arrays from height and weight
|
||||
|
||||
np_height = np.array(height)
|
||||
np_weight = np.array(weight)
|
||||
print(type(np_height))
|
||||
# Calculate bmi
|
||||
bmi = np_weight / np_height ** 2
|
||||
|
||||
# Print the result
|
||||
print(bmi)
|
||||
|
||||
# For a boolean response
|
||||
# bmi > 23
|
||||
|
||||
# Print only those observations above 23
|
||||
print(bmi[bmi > 25])
|
||||
@@ -0,0 +1,11 @@
|
||||
import pandas as pd
|
||||
dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"],
|
||||
"capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"],
|
||||
"area": [8.516, 17.10, 3.286, 9.597, 1.221],
|
||||
"population": [200.4, 143.5, 1252, 1357, 52.98]}
|
||||
|
||||
brics = pd.DataFrame(dict)
|
||||
print(brics)
|
||||
|
||||
brics.index = ["BR", "RU", "IN", "CH", "SA"]
|
||||
print(brics)
|
||||
@@ -0,0 +1,11 @@
|
||||
from adafruit_servokit import ServoKit
|
||||
kit = ServoKit(channels=16)
|
||||
|
||||
print("hello starting")
|
||||
kit.servo[0].actuation_range = 160
|
||||
|
||||
# angle can be 0 - 180
|
||||
kit.servo[0].angle = 180
|
||||
|
||||
kit.servo[0].angle = 0
|
||||
kit.continuous_servo[0].throttle = 1
|
||||
@@ -0,0 +1,24 @@
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
populations = pd.read_csv("data/state-population.csv")
|
||||
areas = pd.read_csv("data/state-areas.csv")
|
||||
abbrevs = pd.read_csv("data/state-abbrevs.csv")
|
||||
|
||||
merged = pd.merge(
|
||||
populations, abbrevs, how="outer", left_on="state/region", right_on="abbreviation"
|
||||
)
|
||||
|
||||
merged = merged.drop("abbreviation", 1)
|
||||
|
||||
final = pd.merge(merged, areas, on="state", how="left")
|
||||
|
||||
final.dropna(inplace=True)
|
||||
|
||||
data2010 = final.query("year == 2010 & ages == 'total'")
|
||||
# print(data2010.head())
|
||||
|
||||
data2010.set_index("state", inplace=True)
|
||||
density = data2010["population"] / data2010["area (sq. mi)"]
|
||||
density.sort_values(ascending=False, inplace=True)
|
||||
print(density.tail())
|
||||
Reference in New Issue
Block a user