#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy
# In[2]:
import matplotlib.pyplot as plt
# In[3]:
import numpy as np
x=np.linspace(0,10,100)
print(x)
# In[4]:
y = np.sin(x)
# In[5]:
y
# In[6]:
plt.plot(x,y)
plt.show()
# In[7]:
cosy= np.cos(x)
# In[8]:
cosy
# In[9]:
cosy.shape
# In[10]:
siny=y.copy()
# In[11]:
plt.plot(x,siny)
plt.plot(x,cosy)
plt.show()
# In[12]:
plt.plot(x,siny)
plt.plot(x,cosy,color='red')
plt.show()
# In[13]:
plt.plot(x,siny)
plt.plot(x,cosy,color='red',linestyle = '--')
plt.axis([-1,11,-2,2])
plt.show()
# In[14]:
plt.plot(x, siny, label="sin(x)")
plt.plot(x, cosy, color="red", linestyle="--", label="cos(x)")
plt.xlabel("x axis")
plt.ylabel("y value")
plt.legend()
plt.title("Welcome to matplotlib world!")
plt.show()
# In[15]:
plt.plot(x,siny,label="sin(x)")
plt.plot(x,cosy,color="red",linestyle='--',label="cos(x)")
plt.xlabel("x axis")
plt.ylabel("y value")
plt.legend()
plt.title("welcom to matplotlib world")
plt.show()
# In[16]:
plt.scatter(x,siny)
plt.show()
# In[17]:
plt.scatter(x,siny)
plt.scatter(x,cosy)
plt.show()
# In[18]:
pip list
# In[19]:
pip install sklearn
# In[20]:
from sklearn import datasets
# In[21]:
iris = datasets.load_iris()
# In[22]:
iris.keys()
# In[24]:
print(iris['DESCR'])
# In[25]:
iris.data
# In[26]:
iris.target.shape
# In[27]:
iris.target_names
# In[28]:
X = iris.data[:,:2]
# In[29]:
plt.scatter(X[:,0], X[:,1])
plt.show()
# In[30]:
y = iris.target
# In[31]:
y
# In[32]:
plt.scatter(X[y==0,0], X[y==0,1], color="red")
plt.scatter(X[y==1,0], X[y==1,1], color="blue")
plt.scatter(X[y==2,0], X[y==2,1], color="green")
plt.show()
# In[33]:
plt.scatter(X[y==0,0], X[y==0,1], color="red", marker="o")
plt.scatter(X[y==1,0], X[y==1,1], color="blue", marker="+")
plt.scatter(X[y==2,0], X[y==2,1], color="green", marker="x")
plt.show()
# In[34]:
plt.scatter(X[y==0,0], X[y==0,1], color="red", marker="o")
plt.scatter(X[y==1,0], X[y==1,1], color="blue", marker="+")
plt.scatter(X[y==2,0], X[y==2,1], color="green", marker="x")
plt.show()
# In[35]:
X = iris.data[:,2:]
# In[36]:
plt.scatter(X[y==0,0], X[y==0,1], color="red", marker="o")
plt.scatter(X[y==1,0], X[y==1,1], color="blue", marker="+")
plt.scatter(X[y==2,0], X[y==2,1], color="green", marker="x")
plt.show()
# In[37]:
raw_data_X = [[3.393533211, 2.331273381],
[3.110073483, 1.781539638],
[1.343808831, 3.368360954],
[3.582294042, 4.679179110],
[2.280362439, 2.866990263],
[7.423436942, 4.696522875],
[5.745051997, 3.533989803],
[9.172168622, 2.511101045],
[7.792783481, 3.424088941],
[7.939820817, 0.791637231]
]
raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
# In[38]:
X_train = np.array(raw_data_X)
y_train = np.array(raw_data_y)
# In[39]:
x = np.array([8.093607318, 3.365731514])
plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color='g')
plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r')
plt.scatter(x[0], x[1], color='b')
plt.show()
# In[40]:
b=np.array([1,2])
# In[41]:
b**2
# In[42]:
sum(b**2)
# In[45]:
from math import sqrt
distances = [sqrt(np.sum((x_train - x)**2)) for x_train in X_train]
# In[46]:
distances
# In[47]:
nearest = np.argsort(distances)
# In[49]:
k = 6
topK_y = [y_train[neighbor] for neighbor in nearest[:k]]
# In[50]:
topK_y
# In[51]:
from collections import Counter
votes = Counter(topK_y)
# In[52]:
votes.most_common(1)
# In[53]:
predict_y = votes.most_common(1)[0][0]
# In[54]:
predict_y
# In[55]:
bb=np.array([2,3,4,4])
# In[57]:
bb.shape
# In[58]:
bb.shape[0]
# In[59]:
pwd
# In[60]:
y = iris.target
# In[61]:
X.shape
# In[62]:
y
# In[64]:
y.shape
# In[65]:
X = iris.data
# In[66]:
X
# In[67]:
shuffled_indexes = np.random.permutation(len(X))
shuffled_indexes
# In[68]:
test_ratio = 0.2
test_size = int(len(X) * test_ratio)
# In[69]:
test_indexes = shuffled_indexes[:test_size]
train_indexes = shuffled_indexes[test_size:]
# In[70]:
X_train = X[train_indexes]
y_train = y[train_indexes]
# In[71]:
X_test = X[test_indexes]
y_test = y[test_indexes]
# In[72]:
print(X_train.shape)
print(y_train.shape)
# In[73]:
print(X_test.shape)
print(y_test.shape)
# In[74]:
digits = datasets.load_digits()
digits.keys()
# In[75]:
print(digits.DESCR)
# In[76]:
X = digits.data
X.shape
# In[77]:
y = digits.target
y.shape
# In[78]:
y[:10]
# In[79]:
X[:10]
# In[80]:
some_digit = X[66]
# In[81]:
y[66]
# In[82]:
some_digit_image = some_digit.reshape(8, 8)
# In[84]:
import matplotlib
import matplotlib.pyplot as plt
plt.imshow(some_digit_image, cmap = matplotlib.cm.binary)
plt.show()
# In[85]:
iris= datasets.load_iris()
# In[86]:
X=iris.data
y=iris.target
# In[87]:
X[:10,:]
# In[88]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=666)
# ## sklearn 中的standardscaler
# In[89]:
from sklearn.preprocessing import StandardScaler
# In[93]:
standardScaler = StandardScaler()
# In[94]:
standardScaler.fit(X_train)
# In[95]:
standardScaler.mean_
# In[96]:
standardScaler.scale_
# In[98]:
X_train=standardScaler.transform(X_train)
# In[99]:
X_test_standard=standardScaler.transform(X_test)
# In[100]:
x=np.array([1,2,3,4,5])
y=np.array([1,3,2,3,5])
# In[101]:
x_mean=np.mean(x)
y_mean=np.mean(y)
# In[102]:
num=0
d=0
for x_i,y_i in zip(x,y):
num+=(x_i-x_mean)*(y_i-y_mean)
d+=(x_i-x_mean)**2
# In[103]:
a=num/d
a
# In[104]:
b=y_mean-a*x_mean
# In[105]:
b
# In[106]:
y_hat=a*x+b
plt.scatter(x,y)
plt.plot(x,y_hat,color='r')
plt.show()
# In[111]:
boston = datasets.load_boston()
# In[113]:
print(boston.DESCR)
# In[114]:
x = boston.data[:,5] # 只使用房间数量这个特征
# In[116]:
y = boston.target
x = x[y < 50.0]
y = y[y < 50.0]
# In[117]:
plt.scatter(x, y)
plt.show()
# ## 使用简单线性回归法
# In[125]:
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data[:,2:]
y = iris.target
plt.scatter(X[y==2,0], X[y==2,1])
plt.scatter(X[y==1,0], X[y==1,1])
plt.scatter(X[y==0,0], X[y==0,1])
plt.show()
# In[ ]:
# In[129]:
import numpy as np
x=np.linspace(0.01,0.99,200)
# In[132]:
def entropy(p):
return -p*np.log(p)-(1-p)*np.log(1-p)
# In[133]:
plt.plot(x,entropy(x))
# In[134]:
def split(X, y, d, value):
index_a = (X[:,d] <= value)
index_b = (X[:,d] > value)
return X[index_a], X[index_b], y[index_a], y[index_b]
# In[ ]:
matplotlib的初步认识
最新推荐文章于 2022-12-17 21:51:03 发布